import pygame
import random
# Initialize pygame
[Link]()
# Screen dimensions
WIDTH, HEIGHT = 800, 600
screen = [Link].set_mode((WIDTH, HEIGHT))
[Link].set_caption("Pixelated Geometry Dash")
# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
# Game variables
clock = [Link]()
player_width, player_height = 50, 50
player_x, player_y = 100, HEIGHT - player_height - 50 # Initial player position
player_velocity = 0
gravity = 0.8
jump_speed = -12
# Obstacle settings
obstacle_width = 50
obstacle_gap = 200
obstacle_velocity = 5
obstacles = []
# New types of obstacles
moving_obstacle_velocity = 3
spike_height = 20
spikes = []
# Fonts
font = [Link]("Arial", 30)
# Game level and score
level = 1
score = 0
# Game loop
running = True
while running:
[Link](BLACK) # Clear the screen with black color
# Handle events (like quitting the game)
for event in [Link]():
if [Link] == [Link]:
running = False
# Player controls (spacebar to jump)
keys = [Link].get_pressed()
if keys[pygame.K_SPACE] and player_y == HEIGHT - player_height - 50:
player_velocity = jump_speed # Set jump speed when space is pressed
# Apply gravity and move the player
player_y += player_velocity
if player_y < HEIGHT - player_height - 50:
player_velocity += gravity # Increase falling speed due to gravity
else:
player_y = HEIGHT - player_height - 50 # Stop falling when player reaches
ground
player_velocity = 0 # Stop vertical movement once player lands
# Draw player (as a white rectangle)
[Link](screen, WHITE, (player_x, player_y, player_width,
player_height))
# Level-up logic: increase difficulty as the player survives longer
if score // 1000 > level - 1: # Each level is gained after surviving 1000ms
level += 1
obstacle_velocity += 1 # Increase obstacle speed
moving_obstacle_velocity += 1 # Increase moving obstacle speed
spike_height += 5 # Increase spike size
# Spawn obstacles
if len(obstacles) == 0 or obstacles[-1][0] < WIDTH - obstacle_gap:
obstacle_height = [Link](100, HEIGHT // 2) # Random height for
obstacles
[Link]([WIDTH, HEIGHT - obstacle_height, obstacle_height]) # Add
obstacle to the list
# Spawn moving obstacles (spikes)
if [Link]() < 0.02: # 2% chance to spawn a moving obstacle
moving_obstacles = [Link](1, 2) # Random number of moving
obstacles
for _ in range(moving_obstacles):
moving_obstacles_x = WIDTH + [Link](100, 400) # Random
position for moving obstacles
moving_obstacles_y = [Link](100, HEIGHT - 100) # Random
vertical position
[Link]([moving_obstacles_x, moving_obstacles_y,
moving_obstacle_velocity]) # Add moving obstacle
# Move and draw obstacles
for obstacle in obstacles:
obstacle[0] -= obstacle_velocity # Move obstacle leftwards
[Link](screen, RED, (obstacle[0], HEIGHT - obstacle[2],
obstacle_width, obstacle[2])) # Draw obstacle
if obstacle[0] < 0: # If obstacle moves out of the screen, remove it
[Link](obstacle)
# Move and draw moving obstacles (spikes)
for spike in spikes:
spike[0] -= spike[2] # Move spike leftwards
[Link](screen, GREEN, (spike[0], spike[1], obstacle_width,
spike_height)) # Draw spike
if spike[0] < 0: # Remove spike if it moves off-screen
[Link](spike)
# Collision detection: Check if player hits any obstacle
for obstacle in obstacles:
if (player_x + player_width > obstacle[0] and player_x < obstacle[0] +
obstacle_width) and \
(player_y + player_height > HEIGHT - obstacle[2]):
running = False # End the game if player hits an obstacle
# Collision with moving obstacles (spikes)
for spike in spikes:
if (player_x + player_width > spike[0] and player_x < spike[0] +
obstacle_width) and \
(player_y + player_height > spike[1] and player_y < spike[1] +
spike_height):
running = False # End the game if player hits a spike
# Display score and level
score = [Link].get_ticks() // 1000 # Calculate score based on time passed
score_text = [Link](f"Score: {score}", True, WHITE)
level_text = [Link](f"Level: {level}", True, WHITE)
[Link](score_text, (WIDTH - 150, 10)) # Display score at top-right
[Link](level_text, (10, 10)) # Display level at top-left
# Update the display
[Link]()
# Frame rate: Limit to 60 frames per second
[Link](60)
# Quit the game
[Link]()