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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
| import pygame import random
pygame.init()
SCREEN_WIDTH = 1500 SCREEN_HEIGHT = 600 size = (SCREEN_WIDTH, SCREEN_HEIGHT) screen = pygame.display.set_mode(size) bg_color = (255, 255, 255) pygame.display.set_caption("Mini DINOSAUR") score_font = pygame.font.Font(None, 30) game_over_font = pygame.font.Font(None, 50) score_list = []
class GameConfig: def __init__(self) -> None: self.SCREEN_WIDTH = 1500 self.SCREEN_HEIGHT = 600 self.BG_COLOR = (255, 255, 255) self.CAPTION = "Mini DINOSAUR" self.JUMP_HEIGHT = 100 self.TURN_TIME = 20 self.DELAY = 10
class Player: def __init__(self, x: int, y: int, speed: int, image: str, height: int, width: int,) -> None: self.speed = speed self.image = pygame.image.load(image) self.height = height self.width = width self.image = pygame.transform.scale( self.image, (self.width, self.height)) self.jump_height = 100 self.is_jumping = False self.score = 0 self.score_surface = score_font.render( str(self.score), True, (0, 0, 0)) self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y
def update_score_surface(self): self.score_surface = score_font.render( str(self.score), True, (0, 0, 0))
t_rex = Player( 0, 500, 10, './dinosaur/assets/t-rex.png', 70, 90)
class Enemy: def __init__(self, x: int, y: int, speed: int, image: str, height: int, width: int, is_left: bool = False) -> None: self.speed = speed self.image = pygame.image.load(image) self.height = height self.width = width self.image = pygame.transform.scale( self.image, (self.width, self.height)) self.is_left = is_left self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y
def turn_right(self): image = pygame.image.load('./dinosaur/assets/BlackBird_Right.png') self.image = pygame.transform.scale( image, (self.width, self.height)) self.is_left = False
def turn_left(self): image = pygame.image.load('./dinosaur/assets/BlackBird_Left.png') self.image = pygame.transform.scale( image, (self.width, self.height)) self.is_left = True
blackbird1 = Enemy( 1000, 100, 5, './dinosaur/assets/BlackBird_Left.png', 50, 40)
blackbird2 = Enemy( 800, 300, 5, './dinosaur/assets/BlackBird_Right.png', 50, 40)
cactus = Enemy(700, 500, 4, './dinosaur/assets/Cactus.png', 60, 70) mountain = Enemy(1200, 500, 4, './dinosaur/assets/Mountain.png', 80, 80) enemies = [blackbird1, blackbird2, cactus, mountain]
is_game_over = False running = True turn_time = 0
game_over_handled = False while running: if not is_game_over: t_rex.score += 1
turn_time += 1 pygame.time.delay(10) if turn_time > 20: if blackbird2.is_left: blackbird1.turn_right() blackbird2.turn_right() else: blackbird2.turn_left() blackbird1.turn_left() turn_time = 0
events = pygame.event.get() for event in events: if event.type == pygame.QUIT: running = False
keys = pygame.key.get_pressed() if keys[pygame.K_SPACE]: t_rex.is_jumping = True if keys[pygame.K_r]: is_game_over = False running = True t_rex.score = 0 game_over_handled = False
if t_rex.is_jumping: if t_rex.rect.y < t_rex.jump_height: t_rex.speed = -8 if t_rex.rect.y > 500: t_rex.speed = 6 t_rex.is_jumping = False t_rex.rect.y -= t_rex.speed
for enemy in enemies: if t_rex.rect.colliderect(enemy.rect): is_game_over = True
for enemy in enemies: enemy.rect.x -= enemy.speed
if enemy.rect.x < 0: enemy.rect.x = 1600 + random.randint(400, 2000)
if is_game_over and not game_over_handled: score_list.append(t_rex.score) score_list.sort(reverse=True) score_list = score_list[:3] game_over_handled = True
if is_game_over: for i in range(len(score_list)): s_surface = game_over_font.render(f"No {i + 1}: {score_list[i]}", True, (0, 0, 0)) screen.blit(s_surface, (SCREEN_WIDTH - 300, SCREEN_HEIGHT / 3 * i)) game_over_surface = game_over_font.render(f"GAME OVER YOUR SCORE: {t_rex.score}", True, (255, 0, 0)) r_surface = game_over_font.render("Click 'r' to restart ", True, (0, 0, 0)) screen.blit(r_surface, (SCREEN_HEIGHT / 3, SCREEN_HEIGHT / 5 * 4)) screen.blit(game_over_surface, (SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2)) else: screen.fill(bg_color) screen.blit(t_rex.image, (t_rex.rect.x, t_rex.rect.y)) t_rex.update_score_surface() screen.blit(t_rex.score_surface, (SCREEN_WIDTH - 100, 100)) for enemy in enemies: screen.blit(enemy.image, (enemy.rect.x, enemy.rect.y)) pygame.display.update()
pygame.quit()
|