import pygame
import random
import sys
# Initialize Pygame
[Link]()
# Set up the game window
WIDTH, HEIGHT = 800, 400
window = [Link].set_mode((WIDTH, HEIGHT))
[Link].set_caption("T-Rex Game Knock-off")
# Define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
# Define game variables
gravity = 0.8
bird_speed = 7
jump_force = 15
score = 0
lives = 3
meat_count = 0
checkpoint_interval = 10000 # 10 seconds
next_checkpoint_time = [Link].get_ticks() + checkpoint_interval
# Load game assets
t_rex_image = [Link]("t_rex.png")
cactus_image = [Link]("[Link]")
pterodactyl_image = [Link]("[Link]")
hunter_image = [Link]("[Link]")
frog_image = [Link]("[Link]")
# Scale game assets to appropriate sizes
t_rex_image = [Link](t_rex_image, (100, 100))
cactus_image = [Link](cactus_image, (50, 50))
pterodactyl_image = [Link](pterodactyl_image, (100, 100))
hunter_image = [Link](hunter_image, (80, 80))
frog_image = [Link](frog_image, (80, 80))
# Define T-Rex class
class TRex([Link]):
def __init__(self):
super().__init__()
[Link] = t_rex_image
[Link] = [Link].get_rect()
[Link].x = 50
[Link].y = HEIGHT - [Link]
self.vel_y = 0
self.is_jumping = False
def update(self):
self.vel_y += gravity
[Link].y += self.vel_y
if [Link].y >= HEIGHT - [Link]:
[Link].y = HEIGHT - [Link]
self.vel_y = 0
self.is_jumping = False
def jump(self):
if not self.is_jumping:
self.vel_y -= jump_force
self.is_jumping = True
def duck(self):
pass
# Define Obstacle class
class Obstacle([Link]):
def __init__(self, image, x, y):
super().__init__()
[Link] = image
[Link] = [Link].get_rect()
[Link].x = x
[Link].y = y
def update(self):
[Link].x -= bird_speed
if [Link] < 0:
[Link]()
# Define PowerUp class
class PowerUp([Link]):
def __init__(self, image, x, y, power_up_type):
super().__init__()
[Link] = image
[Link] = [Link].get_rect()
[Link].x = x
[Link].y = y
self.power_up_type = power_up_type
def update(self):
[Link].x -= bird_speed
if [Link] < 0:
[Link]()
def activate_power_up(self):
global current_power_up, power_up_end_time
current_power_up = self.power_up_type
power_up_end_time = [Link].get_ticks() + 5000 # 5 seconds
# Create sprite groups
all_sprites = [Link]()
obstacles = [Link]()
power_ups = [Link]()
# Create game objects
t_rex = TRex()
all_sprites.add(t_rex)
# Set up game clock
clock = [Link]()
# Load font
font = [Link](None, 36)
# Define current power-up and its duration
current_power_up = None
power_up_end_time = 0
# Define game state
game_over = False
# Define restart game function
def restart_game():
global score, lives, meat_count, next_checkpoint_time, game_over
score = 0
lives = 3
meat_count = 0
next_checkpoint_time = [Link].get_ticks() + checkpoint_interval
all_sprites.empty()
[Link]()
power_ups.empty()
t_rex.rect.x = 50
t_rex.rect.y = HEIGHT - t_rex.[Link]
t_rex.vel_y = 0
t_rex.is_jumping = False
all_sprites.add(t_rex)
game_over = False
# Game loop
running = True
while running:
for event in [Link]():
if [Link] == [Link]:
running = False
elif [Link] == [Link]:
if [Link] == pygame.K_r and game_over:
restart_game()
elif [Link] == [Link]:
if [Link] == 1 and not game_over:
t_rex.jump()
elif [Link] == 3 and not game_over:
t_rex.duck()
# Fill the background with a color
[Link](WHITE)
if not game_over:
# Update T-Rex
all_sprites.update()
# Spawn obstacles
if [Link](100) < 2:
cactus = Obstacle(cactus_image, WIDTH, HEIGHT -
cactus_image.get_height())
all_sprites.add(cactus)
[Link](cactus)
# Spawn power-ups
if [Link](1000) < 1:
power_up_type = [Link](["invincibility", "super_speed",
"super_size", "handgun"])
power_up_x = WIDTH
power_up_y = [Link](50, HEIGHT - 50)
power_up = PowerUp(hunter_image, power_up_x, power_up_y, power_up_type)
all_sprites.add(power_up)
power_ups.add(power_up)
# Check collisions with obstacles
if [Link](t_rex, obstacles, False):
lives -= 1
if lives <= 0:
game_over = True
# Check collisions with power-ups
power_up_collision = [Link](t_rex, power_ups, True)
if power_up_collision:
power_up_collision[0].activate_power_up()
# Check for meat collection at checkpoints
if [Link].get_ticks() >= next_checkpoint_time and meat_count < 5:
meat_count += 1
next_checkpoint_time += checkpoint_interval
lives = min(lives + 1, 3)
# Calculate score based on time
score = [Link].get_ticks() // 1000
# Draw game objects
all_sprites.draw(window)
# Display score and lives
score_text = [Link]("Score: " + str(score), True, BLACK)
[Link](score_text, (10, 10))
lives_text = [Link]("Lives: " + str(lives), True, BLACK)
[Link](lives_text, (WIDTH - lives_text.get_width() - 10, 10))
# Display meat count
meat_count_text = [Link]("Meat: " + str(meat_count), True, BLACK)
[Link](meat_count_text, (10, 50))
# Display power-up status
power_up_status = [Link]("Power-Up: " + str(current_power_up), True,
BLACK)
[Link](power_up_status, (10, 90))
# Check if power-up has expired
if current_power_up is not None and [Link].get_ticks() >=
power_up_end_time:
current_power_up = None
# Game over logic
if game_over:
game_over_text = [Link]("Game Over", True, BLACK)
game_over_rect = game_over_text.get_rect(center=(WIDTH/2, HEIGHT/2))
[Link](game_over_text, game_over_rect)
# Update the display
[Link]()
# Limit the frame rate
[Link](60)
# Quit the game
[Link]()
[Link]()