-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathblock_breaker.py
252 lines (230 loc) · 7.99 KB
/
block_breaker.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import pygame, os
from pygame.locals import *
from random import randint
from time import time
def main():
#Debug Var
init = time()
maior_update = 0
maior_draw = 0
maior_running = 0
soma_update = 0
soma_draw = 0
soma_running = 0
quant = 0
running, settings = load()
#debug
print('load: ' + str(time()-init))
init = time()
while running:
quant+=1
init = time()
running, settings = update(running, settings)
if time()-init>maior_update:
maior_update = time()-init
soma_update += time()-init
init = time()
draw(settings['screen_size'], settings['screen'], settings['game_object'], \
settings['var']['life'], settings['var']['font'], settings['var']['score'])
if time()-init>maior_draw:
maior_draw = time()-init
soma_draw += time()-init
init = time()
if running:
running = check_exit()
if time()-init>maior_running:
maior_running = time()-init
soma_running += time()-init
print
print('maior_update : %.4f sec' % (maior_update))
print('maior_draw : %.4f sec' % (maior_draw))
print('maior_running : %.4f sec' % (maior_running))
print
print('media_update : %.4f sec' % (soma_update/quant))
print('media_draw : %.4f sec' % (soma_draw/quant))
print('media_running : %.4f sec' % (soma_running/quant))
pygame.quit()
def load():
screen_size = (800, 600)
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption("Block Breaker")
pygame.font.init()
game_object = {
'block' : [],
'player': [],
'ball' : []
}
var = {
'folder' : os.path.dirname(os.path.realpath(__file__)),
'life' : 5,
'font' : pygame.font.SysFont("arial", 20),
'playing': False,
'score' : 0,
}
game_object = load_level(1, game_object)
return True, {
'screen_size' : screen_size,
'screen' : screen,
'game_object' : game_object,
'var' : var
}
def load_level(what_level, game_object):
game_object = {
'block' : [],
'player': [],
'ball' : []
}
if what_level == 1:
for i in range(12):
for j in range(15):
if i%4 != 0:
game_object['block'].append(Block2D(10 + 780/15 * j, 40+15*i, 780/15, 15, \
(230/12*i, 250, 100/8*j), 80))
else:
game_object['block'].append(Block2D(10 + 780/15 * j, 40+15*i, 780/15, 15, \
(250, 230/12*i, 100/8*j), 100))
game_object['player'].append(Block2D(320, 550, 160, 20, (100, 100, 200), 0))
game_object['ball'].append(Circle2D(400, 540, 10))
return game_object
def game_over(screen, font, screen_size):
while True:
k = pygame.key.get_pressed()
sair = False
for e in pygame.event.get():
if e.type == QUIT or k[K_ESCAPE]:
sair = True
if sair:
break
screen.fill((0, 0, 0))
font = pygame.font.SysFont("arial", 56)
screen.blit(font.render('Game Over', True, (255, 255, 255)), (25, 7))
pygame.display.flip()
return False
def update(running, settings):
game_object = settings['game_object']
screen_size = settings['screen_size']
ball = game_object['ball']
player = game_object['player'][0]
life = settings['var']['life']
playing = settings['var']['playing']
font = settings['var']['font']
screen = settings['screen']
if ball[0].y>550:
settings['var']['life'] -= 1
if settings['var']['life'] < 0:
running = game_over(screen, font, screen_size)
else:
player.x = 320
ball[0].x = 400
ball[0].y = 545
if ball[0].y_speed > 0:
ball[0].y_speed = -ball[0].y_speed
settings['var']['playing'] = False
k = pygame.key.get_pressed()
if not playing:
if k[K_SPACE]:
settings['var']['playing'] = True
ball[0].x_speed = randint(-4, 4)
if k[K_d]:
player.x += 8
elif k[K_a]:
player.x -= 8
if player.x<0:
player.x = 0
if player.x+player.width>screen_size[0]:
player.x = screen_size[0]-player.width
ball[0].x = player.x+player.width/2
else:
if k[K_d]:
player.x += 8
elif k[K_a]:
player.x -= 8
if player.x<0:
player.x = 0
if player.x+player.width>screen_size[0]:
player.x = screen_size[0]-player.width
ball, settings['var']['score'] = update_ball(ball, game_object, screen_size, settings['var']['score'])
return running, settings
def draw(screen_size, screen, game_object, life, font, score):
screen.fill((0, 0, 0))
for name in game_object:
for gO in game_object[name]:
if gO.__class__==Block2D:
pygame.draw.rect(screen, gO.color, (gO.x, gO.y, gO.width, gO.height))
pygame.draw.rect(screen, (255, 255, 255), (gO.x, gO.y, gO.width, gO.height), 1)
elif gO.__class__==Circle2D:
pygame.draw.circle(screen, (255, 255, 255), (gO.x, gO.y), gO.radius)
elif gO.__class__==Sprite:
screen.blit(gO.img, (gO.x, gO.y))
for i in range(life):
pygame.draw.circle(screen, (255, 255, 255), (90+30*i, 20), 13)
c = 255/10*i
if c < 100:
c = 100
pygame.draw.circle(screen, (c, 100/5*i, 250/5*i), (90+30*i, 20), 13, 2)
screen.blit(font.render('Lifes:', True, (255, 255, 255)), (25, 7))
screen.blit(font.render('Score: ' + str(score), True, (255, 255, 255)), (400, 7))
pygame.display.flip()
fps(60)
pass
def update_ball(ball, game_object, screen_size, score):
for gO in ball:
gO.x += gO.x_speed
gO.y += gO.y_speed
for name in game_object:
for obj in game_object[name]:
if obj.__class__ == Block2D:
if intersects(gO, obj):
if name != 'player':
score += obj.score
game_object[name].remove(obj)
else:
if obj.x+obj.width/2>gO.x:
ball[0].x_speed = -randint(1, 4)
else:
ball[0].x_speed = randint(1, 4)
gO.y_speed = -gO.y_speed
if abs(ball[0].x_speed) > 6:
if ball[0].x_speed > 0:
ball[0].x_speed = 6
else:
ball[0].x_speed = -6
if gO.x-gO.radius<0 or gO.x+gO.radius>screen_size[0]:
gO.x_speed = -gO.x_speed
if gO.y+gO.y_speed+gO.radius<50:
gO.y_speed = -gO.y_speed
return ball, score
def fps(frames):
pygame.time.Clock().tick(frames)
def check_exit():
k = pygame.key.get_pressed()
for e in pygame.event.get():
if e.type == QUIT or k[K_ESCAPE]:
return False
return True
class Block2D():
def __init__(self, x, y, width, height, color, score=0):
self.x = x
self.y = y
self.width = width
self.score = score
self.height = height
self.color = color
class Circle2D():
def __init__(self, x, y, radius):
self.x = x
self.y = y
self.radius = radius
self.x_speed = 3
self.y_speed = -6
class Sprite():
def __init__(self, x, y, path):
self.x = x
self.y = y
self.img = pygame.image.load(path)
def intersects(circle, rect):
if circle.y+circle.radius+circle.y_speed>rect.y and circle.y-circle.radius<rect.y+rect.height:
if circle.x+circle.radius>rect.x and circle.x-circle.radius<rect.x+rect.width:
return True
return False
main()