import pygame import random pygame.init() width, height = 800, 600 screen = pygame.display.set_mode((width, height)) pygame.display.set_caption("Dodge Game") player_width = 50 player_height = 50 player_x = width // 2 - player_width // 2 player_y = height - player_height - 10 player_speed = 7 obstacle_width = 50 obstacle_height = 50 obstacle_speed = 5 obstacles = [] score = 0 font = pygame.font.SysFont("Arial", 30) running = True while running: screen.fill((0, 0, 0)) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False keys = pygame.key.get_pressed() if keys[pygame.K_LEFT] and player_x > 0: player_x -= player_speed if keys[pygame.K_RIGHT] and player_x < width - player_width: player_x += player_speed if random.random() < 0.02: obstacle_x = random.randint(0, width - obstacle_width) obstacle_y = -obstacle_height obstacles.append([obstacle_x, obstacle_y]) for obstacle in obstacles: obstacle[1] += obstacle_speed if obstacle[1] > height: obstacles.remove(obstacle) score += 1 if player_x < obstacle[0] + obstacle_width and player_x + player_width > obstacle[0] and player_y < obstacle[1] + obstacle_height and player_y + player_height > obstacle[1]: running = False pygame.draw.rect(screen, (0, 255, 0), (player_x, player_y, player_width, player_height)) for obstacle in obstacles: pygame.draw.rect(screen, (255, 0, 0), (obstacle[0], obstacle[1], obstacle_width, obstacle_height)) score_text = font.render(f"Score: {score}", True, (255, 255, 255)) screen.blit(score_text, (10, 10)) pygame.display.flip() pygame.time.Clock().tick(60) pygame.quit()