-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
206 lines (154 loc) · 5.29 KB
/
Copy pathmain.py
File metadata and controls
206 lines (154 loc) · 5.29 KB
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
197
198
199
200
201
202
203
204
205
206
import pygame
import random
#### #### #### STATIC VARIABLE #### #### ####
DISPLAY = [1040, 720]
BRICK_SIZE = [80, 40]
SPACING = 5
#### #### #### VARIABLE #### #### #### ####
brickList = []
ballList = []
BrickNum = 0
#### #### #### CLASSES #### #### #### ####
class Player:
def __init__(self, x, y):
self.x = int(x)
self.y = int(y)
self.color = (250, 120, 60)
self.rect = pygame.Rect(self.x, self.y, 130, 25)
self.press_left = False
self.press_right = False
self.speed = 4
self.velocityX = 0
def drawplayer(self):
pygame.draw.rect(screen, self.color, self.rect)
def update(self):
self.velocityX = 0
if self.press_left and not self.press_right:
self.velocityX = -self.speed
if self.press_right and not self.press_left:
self.velocityX = self.speed
if self.x <= 0:
self.x = 1
if self.x >= 1040 - 130:
self.x = 1040 - 129
self.x += self.velocityX
self.rect = pygame.Rect(int(self.x), int(self.y), 130, 25)
class Ball:
def __init__(self):
self.x = 520
self.y = 360
self.sX = random.randint(-3,3)
self.sY = 4
self.rect = pygame.Rect(self.x, self.y, 20, 20)
def render(self):
pygame.draw.rect(screen, (0, 255, 0), self.rect)
def update(self):
self.rect = pygame.Rect(int(self.x), int(self.y), 20, 20)
def checkPlayer(self):
return player.rect.colliderect(self.rect)
def checkBrick():
#for loop
#colliderect
print("hello")
def checkBound(self):
if self.x <= 0 or self.x >= 1040 - 20:
self.sX = -self.sX
if self.y <= 0:
self.sY = -self.sY
if self.y >= 720:
pygame.quit()
def update(self):
self.x += self.sX
self.y += self.sY
self.rect = pygame.Rect(int(self.x), int(self.y), 20, 20)
def bounce_A(self):
if (ball.y > 576):
self.sY = -3
self.sX = -self.sX
self.sX = random.randint(-3, 3)
self.sY = -self.sY
def bounce_B(self):
self.sY = -self.sY
class brick:
def __init__(self, x, y):
self.x = x
self.y = y
self.touched = False
self.rect = pygame.Rect(x, y , BRICK_SIZE[0], BRICK_SIZE[1])
"""
def getXLoc(self):
return self.x
def getYLoc(self):
return self.y
"""
def getTouch(self):
return self.touched
def setTouch(self):
self.touched = True
def checkBrick(self):
return ball.rect.colliderect(self.rect)
def render(self):
pygame.draw.rect(screen, (255, 0, 0), self.rect)
pygame.draw.rect(screen, (255, 255, 255), (self.x, self.y, BRICK_SIZE[0], BRICK_SIZE[1]), width=1)
#### #### #### FUNCTIONS #### #### #### ####
def createBricks():
for yLoc in range(2, (int)((DISPLAY[1] / 3) / BRICK_SIZE[1])):
for xLoc in range(1, (int)(DISPLAY[0] / (BRICK_SIZE[0]) - 1)):
brickList.append(brick(xLoc * BRICK_SIZE[0], yLoc * BRICK_SIZE[1]))
def drawBricks():
for i in brickList:
if not i.getTouch():
i.render()
#### #### #### Main Loop #### #### ####
if __name__ == "__main__":
pygame.init()
pygame.mixer.music.load('acoustic-guitars-ambient-uplifting-background-music-for-videos-5642.wav')
pygame.mixer.music.play(-1)
hitNormal = pygame.mixer.Sound('drum-hitnormal.wav')
screen = pygame.display.set_mode(DISPLAY)
clock = pygame.time.Clock()
pygame.event.set_allowed([pygame.QUIT, pygame.KEYDOWN, pygame.KEYUP])
pygame.display.set_caption("Brick Thing")
player = Player(DISPLAY[0]/2.25, DISPLAY[1]/1.25)
ball = Ball()
createBricks()
BrickNum = len(brickList)
running = True
while running:
#boundary to prevent the paddle from going out of the screen
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT or event.key == ord('a'):
player.press_left = True
if event.key == pygame.K_RIGHT or event.key == ord('d'):
player.press_right = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == ord('a'):
player.press_left = False
if event.key == pygame.K_RIGHT or event.key == ord('d'):
player.press_right = False
screen.fill((255, 255, 255))
drawBricks()
ball.render()
if ball.checkPlayer():
ball.bounce_A()
pygame.mixer.Sound.play(hitNormal)
for i in brickList:
if i.checkBrick() and not i.getTouch():
ball.bounce_B()
pygame.mixer.Sound.play(hitNormal)
BrickNum -= 1
i.setTouch()
break
if BrickNum == 0:
pygame.quit()
ball.checkBound()
ball.update()
player.drawplayer()
player.update()
clock.tick(60)
pygame.display.update()
# pygame.display.flip()
pygame.quit()