-
Notifications
You must be signed in to change notification settings - Fork 0
/
Final project.py
316 lines (267 loc) · 8.37 KB
/
Final project.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
import pgzrun
import random
import sys
TITLE = 'Wizard 102'
WIDTH = 1440
HEIGHT = 719
game = Actor('title_screen')
game.state = ['title', 'game', 'gameover', 'about', 'win', 'about_2']
game.current_state = game.state[0]
fires = []
bolts = []
fire = Actor('fireball')
bolt = Actor('bolt')
dragon2 = Actor('small_dragon')
dragon = Actor('dragon')
wizard = Actor('wizard')
wizard.pos = (WIDTH // 2, HEIGHT - 100)
wizard.score = 0
wizard.lives = 3
dragons2 = []
bolt = Actor('bolt')
bolt.pos = (WIDTH // 2, HEIGHT - 100)
def draw_title():
screen.blit('title_screen', (0,0))
def draw_game():
screen.blit('background', (0,0))
wizard.draw()
dragon.draw()
def draw_game_over():
screen.blit('game_over', (0,0))
screen.blit('about_button', (500, 500))
sounds.game_over.play()
def draw_about_screen():
screen.blit('about_screen', (0,0))
def draw_win_screen():
screen.blit('win', (0,0))
screen.blit('about_button', (500, 500))
def draw_about_screen2():
screen.blit('about_screen', (0,0))
def draw():
if game.current_state == 'title':
draw_title()
screen.draw.text("Press esc to quit", bottomright = (WIDTH//2 + 50, HEIGHT - 100))
elif game.current_state == 'game':
draw_game()
screen.draw.text("Score: " + str(wizard.score), bottomright=(WIDTH-10, HEIGHT-5))
screen.draw.text("lives: " + str(wizard.lives), bottomleft=(10, HEIGHT - 5))
draw_fire()
draw_dragons2()
draw_bolt()
screen.draw.text("Press esc to quit", topleft = (10, 10))
elif game.current_state == 'gameover':
draw_game_over()
elif game.current_state == 'about':
draw_about_screen()
elif game.current_state == 'win':
draw_win_screen()
elif game.current_state == 'about_2':
draw_about_screen2()
def on_key_down(key):
if game.current_state == 'title':
if key == keys.RETURN:
game.current_state = game.state[1]
sounds.main_track.play()
clock.unschedule(spawn_fireball)
clock.unschedule(spawn_dragon2)
clock.schedule_interval(spawn_fireball, .5)
clock.schedule_interval(spawn_dragon2, 1.2)
dragon.pos = (20, 65)
elif game.current_state == 'game':
if wizard.score == 1000:
clock.unschedule(spawn_fireball)
clock.unschedule(spawn_dragon2)
clock.schedule_interval(spawn_fireball, .40)
clock.schedule_interval(spawn_dragon2, .50)
if wizard.score == 2000:
clock.unschedule(spawn_fireball)
clock.unschedule(spawn_dragon2)
clock.schedule_interval(spawn_fireball, .35)
clock.schedule_interval(spawn_dragon2, .12)
if wizard.score == 5000:
clock.unschedule(spawn_fireball)
clock.unschedule(spawn_dragon2)
clock.schedule_interval(spawn_fireball, .35)
clock.schedule_interval(spawn_dragon2, .08)
if wizard.score >= 7500:
sounds.main_track.stop()
sounds.win_music.play()
game.current_state = game.state[4]
print(wizard.score)
if key == keys.SPACE and len(bolts) < 3:
spawn_bolt()
print(wizard.score)
if wizard.lives == 0:
sounds.main_track.stop()
game.current_state = game.state[2]
elif game.current_state == 'gameover':
clock.unschedule(spawn_dragon2)
wizard.score = 0
wizard.lives = 3
if key == keys.RETURN:
game.current_state = game.state[0]
sounds.game_over.stop()
fires = []
dragons2 = []
if key == keys.SPACE:
game.current_state = game.state[3]
sounds.game_over.stop()
elif game.current_state == 'about':
if key == keys.SPACE:
game.current_state = game.state[2]
elif game.current_state == 'win':
clock.unschedule(spawn_dragon2)
wizard.score = 0
wizard.lives = 3
fires = []
dragons2 = []
if key == keys.RETURN:
game.current_state = game.state[0]
sounds.win_music.stop()
if key == keys.SPACE:
game.current_state = game.state[5]
elif game.current_state == 'about_2':
if key == keys.SPACE:
game.current_state = game.state[4]
if key == keys.ESCAPE:
sys.exit()
def draw_fire():
global fires
for fire in fires:
fire.draw()
def draw_bolt():
global bolts
for bolt in bolts:
bolt.draw()
def draw_dragons2():
global dragons2
for dragon2 in dragons2:
dragon2.draw()
def move_dragon(time):
if game.current_state == 'game':
if dragon.left < WIDTH:
dragon.x += time * 400
if dragon.left > WIDTH:
dragon.x = 0
def move_dragons2(time):
global dragons2
for dragon2 in dragons2:
dragon2.y += time * 400
def spawn_fireball():
if game.current_state == 'game':
global fires
fire = Actor('fireball')
fire.pos = (dragon.x, dragon.y+60)
fire.render = True
fires.append(fire)
def spawn_bolt():
if game.current_state == 'game':
global bolts
bolt = Actor('bolt')
bolt.pos = (wizard.x - 20, wizard.y - 50)
bolt.render = True
bolts.append(bolt)
sounds.player_attack.play()
def move_fire(time):
global fires
for fire in fires:
fire.y += time * 100
def move_bolt(time):
global bolts
for bolt in bolts:
bolt.y -= time * 100
def clean_up():
global fires
global dragons2
new_fires = []
for fire in fires:
if not fire.bottom < 0 and fire.render:
new_fires.append(fire)
if game.current_state =='gameover':
fires = new_fires
fires = new_fires
new_dragons = []
for dragon2 in dragons2:
if not dragon2.top > HEIGHT and dragon2.render:
new_dragons.append(dragon2)
dragons2 = new_dragons
global bolts
new_bolts = []
for bolt in bolts:
if not bolt.top < 0 and bolt.render:
new_bolts.append(bolt)
if game.current_state =='gameover':
bolts = new_bolts
bolts = new_bolts
def check_collisions():
global fires
for fire in fires:
if wizard.colliderect(fire):
wizard.lives -= 1
fire.render = False
sounds.hit_sound.play()
sounds.player_attack.stop()
if wizard.colliderect(fire) and wizard.lives == 0 or game.current_state == 'gameover':
fires =[]
sounds.main_track.stop()
if game.current_state == "win":
fires =[]
sounds.main_track.stop()
global dragons2
for dragon2 in dragons2:
if wizard.colliderect(dragon2):
wizard.lives -= 1
dragon2.render = False
sounds.hit_sound.play()
sounds.player_attack.stop()
if wizard.colliderect(dragon2) and wizard.lives == 0 or game.current_state == 'gameover':
dragons2 =[]
sounds.main_track.stop()
if game.current_state == 'win':
dragons2 =[]
sounds.main_track.stop()
global bolts
for bolt in bolts:
if dragon.colliderect(bolt):
wizard.score += 50
bolt.render = False
print(1)
for dragon2 in dragons2:
if dragon2.colliderect(bolt):
wizard.score += 50
bolt.render = False
dragon2.render = False
print(1)
def gameover():
if wizard.lives <= 0:
fires = []
dragons2 = []
game.current_state = game.state[2]
def check_keys(time):
if game.current_state == 'game':
if keyboard.LEFT:
wizard.x -= time * 450
if wizard.left < 0:
wizard.left = 0
if keyboard.RIGHT:
wizard.x += time * 450
if wizard.right > WIDTH:
wizard.right = WIDTH
def spawn_dragon2():
global dragons2
dragon2 = Actor('small_dragon')
xpos = random.randint(dragon2.width, WIDTH - dragon2.width)
dragon2.midbottom = (xpos, 0)
dragon2.render = True
dragons2.append(dragon2)
def update(time):
check_keys(time)
move_dragon(time)
move_fire(time)
move_bolt(time * 6)
check_collisions()
gameover()
dragon.draw()
move_dragons2(time)
clean_up()
pgzrun.go()