-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.py
103 lines (75 loc) · 2.92 KB
/
player.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
import pygame
import random
import colors
class Player(pygame.sprite.Sprite):
def __init__(self, x, y, width, height):
super().__init__()
self.width = width
self.height = height
self.rect = pygame.Rect(x, y, self.width, self.height)
self.image = pygame.Surface((self.width, self.height))
self.speed = 15
self.score = 0
def draw(self, screen):
self.movement()
pygame.draw.rect(screen, (255,255,255), self.rect)
def movement(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
self.move_x(self.speed * -1)
if keys[pygame.K_RIGHT]:
self.move_x(self.speed)
def move_x(self, direction):
self.rect.x += direction
class Ball(pygame.sprite.Sprite):
def __init__(self, x, y, width, height, color=(255,255,255)):
super().__init__()
self.width = width
self.height = height
self.colors = colors.Colors()
self.color = color
self.rect = pygame.Rect(x, y, self.width, self.width)
self.image = pygame.Surface((self.width, self.width))
self.speed = 10
self.dirx = 1
self.diry = -1
def draw(self, screen):
self.move_x(self.dirx)
self.move_y(self.diry)
pygame.draw.rect(screen, self.color, self.rect)
def boxCollision(self, objectBoxes, listBalls):
for column in objectBoxes:
for row in column:
if pygame.sprite.collide_mask(self, row):
self.dirx = random.choice([1, -1])
self.diry = 1
column.remove(row)
listBalls.append(Ball(self.rect.x, self.rect.y, self.width, self.height, row.color))
def collision(self, player, windowSize, listballs):
if pygame.sprite.collide_mask(self, player):
self.dirx = random.choice([1, -1])
self.diry = -1
if self.rect.x <= 0:
self.dirx = 1
self.diry = random.choice([1, -1])
elif self.rect.x >= windowSize['width'] - self.width:
self.dirx = -1
self.diry = random.choice([1, -1])
elif self.rect.y <= 0:
self.dirx = random.choice([1, -1])
self.diry = 1
elif self.rect.y > windowSize['height']:
listballs.remove(self)
def move_x(self, direction):
self.rect.x += self.speed * direction
def move_y(self, direction):
self.rect.y += self.speed * direction
class objectBox(pygame.sprite.Sprite):
def __init__(self, x, y, width, height, color=(255,255,255)):
self.width = width
self.height = height
self.color = color
self.rect = pygame.Rect(x, y, self.width, self.height)
self.image = pygame.Surface((self.width, self.height))
def draw(self, screen):
pygame.draw.rect(screen, self.color, self.rect)