-
Notifications
You must be signed in to change notification settings - Fork 0
/
maryo.py
288 lines (208 loc) · 7.43 KB
/
maryo.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
# Import modules
import pygame, random, sys
from pygame.locals import *
pygame.init()
# intialising variables for ease
window_height = 600
window_width = 1200
blue = (0, 0, 255)
black = (0, 0, 0)
white = (255, 255, 255)
fps = 25
level = 0
addnewflamerate = 20
# defining the required function
class dragon:
global firerect, imagerect, Canvas
up = False
down = True
velocity = 15
def __init__(self):
self.image = load_image('dragon.png')
self.imagerect = self.image.get_rect()
self.imagerect.right = window_width
self.imagerect.top = window_height/2
def update(self):
if self.imagerect.top < cactusrect.bottom:
self.up = False
self.down = True
if self.imagerect.bottom > firerect.top:
self.up = True
self.down = False
if self.down:
self.imagerect.bottom += self.velocity
if self.up:
self.imagerect.top -= self.velocity
Canvas.blit(self.image, self.imagerect)
def return_height(self):
h = self.imagerect.top
return h
class flames:
flamespeed = 20
def __init__(self):
self.image = load_image('fireball.png')
self.imagerect = self.image.get_rect()
self.height = Dragon.return_height() + 20
self.surface = pygame.transform.scale(self.image, (20, 20))
self.imagerect = pygame.Rect(window_width - 106, self.height, 20, 20)
def update(self):
self.imagerect.left -= self.flamespeed
def collision(self):
if self.imagerect.left == 0:
return True
else:
return False
class maryo:
global moveup, movedown, gravity, cactusrect, firerect
speed = 10
downspeed = 20
def __init__(self):
self.image = load_image('maryo.png')
self.imagerect = self.image.get_rect()
self.imagerect.topleft = (50, window_height/2)
self.score = 0
def update(self):
if moveup and self.imagerect.top > cactusrect.bottom:
self.imagerect.top -= self.speed
self.score += 1
if movedown and self.imagerect.bottom < firerect.top:
self.imagerect.bottom += self.downspeed
self.score += 1
if gravity and self.imagerect.bottom < firerect.top:
self.imagerect.bottom += self.speed
def terminate(): # to end the program
pygame.quit()
sys.exit()
def waitforkey():
while True : # to wait for user to start
for event in pygame.event.get():
if event.type == pygame.QUIT:
terminate()
if event.type == pygame.KEYDOWN: # to terminate if the user presses the escape key
if event.key == pygame.K_ESCAPE:
terminate()
return
def flamehitsmario(playerrect, flames): # to check if flame has hit mario or not
for f in flame_list:
if playerrect.colliderect(f.imagerect):
return True
return False
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)
def check_level(score):
global window_height, level, cactusrect, firerect
if score in range(0, 250):
firerect.top = window_height - 50
cactusrect.bottom = 50
level = 1
elif score in range(250, 500):
firerect.top = window_height - 100
cactusrect.bottom = 100
level = 2
elif score in range(500, 750):
level = 3
firerect.top = window_height-150
cactusrect.bottom = 150
elif score in range(750, 1000):
level = 4
firerect.top = window_height - 200
cactusrect.bottom = 200
def load_image(imagename):
return pygame.image.load(imagename)
# end of functions, begin to start the main code
mainClock = pygame.time.Clock()
Canvas = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption('MARYO')
# setting up font and sounds and images
font = pygame.font.SysFont(None, 48)
scorefont = pygame.font.SysFont(None, 30)
fireimage = load_image('fire_bricks.png')
firerect = fireimage.get_rect()
cactusimage = load_image('cactus_bricks.png')
cactusrect = cactusimage.get_rect()
startimage = load_image('start.png')
startimagerect = startimage.get_rect()
startimagerect.centerx = window_width/2
startimagerect.centery = window_height/2
endimage = load_image('end.png')
endimagerect = startimage.get_rect()
endimagerect.centerx = window_width/2
endimagerect.centery = window_height/2
pygame.mixer.music.load('mario_theme.wav')
gameover = pygame.mixer.Sound('mario_dies.wav')
# getting to the start screen
drawtext('Mario', font, Canvas, (window_width/3), (window_height/3))
Canvas.blit(startimage, startimagerect)
pygame.display.update()
waitforkey()
# start for the main code
topscore = 0
Dragon = dragon()
while True:
flame_list = []
player = maryo()
moveup = movedown = gravity = False
flameaddcounter = 0
gameover.stop()
pygame.mixer.music.play(-1, 0.0)
while True: # the main game loop
for event in pygame.event.get():
if event.type == QUIT:
terminate()
if event.type == KEYDOWN:
if event.key == K_UP:
movedown = False
moveup = True
gravity = False
if event.key == K_DOWN:
movedown = True
moveup = False
gravity = False
if event.type == KEYUP:
if event.key == K_UP:
moveup = False
gravity = True
if event.key == K_DOWN:
movedown = False
gravity = True
if event.key == K_ESCAPE:
terminate()
flameaddcounter += 1
check_level(player.score)
if flameaddcounter == addnewflamerate:
flameaddcounter = 0
newflame = flames()
flame_list.append(newflame)
for f in flame_list:
flames.update(f)
for f in flame_list:
if f.imagerect.left <= 0:
flame_list.remove(f)
player.update()
Dragon.update()
Canvas.fill(black)
Canvas.blit(fireimage, firerect)
Canvas.blit(cactusimage, cactusrect)
Canvas.blit(player.image, player.imagerect)
Canvas.blit(Dragon.image, Dragon.imagerect)
drawtext('Score : %s | Top score : %s | Level : %s' % (player.score, topscore, level), scorefont, Canvas, 350, cactusrect.bottom + 10)
for f in flame_list:
Canvas.blit(f.surface, f.imagerect)
if flamehitsmario(player.imagerect, flame_list):
if player.score > topscore:
topscore = player.score
break
if player.imagerect.top <= cactusrect.bottom or player.imagerect.bottom >= firerect.top:
if player.score > topscore:
topscore = player.score
break
pygame.display.update()
mainClock.tick(fps)
pygame.mixer.music.stop()
gameover.play()
Canvas.blit(endimage, endimagerect)
pygame.display.update()
waitforkey()