-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.py
123 lines (97 loc) · 2.61 KB
/
project.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
import pygame
class Paddles:
def __init__(self, color, coords, size, speed):
self.color = color
self.initial_coords = list(coords)
self.coords = coords
self.size = size
self.speed = speed
def move(self, direction):
if direction == 'up':
self.coords[1] -= self.speed
elif direction == 'down':
self.coords[1] += self.speed
def draw(self, window):
pygame.draw.rect(window, self.color, (self.coords, self.size))
def check_boundaries(self, screen_size):
for i in [0, 1]:
if (self.coords[i] < 0):
self.coords[i] = 0
elif (self.coords[i] >= screen_size[i] - self.size[i]):
self.coords[i] = screen_size[i] - self.size[i]
def reset(self):
self.coords = list(self.initial_coords)
class Ball:
def __init__(self, color, coords, size, speed):
self.color = color
self.initial_coords = list(coords)
self.coords = coords
self.size = size
self.speed = speed
def move(self, direction):
if direction == 'up':
self.coords[1] -= self.speed
elif direction == 'down':
self.coords[1] += self.speed
elif direction == 'left':
self.coords[0] -= self.speed
else:
self.coords[0] += self.speed
def draw(self, window):
pygame.draw.circle(window, self.color, (self.coords, self.size))
def check_boundaries(self, screen_size):
for i in [0, 1]:
if (self.coords[i] < 0):
self.coords[i] = 0
elif (self.coords[i] >= screen_size[i] - self.size[i]):
self.coords[i] = screen_size[i] - self.size[i]
def has_collided(self, player):
return ((self.coords[0] < player.coords[0] + player.size[0]) and
(player.coords[0] < self.coords[0] + self.size[0]) and
(self.coords[1] < player.coords[1] + player.size[1]) and
(player.coords[1] < self.coords[1] + self.size[1]))
def reset(self):
self.coords = list(self.initial_coords)
WINDOW_SIZE = [200, 200]
PADDLE_SIZE = [5, 20]
BALL_SIZE = 5
paddle1 = Paddles(
pygame.Color('white'),
[0, 0],
PADDLE_SIZE,
5
)
paddle2 = Paddles(
pygame.Color('magenta'),
[200, 200],
PADDLE_SIZE,
5
)
ball = Ball(
pygame.Color('red'),
[100, 100],
BALL_SIZE,
5
)
window = pygame.display.set_mode(WINDOW_SIZE)
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
paddle2.move('up')
elif keys[pygame.K_DOWN]:
paddle2.move('down')
if keys[pygame.K_w]:
paddle1.move('up')
elif keys[pygame.K_s]:
paddle1.move('down')
paddle1.check_boundaries(WINDOW_SIZE)
paddle2.check_boundaries(WINDOW_SIZE)
window.fill(pygame.Color('black'))
paddle1.draw(window)
paddle2.draw(window)
pygame.display.update()
clock.tick(60)