-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
94 lines (79 loc) · 2.86 KB
/
main.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
from sprites import *
from os import path
class Game:
def __init__(self):
pg.init()
pg.mixer.init()
self.flags = pg.FULLSCREEN | pg.DOUBLEBUF
pg.display.set_caption(TITLE)
self.screen = pg.display.set_mode(SIZE, vsync=1)
self.clock = pg.time.Clock()
self.running = True
self.playing = True
self.load_data()
def load_data(self):
self.dir = path.dirname(__file__)
img_dir = path.join(self.dir, 'assets/img')
self.background = pg.transform.scale(pg.image.load('assets/img/background.png').convert(), (WIDTH, HEIGHT))
self.spritesheet = Spritesheet(path.join(img_dir, SPRITESHEET))
def new(self):
self.all_sprites = pg.sprite.LayeredUpdates()
self.platforms = pg.sprite.Group()
self.lights = pg.sprite.Group()
self.player = Player(self)
ground_x_pos = 0
for i in range(5):
Ground(self, ground_x_pos, HEIGHT - 96)
ground_x_pos += 288
for urn in URN_POSITIONS:
Urns(self, *urn)
for lamp in LAMP_POSITIONS:
Lamps(self, *lamp)
for fire in FIRE_POSITIONS:
Fire(self, *fire)
self.run()
def run(self):
while self.playing:
self.clock.tick(FPS)
self.events()
self.update()
self.draw()
def update(self):
self.all_sprites.update()
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.y < lowest.rect.centery:
self.player.pos.y = lowest.rect.top
self.player.vel.y = 0
self.player.jumping = False
# self.all_sprites.update()
# if self.player.vel.y > 0:
# hits = pg.sprite.spritecollide(self.player, self.platforms, False)
# if hits:
# self.player.pos.y = hits[0].rect.top
# self.player.vel.y = 0
# self.player.jumping = False
def events(self):
for event in pg.event.get():
if event.type == pg.QUIT:
if self.playing:
self.playing = False
self.running = False
if event.type == pg.KEYDOWN:
if event.key == pg.K_SPACE or event.key == pg.K_UP:
self.player.jump()
if event.type == pg.KEYUP:
self.player.jump_cut()
def draw(self):
self.screen.blit(self.background, (0, 0))
self.all_sprites.draw(self.screen)
pg.display.flip()
game = Game()
while game.running:
game.new()
pg.quit()