import pygame import random import sys # Initialize Pygame pygame.init() # Constants WIDTH, HEIGHT = 800, 600 FPS = 60 WHITE = (255, 255, 255) BLACK = (0, 0, 0) # Create the window screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Geometry Dash") # Player attributes player_width = 50 player_height = 50 player_x = WIDTH // 4 player_y = HEIGHT // 2 player_speed = 5 player_jump = 10 gravity = 1 # Obstacle attributes obstacle_width = 50 min_obstacle_height = 100 max_obstacle_height = 300 # Adjusted maximum height obstacle_speed = 5 obstacles = [] def draw_player(x, y): pygame.draw.rect(screen, BLACK, (x, y, player_width, player_height)) def draw_obstacles(obstacles): for obstacle in obstacles: pygame.draw.rect(screen, BLACK, obstacle) def generate_obstacle(): obstacle_height = random.randint(min_obstacle_height, max_obstacle_height) obstacle_x = WIDTH obstacle_y = HEIGHT - obstacle_height obstacle = pygame.Rect(obstacle_x, obstacle_y, obstacle_width, obstacle_height) obstacles.append(obstacle) # Game loop clock = pygame.time.Clock() running = True while running: screen.fill(WHITE) # Event handling for event in pygame.event.get(): if event.type == pygame.QUIT: running = False pygame.quit() sys.exit() # Player movement keys = pygame.key.get_pressed() if keys[pygame.K_SPACE] and player_y == HEIGHT - player_height: player_jump = -20 player_y += player_jump player_jump += gravity if player_y > HEIGHT - player_height: player_y = HEIGHT - player_height player_jump = 0 # Generate obstacles if len(obstacles) == 0 or obstacles[-1].x < WIDTH * 0.7: generate_obstacle() # Move obstacles for obstacle in obstacles: obstacle.x -= obstacle_speed # Remove obstacles that have passed obstacles = [obstacle for obstacle in obstacles if obstacle.x + obstacle.width > 0] # Draw player and obstacles draw_player(player_x, player_y) draw_obstacles(obstacles) # Collision detection player_rect = pygame.Rect(player_x, player_y, player_width, player_height) for obstacle in obstacles: if player_rect.colliderect(obstacle): if (player_x + player_width >= obstacle.x and player_x <= obstacle.x + obstacle.width) and \ (player_y + player_height >= obstacle.y and player_y <= obstacle.y + obstacle.height): print("Game Over") running = False pygame.display.flip() clock.tick(FPS) pygame.quit()