You are on page 1of 5

import pygame

import random
import sys

# Initialize Pygame
pygame.init()

# Set up the game window


WIDTH, HEIGHT = 800, 400
window = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.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 = pygame.time.get_ticks() + checkpoint_interval

# Load game assets


t_rex_image = pygame.image.load("t_rex.png")
cactus_image = pygame.image.load("cactus.png")
pterodactyl_image = pygame.image.load("pterodactyl.png")
hunter_image = pygame.image.load("hunter.png")
frog_image = pygame.image.load("frog.png")

# Scale game assets to appropriate sizes


t_rex_image = pygame.transform.scale(t_rex_image, (100, 100))
cactus_image = pygame.transform.scale(cactus_image, (50, 50))
pterodactyl_image = pygame.transform.scale(pterodactyl_image, (100, 100))
hunter_image = pygame.transform.scale(hunter_image, (80, 80))
frog_image = pygame.transform.scale(frog_image, (80, 80))

# Define T-Rex class


class TRex(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = t_rex_image
self.rect = self.image.get_rect()
self.rect.x = 50
self.rect.y = HEIGHT - self.rect.height
self.vel_y = 0
self.is_jumping = False

def update(self):
self.vel_y += gravity
self.rect.y += self.vel_y

if self.rect.y >= HEIGHT - self.rect.height:


self.rect.y = HEIGHT - self.rect.height
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(pygame.sprite.Sprite):
def __init__(self, image, x, y):
super().__init__()
self.image = image
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y

def update(self):
self.rect.x -= bird_speed
if self.rect.right < 0:
self.kill()

# Define PowerUp class


class PowerUp(pygame.sprite.Sprite):
def __init__(self, image, x, y, power_up_type):
super().__init__()
self.image = image
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.power_up_type = power_up_type

def update(self):
self.rect.x -= bird_speed
if self.rect.right < 0:
self.kill()

def activate_power_up(self):
global current_power_up, power_up_end_time
current_power_up = self.power_up_type
power_up_end_time = pygame.time.get_ticks() + 5000 # 5 seconds

# Create sprite groups


all_sprites = pygame.sprite.Group()
obstacles = pygame.sprite.Group()
power_ups = pygame.sprite.Group()

# Create game objects


t_rex = TRex()
all_sprites.add(t_rex)

# Set up game clock


clock = pygame.time.Clock()

# Load font
font = pygame.font.Font(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 = pygame.time.get_ticks() + checkpoint_interval

all_sprites.empty()
obstacles.empty()
power_ups.empty()

t_rex.rect.x = 50
t_rex.rect.y = HEIGHT - t_rex.rect.height
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 pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_r and game_over:
restart_game()
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1 and not game_over:
t_rex.jump()
elif event.button == 3 and not game_over:
t_rex.duck()

# Fill the background with a color


window.fill(WHITE)

if not game_over:
# Update T-Rex
all_sprites.update()

# Spawn obstacles
if random.randrange(100) < 2:
cactus = Obstacle(cactus_image, WIDTH, HEIGHT -
cactus_image.get_height())
all_sprites.add(cactus)
obstacles.add(cactus)

# Spawn power-ups
if random.randrange(1000) < 1:
power_up_type = random.choice(["invincibility", "super_speed",
"super_size", "handgun"])
power_up_x = WIDTH
power_up_y = random.randint(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 pygame.sprite.spritecollide(t_rex, obstacles, False):
lives -= 1
if lives <= 0:
game_over = True

# Check collisions with power-ups


power_up_collision = pygame.sprite.spritecollide(t_rex, power_ups, True)
if power_up_collision:
power_up_collision[0].activate_power_up()

# Check for meat collection at checkpoints


if pygame.time.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 = pygame.time.get_ticks() // 1000

# Draw game objects


all_sprites.draw(window)

# Display score and lives


score_text = font.render("Score: " + str(score), True, BLACK)
window.blit(score_text, (10, 10))

lives_text = font.render("Lives: " + str(lives), True, BLACK)


window.blit(lives_text, (WIDTH - lives_text.get_width() - 10, 10))

# Display meat count


meat_count_text = font.render("Meat: " + str(meat_count), True, BLACK)
window.blit(meat_count_text, (10, 50))

# Display power-up status


power_up_status = font.render("Power-Up: " + str(current_power_up), True,
BLACK)
window.blit(power_up_status, (10, 90))

# Check if power-up has expired


if current_power_up is not None and pygame.time.get_ticks() >=
power_up_end_time:
current_power_up = None

# Game over logic


if game_over:
game_over_text = font.render("Game Over", True, BLACK)
game_over_rect = game_over_text.get_rect(center=(WIDTH/2, HEIGHT/2))
window.blit(game_over_text, game_over_rect)

# Update the display


pygame.display.flip()

# Limit the frame rate


clock.tick(60)

# Quit the game


pygame.quit()
sys.exit()

You might also like