Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions Obstacle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
19 changes: 13 additions & 6 deletions Player.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
import pygame as pg
from constants import *
from sound import Sound


class Player:

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')
Comment on lines +26 to +27
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This kind of logic should probably be in the sound class

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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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/
Binary file added assets/background_music.mp3
Binary file not shown.
Binary file added assets/game_over_sound.wav
Binary file not shown.
Binary file added assets/jump_sound.wav
Binary file not shown.
2 changes: 1 addition & 1 deletion constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 20 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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.")

Comment on lines +30 to +35
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe put this logic in the sound class (play method)

player = Player(screen)
obstacles = []
next_spawn = random.randint(SPAWN_MIN, SPAWN_MAX)
Expand All @@ -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()
Expand Down
31 changes: 31 additions & 0 deletions sound.py
Original file line number Diff line number Diff line change
@@ -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()