diff --git a/Obstacle.py b/Obstacle.py index 1769600..792602e 100644 --- a/Obstacle.py +++ b/Obstacle.py @@ -3,20 +3,22 @@ import pygame as pg from constants import * + class Obstacle: def __init__(self, surface: pg.Surface): self.width = random.randint(MIN_WIDTH, MAX_WIDTH) self.height = random.randint(MIN_HEIGHT, MAX_HEIGHT) - self.rect = pg.rect.Rect(surface.get_width(), surface.get_height() - self.height, self.width, + self.rect = pg.rect.Rect(surface.get_width(), surface.get_height() - + self.height, self.width, self.height) self.jumping = False self.velocity = 0 self.color = random.choice(COLORS) self.speed = random.randint(MIN_SPEED, MAX_SPEED) - def Show(self, surface: pg.Surface): + def show(self, surface: pg.Surface): pg.draw.rect(surface, self.color, self.rect) - def UpdateCoords(self, dt): + def update_coords(self, dt): self.rect.move_ip(-self.speed * dt, 0) diff --git a/Player.py b/Player.py index 7163a2a..ba586e3 100644 --- a/Player.py +++ b/Player.py @@ -1,5 +1,7 @@ import pygame as pg from constants import * +from sound import Sound + class Player: @@ -7,24 +9,29 @@ def __init__(self, surface: pg.Surface): self.width = PLAYER_WIDTH self.height = PLAYER_HEIGHT self.initial_pos = X_OFFSET, surface.get_height() - self.height - self.rect = pg.rect.Rect(X_OFFSET, surface.get_height() - self.height, self.width, + self.rect = pg.rect.Rect(X_OFFSET, surface.get_height() - self.height, + self.width, self.height) self.jumping = False self.velocity = 0 + self.sound = Sound() - def Show(self, surface: pg.Surface): + def show(self, surface: pg.Surface): pg.draw.rect(surface, PLAYER_COLOR, self.rect) - def Jump(self): + def jump(self): if self.jumping: return self.jumping = True + if not self.sound.no_music: + self.sound.play('jump') self.velocity = PLAYER_JUMP_FORCE - def UpdateCoords(self, dt): + def update_coords(self, dt): if self.jumping: - self.rect.move_ip(0,-dt * self.velocity*PLAYER_JUMP_COEFFICIENT) + self.rect.move_ip(0, -dt * self.velocity * PLAYER_JUMP_COEFFICIENT) self.velocity = self.velocity + dt * GRAVITY if self.rect.y > self.initial_pos[1]: - self.rect.update(self.initial_pos[0], self.initial_pos[1], PLAYER_WIDTH, PLAYER_HEIGHT) + self.rect.update(self.initial_pos[0], self.initial_pos[1], + PLAYER_WIDTH, PLAYER_HEIGHT) self.jumping = False diff --git a/README.md b/README.md index e85d3b5..a1d357f 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,8 @@ # dino-game This project is a replicated dino-game from Chrome, running on Python. This is a work in progress game made by the UTM GDSC. To start contributing please check the "issues" tab. + +Background Music Credit: +"Pixelland" +Kevin MacLeod (incompetech.com) +Licensed under Creative Commons: By Attribution 3.0 http://creativecommons.org/licenses/by/3.0/ \ No newline at end of file diff --git a/assets/background_music.mp3 b/assets/background_music.mp3 new file mode 100644 index 0000000..9abed33 Binary files /dev/null and b/assets/background_music.mp3 differ diff --git a/assets/game_over_sound.wav b/assets/game_over_sound.wav new file mode 100644 index 0000000..57c8275 Binary files /dev/null and b/assets/game_over_sound.wav differ diff --git a/assets/jump_sound.wav b/assets/jump_sound.wav new file mode 100644 index 0000000..30328c6 Binary files /dev/null and b/assets/jump_sound.wav differ diff --git a/constants.py b/constants.py index ae852c8..aaa1434 100644 --- a/constants.py +++ b/constants.py @@ -18,7 +18,7 @@ PLAYER_COLOR = (0, 0, 255) GRAVITY = -10 -# Obstacle constantss +# Obstacle constants COLORS = [(0, 255, 0), (255, 0, 0)] MIN_WIDTH = 50 MAX_WIDTH = 100 diff --git a/main.py b/main.py index ddcbe7e..c3fc69f 100644 --- a/main.py +++ b/main.py @@ -6,16 +6,19 @@ from Obstacle import Obstacle from Player import Player +from sound import Sound from constants import * game_active = True + def game_over(): # TODO global game_active game_active = False print('You lost!') + def main(): # Initialize pygame pg.init() @@ -24,6 +27,12 @@ def main(): clock = pg.time.Clock() pg.mouse.set_visible(True) + sound = Sound() + if not sound.no_music: + sound.play('background_music') + else: + print("Warning: A Music file could not be found. The game will run without music.") + player = Player(screen) obstacles = [] next_spawn = random.randint(SPAWN_MIN, SPAWN_MAX) @@ -37,21 +46,27 @@ def main(): return if event.type == KEYDOWN: if event.key == constants.K_SPACE: - player.Jump() + player.jump() next_spawn -= dt if next_spawn <= 0: obstacles.append(Obstacle(screen)) next_spawn = random.randint(SPAWN_MIN, SPAWN_MAX) screen.fill(BG_RGB) - player.Show(screen) - player.UpdateCoords(dt) + player.show(screen) + player.update_coords(dt) for obstacle in obstacles: if obstacle.rect.right <= 0: obstacles.remove(obstacle) - obstacle.UpdateCoords(dt) - obstacle.Show(screen) + obstacle.update_coords(dt) + obstacle.show(screen) if player.rect.collidelist([obstacle.rect for obstacle in obstacles]) != -1: + pg.display.update() + if not sound.no_music: + sound.stop('background_music') + sound.play('game_over') + + pg.time.wait(1200) game_over() pg.display.update() diff --git a/sound.py b/sound.py new file mode 100644 index 0000000..8eda835 --- /dev/null +++ b/sound.py @@ -0,0 +1,31 @@ +import pygame.mixer + + +class Sound: + sounds: dict + no_music: bool + + def __init__(self): + self.sounds = {} + self.no_music = False + try: + self.sounds['jump'] = pygame.mixer.Sound('assets/jump_sound.wav') + + background_music = pygame.mixer.Sound('assets/background_music.mp3') + background_music.set_volume(0.4) + self.sounds['background_music'] = background_music + + game_over_sound = pygame.mixer.Sound('assets/game_over_sound.wav') + game_over_sound.set_volume(0.4) + self.sounds['game_over'] = game_over_sound + except FileNotFoundError: + self.no_music = True + + def play(self, sound: str): + if sound == "background_music": + self.sounds[sound].play(-1) + else: + self.sounds[sound].play() + + def stop(self, sound: str): + self.sounds[sound].stop()