Here's a simple implementation of the Chrome Dino game using Python and Pygame. In this version, the dinosaur will move automatically, and the player can only restart the game or pause/resume the automatic movement:
import pygame
import sys
import random
pygame.init()
WIDTH = 800
HEIGHT = 600
FPS = 60
GRAVITY = 0.5
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Chrome Dino")
clock = pygame.time.Clock()
def random_int(lower, upper):
return random.randint(lower, upper)
def draw_rect(rect, color):
pygame.draw.rect(screen, color, rect)
def draw_oval(oval, color):
pygame.draw.arc(screen, color, (oval.x, oval.y, oval.width, oval.height), 0, 360)
class Dino:
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.speed = 5
self.on_ground = False
def move(self, dt):
self.y += dt * GRAVITY
self.on_ground = False
if self.y > ground.height:
self.y = ground.height
self.x += dt * self.speed
self.on_ground = True
if self.x + self.width > WIDTH or self.x < 0:
self.x = WIDTH - self.width
self.y = random_int(0, ground.height)
self.on_ground = True
def draw(self, dt):
if not self.on_ground:
color = (255, 255, 255)
else:
color = (150, 150, 150)
rect = (self.x, self.y, self.width, self.height)
draw_rect(rect, color)
class Ground:
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
def draw(self, dt):
draw_rect((self.x, self.y, self.width, self.height), (100, 100, 100))
class Obstacle:
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
def draw(self, dt):
color = (255, 0, 0)
draw_rect((self.x, self.y, self.width, self.height), color)
class ScoreText(pygame.sprite.Sprite):
def __init__(self, x, y, value):
super().__init__()
self.x = x
self.y = y
font = pygame.font.Font(None, 36)
text = font.render(str(value), True, (255, 255, 255))
self.image = text
def update(self, dt):
pass
player = Dino(50, 400, 50, 50)
ground = Ground(0, HEIGHT - 50, WIDTH, 50)
obstacles = pygame.sprite.Group()
score_text = ScoreText(100, 10, 0)
all_sprites = pygame.sprite.Group()
all_sprites.add(player)
all_sprites.add(ground)
all_sprites.add(score_text)
running = True
paused = False
while running:
clock.tick(FPS)
dt = clock.get_time()/1000
for event in pygame.event.get():