0% found this document useful (0 votes)
6 views3 pages

Import Pygame

This document contains a Python script that implements a simple game using the Pygame library, where a player controls a character that jumps to avoid obstacles. The game features gravity, scoring based on survival time, and increasing difficulty levels as the player progresses. Obstacles include static and moving spikes, with collision detection to end the game if the player hits any obstacles.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Import Pygame

This document contains a Python script that implements a simple game using the Pygame library, where a player controls a character that jumps to avoid obstacles. The game features gravity, scoring based on survival time, and increasing difficulty levels as the player progresses. Obstacles include static and moving spikes, with collision detection to end the game if the player hits any obstacles.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import pygame

import random

# Initialize pygame
pygame.init()

# Screen dimensions
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.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 = pygame.time.Clock()
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 = pygame.font.SysFont("Arial", 30)

# Game level and score


level = 1
score = 0

# Game loop
running = True
while running:
screen.fill(BLACK) # Clear the screen with black color

# Handle events (like quitting the game)


for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

# Player controls (spacebar to jump)


keys = pygame.key.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)


pygame.draw.rect(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 = random.randint(100, HEIGHT // 2) # Random height for
obstacles
obstacles.append([WIDTH, HEIGHT - obstacle_height, obstacle_height]) # Add
obstacle to the list

# Spawn moving obstacles (spikes)


if random.random() < 0.02: # 2% chance to spawn a moving obstacle
moving_obstacles = random.randint(1, 2) # Random number of moving
obstacles
for _ in range(moving_obstacles):
moving_obstacles_x = WIDTH + random.randint(100, 400) # Random
position for moving obstacles
moving_obstacles_y = random.randint(100, HEIGHT - 100) # Random
vertical position
spikes.append([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
pygame.draw.rect(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
obstacles.remove(obstacle)

# Move and draw moving obstacles (spikes)


for spike in spikes:
spike[0] -= spike[2] # Move spike leftwards
pygame.draw.rect(screen, GREEN, (spike[0], spike[1], obstacle_width,
spike_height)) # Draw spike
if spike[0] < 0: # Remove spike if it moves off-screen
spikes.remove(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 = pygame.time.get_ticks() // 1000 # Calculate score based on time passed
score_text = font.render(f"Score: {score}", True, WHITE)
level_text = font.render(f"Level: {level}", True, WHITE)
screen.blit(score_text, (WIDTH - 150, 10)) # Display score at top-right
screen.blit(level_text, (10, 10)) # Display level at top-left

# Update the display


pygame.display.flip()

# Frame rate: Limit to 60 frames per second


clock.tick(60)

# Quit the game


pygame.quit()

You might also like