-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvampirepizza_chapter6.py
129 lines (87 loc) · 2.99 KB
/
vampirepizza_chapter6.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
#Set-up Game
#import libraries
import pygame
from pygame import *
from random import randint
#initialize pygame
pygame.init()
#-----------------------------
#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
#--------------------------------------------------------------
#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))
#---------------------------------------------
#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(self.image, (self.rect.x, self.rect.y))
#-------------------------------------------------------------
#Create class instances
#create a sprite group for all the VampireSprite instances
all_vampires = sprite.Group()
#--------------------------------------------------------------
# Initialize and draw Background Grid
# Populate the grid
tile_color = WHITE
for row in range(6):
for column in range(11):
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
#-------------------------------------------------
#Create VampireSprite instances
if randint(1, SPAWNRATE) == 1:
VampireSprite()
#-------------------------------------------------
#Update displays
for vampire in all_vampires:
vampire.update(GAME_WINDOW)
display.update()
#Close Main Game Loop
#------------------------------------------------------------------------------------------------------------------------
#End of game loop
#Clean-up Game
pygame.quit()