Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to duck and a flying object #17

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
46 changes: 28 additions & 18 deletions Obstacle.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,36 @@ class Obstacle:

def __init__(self, surface: pg.Surface):
# randomly choose an obstacle
obstacle_type = random.randint(1, 3)

# trees
if obstacle_type == 1:
self.width = TREES_WIDTH
self.height = TREES_HEIGHT
self.img = pg.image.load('assets/trees.png').convert_alpha()
# rock
elif obstacle_type == 2:
self.width = ROCK_WIDTH
self.height = ROCK_HEIGHT
self.img = pg.image.load('assets/rock.png').convert_alpha()
# grass
obstacle_type = random.randint(1, 4)

# land objects
if obstacle_type != 4:
# trees
if obstacle_type == 1:
self.width = TREES_WIDTH
self.height = TREES_HEIGHT
self.img = pg.image.load('assets/trees.png').convert_alpha()
# rock
elif obstacle_type == 2:
self.width = ROCK_WIDTH
self.height = ROCK_HEIGHT
self.img = pg.image.load('assets/rock.png').convert_alpha()
# grass
else:
self.width = GRASS_WIDTH
self.height = GRASS_HEIGHT
self.img = pg.image.load('assets/grass.png').convert_alpha()

top_location = surface.get_height() - self.height
# flying bird
else:
self.width = GRASS_WIDTH
self.height = GRASS_HEIGHT
self.img = pg.image.load('assets/grass.png').convert_alpha()
self.width = BIRD_WIDTH
self.height = BIRD_HEIGHT
self.img = pg.image.load('assets/bird.png').convert_alpha()
top_location = surface.get_height() - \
random.randint(self.height, self.height*7)

self.rect = pg.rect.Rect(surface.get_width(), surface.get_height() -
self.height, self.width,
self.rect = pg.rect.Rect(surface.get_width(), top_location, self.width,
self.height)
self.jumping = False
self.velocity = 0
Expand Down
22 changes: 22 additions & 0 deletions Player.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pygame as pg

from constants import *
from sound import Sound

Expand All @@ -15,6 +16,7 @@ def __init__(self, surface: pg.Surface):
self.img = pg.image.load('assets/dino.png').convert_alpha()

self.jumping = False
self.ducking = False
self.velocity = 0
self.sound = Sound()

Expand All @@ -28,11 +30,31 @@ def jump(self):
self.sound.play('jump')
self.velocity = PLAYER_JUMP_FORCE

def duck(self):
if self.ducking:
return
self.ducking = True

def unduck(self):
if not self.ducking:
return
self.rect.update(self.initial_pos[0], self.initial_pos[1],
PLAYER_WIDTH, PLAYER_HEIGHT)
self.img = pg.image.load('assets/dino.png').convert_alpha()
self.ducking = False

def update_coords(self, dt):
if self.jumping:
if self.ducking:
self.unduck()
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.jumping = False

if self.ducking:
self.rect.update(self.initial_pos[0], self.initial_pos[1] +
self.height - DUCK_HEIGHT, DUCK_WIDTH, DUCK_HEIGHT)
self.img = pg.image.load('assets/dino_duck.png').convert_alpha()
Binary file added assets/bird.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/dino_duck.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
PLAYER_JUMP_COEFFICIENT = 50
PLAYER_WIDTH = 100
PLAYER_HEIGHT = 130
DUCK_WIDTH = 180
DUCK_HEIGHT = 75
DUCK_TIME = 1.5
X_OFFSET = 50
PLAYER_COLOR = (0, 0, 255)
GRAVITY = -10
Expand All @@ -32,3 +35,6 @@
ROCK_HEIGHT = 75
GRASS_WIDTH = 130
GRASS_HEIGHT = 70
BIRD_WIDTH = 75
BIRD_HEIGHT = 45

11 changes: 11 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def main():
player = Player(screen)
obstacles = []
next_spawn = random.randint(SPAWN_MIN, SPAWN_MAX)
time_to_unduck = None
global game_active
while game_active:
dt = clock.tick(fps) / 1000.0
Expand All @@ -43,6 +44,16 @@ def main():
if event.type == KEYDOWN:
if event.key == constants.K_SPACE:
player.jump()
elif event.key == constants.K_DOWN:
player.duck()
time_to_unduck = DUCK_TIME

if time_to_unduck:
time_to_unduck -= dt
if time_to_unduck <= 0:
player.unduck()
time_to_unduck = None

next_spawn -= dt
if next_spawn <= 0:
obstacles.append(Obstacle(screen))
Expand Down