Загрузка данных
import pygame
import sys
# Инициализация
pygame.init()
# Константы
WIDTH, HEIGHT = 800, 600
FPS = 60
GRAVITY = 0.8
JUMP_STRENGTH = -15
MOVE_SPEED = 5
# Цвета
SKY = (135, 206, 235)
GREEN = (34, 139, 34)
BROWN = (139, 69, 19)
RED = (220, 50, 50)
YELLOW = (255, 215, 0)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Простой платформер")
clock = pygame.time.Clock()
font = pygame.font.SysFont("Arial", 32, bold=True)
class Player(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.image = pygame.Surface((40, 50))
self.image.fill(RED)
self.rect = self.image.get_rect(topleft=(x, y))
self.vel_x = 0
self.vel_y = 0
self.on_ground = False
def update(self, platforms):
keys = pygame.key.get_pressed()
self.vel_x = 0
if keys[pygame.K_LEFT] or keys[pygame.K_a]:
self.vel_x = -MOVE_SPEED
if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
self.vel_x = MOVE_SPEED
# Прыжок
if (keys[pygame.K_SPACE] or keys[pygame.K_UP] or keys[pygame.K_w]) and self.on_ground:
self.vel_y = JUMP_STRENGTH
self.on_ground = False
# Гравитация
self.vel_y += GRAVITY
if self.vel_y > 20:
self.vel_y = 20
# Движение по X
self.rect.x += self.vel_x
for plat in platforms:
if self.rect.colliderect(plat):
if self.vel_x > 0:
self.rect.right = plat.left
elif self.vel_x < 0:
self.rect.left = plat.right
# Движение по Y
self.rect.y += self.vel_y
self.on_ground = False
for plat in platforms:
if self.rect.colliderect(plat):
if self.vel_y > 0:
self.rect.bottom = plat.top
self.vel_y = 0
self.on_ground = True
elif self.vel_y < 0:
self.rect.top = plat.bottom
self.vel_y = 0
# Границы экрана
if self.rect.left < 0:
self.rect.left = 0
if self.rect.right > WIDTH:
self.rect.right = WIDTH
class Coin(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.image = pygame.Surface((20, 20), pygame.SRCALPHA)
pygame.draw.circle(self.image, YELLOW, (10, 10), 10)
self.rect = self.image.get_rect(center=(x, y))
def main():
# Платформы
platforms = [
pygame.Rect(0, HEIGHT - 40, WIDTH, 40), # земля
pygame.Rect(150, 450, 150, 20),
pygame.Rect(400, 350, 150, 20),
pygame.Rect(600, 250, 150, 20),
pygame.Rect(300, 200, 120, 20),
]
player = Player(50, HEIGHT - 150)
# Монетки
coins = pygame.sprite.Group()
for (x, y) in [(200, 400), (450, 300), (650, 200), (340, 150)]:
coins.add(Coin(x, y))
score = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Обновление
player.update(platforms)
# Сбор монет
collected = pygame.sprite.spritecollide(player, coins, True)
score += len(collected) * 10
# Падение вниз — респавн
if player.rect.top > HEIGHT:
player.rect.topleft = (50, HEIGHT - 150)
player.vel_y = 0
score = max(0, score - 5)
# Отрисовка
screen.fill(SKY)
for plat in platforms:
pygame.draw.rect(screen, BROWN, plat)
pygame.draw.rect(screen, GREEN, (plat.x, plat.y, plat.width, 6))
coins.draw(screen)
screen.blit(player.image, player.rect)
# Счёт
score_text = font.render(f"Счёт: {score}", True, WHITE)
screen.blit(score_text, (10, 10))
# Победа
if len(coins) == 0:
win = font.render("Победа! Все монеты собраны!", True, WHITE)
screen.blit(win, (WIDTH // 2 - win.get_width() // 2, HEIGHT // 2))
pygame.display.flip()
clock.tick(FPS)
if __name__ == "__main__":
main()