-
Notifications
You must be signed in to change notification settings - Fork 5
/
vampirepizza_mod1.py
405 lines (316 loc) · 12 KB
/
vampirepizza_mod1.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#Set-up Game
#import libraries
import pygame
from pygame import *
from random import randint
#initialize pygame
pygame.init()
#set up clock
clock = time.Clock()
#-----------------------------
#Define constant variables
#Define the parameters of the game window
WINDOW_WIDTH = 1100
WINDOW_HEIGHT = 600
WINDOW_RES = (WINDOW_WIDTH, WINDOW_HEIGHT)
# Define the parameters of each tile
WIDTH = 100
HEIGHT = 100
# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
#Set up rates
FRAMERATE = 20
SPAWNRATE = 360
#Set up counters
STARTING_BUCKS = 15
BUCK_RATE = 10
BUCK_BOOSTER = 1
STARTING_REVIEWS = 0
WIN_TIME = FRAMERATE * 60 * 3
STARTING_HEALTH = 150
#Define speeds
REG_SPEED = 2
SLOW_SPEED = 1
#--------------------------------------------------------------
#Load Assets
#create window
GAME_WINDOW = display.set_mode(WINDOW_RES)
display.set_caption('Vampire Pizza')
#set up background image
background_img = image.load('restaurant.jpg')
background_surf = Surface.convert(background_img)
BACKGROUND = transform.scale(background_surf, (WINDOW_RES))
#set up enemy imgage
pizza_img = image.load('vampire.png')
pizza_surf = Surface.convert(pizza_img)
VAMPIRE_PIZZA = transform.scale(pizza_surf, (WIDTH, HEIGHT))
# tile trap images
garlic_img = image.load('garlic.jpg')
garlic_surf = Surface.convert(garlic_img)
GARLIC = transform.scale(garlic_surf, (WIDTH, HEIGHT))
GARLIC.set_alpha(127)
cutter_img = image.load('pizzacutter.png')
cutter_surf = Surface.convert(cutter_img)
CUTTER = transform.scale(cutter_surf, (WIDTH, HEIGHT))
CUTTER.set_alpha(127)
pepperoni_img = image.load('pepperoni.jpg')
pepperoni_surf = Surface.convert(pepperoni_img)
PEPPERONI = transform.scale(pepperoni_surf, (WIDTH, HEIGHT))
PEPPERONI.set_alpha(127)
#load pizza health images
med_health_img = image.load('pizza60health.jpg')
med_health_surf = Surface.convert(med_health_img)
MED_HEALTH = transform.scale(med_health_surf, (WIDTH, HEIGHT))
low_health_img = image.load('pizza30health.png')
low_health_surf = Surface.convert(low_health_img)
LOW_HEALTH = transform.scale(low_health_surf, (WIDTH, HEIGHT))
#---------------------------------------------
#Set up classes
#Create an enemy class
class VampireSprite(sprite.Sprite):
#This function creates an instance of the enemy
def __init__(self):
super(VampireSprite, self).__init__()
self.speed = REG_SPEED
self.lane = randint(0, 4)
all_vampires.add(self)
self.image = VAMPIRE_PIZZA.copy()
y = 50 + self.lane * 100
self.rect = self.image.get_rect(center=(1150, y))
self.health = STARTING_HEALTH
#This function moves the enemies from right to left and destroys them after they've left the screen
def update(self, GAME_WINDOW, counters):
GAME_WINDOW.blit(BACKGROUND, (self.rect.x, self.rect.y), self.rect)
self.rect.x -= self.speed
if self.health <= 0 or self.rect.x <= 100:
if self.rect.x <= 100:
counters.reviews += 1
self.kill()
else:
if self.health * 100 / STARTING_HEALTH > 60:
self.image = VAMPIRE_PIZZA.copy()
elif self.health * 100 / STARTING_HEALTH > 30:
self.image = MED_HEALTH.copy()
else:
self.image = LOW_HEALTH.copy()
GAME_WINDOW.blit(self.image, (self.rect.x, self.rect.y))
def attack(self, tile):
if tile.trap == SLOW:
self.speed = SLOW_SPEED
if tile.trap == DAMAGE:
self.health -= 1
class Counters:
#def __init__(self, pizza_bucks, buck_rate, buck_booster):
def __init__(self, pizza_bucks, buck_rate, buck_booster, reviews, timer):
self.loop_count = 0
self.display_font = display_font = pygame.font.Font('pizza-font.ttf', 25)
self.pizza_bucks = pizza_bucks
self.buck_rate = buck_rate
self.buck_booster = buck_booster
self.bucks_rect = None
self.reviews = reviews
self.reviews_rect = None
self.timer = timer
self.timer_rect = None
def increment_bucks(self):
if self.loop_count % self.buck_rate == 0:
self.pizza_bucks += self.buck_booster
def draw_bucks(self, GAME_WINDOW):
if bool(self.bucks_rect):
GAME_WINDOW.blit(BACKGROUND, (self.bucks_rect.x, self.bucks_rect.y), self.bucks_rect)
bucks_surf = self.display_font.render(str(self.pizza_bucks), True, WHITE)
self.bucks_rect = bucks_surf.get_rect()
self.bucks_rect.x = WINDOW_WIDTH - 50
self.bucks_rect.y = WINDOW_HEIGHT - 50
GAME_WINDOW.blit(bucks_surf, self.bucks_rect)
def draw_reviews(self, GAME_WINDOW):
if bool(self.reviews_rect):
GAME_WINDOW.blit(BACKGROUND, (self.reviews_rect.x, self.reviews_rect.y), self.reviews_rect)
reviews_surf = self.display_font.render(str(self.reviews), True, WHITE)
self.reviews_rect = reviews_surf.get_rect()
self.reviews_rect.x = WINDOW_WIDTH - 150
self.reviews_rect.y = WINDOW_HEIGHT - 50
GAME_WINDOW.blit(reviews_surf, self.reviews_rect)
def draw_time(self, GAME_WINDOW):
if bool(self.timer_rect):
GAME_WINDOW.blit(BACKGROUND, (self.timer_rect.x, self.timer_rect.y), self.timer_rect)
timer_surf = self.display_font.render(str(int((WIN_TIME - self.loop_count) / FRAMERATE)), True, WHITE)
self.timer_rect = timer_surf.get_rect()
self.timer_rect.x = WINDOW_WIDTH - 250
self.timer_rect.y = WINDOW_HEIGHT - 50
GAME_WINDOW.blit(timer_surf, self.timer_rect)
def update(self, GAME_WINDOW):
self.loop_count += 1
self.increment_bucks()
self.draw_bucks(GAME_WINDOW)
self.draw_reviews(GAME_WINDOW)
self.draw_time(GAME_WINDOW)
#Trap types
class Trap(object):
def __init__(self, trap_type, cost, trap_img):
self.trap_type = trap_type
self.cost = cost
self.trap_img = trap_img
class TrapApplicator(object):
def __init__(self):
self.selected = None
def select_trap(self, trap):
if trap.cost <= counters.pizza_bucks:
self.selected = trap
def select_tile(self, tile, counters):
self.selected = tile.trap_applied(self.selected, counters)
#Create a class of sprites. Each tile has an invisible interactive field attached to it which is a sprite in this class.
class BackgroundTile(sprite.Sprite):
def __init__(self):
super(BackgroundTile, self).__init__()
self.trap = None
class PlayTile(BackgroundTile):
def trap_applied(self, trap, counters):
if bool(trap) and not bool(self.trap):
counters.pizza_bucks -= trap.cost
self.trap = trap
if trap == EARN:
counters.buck_booster += 1
return None
def draw_trap(self, GAME_WINDOW, trap_applicator):
if bool(self.trap):
GAME_WINDOW.blit(self.trap.trap_img, (self.rect.x, self.rect.y))
class ButtonTile(BackgroundTile):
def trap_applied(self, trap, counters):
if counters.pizza_bucks >= self.trap.cost:
return self.trap
return trap
def draw_trap(self, GAME_WINDOW, trap_applicator):
if bool(trap_applicator.selected) and trap_applicator.selected == self.trap:
GAME_WINDOW.blit(self.trap.trap_img, (self.rect.x, self.rect.y))
class InactiveTile(BackgroundTile):
def trap_applied(self, trap, counters):
return trap
def draw_trap(self, GAME_WINDOW, trap_applicator):
pass
#-------------------------------------------------------------
#Create class instances
#create a sprite group for all the VampireSprite instances
all_vampires = sprite.Group()
#counters = Counters(STARTING_BUCKS, BUCK_RATE, BUCK_BOOSTER)
counters = Counters(STARTING_BUCKS, BUCK_RATE, BUCK_BOOSTER, STARTING_REVIEWS, WIN_TIME)
SLOW = Trap('SLOW', 5, GARLIC)
DAMAGE = Trap('DAMAGE', 3, CUTTER)
EARN = Trap('EARN', 7, PEPPERONI)
trap_applicator = TrapApplicator()
#--------------------------------------------------------------
#Draw BAckground Grid
#create an empty array for each tile location
tile_grid = []
# Draw the grid
tile_color = WHITE
for row in range(6):
row_of_tiles = []
tile_grid.append(row_of_tiles)
for column in range(11):
if column <= 1:
new_tile = InactiveTile()
else:
if row == 5:
if 1 < column < 5:
new_tile = ButtonTile()
new_tile.trap = [SLOW, DAMAGE, EARN][column - 2]
else:
new_tile = InactiveTile()
else:
new_tile = PlayTile()
new_tile.rect = pygame.Rect(WIDTH * column, HEIGHT * row, WIDTH, HEIGHT)
row_of_tiles.append(new_tile)
if row == 5 and 1 < column < 5:
BACKGROUND.blit(new_tile.trap.trap_img, (new_tile.rect.x, new_tile.rect.y))
if column != 0 and row != 5:
if column != 1:
draw.rect(BACKGROUND, tile_color, (WIDTH * column, HEIGHT * row, WIDTH, HEIGHT), 1)
GAME_WINDOW.blit(BACKGROUND, (0,0))
#--------------------------------------------------------------------------------------------------------------------------------------
#Start Main Game Loop
#Game Loop
running = True
waiting = True
while running:
#------------------------------------------
#Check for events
#checking for and handling events
for event in pygame.event.get():
#exit loop on quit
if event.type == QUIT:
running = False
waiting = False
#Set up the background tiles to respond to a mouse click
elif event.type == pygame.MOUSEBUTTONDOWN:
x, y = pygame.mouse.get_pos()
trap_applicator.select_tile(tile_grid[y // 100][x // 100], counters)
#-------------------------------------------------
#Create VampireSprite instances
if randint(0, SPAWNRATE) == 1:
VampireSprite()
#------------------------------------------------
#Set up collision detection
#draw the background grid
for tile_row in tile_grid:
for tile in tile_row:
if bool(tile.trap):
GAME_WINDOW.blit(BACKGROUND, (tile.rect.x, tile.rect.y), tile.rect)
#set up collision detection
for vampire in all_vampires:
tile_row = tile_grid[vampire.rect.y // 100]
vampire_left_side_x = vampire.rect.x // 100
vampire_right_side_x = (vampire.rect.x + vampire.rect.width) // 100
if -1 < vampire_left_side_x < 10:
left_tile = tile_row[vampire_left_side_x]
else:
left_tile = None
if -1 < vampire_right_side_x < 10:
right_tile_wall = tile_row[vampire_right_side_x]
else:
right_tile = None
if bool(left_tile):
vampire.attack(left_tile)
if bool(right_tile) and right_tile.x != left_tile.x:
vampire.attack(right_tile)
#-------------------------------------------------
#Set win/lose conditions
if counters.reviews >= 3:
running = False
if counters.loop_count > WIN_TIME:
running = False
#-------------------------------------------------
#Update displays
for vampire in all_vampires:
vampire.update(GAME_WINDOW, counters)
for tile_row in tile_grid:
for tile in tile_row:
tile.draw_trap(GAME_WINDOW, trap_applicator)
counters.update(GAME_WINDOW)
display.update()
#set the framerate
clock.tick(FRAMERATE)
#Close Main Game Loop
#------------------------------------------------------------------------------------------------------------------------
#End of game loop
#Set end game message
end_font = pygame.font.Font('pizza-font.ttf', 50)
if waiting:
if counters.reviews >= 3:
end_surf = end_font.render('Game Over', True, WHITE)
else:
end_surf = end_font.render('You Win!!', True, WHITE)
GAME_WINDOW.blit(end_surf, (350, 200))
display.update()
#-------------------
#Enable exit from end game loop
while waiting:
for event in pygame.event.get():
if event.type == QUIT:
waiting = False
clock.tick(FRAMERATE)
#Close end game loop
#---------------------
#Clean-up Game
pygame.quit()