-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathvampirepizza_chapter8.py
More file actions
170 lines (118 loc) · 4.21 KB
/
vampirepizza_chapter8.py
File metadata and controls
170 lines (118 loc) · 4.21 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
#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
WHITE = (255, 255, 255)
#Set up rates
SPAWNRATE = 360
FRAMERATE = 60
#--------------------------------------------------------------
#Load Assets
#create window
GAME_WINDOW = display.set_mode(WINDOW_RES)
display.set_caption('Vampire Pizza')
#set up enemy imgage
pizza_img = image.load('vampire.png')
pizza_surf = Surface.convert(pizza_img)
VAMPIRE_PIZZA = transform.scale(pizza_surf, (100, 100))
#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 classes
#Create an enemy class
class VampireSprite(sprite.Sprite):
#This function creates an instance of the enemy
def __init__(self):
super().__init__()
self.speed = 2
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=(1100, y))
#This function moves the enemies from right to left and destroys them after they've left the screen
def update(self, game_window):
game_window.blit(BACKGROUND, (self.rect.x, self.rect.y), self.rect)
self.rect.x -= self.speed
game_window.blit(self.image, (self.rect.x, self.rect.y))
#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().__init__()
self.effect = False
#-------------------------------------------------------------
#Create class instances
#create a sprite group for all the VampireSprite instances
all_vampires = sprite.Group()
#--------------------------------------------------------------
# Initialize and draw Background Grid
# Create an empty list to hold the tile grid
tile_grid = []
# Populate the grid
tile_color = WHITE
for row in range(6):
row_of_tiles = []
tile_grid.append(row_of_tiles)
for column in range(11):
new_tile = BackgroundTile()
new_tile.rect = pygame.Rect(WIDTH * column, HEIGHT * row, WIDTH, HEIGHT)
row_of_tiles.append(new_tile)
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
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
#Set up the background tiles to respond to a mouse click
elif event.type == pygame.MOUSEBUTTONDOWN:
x, y = pygame.mouse.get_pos()
tile_grid[y // 100][x //100].effect = True
print(x, y)
print('You clicked me!')
#-------------------------------------------------
#Create VampireSprite instances
if randint(1, SPAWNRATE) == 1:
VampireSprite()
#------------------------------------------------
#Set up collision detection
#draw the background grid
for tile_row in tile_grid:
for tile in tile_row:
GAME_WINDOW.blit(BACKGROUND, (tile.rect.x, tile.rect.y), tile.rect)
#-------------------------------------------------
#Update displays
for vampire in all_vampires:
vampire.update(GAME_WINDOW)
display.update()
#set the framerate
clock.tick(FRAMERATE)
#Close Main Game Loop
#------------------------------------------------------------------------------------------------------------------------
#End of game loop
#Clean-up Game
pygame.quit()