Professional Documents
Culture Documents
import random
import sys
# Initialize Pygame
pygame.init()
# Define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
def update(self):
self.vel_y += gravity
self.rect.y += self.vel_y
def duck(self):
pass
def update(self):
self.rect.x -= bird_speed
if self.rect.right < 0:
self.kill()
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
# Load font
font = pygame.font.Font(None, 36)
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()
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)