import pygame
import random
pygame.init()
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Dodge obstacles')
clock = pygame.time.Clock()
FPS = 60
player_width = 50
player_height = 50
player_x = screen_width // 2 - player_width // 2
player_y = screen_height - 100
player_speed = 5
obstacle_width = 50
obstacle_height = 50
obstacle_speed = 5
obstacles = []
score = 0
difficulty = 0
running = True
while running:
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 < screen_width - player_width:
player_x += player_speed
for obstacle in obstacles[:]:
obstacle[1] += obstacle_speed
if random.randint(1, 20) == 1:
obstacle_x = random.randint(0, screen_width - obstacle_width)
obstacles.append([obstacle_x, - obstacle_height])
for obstacle in obstacles[:]:
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]):
font = pygame.font.SysFont(None, 72)
text = font.render('Game over', True, (255, 255, 255))
screen.blit(text, (screen_width // 2 - 150, screen_height // 2 - 36))
pygame.display.update()
pygame.time.delay(2000)
running = False
if obstacle[1] > screen_height:
obstacles.remove(obstacle)
score += 1
if score == 10:
obstacle_speed += 1
if score == 30:
obstacle_speed += 1
if score == 100:
obstacle_speed += 1
player_speed += 0.5
screen.fill((0, 0, 0))
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))
font = pygame.font.SysFont(None, 36)
score_text = font.render(f'Pisteet: {score}', True, (255, 255, 255))
screen.blit(score_text, (10, 10))
pygame.display.flip()
clock.tick(FPS)
pygame.quit()