-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
159 lines (131 loc) · 4.75 KB
/
main.py
File metadata and controls
159 lines (131 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import pygame
import os
import random
from Dinosaur import Dinosaur
from Cloud import Cloud
from SmallCactus import SmallCactus
from LargeCactus import LargeCactus
from Bird import Bird
pygame.init()
pygame.display.set_caption("Chrome Dinosaur")
# Constants
SCREEN_HEIGHT = 600
SCREEN_WIDTH = 1100
SCREEN = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# Images Loading
DINO = pygame.image.load(os.path.join("Assets/Dino", "DinoRun1.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"))]
BG = pygame.image.load(os.path.join("Assets/Other", "Track.png"))
#* Main function
def main():
global game_speed, x_pos_bg, y_pos_bg, points, obstacles
run = True
clock = pygame.time.Clock()
game_speed = 14
# BG Constants
x_pos_bg = 0
y_pos_bg = 380
# Score and Font
points = 0
font = pygame.font.Font('freesansbold.ttf', 20)
# Instanciate Classes
cloud = Cloud(SCREEN_WIDTH)
player = Dinosaur()
obstacles = []
death_count = 0
# Score
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)
# Background
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
# Game Loop
while run:
# Quit event
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
SCREEN.fill((255, 255, 255))
userInput = pygame.key.get_pressed()
# Draw and Update the Dino
player.draw(SCREEN)
player.update(userInput)
# Create Random Obstacles
if len(obstacles) == 0:
if random.randint(0, 2) == 0:
obstacles.append(SmallCactus(SMALL_CACTUS, SCREEN_WIDTH))
elif random.randint(0, 2) == 1:
obstacles.append(LargeCactus(LARGE_CACTUS, SCREEN_WIDTH))
elif random.randint(0, 2) == 2:
obstacles.append(Bird(BIRD, SCREEN_WIDTH))
# Draw Obstacles
for obstacle in obstacles:
obstacle.draw(SCREEN)
obstacle.update(game_speed, obstacles)
# Check Collisions
if player.dino_rect.colliderect(obstacle.rect):
pygame.time.delay(500)
death_count += 1
menu(death_count)
# Background
background()
# Draw and Update Cloud
cloud.draw(SCREEN)
cloud.update(SCREEN_WIDTH, game_speed)
# Score display
score()
clock.tick(30)
pygame.display.update()
# Menu function
def menu(death_count):
global points
run = True
# Menu loop
while run:
SCREEN.fill((255, 255, 255))
font = pygame.font.Font('freesansbold.ttf', 30)
# First Screen
if death_count == 0:
text = font.render("Press any Key to Start", True, (0, 0, 0))
# Second Screen
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(DINO, (SCREEN_WIDTH // 2 - 20, SCREEN_HEIGHT // 2 - 140))
pygame.display.update()
# Quit Event
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
if event.type == pygame.KEYDOWN:
main()
# Launch the game
menu(death_count=0)