forked from Kunal-Chauhan/Hoppy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
261 lines (233 loc) · 8.76 KB
/
main.py
File metadata and controls
261 lines (233 loc) · 8.76 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
# importing files and options
# file I/O
import pygame as pg
import random
from settings import *
from sprites import *
from os import path
class Game:
# initialising class attributes
def __init__(self):
# initialise game window
pg.init()
pg.mixer.init()
# making game window
self.screen = pg.display.set_mode((WIDTH, HEIGHT))
# setting a title
pg.display.set_caption(TITLE)
# clock
self.clock = pg.time.Clock()
# declaring variable for loop to run
self.running = True
self.font_name = pg.font.match_font(FONT_NAME)
self.load_data()
def load_data(self):
# loading highscore
self.dir = path.dirname(__file__)
img_dir = path.join(self.dir, 'img')
with open(path.join(self.dir, HS_FILE), 'w') as f:
try:
self.highscore = int(f.read())
except:
self.highscore = 0
# loading spritesheet image
self.spritesheet = Spritesheet(path.join(img_dir, SPRITESHEET))
# cloud images
self.cloud_images = []
for i in range(1, 4):
self.cloud_images.append(pg.image.load(
path.join(img_dir, 'cloud{}.png'.format(i))).convert())
# loading sounds
self.snd_dir = path.join(self.dir, 'snd')
self.jump_sound = pg.mixer.Sound(path.join(self.snd_dir, 'Jump33.wav'))
self.boost_sound = pg.mixer.Sound(
path.join(self.snd_dir, 'Boost1.ogg'))
# start new game
def new(self):
self.score = 0
# making a new sprite group
self.all_sprites = pg.sprite.LayeredUpdates()
# making platform group to hold all the platforms
self.platforms = pg.sprite.Group()
# powerup group
self.powerups = pg.sprite.Group()
# mob group
self.mobs = pg.sprite.Group()
self.clouds = pg.sprite.Group()
self.player = Player(self)
# spawning a platform
for plat in PLATFORM_LIST:
Platform(self, *plat)
self.mob_timer = 0
# loading music
pg.mixer.music.load(path.join(self.snd_dir, 'GameTheme.ogg'))
# spawning clouds in new screen
for i in range(8):
c = Cloud(self)
c.rect.y += 500
# running new game
self.run()
def run(self):
# game loop
# playing music as the game starts
pg.mixer.music.play(loops=-1)
# keep loop running at right speed
self.playing = True
while self.playing:
self.clock.tick(FPS)
self.events()
self.update()
self.draw()
# music stops when the game ends
pg.mixer.music.fadeout(250)
# game loop update
def update(self):
# updating properties
self.all_sprites.update()
# spawning enemy mob
now = pg.time.get_ticks()
if (now-self.mob_timer) > 5000 + random.choice([-1000, -500, 0, 500, 1000]):
self.mob_timer = now
Mob(self)
# player hit mob
mob_hits = pg.sprite.spritecollide(
self.player, self.mobs, False, pg.sprite.collide_mask)
if mob_hits:
self.playing = False
# check if playeer hits a platfrom only if falling
# if velocity is downward
if self.player.vel.y > 0:
hits = pg.sprite.spritecollide(self.player, self.platforms, False)
if hits:
lowest = hits[0]
for hit in hits:
if hit.rect.bottom > lowest.rect.bottom:
lowest = hit
if self.player.pos.x < lowest.rect.right+10 and self.player.pos.x > lowest.rect.left-10:
# setting player y position to platform top
if self.player.pos.y < hits[0].rect.centery:
self.player.pos.y = hits[0].rect.top
self.player.vel.y = 0
self.player.jumping = False
# if player reaches 1/4 of screen
if self.player.rect.top <= HEIGHT/4:
if random.randrange(100) < 15:
Cloud(self)
self.player.pos.y += max(abs(self.player.vel.y), 2)
for cloud in self.clouds:
cloud.rect.y += max(abs(self.player.vel.y / 2), 2)
for mob in self.mobs:
mob.rect.y += max(abs(self.player.vel.y), 2)
for plat in self.platforms:
plat.rect.y += max(abs(self.player.vel.y), 2)
if plat.rect.top >= HEIGHT:
plat.kill()
self.score += 10
# player hit power
pow_hits = pg.sprite.spritecollide(self.player, self.powerups, True)
for pow in pow_hits:
if pow.type == 'boost':
self.boost_sound.play()
self.player.vel.y = -BOOST_POWER
self.player.jumping = False
# player die
if self.player.rect.bottom > HEIGHT:
for sprite in self.all_sprites:
sprite.rect.y -= max(self.player.vel.y, 10)
if sprite.rect.bottom < 0:
sprite.kill()
if len(self.platforms) == 0:
self.playing = False
# spawning new platform to keep same number of platforms
while len(self.platforms) < 6:
width = random.randrange(50, 100)
Platform(self, random.randrange(0, WIDTH-width),
random.randrange(-75, -30))
def events(self):
# game loops events
# processing input
for event in pg.event.get():
# checking to close window
if event.type == pg.QUIT:
if self.playing:
self.playing = False
self.running = False
# checking jump event
if event.type == pg.KEYDOWN:
if event.key == pg.K_SPACE:
self.player.jump()
if event.type == pg.KEYUP:
if event.key == pg.K_SPACE:
self.player.jump_cut()
# game loop draw
def draw(self):
# draw
# adding backgroud color
self.screen.fill(BGCOLOR)
# drawing sprite on screen
self.all_sprites.draw(self.screen)
self.draw_text(str(self.score), 22, WHITE, WIDTH/2, 15)
# post drawing
pg.display.flip()
# start screen
def show_start_screen(self):
pg.mixer.music.load(path.join(self.snd_dir, 'HappyTune.ogg'))
pg.mixer.music.play(loops=-1)
self.screen.fill(BGCOLOR)
self.draw_text(TITLE, 48, WHITE, WIDTH/2, HEIGHT/4)
self.draw_text("Arrows to move and Space to jump",
22, WHITE, WIDTH/2, HEIGHT / 2)
self.draw_text("Press a key to play", 22, WHITE, WIDTH/2, HEIGHT * 3/4)
self.draw_text("High Score: " + str(self.highscore),
22, WHITE, WIDTH / 2, 15)
pg.display.flip()
self.wait_for_key()
pg.mixer.music.fadeout(500)
# game over or continue
def show_go_screen(self):
if not self.running:
return
pg.mixer.music.load(path.join(self.snd_dir, 'Yippee.ogg'))
pg.mixer.music.play(loops=-1)
self.screen.fill(BGCOLOR)
self.draw_text("Game Over!", 48, WHITE, WIDTH/2, HEIGHT/4)
self.draw_text("Score: "+str(self.score),
22, WHITE, WIDTH/2, HEIGHT / 2)
self.draw_text("Press a key to play again", 22,
WHITE, WIDTH/2, HEIGHT * 3/4)
if self.score > self.highscore:
self.highscore = self.score
self.draw_text("NEW HIGH SCORE: ", 22,
WHITE, WIDTH/2, HEIGHT / 2+40)
with open(path.join(self.dir, HS_FILE), 'w') as f:
f.write(str(self.score))
else:
self.draw_text("Highscore: " + str(self.highscore),
22, WHITE, WIDTH / 2, HEIGHT / 2+40)
pg.display.flip()
self.wait_for_key()
pg.mixer.music.fadeout(500)
def wait_for_key(self):
waiting = True
while waiting:
self.clock.tick(60)
for event in pg.event.get():
if event.type == pg.QUIT:
waiting = False
self.running = False
if event.type == pg.KEYUP:
waiting = False
def draw_text(self, text, size, color, x, y):
font = pg.font.Font(self.font_name, size)
text_surface = font.render(text, True, color)
text_rect = text_surface.get_rect()
text_rect.midtop = (x, y)
self.screen.blit(text_surface, text_rect)
G = Game()
# showing start screen
G.show_start_screen()
while G.running:
# creating new game
G.new()
G.show_go_screen()
pg.quit()