-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
340 lines (280 loc) · 12.3 KB
/
main.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
# Import the needed libraries
import pygame
from random import randint
from pygame import *
# Start pygame
pygame.init()
# Non-adjustable constants
WINDOW_WIDTH = 1100
WINDOW_HEIGHT = 600
TILE_SIZE = 100
FRAME_RATE = 60
# Adjustable constants
WHITE = (255, 255, 255)
HIGHLIGHT_COLOUR = (238, 190, 47)
GAME_OVER_COLOUR = (255, 0, 0)
WIN_COLOUR = (0, 255, 0)
SPAWN_RATE = 360
STARTING_BUCKS = 15
BUCK_RATE = 120
STARTING_BUCK_BOOSTER = 1
MAX_BAD_REVIEWS = 3
WIN_TIME = FRAME_RATE * 60 * 1
REG_SPEED = 2
SLOW_SPEED = 1
DRAW_GRID = False
# load an image, if only a name is provided, assume that it is a square tile (because most objects in this game are)
def load_img(filename, width=TILE_SIZE, height=TILE_SIZE):
img = image.load(filename)
surf = Surface.convert_alpha(img)
return transform.scale(surf, (width, height))
# Get the Rect() object corresponding to a given row and column
def rect_from_position(row, col):
return Rect(TILE_SIZE * col, TILE_SIZE * row, TILE_SIZE, TILE_SIZE)
# Draw some text at a given position
def draw_text(game_window, font, text, x, y):
surf = font.render(text, True, WHITE)
rect = surf.get_rect()
rect.x = x
rect.y = y
game_window.blit(surf, rect)
# Set up the window with a size and name
GAME_WINDOW = display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
display.set_caption('Attack of the Vampire Pizzas!')
# Make a clock, this will regulate the framerate of the game
clock = time.Clock()
# Load all of the assets
BACKGROUND = load_img('restaurant.jpg', WINDOW_WIDTH, WINDOW_HEIGHT)
VAMPIRE_PIZZA = load_img('vampire.png')
GARLIC = load_img('garlic.png')
CUTTER = load_img('pizzacutter.png')
PEPPERONI = load_img('pepperoni.png')
# Load the fonts
SMALL_FONT = font.Font('pizza_font.ttf', 25)
END_FONT = font.Font('pizza_font.ttf', 50)
# VampireSprite is the main opponent. This class sets up a vampire, updates its logic, and draw it
class VampireSprite(sprite.Sprite):
def __init__(self):
super().__init__()
self.speed = REG_SPEED
self.image = VAMPIRE_PIZZA.copy()
y = 50 + randint(0, 4) * 100
self.rect = self.image.get_rect(center = (WINDOW_WIDTH, y))
self.health = 200
def update(self, game_window, counters):
# Move leftward
self.rect.x -= self.speed
# Find out where we are
tile_row = tile_grid[self.rect.y//100]
vamp_left_side = self.rect.x//100
vamp_right_side = (self.rect.x + self.rect.width)//100
# Assume that we aren't touching a tile on the left or right
right_tile = None
left_tile = None
# If we are, then update those assumptions...
if 0 <= vamp_left_side <= 10:
left_tile = tile_row[vamp_left_side]
if 0 <= vamp_right_side <= 10:
right_tile = tile_row[vamp_right_side]
# If there is a tile to the left, hit it
if not left_tile is None:
self.attack(left_tile)
# And if there is one on the right, that isn't the same as the one on the left,
# hit it too.
if not right_tile is None:
if right_tile != left_tile:
self.attack(right_tile)
# Remove the vampire if it is out of health, or got to the left
if self.health <= 0 or self.rect.x <= TILE_SIZE:
self.kill()
# If it was removed because it got to the far left, then increment the bad reviews
if self.rect.x <= 100:
counters.bad_reviews += 1
# Finally, draw it
game_window.blit(self.image, (self.rect.x, self.rect.y))
def attack(self, tile):
# If a vampire has been slowed, update its speed permanently to show that
if tile.trap == SLOW:
self.speed = SLOW_SPEED
# Pizza cutters just deal damage every frame, so reduce its health
if tile.trap == DAMAGE:
self.health -= 1
# There are other kinds of tiles, but if we don't handle them, they won't do anything, which
# is convenient for InactiveTile, etc
# Counters will manage keeping track of all of the game variables that change over time
class Counters(object):
def __init__(self):
self.loop_count = 0
self.pizza_bucks = STARTING_BUCKS
self.buck_booster = STARTING_BUCK_BOOSTER
self.bad_reviews = 0
def update(self, game_window):
# Increase the number of frames that have passed
self.loop_count += 1
# Every few frames, award more bucks
if self.loop_count % BUCK_RATE == 0:
self.pizza_bucks += self.buck_booster
# Draw the amount of money
draw_text(game_window, SMALL_FONT, str(self.pizza_bucks), WINDOW_WIDTH - 50, WINDOW_HEIGHT - 50)
# Draw the number of bad reviews
draw_text(game_window, SMALL_FONT, str(self.bad_reviews), WINDOW_WIDTH - 150, WINDOW_HEIGHT - 50)
# Draw the time remaining
draw_text(game_window, SMALL_FONT, str((WIN_TIME - self.loop_count) // FRAME_RATE), WINDOW_WIDTH - 250, WINDOW_HEIGHT - 50)
# A trap object knows its kind, how expensive it is, and what it looks like.
# Since it doesn't need to do anything else, it only has an __init__ method
class Trap(object):
def __init__(self, trap_kind, cost, trap_img):
self.trap_kind = trap_kind
self.cost = cost
self.trap_img = trap_img
# The TrapApplicator tracks which trap is selected (if any)
# Later, the PlayTile will check to see if the TrapApplicator has a selection,
# and if it does, it will set its own trap to the selection.
# In professional terms, this kind of object (a single use object that other objects
# look to for reference) is called a "Singleton"
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.set_trap(self.selected, counters)
# Every part of the game will be represented by a tile. What they all have in common is that they have a rectangle
# for their position and size, and they refer to a trap in some way
class BackgroundTile(sprite.Sprite):
def __init__(self, rect):
super().__init__()
self.trap = None
self.rect = rect
# A PlayTile represents tiles in the 'lanes' of the game. They can be either empty, or hold one trap in them
class PlayTile(BackgroundTile):
# When the user clicks, set_trap is called by TrapApplicator
def set_trap(self, trap, counters):
# If TrapApplicator gave us a trap, and we don't have one already (self.trap is)
if bool(trap) and self.trap is None:
# Pay the cost
counters.pizza_bucks -= trap.cost
self.trap = trap
# In the case of the earning trap, we can increase the rate of bucks right away
if trap == EARN:
counters.buck_booster += 1
# If and only if this PlayTile also holds a trap, draw the trap image
def draw(self, game_window, trap_applicator):
if not self.trap is None:
game_window.blit(self.trap.trap_img, (self.rect.x, self.rect.y))
# ButtonTiles represent the three buttons along the bottom of the screen
class ButtonTile(BackgroundTile):
def __init__(self, rect, trap):
super().__init__(rect)
self.trap = trap
# When clicked, if the player can afford to buy the trap, return that trap to the caller
def set_trap(self, trap, counters):
if counters.pizza_bucks >= self.trap.cost:
return self.trap
# draw the button
def draw(self, game_window, trap_applicator):
# Always draw the image of the trap it represents
GAME_WINDOW.blit(self.trap.trap_img, (self.rect.x, self.rect.y))
# But if it is also the selected trap, draw a rectangle around it
if not trap_applicator.selected is None:
if trap_applicator.selected == self.trap:
draw.rect(game_window, HIGHLIGHT_COLOUR, (self.rect.x, self.rect.y, TILE_SIZE, TILE_SIZE), 5)
# InactiveTiles do nothing, so we can call pass for both methods
class InactiveTile(BackgroundTile):
def set_trap(self, trap, counters):
pass
def draw(self, game_window, trap_applicator):
pass
# Here, we set up all of the game elements, and store them in varibles
all_vampires = sprite.Group()
counters = Counters()
SLOW = Trap('SLOW', 5, GARLIC)
DAMAGE = Trap('DAMAGE', 3, CUTTER)
EARN = Trap('EARN', 7, PEPPERONI)
trap_applicator = TrapApplicator()
# Set up all of the game tiles. Start by turning tile_grid into a 2D array, and fill it with InactiveTiles
tile_grid = []
for row in range(6):
# Every row will be its own list
row_of_tiles = []
tile_grid.append(row_of_tiles)
# For every column in that row...
for column in range(11):
# Add an InactiveTile at that position
tile_rect = rect_from_position(row, column)
row_of_tiles.append(InactiveTile(tile_rect))
# Go back and add our button tiles at set positions
tile_grid[5][2] = ButtonTile(rect_from_position(5, 2), SLOW)
tile_grid[5][3] = ButtonTile(rect_from_position(5, 3), DAMAGE)
tile_grid[5][4] = ButtonTile(rect_from_position(5, 4), EARN)
# Finally, replace the tiles over the lanes with PlayTiles instead of InactiveTiles
# (since PlayTiles are the only ones that can hold traps)
for column in range(2, 11):
for row in range(0, 5):
tile_grid[row][column] = PlayTile(rect_from_position(row, column))
# IF we want the grid overlay (as decided by the DRAW_GRID constant), draw a box around some of the tiles
if DRAW_GRID:
draw.rect(BACKGROUND, WHITE, rect_from_position(row, column), 1)
# Track the game state with two booleans. game_running means we are playing,
# program_running means that the game is over, but the window should stick around
game_running = True
program_running = True
while game_running:
# Handle clicking in the game, and trying to close the window
for event in pygame.event.get():
if event.type == QUIT:
# If we're trying to close the window, both the game and program stop
game_running = False
program_running = False
elif event.type == MOUSEBUTTONDOWN:
# If the user clicks, find out where
coordinates = mouse.get_pos()
x = coordinates[0]
y = coordinates[1]
# Convert the click position to tile coordinates
tile_y = y//100
tile_x = x//100
# Let the trap applicator deal with the click by passing it the tile
# that the player clicked on
trap_applicator.select_tile(tile_grid[tile_y][tile_x], counters)
# Every frame, paint over the whole screen with the background
GAME_WINDOW.blit(BACKGROUND, (0, 0))
# Very rarely, spawn a new vampire
if randint(1, SPAWN_RATE) == 1:
all_vampires.add(VampireSprite())
# Update every vampire
for vampire in all_vampires:
vampire.update(GAME_WINDOW, counters)
# Draw every tile
for tile_row in tile_grid:
for tile in tile_row:
tile.draw(GAME_WINDOW, trap_applicator)
# Check our win and lose conditions:
if counters.bad_reviews >= MAX_BAD_REVIEWS or counters.loop_count > WIN_TIME:
game_running = False
# Tick all of the counters
counters.update(GAME_WINDOW)
# Make the changes visible onscreen
display.update()
# Let the clock wait until the next frame should start
clock.tick(FRAME_RATE)
# After game_running stops being true, if program_running is still true, we've
# either won or lost. Figure out which, and draw it to the screen
if program_running:
if counters.bad_reviews >= MAX_BAD_REVIEWS:
end_surf = END_FONT.render('Game Over', True, GAME_OVER_COLOUR)
else:
end_surf = END_FONT.render('You Win!', True, WIN_COLOUR)
# Draw the text to the screen
GAME_WINDOW.blit(end_surf, (350, 200))
# Update the screen
display.update()
# Finally, loop until the user closes the window
while program_running:
for event in pygame.event.get():
if event.type == QUIT:
program_running = False
clock.tick(FRAME_RATE)
# Tell pygame we're done
pygame.quit()