-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpong.py
165 lines (137 loc) · 5.1 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
import pygame
from pygame.locals import *
import random
from enum import Enum
WIDTH = 600
HEIGHT = 400
BALL_RADIUS = 10
PADDLE_HEIGHT = 80
PADDLE_WIDTH = 8
class Direction(Enum):
LEFT = 1
DOWN = 2
RIGHT = 3
UP = 4
class Color(Enum):
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLACK = (0, 0, 0)
class Ball:
def __init__(self, x, y):
self.x = x
self.y = y
self.vx = random.choice([-1,1])
self.vy = random.choice([-1,1])
self.r = BALL_RADIUS
def move(self):
self.x += self.vx
self.y += self.vy
def reflect(self, dir):
if dir == Direction.LEFT or dir == Direction.RIGHT:
self.vx *= -1
self.vx *= 1.1
self.vy *= 1.1
elif dir == Direction.UP or dir == Direction.DOWN:
self.vy *= -1
def draw(self, canvas):
pygame.draw.circle(canvas, Color.RED.value, [self.x, self.y], self.r, 0)
class Paddle:
def __init__(self, x, y):
self.x = x
self.y = y
self.l = PADDLE_HEIGHT
self.w = PADDLE_WIDTH
self.v = 0
def move(self):
if self.y > self.l//2 and self.y < HEIGHT - self.l//2:
self.y += self.v
elif self.y == self.l//2 and self.v > 0:
self.y += self.v
elif self.y == HEIGHT - self.l//2 and self.v < 0:
self.y += self.v
def draw(self, canvas):
pygame.draw.polygon(canvas, Color.GREEN.value, [
[self.x - self.w //2 , self.y - self.l // 2],
[self.x - self.w // 2, self.y + self.l // 2],
[self.x + self.w // 2, self.y + self.l // 2],
[self.x + self.w // 2, self.y - self.l // 2]])
class Pong:
def __init__(self, canvas, AIPlayer):
self.canvas = canvas
self.b = Ball(WIDTH//2, HEIGHT//2)
self.l_p = Paddle(PADDLE_WIDTH // 2 - 1, HEIGHT//2)
self.r_p = Paddle(WIDTH - PADDLE_WIDTH // 2, HEIGHT//2)
self.l_score = 0
self.r_score = 0
self.AIPlayer = AIPlayer
self.scored = False
def draw(self):
self.canvas.fill(Color.BLACK.value)
self.b.draw(self.canvas)
self.l_p.draw(self.canvas)
self.r_p.draw(self.canvas)
# updating scores
font = pygame.font.SysFont("Comic Sans MS", 20)
label = font.render("Score " + str(self.l_score), 1, (255, 255, 0))
self.canvas.blit(label, (50, 20))
font = pygame.font.SysFont("Comic Sans MS", 20)
label = font.render("Score " + str(self.r_score), 1, (255, 255, 0))
self.canvas.blit(label, (470, 20))
def play(self):
if self.AIPlayer:
if abs(self.b.y - self.l_p.y) <= 10:
self.l_p.v = 4 if self.b.y > self.l_p.y else -4
self.b.move()
self.r_p.move()
self.l_p.move()
self.handleCollision()
self.draw()
def handleCollision(self):
# ball collision on top and bottom walls
if int(self.b.y) <= BALL_RADIUS:
self.b.reflect(Direction.UP)
if int(self.b.y) >= HEIGHT + 1 - BALL_RADIUS:
self.b.reflect(Direction.DOWN)
# ball collision on the left paddle
if int(self.b.x) <= BALL_RADIUS + PADDLE_WIDTH and \
int(self.b.y) in range(int(self.l_p.y - PADDLE_HEIGHT // 2),
int(self.l_p.y + PADDLE_HEIGHT // 2), 1):
self.b.reflect(Direction.LEFT)
# ball collision on the left wall
elif int(self.b.x) <= BALL_RADIUS + PADDLE_WIDTH:
self.r_score += 1
self.restart()
# ball collision on the right paddle
if int(self.b.x) >= WIDTH + 1 - BALL_RADIUS - PADDLE_WIDTH and \
int(self.b.y) in range(int(self.r_p.y - PADDLE_HEIGHT // 2),
int(self.r_p.y + PADDLE_HEIGHT // 2), 1):
self.b.reflect(Direction.RIGHT)
# ball collision on the right wall
elif int(self.b.x) >= WIDTH + 1 - BALL_RADIUS - PADDLE_WIDTH:
self.l_score += 1
self.restart()
def keydown(self, event):
if event.key == K_UP:
self.move(-8, Direction.RIGHT)
elif event.key == K_DOWN:
self.move(8, Direction.RIGHT)
elif event.key == K_w:
self.move(-8, Direction.LEFT)
elif event.key == K_s:
self.move(8, Direction.LEFT)
def keyup(self, event):
if event.key in (K_w, K_s):
self.move(0, Direction.LEFT)
elif event.key in (K_UP, K_DOWN):
self.move(0, Direction.RIGHT)
def move(self, velocity, panel):
if panel == Direction.LEFT:
self.l_p.v = velocity
elif panel == Direction.RIGHT:
self.r_p.v = velocity
def restart(self):
self.scored = True
self.b = Ball(WIDTH // 2, HEIGHT // 2)
self.l_p = Paddle(PADDLE_WIDTH // 2 - 1, HEIGHT//2)
self.r_p = Paddle(WIDTH - PADDLE_WIDTH // 2, HEIGHT//2)