-
Notifications
You must be signed in to change notification settings - Fork 0
258 lines (213 loc) · 7.62 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
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
253
254
255
256
import pygame, sys, random
import time
from pygame.locals import *
from constants import *
print("began")
pygame.init()
wid, hei = pygame.display.Info().current_w, pygame.display.Info().current_h
DISPLAYSURF = pygame.display.set_mode([WIDTH,HEIGHT])
pygame.display.set_caption("Kaboom")
clock = pygame.time.Clock()
pygame.font.init() # you have to call this at the start,
# if you want to use this module.
myfont = pygame.font.SysFont('Comic Sans MS', 30)
scoreSurf = myfont.render('Score', False, (0, 0, 0))
def resetScreen():
DISPLAYSURF.fill((120,80,80))
pygame.draw.rect(DISPLAYSURF,(0,0,255), (0,0,WIDTH, SKY_HEIGHT))
def waitForClick():
cont = False
while cont == False:
for event in pygame.event.get():
if event.type == MOUSEBUTTONUP:
cont = True
if event.type==QUIT:
pygame.quit()
print("quit")
sys.exit()
class Image:
def __init__(self, pygImg):
self.img = pygImg
self.hitbox = self.img.get_rect().size
#self.hitbox = hitbox
class Sprite:
def __init__(self, img, posn):
self._img = Image(img)
self._x = posn[0]
self._y = posn[1]
def posn(self):
return [ self._x, self._y ]
def center_of(self):
return [ self._x + self._img.hitbox[0]/2, self._y + self._img.hitbox[1]/2 ]
def set_posn(self, posn):
self._x = posn[0]
self._y = posn[1]
def set_center(self, posn):
self._x = posn[0] - self._img.hitbox[0]/2
self._y = posn[1] - self._img.hitbox[1]/2
def show(self):
DISPLAYSURF.blit(self._img.img, self.posn())
def hitbox(self):
return self._img.hitbox
def is_touching(self, obj2):
if abs(self.center_of()[0] - obj2.center_of()[0]) < (self.hitbox()[0]/2 + obj2.hitbox()[0]/2) and abs(self.center_of()[1] - obj2.center_of()[1]) < (self.hitbox()[1]/2 + obj2.hitbox()[1]/2):
return True
else:
return False
#initialize bomber
class Level:
def __init__(self): #initialize
self._level = 1
self._bomb_step = BOMB_STEP_START
self._bomber_step = BOMBER_STEP_START
self._score = 0
self._length = LEVEL_LENGTH_START
self._level_start = pygame.time.get_ticks()
self._drop_interval = DROP_INTERVAL_START
self._last_drop = 0
def level_up(self): #for use after all bombs are caught
self._score = self._score + self._level * 20
self._level = self._level + 1
self._bomb_step = self._bomb_step + BOMB_STEP_INCREMENT if self._bomb_step < MAX_BOMB_STEP else self._bomb_step
self._bomber_step = self._bomber_step + BOMBER_STEP_INCREMENT if self._bomber_step < MAX_BOMBER_STEP else self._bomber_step
self._length = self._length + 4000/self._level
self._drop_interval = self._drop_interval - DROP_INTERVAL_DECREMENT
def wait_and_start(self):
waitForClick()
self._level_start = pygame.time.get_ticks()
def is_going(self): #returns false when no more bombs should be dropped
return pygame.time.get_ticks() < self._level_start + self._length
def score_up(self): #adds score -- use when bomb is caught
self._score = self._score + self._level
def should_drop(self):
if self._last_drop + self._drop_interval < pygame.time.get_ticks():
self._last_drop = pygame.time.get_ticks()
return True
else:
return False
def bomb_step(self):
return self._bomb_step
def bomber_step(self):
return self._bomber_step
def score(self):
return self._score
class Game:
def __init__(self):
self.bomber = Bomber()
self.bucket = Bucket()
self.level = Level()
self.bombs = []
self.lives = 3
self.lastDrop = pygame.time.get_ticks()
self.surf = DISPLAYSURF
def update(self):
resetScreen()
print(self.level.score())
self.move_all()
if self.level.is_going():
self.drop()
self.update_score()
self.update_screen()
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
print("quit")
sys.exit()
if not(self.level.is_going()) and self.bombs == []:
resetScreen()
self.move_all()
if self.level.is_going():
self.drop()
self.level.level_up()
self.update_score()
self.update_screen()
self.level.wait_and_start()
def update_score(self):
scoreSurf = myfont.render(str(self.level.score()), False, (0, 0, 0))
DISPLAYSURF.blit(scoreSurf, [0,0])
def update_screen(self):
pygame.display.update()
def move_all(self):
if self.level.is_going():
self.bomber.move(self.level.bomber_step())
else:
self.bomber.show()
self.bucket.move()
to_delete = []
for index, bomb in enumerate(self.bombs):
ret = bomb.move(self.level.bomb_step())
if ret == KABOOM:
self.update_screen()
self.kaboom()
elif bomb.is_touching(self.bucket):
self.level.score_up()
to_delete.append(index)
for i in to_delete:
del self.bombs[i]
def drop(self):
if self.level.should_drop():
self.bombs.append(Bomb(self.bomber.center_of()))
def kaboom(self):
print("kaboom")
self.bomber.reset()
self.lives = self.lives - 1
if self.lives == 0:
print("lost")
pygame.quit()
sys.exit()
self.bombs = []
#change screen
self.level.wait_and_start()
#lose life and restart level
#add (drop) bomb?
#stop dropping?
#levelup
#lose a life / kaboom? -> restart level
#move them
#check for catches and drops // do in Bomb.move()
class Bomber(Sprite):
def __init__(self):
Sprite.__init__(self, pygame.image.load("img/bomber.png"), [int(WIDTH/2 - 32), SKY_HEIGHT - 48])
self._nextx = int(random.randrange(32,WIDTH - 32))
def reset(self):
self._x = int(WIDTH/2 - 32)
self._y = SKY_HEIGHT - 48
self._nextx = int(random.randrange(32,WIDTH - 32))
def move(self, step):
if abs(self._x - self._nextx) < step:
self._nextx = int(random.randrange(32,WIDTH - 32))
if self._x < self._nextx:
self._x = self._x + step
elif self._x > self._nextx:
self._x = self._x - step
self.show()
#bucket initialization
class Bucket(Sprite):
def __init__(self):
Sprite.__init__(self, pygame.image.load("img/3buckets.png"), [int(WIDTH/2), HEIGHT])
self.set_posn([int(WIDTH/2), HEIGHT - self._img.hitbox[1] - 15])
self._num_buckets = 3
def move(self):
ex, z = pygame.mouse.get_pos()
self._x = ex - self._img.hitbox[0]/2
self.show()
def loseBucket(self):
Sprite.__init__(self, pygame.image.load("img/3buckets.png"), [int(WIDTH/2), HEIGHT])
#bombs initialization
class Bomb(Sprite):
def __init__(self, center):
Sprite.__init__(self, pygame.image.load("img/bomb.png"), [center[0], center[1]] )
def move(self, stp):
self._y = self._y + stp
if self._y + self.hitbox()[1] > HEIGHT:
return KABOOM
self.show()
game = Game()
while True: #main loop
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
print("quit")
sys.exit()
game.update()
clock.tick(FPS)