-
Notifications
You must be signed in to change notification settings - Fork 0
/
BrickColliderv3.py
129 lines (106 loc) · 3.88 KB
/
BrickColliderv3.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
import pygame, os
#Colours (R, G, B)
GREEN = (0, 255, 0)
class BrickCollider:
def main(self):
clock = pygame.time.Clock()
running = True
#Create objects
ball = Ball()
paddle = Paddle()
#Create Sprite Groups
b_group = pygame.sprite.Group()
p_group = pygame.sprite.Group()
#Add Objects to Sprite Groups
b_group.add(ball)
p_group.add(paddle)
#Game Loop
while running:
clock.tick(60) #Refresh screen
screen.fill(GREEN) #Colour Screen
#Draw Sprite Groups
b_group.draw(screen)
p_group.draw(screen)
pygame.display.flip() #Update(flip) display
#Move ball(s)
for balls in b_group:
balls.move()
balls.pad_colllide(paddle)
#Check for events
for event in pygame.event.get():
pause = False
if event.type == pygame.QUIT: #X at top right
running = False
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: #Escape key press
running = False
if event.type == pygame.KEYDOWN and event.key == pygame.K_p: #Pause game
pause = True
#Paused Game and event
while pause:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pause = False
running = False
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
pause = False
running = False
if event.type == pygame.KEYDOWN and event.key == pygame.K_p:
pause = False
#Move Paddle
key = pygame.key.get_pressed()
if key[pygame.K_LEFT]:
paddle.move_left()
if key[pygame.K_RIGHT]:
paddle.move_right()
class Ball(pygame.sprite.Sprite):
def __init__(self):
super(Ball, self).__init__() #Set Ball as the super class of Sprite
#Create the Ball image
self.image = pygame.image.load("ball.png")
self.rect = self.image.get_rect()
self.rect.x = 150 #Start Position for Ball on x-axis
self.rect.y = 300 #Start Position for Ball on y-axis
self.dirx = 1 #Movement for ball along x-axis
self.diry = 1 #Movement for ball along y-axis
def move(self):
#If right or left sides of the Ball touch the sides of the screen
if self.rect.right >= 500 or self.rect.left <= 0:
self.dirx = -self.dirx
#If the bottom or top of Ball touch the bottom or top of screen
if self.rect.bottom >= 600 or self.rect.top <= 0:
self.diry = -self.diry
#Add movement to the current values of rect x and y
self.rect.x += self.dirx
self.rect.y += self.diry
def pad_colllide(self, paddle):
# Ball hits the top of the paddle
if self.rect.bottom == paddle.rect.top:
if self.rect.right > paddle.rect.left and self.rect.left < paddle.rect.right:
self.diry = -self.diry
# Ball hits the bottom of the paddle
if self.rect.top == paddle.rect.bottom:
if self.rect.right > paddle.rect.left and self.rect.left < paddle.rect.right:
self.diry = -self.diry
# Ball hits the sides of the paddle
if self.rect.bottom > paddle.rect.top and self.rect.top < paddle.rect.bottom:
if self.rect.right >= paddle.rect.left and self.rect.left <= paddle.rect.right:
self.dirx = -self.dirx
class Paddle(pygame.sprite.Sprite):
def __init__(self):
super(Paddle, self).__init__() #Set Paddle as the super class of Sprite
#Create the Paddle image
self.image = pygame.image.load("paddle.png")
self.rect = self.image.get_rect()
self.rect.x = 200 #Start Position for Paddle on x-axis
self.rect.y = 450 #Start Position for Paddle on y-axis
def move_left(self):
if self.rect.left > 0:
self.rect.x += -3
def move_right(self):
if self.rect.right < 500:
self.rect.x += 3
if __name__ == '__main__':
os.environ["SDL_VIDEO_CENTERED"] = "1" #Sets up for center of OS display
pygame.init() #Initalise Pygame
screen = pygame.display.set_mode((500, 600)) #Create a screen for game
BrickCollider().main() #Create game and run main