Repository navigation
Expand file tree
/
Copy pathFinal Project.py
More file actions
281 lines (234 loc) · 8.29 KB
/
Copy pathFinal Project.py
File metadata and controls
281 lines (234 loc) · 8.29 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
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
import pygame,sys,random
from pygame.locals import *
pygame.init()
#variables
window_width=1200
window_height=600
black=(0,0,0)
white=(255,255,255)
no_of_fireballs=0
fireballs_list=[]
fireball_limit=15
score=0
score_update=0
level=1
topscore=0
#creating gamescreen
mainclock=pygame.time.Clock()
canvas=pygame.display.set_mode((window_width,window_height))
pygame.display.set_caption('Maryo')
canvas.fill(black)
pygame.display.update()
font = pygame.font.SysFont(None, 48)
scorefont = pygame.font.SysFont(None, 30)
gamestart=pygame.mixer.Sound('mario_theme.wav')
gameover=pygame.mixer.Sound('mario_dies.wav')
class flames:
def __init__(self):
self.image_flames = pygame.image.load('fire_bricks.png')
self.image_flames_rect = self.image_flames.get_rect()
self.image_flames_rect.top = 550
canvas.blit(self.image_flames, self.image_flames_rect)
def render_flames_image(self):
canvas.blit(self.image_flames, self.image_flames_rect)
def update_flames(self):
self.image_flames_rect.top -= 50
class cactus:
def __init__(self):
self.image_cactus = pygame.image.load('cactus_bricks.png')
self.image_cactus_rect = self.image_cactus.get_rect()
self.image_cactus_rect.top = -150
canvas.blit(self.image_cactus, self.image_cactus_rect)
def render_cactus_image(self):
canvas.blit(self.image_cactus, self.image_cactus_rect)
def update_cactus(self):
self.image_cactus_rect.top+=50
class maryo(cactus, flames):
def __init__(self):
self.pos_y = 300
self.pos_y_change = 18
self.image = pygame.image.load('pco5xxecE.png')
self.image = pygame.transform.scale(self.image,(40,40))
self.image_rect = self.image.get_rect()
self.image_rect.top = self.pos_y
self.image_rect.left = 50
canvas.blit(self.image, self.image_rect)
self.score_change=0
self.top_score=0
def movement(self, event):
if event.type == pygame.KEYDOWN:
if event.key == K_UP:
self.pos_y_change = -18
if event.type == pygame.KEYUP:
if event.key == K_UP:
self.pos_y_change = 18
def score_update(self,event):
if event.type == pygame.KEYDOWN:
if event.key == K_UP:
self.score_change=1
if event.type == pygame.KEYUP:
if event.key == K_UP:
self.score_change=0
return self.score_change
def render_player_image(self):
self.pos_y += self.pos_y_change
self.image_rect.top = self.pos_y
canvas.blit(self.image, self.image_rect)
class dragon(cactus, flames):
def __init__(self):
self.pos_y = 300
self.pos_y_change = -25
self.image = pygame.image.load('dragon.png')
self.image_rect = self.image.get_rect()
self.image_rect.top = self.pos_y
self.image_rect.right = 1180
canvas.blit(self.image, self.image_rect)
def movement_and_render(self):
if self.image_rect.top<=cactus.image_cactus_rect.bottom:
self.pos_y_change=25
elif self.image_rect.bottom>=550:
self.pos_y_change=-25
self.image_rect.top+=self.pos_y_change
canvas.blit(self.image, self.image_rect)
class fireballs(dragon):
def __init__(self):
self.image_fireballs = pygame.image.load('fireball.png')
self.image_fireballs = pygame.transform.scale(self.image_fireballs,(20,20))
self.image_fireballs_rect = self.image_fireballs.get_rect()
self.image_fireballs_rect.centery = dragon.image_rect.centery
self.image_fireballs_rect.centerx = dragon.image_rect.centerx - 5
canvas.blit(self.image_fireballs, self.image_fireballs_rect)
def update_fireballs(self):
self.image_fireballs_rect.left-=30
canvas.blit(self.image_fireballs,self.image_fireballs_rect)
def checklimit(self):
if(self.image_fireballs_rect).left<=0:
return True
return False
def check_boundary_collision(maryo,cactus,flames):
if maryo.image_rect.top<=cactus.image_cactus_rect.bottom-12 or maryo.image_rect.bottom>=flames.image_flames_rect.top+29:
return True
return False
def check_fireball_collision(fireballs_obj,player):
if player.image_rect.colliderect(fireballs_obj.image_fireballs_rect):
return True
return False
def level_update(score):
if score in range(0,250):
level=1
elif score in range (250,500):
level=2
elif score in range (500,750):
level=3
else:
level=4
return level
def drawtext(text, font, surface, x, y): #to display text on the screen
textobj = font.render(text, 1, white)
textrect = textobj.get_rect()
textrect.topleft = (x,y)
surface.blit(textobj, textrect)
#game objects
pygame.display.update()
#game variables
game=True
startscreen=True
gameplay=False
endscreen=False
#game objects
player = maryo()
cactus = cactus()
flames = flames()
dragon = dragon()
while game:
gameover.stop()
gamestart.play(-1,0)
while startscreen:
image_start=pygame.image.load('start.png')
image_start_rect=image_start.get_rect()
image_start_rect.centerx=600
image_start_rect.centery=300
canvas.blit(image_start,image_start_rect)
for event in pygame.event.get():
if event.type==pygame.KEYDOWN:
if event.key==K_ESCAPE:
game=False
gameplay=False
endscreen=False
startscreen=False
gameplay=True
pygame.display.update()
#main gameplay loop
while gameplay:
for event in pygame.event.get():
if event.key == K_ESCAPE:
game = False
gameplay = False
endscreen = False
player.movement(event)
score_update=player.score_update(event)
score += score_update
if level != level_update(score):
cactus.update_cactus()
flames.update_flames()
fireball_limit-=4
level=level_update(score)
canvas.fill(black)
cactus.render_cactus_image()
flames.render_flames_image()
player.render_player_image()
dragon.movement_and_render()
if(topscore<=score):
topscore=score
drawtext('Score : %s | Top Score : %s | Level %s' % (score,topscore,level),scorefont,canvas,350,cactus.image_cactus_rect.bottom+10)
#loop for creating fireballs
no_of_fireballs+=1
if no_of_fireballs>=fireball_limit:
newfireball=fireballs()
fireballs_list.append(newfireball)
no_of_fireballs=0
for i in fireballs_list:
fireballs.update_fireballs(i)
if check_fireball_collision(i,player):
gameplay=False
endscreen=True
if fireballs.checklimit(i):
fireballs_list.remove(i)
if check_boundary_collision(player,cactus,flames):
gameplay=False
endscreen=True
pygame.display.update()
mainclock.tick(15)
gamestart.stop()
gameover.play(0)
#game over screen loop
while endscreen:
image_end=pygame.image.load('end.png')
image_end_rect=image_end.get_rect()
image_end_rect.centerx = 600
image_end_rect.centery = 300
canvas.fill(black)
canvas.blit(image_end, image_end_rect)
canvas.blit(player.image,player.image_rect)
canvas.blit(flames.image_flames,flames.image_flames_rect)
canvas.blit(cactus.image_cactus,cactus.image_cactus_rect)
canvas.blit(dragon.image,dragon.image_rect)
for event in pygame.event.get():
if event.type==pygame.KEYDOWN:
if event.key == K_ESCAPE:
game = False
gameplay=True
endscreen = False
player.image_rect.top=300
player.image_rect.left=50
player.pos_y=300
player.pos_y_change=18
cactus.image_cactus_rect.top = -150
flames.image_flames_rect.top = 550
score=0
level=1
no_of_fireballs=0
fireballs_list.clear()
pygame.display.update()
pygame.quit()
quit()