|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import math |
| 4 | +import random |
| 5 | +import pygame |
| 6 | + |
| 7 | +WIDTH = 623 |
| 8 | +HEIGHT = 150 |
| 9 | + |
| 10 | +pygame.init() |
| 11 | +pygame.mixer.init() |
| 12 | +pygame.mixer.init(frequency=44100, size=-16, channels=2, buffer=512) |
| 13 | +screen = pygame.display.set_mode((WIDTH, HEIGHT)) |
| 14 | +pygame.display.set_caption("Dino Game") |
| 15 | + |
| 16 | + |
| 17 | +class BG: |
| 18 | + def __init__(self, x): |
| 19 | + self.width = WIDTH |
| 20 | + self.height = HEIGHT |
| 21 | + self.x = x |
| 22 | + self.y = 0 |
| 23 | + self.set_texture() |
| 24 | + self.show() |
| 25 | + |
| 26 | + def update(self, dx): |
| 27 | + self.x += dx |
| 28 | + if self.x <= -WIDTH: |
| 29 | + self.x = WIDTH |
| 30 | + |
| 31 | + def show(self): |
| 32 | + screen.blit(self.texture, (self.x, self.y)) |
| 33 | + |
| 34 | + def set_texture(self): |
| 35 | + path = os.path.join("assets/images/bg.png") |
| 36 | + self.texture = pygame.image.load(path) |
| 37 | + self.texture = pygame.transform.scale(self.texture, (self.width, self.height)) |
| 38 | + |
| 39 | + |
| 40 | +class Dino: |
| 41 | + def __init__(self): |
| 42 | + self.width = 44 |
| 43 | + self.height = 44 |
| 44 | + self.x = 10 |
| 45 | + self.y = 80 |
| 46 | + self.texture_num = 0 |
| 47 | + self.dy = 2.6 |
| 48 | + self.gravity = 1.2 |
| 49 | + self.onground = True |
| 50 | + self.jumping = False |
| 51 | + self.jump_stop = 10 |
| 52 | + self.falling = False |
| 53 | + self.fall_stop = self.y |
| 54 | + self.set_texture() |
| 55 | + self.set_sound() |
| 56 | + self.show() |
| 57 | + |
| 58 | + def update(self, loops): |
| 59 | + # dino jump |
| 60 | + if self.jumping: |
| 61 | + self.y -= self.dy |
| 62 | + if self.y <= self.jump_stop: |
| 63 | + self.fall() |
| 64 | + |
| 65 | + # dino onground after jump |
| 66 | + elif self.falling: |
| 67 | + self.y += self.gravity * self.dy |
| 68 | + if self.y >= self.fall_stop: |
| 69 | + self.stop() |
| 70 | + |
| 71 | + # dino moving(running) |
| 72 | + elif self.onground and loops % 4 == 0: |
| 73 | + self.texture_num = (self.texture_num + 1) % 3 |
| 74 | + self.set_texture() |
| 75 | + |
| 76 | + def show(self): |
| 77 | + screen.blit(self.texture, (self.x, self.y)) |
| 78 | + |
| 79 | + def set_texture(self): |
| 80 | + path = os.path.join(f"assets/images/dino{self.texture_num}.png") |
| 81 | + self.texture = pygame.image.load(path) |
| 82 | + self.texture = pygame.transform.scale(self.texture, (self.width, self.height)) |
| 83 | + |
| 84 | + def set_sound(self): |
| 85 | + path = os.path.join("assets/sounds/jump.wav") |
| 86 | + self.sound = pygame.mixer.Sound(path) |
| 87 | + |
| 88 | + def jump(self): |
| 89 | + self.sound.play() |
| 90 | + self.jumping = True |
| 91 | + self.onground = False |
| 92 | + |
| 93 | + def fall(self): |
| 94 | + self.jumping = False |
| 95 | + self.falling = True |
| 96 | + |
| 97 | + def stop(self): |
| 98 | + self.falling = False |
| 99 | + self.onground = True |
| 100 | + |
| 101 | + |
| 102 | +class Cactus: |
| 103 | + def __init__(self, x): |
| 104 | + self.width = 34 |
| 105 | + self.height = 44 |
| 106 | + self.x = x |
| 107 | + self.y = 80 |
| 108 | + self.set_texture() |
| 109 | + self.show() |
| 110 | + |
| 111 | + def update(self, dx): |
| 112 | + self.x += dx |
| 113 | + |
| 114 | + def show(self): |
| 115 | + screen.blit(self.texture, (self.x, self.y)) |
| 116 | + |
| 117 | + def set_texture(self): |
| 118 | + path = os.path.join("assets/images/cactus.png") |
| 119 | + self.texture = pygame.image.load(path) |
| 120 | + self.texture = pygame.transform.scale(self.texture, (self.width, self.height)) |
| 121 | + |
| 122 | + |
| 123 | +class Collision: |
| 124 | + def between(self, obj1, obj2): |
| 125 | + distance = math.sqrt((obj1.x - obj2.x) ** 2 + (obj1.y - obj2.y) ** 2) |
| 126 | + return distance < 35 |
| 127 | + |
| 128 | + |
| 129 | +class Score: |
| 130 | + def __init__(self, hs): |
| 131 | + self.hs = hs |
| 132 | + self.act = 0 |
| 133 | + self.font = pygame.font.SysFont("monospace", 20) |
| 134 | + self.color = (0, 0, 0) |
| 135 | + self.set_sound() |
| 136 | + self.show() |
| 137 | + |
| 138 | + def update(self, loops): |
| 139 | + self.act = loops // 10 |
| 140 | + self.check_hs() |
| 141 | + self.check_sound() |
| 142 | + |
| 143 | + def show(self): |
| 144 | + self.lbl = self.font.render(f"HI {self.hs} {self.act}", 1, self.color) |
| 145 | + lbl_width = self.lbl.get_rect().width |
| 146 | + screen.blit(self.lbl, (WIDTH - lbl_width - 10, 10)) |
| 147 | + |
| 148 | + def set_sound(self): |
| 149 | + path = os.path.join("assets/sounds/point.wav") |
| 150 | + self.sound = pygame.mixer.Sound(path) |
| 151 | + |
| 152 | + def check_hs(self): |
| 153 | + if self.act >= self.hs: |
| 154 | + self.hs = self.act |
| 155 | + |
| 156 | + def check_sound(self): |
| 157 | + if self.act % 100 == 0 and self.act != 0: |
| 158 | + self.sound.play() |
| 159 | + |
| 160 | + |
| 161 | +class Game: |
| 162 | + def __init__(self, hs=0): |
| 163 | + self.bg = [BG(x=0), BG(x=WIDTH)] |
| 164 | + self.dino = Dino() |
| 165 | + self.obstacles = [] |
| 166 | + self.collision = Collision() |
| 167 | + self.score = Score(hs) |
| 168 | + self.speed = 3 |
| 169 | + self.playing = False |
| 170 | + self.set_sound() |
| 171 | + self.set_labels() |
| 172 | + self.spawn_cactus() |
| 173 | + |
| 174 | + def set_labels(self): |
| 175 | + big_font = pygame.font.SysFont("monospace", 26, bold=True) |
| 176 | + small_font = pygame.font.SysFont("monospace", 20) |
| 177 | + self.big_lbl = big_font.render(f"G A M E O V E R", 1, (0, 0, 0)) |
| 178 | + self.small_lbl = small_font.render(f"Press Enter to Restart", 1, (0, 0, 0)) |
| 179 | + |
| 180 | + def set_sound(self): |
| 181 | + path = os.path.join("assets/sounds/die.wav") |
| 182 | + self.sound = pygame.mixer.Sound(path) |
| 183 | + |
| 184 | + def start(self): |
| 185 | + self.playing = True |
| 186 | + |
| 187 | + def over(self): |
| 188 | + self.sound.play() |
| 189 | + screen.blit( |
| 190 | + self.big_lbl, (WIDTH // 2 - self.big_lbl.get_width() // 2, HEIGHT // 4) |
| 191 | + ) |
| 192 | + screen.blit( |
| 193 | + self.small_lbl, |
| 194 | + (WIDTH // 2 - self.small_lbl.get_width() // 2, HEIGHT // 2), |
| 195 | + ) |
| 196 | + self.playing = False |
| 197 | + |
| 198 | + def tospawn(self, loops): |
| 199 | + return loops % 100 == 0 |
| 200 | + |
| 201 | + def spawn_cactus(self): |
| 202 | + # list with cactus |
| 203 | + if len(self.obstacles) > 0: |
| 204 | + prev_cactus = self.obstacles[-1] |
| 205 | + # 44 pixels is width of dino |
| 206 | + # 84 pixels is random value for dino to jump between two cactus without dying |
| 207 | + x = random.randint( |
| 208 | + prev_cactus.x + self.dino.width + 130, |
| 209 | + WIDTH + prev_cactus.x + self.dino.width + 100, |
| 210 | + ) |
| 211 | + else: |
| 212 | + x = random.randint(WIDTH + 100, 1000) |
| 213 | + |
| 214 | + # create new cactus |
| 215 | + cactus = Cactus(x) |
| 216 | + self.obstacles.append(cactus) |
| 217 | + |
| 218 | + def restart(self): |
| 219 | + self.__init__(hs=self.score.hs) |
| 220 | + |
| 221 | + |
| 222 | +def main(): |
| 223 | + # Objects |
| 224 | + game = Game() |
| 225 | + dino = game.dino |
| 226 | + |
| 227 | + clock = pygame.time.Clock() |
| 228 | + loops = 0 |
| 229 | + over = False |
| 230 | + |
| 231 | + while True: |
| 232 | + if game.playing: |
| 233 | + loops += 1 |
| 234 | + |
| 235 | + # Code to display Background |
| 236 | + for bg in game.bg: |
| 237 | + bg.update(-game.speed) |
| 238 | + bg.show() |
| 239 | + |
| 240 | + # Code to display Dino |
| 241 | + dino.update(loops) |
| 242 | + dino.show() |
| 243 | + |
| 244 | + # Code to display Cactus |
| 245 | + if game.tospawn(loops): |
| 246 | + game.spawn_cactus() |
| 247 | + |
| 248 | + for cactus in game.obstacles: |
| 249 | + cactus.update(-game.speed) |
| 250 | + cactus.show() |
| 251 | + |
| 252 | + # logic for collisions |
| 253 | + if game.collision.between(dino, cactus): |
| 254 | + over = True |
| 255 | + |
| 256 | + if over: |
| 257 | + game.over() |
| 258 | + |
| 259 | + # score |
| 260 | + game.score.update(loops) |
| 261 | + game.score.show() |
| 262 | + |
| 263 | + for event in pygame.event.get(): |
| 264 | + # end the game on clicking quit button |
| 265 | + if event.type == pygame.QUIT: |
| 266 | + pygame.quit() |
| 267 | + sys.exit() |
| 268 | + if event.type == pygame.KEYDOWN: |
| 269 | + if event.key == pygame.K_SPACE: |
| 270 | + if not over: |
| 271 | + if dino.onground: |
| 272 | + dino.jump() |
| 273 | + |
| 274 | + if not game.playing: |
| 275 | + game.start() |
| 276 | + |
| 277 | + if event.key == pygame.K_RETURN: |
| 278 | + game.restart() |
| 279 | + dino = game.dino |
| 280 | + loops = 0 |
| 281 | + over = False |
| 282 | + |
| 283 | + clock.tick(120) |
| 284 | + pygame.display.update() |
| 285 | + |
| 286 | + |
| 287 | +main() |
0 commit comments