Skip to content
Open
Show file tree
Hide file tree
Changes from 8 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)
18 changes: 12 additions & 6 deletions Player.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
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
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
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/jump_sound.wav
Binary file not shown.
4 changes: 2 additions & 2 deletions constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 600
SCREENRECT = pg.rect.Rect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)
fps = 30
fps = 60
Comment thread
nitic04 marked this conversation as resolved.
Outdated
BG_RGB = [255, 255, 255]
SPAWN_MIN = 1
SPAWN_MAX = 5
Expand All @@ -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
19 changes: 13 additions & 6 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,9 @@ def main():
clock = pg.time.Clock()
pg.mouse.set_visible(True)

sound = Sound()
sound.play('background')

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 +43,22 @@ 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:
if player.rect.collidelist(
[obstacle.rect for obstacle in obstacles]) != -1:
game_over()

pg.display.update()
Expand Down
14 changes: 14 additions & 0 deletions sound.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import pygame.mixer


class Sound:
sounds: dict

def __init__(self):
self.sounds = {}
self.sounds['jump'] = pygame.mixer.Sound('assets/jump_sound.wav')
# more sounds to be added
self.sounds['background'] = pygame.mixer.Sound('assets/background_music.mp3')

def play(self, sound: str):
self.sounds[sound].play()