-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #326 from Ian-Yy/7-dinosaur-pygame
- Loading branch information
Showing
25 changed files
with
332 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Dinosaur Game in Pygame | ||
A simple dinosaur game that is similar to chrome no internet dinosaur game | ||
|
||
![Alt text](Images/dinosaur-game-menu.png?raw=true "dinosaur game menu") | ||
|
||
![Alt text](Images/dinosaur-game-play.png?raw=true "dinosaur game play") | ||
|
||
# Requirements | ||
```bash | ||
pip install -r requirements.txt | ||
``` | ||
|
||
# To play | ||
1. Clone the project. | ||
2. Go into folder | ||
```bash | ||
cd Python/dinosaur_game | ||
``` | ||
3. Execute the main file | ||
```bash | ||
python main.py | ||
# or if you use Python 3 | ||
python3 main.py | ||
``` | ||
|
||
# Instructions | ||
Just survive by not crashing into obstacles. Use up/space to jump and down to duck | ||
|
||
Enjoy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,301 @@ | ||
import pygame | ||
import os | ||
import random | ||
|
||
pygame.init() | ||
|
||
# Global Constants | ||
SCREEN_HEIGHT = 600 | ||
SCREEN_WIDTH = 1100 | ||
SCREEN = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) | ||
|
||
START = pygame.image.load(os.path.join("Assets/Dino", "DinoStart.png")) | ||
|
||
RUNNING = [pygame.image.load(os.path.join("Assets/Dino", "DinoRun1.png")), | ||
pygame.image.load(os.path.join("Assets/Dino", "DinoRun2.png"))] | ||
|
||
JUMPING = pygame.image.load(os.path.join("Assets/Dino", "DinoJump.png")) | ||
|
||
DUCKING = [pygame.image.load(os.path.join("Assets/Dino", "DinoDuck1.png")), | ||
pygame.image.load(os.path.join("Assets/Dino", "DinoDuck2.png"))] | ||
|
||
DEAD = pygame.image.load(os.path.join("Assets/Dino", "DinoDead.png")) | ||
|
||
SMALL_CACTUS = [pygame.image.load(os.path.join("Assets/Cactus", "SmallCactus1.png")), | ||
pygame.image.load(os.path.join("Assets/Cactus", "SmallCactus2.png")), | ||
pygame.image.load(os.path.join("Assets/Cactus", "SmallCactus3.png"))] | ||
|
||
LARGE_CACTUS = [pygame.image.load(os.path.join("Assets/Cactus", "LargeCactus1.png")), | ||
pygame.image.load(os.path.join("Assets/Cactus", "LargeCactus2.png")), | ||
pygame.image.load(os.path.join("Assets/Cactus", "LargeCactus3.png"))] | ||
|
||
BIRD = [pygame.image.load(os.path.join("Assets/Bird", "Bird1.png")), | ||
pygame.image.load(os.path.join("Assets/Bird", "Bird2.png"))] | ||
|
||
CLOUD = pygame.image.load(os.path.join("Assets/Other", "Cloud.png")) | ||
|
||
BG = pygame.image.load(os.path.join("Assets/Other", "Track.png")) | ||
|
||
class Dinosaur: | ||
X_POS = 80 | ||
Y_POS = 310 | ||
Y_POS_DUCK = 340 | ||
JUMP_VEL = 8.5 | ||
|
||
def __init__(self): | ||
self.duck_img = DUCKING | ||
self.run_img = RUNNING | ||
self.jump_img = JUMPING | ||
self.start_img = START | ||
self.dead_img = DEAD | ||
|
||
self.dino_duck = False | ||
self.dino_run = True | ||
self.dino_jump = False | ||
|
||
self.step_index = 0 | ||
self.jump_vel = self.JUMP_VEL | ||
self.image = self.run_img[0] | ||
self.dino_rect = self.image.get_rect() | ||
self.dino_rect.x = self.X_POS | ||
self.dino_rect.y = self.Y_POS | ||
|
||
def update(self, userInput): | ||
if self.dino_duck: | ||
self.duck() | ||
if self.dino_run: | ||
self.run() | ||
if self.dino_jump: | ||
self.jump() | ||
|
||
if self.step_index >= 10: | ||
self.step_index = 0 | ||
|
||
# if player press up and dino not jump | ||
if (userInput[pygame.K_UP] or userInput[pygame.K_SPACE]) and not self.dino_jump: | ||
self.dino_duck = False | ||
self.dino_run = False | ||
self.dino_jump = True | ||
# if player press down and dino not jump | ||
elif userInput[pygame.K_DOWN] and not self.dino_jump: | ||
self.dino_duck = True | ||
self.dino_run = False | ||
self.dino_jump = False | ||
# if player not jumping or pressing down | ||
elif not (self.dino_jump or userInput[pygame.K_DOWN]): | ||
self.dino_duck = False | ||
self.dino_run = True | ||
self.dino_jump = False | ||
|
||
def duck(self): | ||
self.image = self.duck_img[self.step_index // 5] | ||
self.dino_rect = self.image.get_rect() | ||
self.dino_rect.x = self.X_POS | ||
self.dino_rect.y = self.Y_POS_DUCK | ||
self.step_index += 1 | ||
|
||
def run(self): | ||
self.image = self.run_img[self.step_index // 5] | ||
self.dino_rect = self.image.get_rect() | ||
self.dino_rect.x = self.X_POS | ||
self.dino_rect.y = self.Y_POS | ||
self.step_index += 1 | ||
|
||
def jump(self): | ||
self.image = self.jump_img | ||
if self.dino_jump: | ||
self.dino_rect.y -= self.jump_vel * 4 | ||
self.jump_vel -= 0.8 | ||
if self.jump_vel < - self.JUMP_VEL: | ||
self.dino_jump = False | ||
self.jump_vel = self.JUMP_VEL | ||
|
||
def draw(self, SCREEN): | ||
SCREEN.blit(self.image, (self.dino_rect.x, self.dino_rect.y)) | ||
|
||
def die(self): | ||
self.image = self.dead_img | ||
if self.dino_duck: | ||
self.dino_rect = self.image.get_rect() | ||
self.dino_rect.x = self.X_POS | ||
self.dino_rect.y = self.Y_POS | ||
|
||
class Cloud: | ||
def __init__(self): | ||
self.x = SCREEN_WIDTH + random.randint(800, 1000) | ||
self.y = random.randint(50, 100) | ||
self.image = CLOUD | ||
self.width = self.image.get_width() | ||
|
||
def update(self): | ||
self.x -= game_speed | ||
# when cloud fully exit left side of screen | ||
if self.x < - self.width: | ||
self.x = SCREEN_WIDTH + random.randint(2500, 3000) | ||
self.y = random.randint(50, 100) | ||
|
||
def draw(self, SCREEN): | ||
SCREEN.blit(self.image, (self.x, self.y)) | ||
|
||
class Obstacle: | ||
def __init__(self, image, type): | ||
self.image = image | ||
self.type = type | ||
self.rect = self.image[self.type].get_rect() | ||
self.rect.x = SCREEN_WIDTH | ||
|
||
def update(self): | ||
self.rect.x -= game_speed | ||
if self.rect.x < - self.rect.width: | ||
obstacles.pop() | ||
|
||
def draw(self, SCREEN): | ||
SCREEN.blit(self.image[self.type], self.rect) | ||
|
||
class SmallCactus(Obstacle): | ||
def __init__(self, image): | ||
self.type = random.randint(0, 2) | ||
super().__init__(image, self.type) | ||
self.rect.y = 325 | ||
|
||
class LargeCactus(Obstacle): | ||
def __init__(self, image): | ||
self.type = random.randint(0, 2) | ||
super().__init__(image, self.type) | ||
self.rect.y = 300 | ||
|
||
class Bird(Obstacle): | ||
def __init__(self, image): | ||
self.type = 0 | ||
super().__init__(image, self.type) | ||
if random.randint(0, 1) == 0: | ||
self.rect.y = 250 | ||
else: | ||
self.rect.y = 300 | ||
self.index = 0 | ||
|
||
def draw(self, SCREEN): | ||
if self.index >= 10: | ||
self.index = 0 | ||
SCREEN.blit(self.image[self.index//5], self.rect) | ||
self.index += 1 | ||
|
||
def main(): | ||
global game_speed, x_pos_bg, y_pos_bg, points, obstacles | ||
run = True | ||
clock = pygame.time.Clock() | ||
player = Dinosaur() | ||
cloud = Cloud() | ||
game_speed = 14 | ||
x_pos_bg = 0 | ||
y_pos_bg = 380 | ||
points = 0 | ||
font = pygame.font.Font('freesansbold.ttf', 20) | ||
obstacles = [] | ||
death_count = 0 | ||
end = False | ||
|
||
def score(): | ||
global points, game_speed | ||
points += 1 | ||
if points % 100 == 0: | ||
game_speed += 1 | ||
|
||
text = font.render("Points: " + str(points), True, (0, 0, 0)) | ||
textRect = text.get_rect() | ||
textRect.center = (1000, 40) | ||
SCREEN.blit(text, textRect) | ||
|
||
def background(): | ||
global x_pos_bg, y_pos_bg | ||
image_width = BG.get_width() | ||
SCREEN.blit(BG, (x_pos_bg, y_pos_bg)) | ||
SCREEN.blit(BG, (image_width + x_pos_bg, y_pos_bg)) | ||
if x_pos_bg <= - image_width: | ||
SCREEN.blit(BG, (image_width + x_pos_bg, y_pos_bg)) | ||
x_pos_bg = 0 | ||
x_pos_bg -= game_speed | ||
|
||
while run: | ||
|
||
if end: | ||
pygame.time.delay(2000) | ||
death_count += 1 | ||
menu(death_count) | ||
|
||
|
||
for event in pygame.event.get(): | ||
if event.type == pygame.QUIT: | ||
pygame.quit() | ||
exit() | ||
|
||
SCREEN.fill((255, 255, 255)) | ||
userInput = pygame.key.get_pressed() | ||
|
||
background() | ||
|
||
cloud.draw(SCREEN) | ||
cloud.update() | ||
|
||
player.update(userInput) | ||
|
||
if len(obstacles) == 0: | ||
if random.randint(0, 2) == 0: | ||
obstacles.append(SmallCactus(SMALL_CACTUS)) | ||
elif random.randint(0, 2) == 1: | ||
obstacles.append(LargeCactus(LARGE_CACTUS)) | ||
elif random.randint(0, 2) == 2: | ||
obstacles.append(Bird(BIRD)) | ||
|
||
for obstacle in obstacles: | ||
obstacle.draw(SCREEN) | ||
obstacle.update() | ||
# Create a slower obstacle rect for collide because apparently the rect transform faster than the img | ||
# Looks like issue with pygame lib, the faster the game speed the more the imgs lag behind | ||
# obst_collide_rect = obstacle.rect.copy() | ||
# obst_collide_rect.x += 10 | ||
|
||
if player.dino_rect.colliderect(obstacle.rect): | ||
player.die() | ||
end = True | ||
|
||
player.draw(SCREEN) | ||
|
||
score() | ||
|
||
clock.tick(30) | ||
pygame.display.update() | ||
|
||
def menu(death_count): | ||
global points | ||
run = True | ||
while run: | ||
SCREEN.fill((255, 255, 255)) | ||
font = pygame.font.Font('freesansbold.ttf', 30) | ||
|
||
if death_count == 0: | ||
text = font.render("Press any Key to Start", True, (0, 0, 0)) | ||
elif death_count > 0: | ||
text = font.render("Press any Key to Restart", True, (0, 0, 0)) | ||
score = font.render("Your Score: " + str(points), True, (0, 0, 0)) | ||
scoreRect = score.get_rect() | ||
scoreRect.center = (SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2 + 50) | ||
SCREEN.blit(score, scoreRect) | ||
|
||
textRect = text.get_rect() | ||
textRect.center = (SCREEN_WIDTH //2, SCREEN_HEIGHT // 2) | ||
SCREEN.blit(text, textRect) | ||
SCREEN.blit(START, (SCREEN_WIDTH // 2 - 20, SCREEN_HEIGHT // 2 - 140)) | ||
pygame.display.update() | ||
|
||
for event in pygame.event.get(): | ||
if event.type == pygame.QUIT: | ||
pygame.quit() | ||
exit() | ||
if event.type == pygame.KEYDOWN: | ||
if event.key == pygame.K_ESCAPE: | ||
pygame.quit() | ||
|
||
main() | ||
|
||
menu(death_count=0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pygame==2.0.1 |