-
Notifications
You must be signed in to change notification settings - Fork 0
/
pong-game.py
153 lines (120 loc) · 3.36 KB
/
pong-game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# # Pong game with pygame
# import pygame
# import sys
# # Initialize the game
# pygame.init()
# # Set up the screen
# screen = pygame.display.set_mode((800, 600))
# # Set up color
# BROWN = (225, 118, 47)
# BLUE = (93, 173, 226)
# GRAY = (123, 125, 125)
# # Set up clock
# clock = pygame.time.Clock()
# # Set up paddles
# paddle1 = pygame.Rect(10, 250, 10, 100)
# paddle2 = pygame.Rect(780, 250, 10, 100)
# # Set up ball
# ball = pygame.Rect(400,300,20,20)
# ball_speed_x = 7
# ball_speed_y = 7
# # Game Loop
# playing = True
# while playing:
# for event in pygame.event.get():
# if event.type == pygame.QUIT:
# pygame.quit()
# sys.exit()
# # Move the paddles
# keys = pygame.key.get_pressed()
# if keys[pygame.K_w]:
# paddle1.y -= 5
# if keys[pygame.K_s]:
# paddle1.y += 5
# if keys[pygame.K_UP]:
# paddle2.y -= 5
# if keys[pygame.K_DOWN]:
# paddle2.y += 5
# # Move the ball
# ball.x += ball_speed_x
# ball.y += ball_speed_y
# # Bounce the ball off the top and bottom walls
# if ball.top <= 0 or ball.bottom >= 600:
# ball_speed_y = -ball_speed_y
# # Bounce the ball off the paddles
# if ball.colliderect(paddle1) or ball.colliderect(paddle2):
# ball_speed_x = -ball_speed_x
# # Check if the ball goes out of bounds
# if ball.left <= 0 or ball.right >= 800:
# ball.x = 400
# ball.y = 300
# ball_speed_x = 7
# ball_speed_y = 7
# # Fill the window with black color
# screen.fill(BROWN)
# # Draw the paddles and ball
# pygame.draw.rect(screen, BLUE, paddle1)
# pygame.draw.rect(screen, BLUE, paddle2)
# pygame.draw.ellipse(screen, GRAY, ball)
# pygame.display.update()
# clock.tick(10)
# Pong game with pygame
import pygame
import sys
# Initialize the game
pygame.init()
# Set up the screen
screen = pygame.display.set_mode((800, 600))
# Set up color
BROWN = (225, 118, 47)
BLUE = (93, 173, 226)
GRAY = (123, 125, 125)
# Set up clock
clock = pygame.time.Clock()
# Set up paddles
paddle1 = pygame.Rect(10, 250, 10, 100)
paddle2 = pygame.Rect(780, 250, 10, 100)
# Set up ball
ball = pygame.Rect(400,300,20,20)
ball_speed_x = 7
ball_speed_y = 7
# Game Loop
playing = True
while playing:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Move the paddles
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
paddle1.y -= 5
if keys[pygame.K_s]:
paddle1.y += 5
if keys[pygame.K_UP]:
paddle2.y -= 5
if keys[pygame.K_DOWN]:
paddle2.y += 5
# Move the ball
ball.x += ball_speed_x
ball.y += ball_speed_y
# Bounce the ball off the top and bottom walls
if ball.top <= 0 or ball.bottom >= 600:
ball_speed_y = -ball_speed_y
# Bounce the ball off the paddles
if ball.colliderect(paddle1) or ball.colliderect(paddle2):
ball_speed_x = -ball_speed_x
# Check if the ball goes out of bounds
if ball.left <= 0 or ball.right >= 800:
ball.x = 400
ball.y = 300
ball_speed_x = 7
ball_speed_y = 7
# Fill the window with black color
screen.fill(BROWN)
# Draw the paddles and ball
pygame.draw.rect(screen, BLUE, paddle1)
pygame.draw.rect(screen, BLUE, paddle2)
pygame.draw.ellipse(screen, GRAY, ball)
pygame.display.update()
clock.tick(60)