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fc3ByaXRlcy51cGRhdGUoKQogICAgcGxheWVyLmFwcGx5X2dyYXZpdHkoKQogICAgCiAgICAjIENvbGxpc2lvbiBkZXRlY3Rpb24KICAgIGlmIHB5Z2FtZS5zcHJpdGUuc3ByaXRlY29sbGlkZShwbGF5ZXIsIG9ic3RhY2xlX2dyb3VwLCBGYWxzZSk6CiAgICAgICAgcnVubmluZyA9IEZhbHNlCiAgICAKICAgICMgU2NvcmUgdXBkYXRlCiAgICBzY29yZSArPSAxCiAgICAKICAgICMgRHJhdwogICAgc2NyZWVuLmZpbGwoV0hJVEUpCiAgICBhbGxfc3ByaXRlcy5kcmF3KHNjcmVlbikKICAgIGRpc3BsYXlfc2NvcmUoKQogICAgCiAgICBweWdhbWUuZGlzcGxheS51cGRhdGUoKQogICAgY2xvY2sudGljayhGUFMpCgpweWdhbWUucXVpdCgp
import pygame
import random
# Initialize Pygame
pygame.init()
# Game constants
WIDTH, HEIGHT = 800, 400
FPS = 60
GRAVITY = 0.8
JUMP_STRENGTH = -15
OBSTACLE_SPEED = 7
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
# Set up display
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pixel Runner")
clock = pygame.time.Clock()
# Load assets
player_img = pygame.image.load("player.png") # Replace with your image
obstacle_img = pygame.image.load("obstacle.png") # Replace with your image
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = player_img
self.rect = self.image.get_rect(midbottom=(80, HEIGHT - 10))
self.velocity = 0
self.jump_sound = pygame.mixer.Sound("jump.wav") # Add sound file
def apply_gravity(self):
self.velocity += GRAVITY
self.rect.y += self.velocity
if self.rect.bottom >= HEIGHT - 10:
self.rect.bottom = HEIGHT - 10
self.velocity = 0
def jump(self):
if self.rect.bottom == HEIGHT - 10:
self.velocity = JUMP_STRENGTH
self.jump_sound.play()
class Obstacle(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = obstacle_img
self.rect = self.image.get_rect(midbottom=(random.randint(WIDTH + 100, WIDTH + 300), HEIGHT - 10))
self.speed = OBSTACLE_SPEED
def update(self):
self.rect.x -= self.speed
if self.rect.right < 0:
self.kill()
# Game setup
player = Player()
obstacle_group = pygame.sprite.Group()
all_sprites = pygame.sprite.Group()
all_sprites.add(player)
# Score
score = 0
font = pygame.font.Font(None, 36)
def display_score():
score_surf = font.render(f"Score: {score}", True, BLACK)
score_rect = score_surf.get_rect(center=(WIDTH/2, 50))
screen.blit(score_surf, score_rect)
# Game loop
running = True
obstacle_timer = pygame.USEREVENT + 1
pygame.time.set_timer(obstacle_timer, 1500)
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
player.jump()
if event.type == obstacle_timer:
obstacle_group.add(Obstacle())
all_sprites.add(Obstacle())
# Update
all_sprites.update()
player.apply_gravity()
# Collision detection
if pygame.sprite.spritecollide(player, obstacle_group, False):
running = False
# Score update
score += 1
# Draw
screen.fill(WHITE)
all_sprites.draw(screen)
display_score()
pygame.display.update()
clock.tick(FPS)
pygame.quit()