Skip to content

Commit da47b54

Browse files
authored
Add files via upload
1 parent fc523c0 commit da47b54

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed

pong-pygame/Assets/ball.png

4.22 KB
Loading

pong-pygame/Assets/paddle.png

1.35 KB
Loading

pong-pygame/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Pygame_pong
2+
3+
useage:
4+
5+
install python from ['https://www.python.org/downloads/']
6+
7+
>> pip install pygame
8+
9+
```
10+
python main.py
11+
```
12+
13+
14+
![Screenshot from 2023-01-01 14-17-38](https://user-images.githubusercontent.com/68957369/210165478-2617e876-234c-4d95-b9fd-8289f8973ba2.png)

pong-pygame/pong.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import pygame
2+
# Initialize pygame
3+
4+
pygame.init() # Initialize pygame
5+
6+
# Set the window size
7+
screen = pygame.display.set_mode((600, 400)) # Set the window size
8+
9+
# Set the title of the window
10+
pygame.display.set_caption("Pong") # Set the title of the window
11+
12+
# Set up the game clock
13+
clock = pygame.time.Clock() # Set up the game clock
14+
15+
# Load the images for the ball and paddles
16+
ball_image = pygame.image.load("Assets/ball.png")
17+
paddle_image = pygame.image.load("Assets/paddle.png")
18+
19+
# Create the ball sprite
20+
ball = pygame.sprite.Sprite() # Create the ball sprite
21+
ball.image = ball_image # Load the ball image
22+
ball.rect = ball.image.get_rect() # Get the ball's rects
23+
24+
# Create the paddles
25+
left_paddle = pygame.sprite.Sprite() # Create the left paddle
26+
left_paddle.image = paddle_image # Load the left paddle image
27+
left_paddle.rect = left_paddle.image.get_rect() # Get the left paddle's rect
28+
29+
right_paddle = pygame.sprite.Sprite() # Create the right paddle
30+
right_paddle.image = paddle_image # Load the right paddle image
31+
right_paddle.rect = right_paddle.image.get_rect() # Get the right paddle's rect
32+
33+
# Set the initial positions of the ball and paddles
34+
ball.rect.center = (300, 200) # Set the ball's initial position
35+
left_paddle.rect.left = 20 # Set the left paddle's initial position
36+
left_paddle.rect.centery = 200 # Set the left paddle's initial position
37+
right_paddle.rect.right = 580 # Set the right paddle's initial position
38+
right_paddle.rect.centery = 200 # Set the right paddle's initial position
39+
40+
# Set the ball's initial velocity
41+
ball_vx = 5 #
42+
ball_vy = 5 #
43+
44+
# Game loop
45+
while True: #
46+
# Handle events
47+
for event in pygame.event.get(): # Handle events
48+
if event.type == pygame.QUIT: # Handle the QUIT event
49+
pygame.quit() # Quit pygame
50+
exit() # Exit the program
51+
52+
# Update game state
53+
ball.rect.x += ball_vx # Update the ball's x position
54+
ball.rect.y += ball_vy # Update the ball's y position
55+
56+
# Update game state
57+
keys = pygame.key.get_pressed() # Get the keys that are pressed
58+
if keys[pygame.K_UP]: # Check if the up key is pressed
59+
right_paddle.rect.y -= 5 # Update the right paddle's y position
60+
if keys[pygame.K_DOWN]: # Check if the down key is pressed
61+
right_paddle.rect.y += 5 # Update the right paddle's y position
62+
if keys[pygame.K_w]: # Check if the w key is pressed
63+
left_paddle.rect.y -= 5 # Update the left paddle's y position
64+
if keys[pygame.K_s]: # Check if the s key is pressed
65+
left_paddle.rect.y += 5 # Update the left paddle's y position
66+
67+
# Check for ball collision with paddles
68+
if ball.rect.colliderect(left_paddle.rect) or ball.rect.colliderect(right_paddle.rect): # Check for ball collision with paddles
69+
ball_vx = -ball_vx # Reverse the ball's x velocity
70+
71+
# Check for ball collision with walls
72+
if ball.rect.top < 0 or ball.rect.bottom > 400: # Check for ball collision with walls
73+
ball_vy = -ball_vy # Reverse the ball's y velocity
74+
75+
# Draw the screen
76+
screen.fill((0, 0, 0)) # Fill the screen with black
77+
screen.blit(ball.image, ball.rect) # Draw the ball
78+
screen.blit(left_paddle.image, left_paddle.rect) # Draw the left paddle
79+
screen.blit(right_paddle.image, right_paddle.rect) # Draw the right paddle
80+
pygame.display.flip() # Update the display
81+
82+
# Limit the frame rate
83+
clock.tick(60)

0 commit comments

Comments
 (0)