diff --git a/__pycache__/settings.cpython-38.pyc b/__pycache__/settings.cpython-38.pyc index 847b03f..470431f 100644 Binary files a/__pycache__/settings.cpython-38.pyc and b/__pycache__/settings.cpython-38.pyc differ diff --git a/__pycache__/sprites.cpython-38.pyc b/__pycache__/sprites.cpython-38.pyc index 8bf4787..f370e6b 100644 Binary files a/__pycache__/sprites.cpython-38.pyc and b/__pycache__/sprites.cpython-38.pyc differ diff --git a/main.py b/main.py index 5317d52..1031828 100644 --- a/main.py +++ b/main.py @@ -6,6 +6,8 @@ from settings import * from sprites import * import os.path +# music: Licensed CC-BY. Attribute to Avgvst. +# https://opengameart.org/content/generic-8-bit-jrpg-soundtrack class Game: def __init__(self): @@ -30,6 +32,10 @@ class Game: self.spritesheet1 = SpriteSheet(os.path.join(img_dir, spritesheet1)) self.spritesheet2 = SpriteSheet(os.path.join(img_dir, spritesheet2)) + # load sound + self.sound_dir = os.path.join(self.dir, 'sound') + self.jump_sound = pygame.mixer.Sound(os.path.join(self.sound_dir, 'Jump13.wav')) + def new(self): # starts a new game not a new program self.score = 0 @@ -41,16 +47,19 @@ class Game: p = Platform(self, *plat) self.all_sprites.add(p) self.platforms.add(p) + pygame.mixer.music.load(os.path.join(self.sound_dir, '04 - Sanctuary.ogg')) self.run() def run(self): # Game loop + pygame.mixer.music.play(loops=-1) self.playing = True while self.playing: self.clock.tick(fps) self.events() self.update() self.draw() + pygame.mixer.music.fadeout(1000) def update(self): # Game loop - Update @@ -59,24 +68,30 @@ class Game: if self.player.vel.y > 0: hits = pygame.sprite.spritecollide(self.player, self.platforms, False) if hits: - self.player.pos.y = hits[0].rect.top - self.player.vel.y = 0 + lowest = hits[0] + for hit in hits: + if hit.rect.bottom > lowest.rect.bottom: + lowest = hit + #if self.player.pos.x < lowest.rect.right +20 and self.player.pos.x > lowest.rect.left + 20: + if self.player.pos.y < lowest.rect.centery: + self.player.pos.y = lowest.rect.top + self.player.vel.y = 0 + self.player.jumping = False # if player reaches 3/4 of width of the screen if self.player.rect.x >= (width * 0.6): self.player.pos.x -= abs(self.player.vel.x) for plat in self.platforms: plat.rect.x -= self.player.vel.x - # if player reaches goes left screen moves left 1/10 + + # if player reaches 1/5 of the left side of the screen move screen if self.player.rect.x <= (width * 0.2): self.player.pos.x += abs(self.player.vel.x) for plat in self.platforms: plat.rect.x -= self.player.vel.x # Die, by falling of the screen - if self.player.rect.bottom > height + 20: - for sprite in self.all_sprites: - sprite.rect.y -= max(self.player.vel.y, 10) + if self.player.rect.bottom > height + 60: self.playing = False def events(self): @@ -89,11 +104,15 @@ class Game: if event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: self.player.jump() + if event.type == pygame.KEYUP: + if event.key == pygame.K_UP: + self.player.jump_cut() def draw(self): # Game loop - draw/render self.screen.fill(BG_colour) self.all_sprites.draw(self.screen) + self.screen.blit(self.player.image, self.player.rect) self.draw_text("Score: " + str(self.score), 26, (255, 255, 255), 40, 15) # after everything is drawn "flip the board" @@ -101,15 +120,20 @@ class Game: def show_start_screen(self): # game start screen + pygame.mixer.music.load(os.path.join(self.sound_dir, '01 - Opening.ogg')) + pygame.mixer.music.play(loops=-1) self.screen.fill(BG_colour) self.draw_text(title, 48, (255, 255, 255), width / 2, int(height / 4) + 50) self.draw_text("Arrows to move, Z to shoot", 22, (255, 255, 255), width / 2, int(height / 2)) self.draw_text("Press a key to play", 22, (255, 255, 255), width / 2, int(height / 2) + 40) pygame.display.flip() self.wait_for_key() + pygame.mixer.music.fadeout(1000) def show_go_screen(self): # game over screen + pygame.mixer.music.load(os.path.join(self.sound_dir, '20 - Game Over.ogg')) + pygame.mixer.music.play(loops=-1) if not self.running: return self.screen.fill(BG_colour) @@ -118,6 +142,7 @@ class Game: self.draw_text("Press a key to play again", 22, (255, 255, 255), width / 2, int(height / 2) + 40) pygame.display.flip() self.wait_for_key() + pygame.mixer.music.fadeout(1000) def wait_for_key(self): waiting = True diff --git a/settings.py b/settings.py index 546ff4f..188ac6b 100644 --- a/settings.py +++ b/settings.py @@ -26,8 +26,7 @@ player_jump = 15 # Starting platforms # X Y -platform_list = [(0, 220), - (0, 425), - (800 / 2, 250), - (90, 425 - 300)] - +platform_list = [(0, 220),(35, 220), (70, 220), (105, 220), + (0, 425), (35, 425), (70, 425), (105, 425), + (800 / 2, 250), (365, 250), (330, 250), (295, 250), + (90, 125)] diff --git a/sound/01 - Opening.ogg b/sound/01 - Opening.ogg new file mode 100644 index 0000000..25ef5ad Binary files /dev/null and b/sound/01 - Opening.ogg differ diff --git a/sound/04 - Sanctuary.ogg b/sound/04 - Sanctuary.ogg new file mode 100644 index 0000000..0ff3641 Binary files /dev/null and b/sound/04 - Sanctuary.ogg differ diff --git a/sound/20 - Game Over.ogg b/sound/20 - Game Over.ogg new file mode 100644 index 0000000..e6af769 Binary files /dev/null and b/sound/20 - Game Over.ogg differ diff --git a/sound/31 Flashback.mp3 b/sound/31 Flashback.mp3 deleted file mode 100644 index e4c29ae..0000000 Binary files a/sound/31 Flashback.mp3 and /dev/null differ diff --git a/sound/Jump.wav b/sound/Jump.wav new file mode 100644 index 0000000..f09031f Binary files /dev/null and b/sound/Jump.wav differ diff --git a/sound/Jump13.wav b/sound/Jump13.wav new file mode 100644 index 0000000..a09a16d Binary files /dev/null and b/sound/Jump13.wav differ diff --git a/sprites.py b/sprites.py index 94c353b..db6386f 100644 --- a/sprites.py +++ b/sprites.py @@ -66,12 +66,20 @@ class Player(pygame.sprite.Sprite): def jump(self): # jump only if standing on a platform - self.rect.x += 1 + self.rect.y += 2 hits = pygame.sprite.spritecollide(self, self.game.platforms, False) - self.rect.x -= 1 - if hits: + self.rect.y -= 2 + if hits and not self.jumping: + self.game.jump_sound.play() + self.jumping = True self.vel.y = -player_jump + + def jump_cut(self): + if self.jumping: + if self.vel.y < -3: + self.vel.y = -3 + def update(self): self.animate() self.acc = vec(0, player_gravity)