-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpong.py
196 lines (149 loc) · 5.4 KB
/
pong.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!usr/bin/python
import pygame, random, math
# game settings
SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480
COLOR_BLACK = 0, 0, 0
COLOR_GREEN = 166, 206, 57
TEXT_PADDING = 25
PADDLE_SPEED = 5
PADDLE_OFFSET = 10
BALL_SPEED = 3
SPIN_PERCENT = 0.5
# classes
class Vector2:
def __init__(self, x = 0, y = 0):
self.x = x
self.y = y
def __add__(self, other):
if isinstance(other, Vector2):
return Vector2(self.x + other.x,
self.y + other.y)
else:
return Vector2(self.x + other,
self.y + other)
def __mul__(self, other):
if isinstance(other, Vector2):
return Vector2(self.x * other.x,
self.y * other.y)
else:
return Vector2(self.x * other,
self.y * other)
def set_zero(self):
self.x = 0
self.y = 0
class Actor:
def __init__(self, texture):
self.position = Vector2()
self.velocity = Vector2()
self.texture = texture
def get_bounds(self):
return pygame.Rect(self.position.x,
self.position.y,
self.texture.get_rect().width,
self.texture.get_rect().height)
def move(self, amount):
self.position += amount
def center_y(self):
self.position.y = SCREEN_HEIGHT / 2 - self.texture.get_rect().height / 2
def center_xy(self):
self.position = Vector2(SCREEN_WIDTH / 2 - self.texture.get_rect().width / 2,
SCREEN_HEIGHT / 2 - self.texture.get_rect().height / 2)
class Ball(Actor):
def move(self, amount):
global player1Score, player2Score
Actor.move(self, amount)
if self.position.y < 0:
self.velocity.y = math.fabs(self.velocity.y)
elif self.position.y > SCREEN_HEIGHT - self.get_bounds().height:
self.velocity.y = -math.fabs(self.velocity.y)
if self.get_bounds().right < 0:
player2.score += 1
player2Score = hudFont.render("%d" % player2.score, 1, COLOR_GREEN)
self.launch(BALL_SPEED)
elif self.position.x > SCREEN_WIDTH:
player1.score += 1
player1Score = hudFont.render("%d" % player1.score, 1, COLOR_GREEN)
self.launch(BALL_SPEED)
def launch(self, speed):
self.center_xy()
var = random.uniform(-1, 1)
angle = math.pi / 2 + var
if random.randint(0, 1) == 0:
angle += math.pi
self.velocity.x = math.sin(angle)
self.velocity.y = math.cos(angle)
self.velocity *= speed
class Player(Actor):
score = 0
def move(self, amount):
Actor.move(self, amount)
if self.position.y < 0:
self.position.y = 0
elif self.position.y > SCREEN_HEIGHT - self.texture.get_rect().height:
self.position.y = SCREEN_HEIGHT - self.texture.get_rect().height
# functions
def update(elapsedTime):
timefactor = elapsedTime * 0.05
player1.velocity.set_zero()
player2.velocity.set_zero()
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
player1.velocity.y -= PADDLE_SPEED
if keys[pygame.K_s]:
player1.velocity.y += PADDLE_SPEED
if keys[pygame.K_UP]:
player2.velocity.y -= PADDLE_SPEED
if keys[pygame.K_DOWN]:
player2.velocity.y += PADDLE_SPEED
ball.move(ball.velocity * timefactor)
player1.move(player1.velocity * timefactor)
player2.move(player2.velocity * timefactor)
if pygame.Rect.colliderect(ball.get_bounds(), player1.get_bounds()):
ball.velocity.x = math.fabs(ball.velocity.x)
ball.velocity.y += player1.velocity.y * SPIN_PERCENT
elif pygame.Rect.colliderect(ball.get_bounds(), player2.get_bounds()):
ball.velocity.x = -math.fabs(ball.velocity.x)
ball.velocity.y += player2.velocity.y * SPIN_PERCENT
def draw():
screen.fill(COLOR_BLACK)
screen.blit(ball.texture, ball.get_bounds())
screen.blit(player1.texture, player1.get_bounds())
screen.blit(player2.texture, player2.get_bounds())
screen.blit(player1Score, (TEXT_PADDING, TEXT_PADDING))
screen.blit(player2Score, (SCREEN_WIDTH - player2Score.get_width() - TEXT_PADDING, TEXT_PADDING))
pygame.display.flip()
# initialization
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# textures
ballTexture = pygame.image.load("ball.png")
playerTexture = pygame.image.load("paddle.png")
hudFont = pygame.font.Font(None, 32)
player1Score = hudFont.render("0", 1, COLOR_GREEN)
player2Score = hudFont.render("0", 1, COLOR_GREEN)
# game objects
ball = Ball(ballTexture)
ball.launch(BALL_SPEED)
player1 = Player(playerTexture)
player1.position.x = PADDLE_OFFSET
player1.center_y()
player2 = Player(playerTexture)
player2.position.x = SCREEN_WIDTH - player2.get_bounds().width - PADDLE_OFFSET
player2.center_y()
# loop control and timing
gameover = False
lastTick = pygame.time.get_ticks()
elapsedTime = 0
# game loop
while not gameover:
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
gameover = True
elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
gameover = True
elapsedTime = pygame.time.get_ticks() - lastTick
lastTick = pygame.time.get_ticks()
update(elapsedTime)
draw()