Загрузка данных
import pygame
pygame.init()
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Space Shooter!")
# Цвета
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)
# Параметры игры
BORDER = pygame.Rect(WIDTH // 2 - 5, 0, 10, HEIGHT)
HEALTH_FONT = pygame.font.SysFont('comicsans', 40)
WINNER_FONT = pygame.font.SysFont('comicsans', 100)
FPS = 60
VEL = 5
BULLET_VEL = 7
MAX_BULLETS = 3
SPACESHIP_WIDTH, SPACESHIP_HEIGHT = 55, 40
# События для попадания
YELLOW_HIT = pygame.USEREVENT + 1
RED_HIT = pygame.USEREVENT + 2
# Загрузка изображений
YELLOW_SPACESHIP = pygame.transform.rotate(pygame.transform.scale(
pygame.image.load('Assets/spaceship_yellow.png'), (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), 90)
RED_SPACESHIP = pygame.transform.rotate(pygame.transform.scale(
pygame.image.load('Assets/spaceship_red.png'), (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), 270)
SPACE = pygame.transform.scale(pygame.image.load('Assets/space.png'), (WIDTH, HEIGHT))
def draw_window(red, yellow, red_bullets, yellow_bullets, red_health, yellow_health):
WIN.blit(SPACE, (0, 0))
pygame.draw.rect(WIN, BLACK, BORDER)
# Отображение здоровья
WIN.blit(HEALTH_FONT.render(f"Health: {red_health}", 1, WHITE), (WIDTH - 200, 10))
WIN.blit(HEALTH_FONT.render(f"Health: {yellow_health}", 1, WHITE), (10, 10))
# Отображение кораблей
WIN.blit(YELLOW_SPACESHIP, (yellow.x, yellow.y))
WIN.blit(RED_SPACESHIP, (red.x, red.y))
# Отображение пуль
for bullet in red_bullets:
pygame.draw.rect(WIN, RED, bullet)
for bullet in yellow_bullets:
pygame.draw.rect(WIN, YELLOW, bullet)
pygame.display.update()
def handle_movement(keys_pressed, spaceship, left_keys, right_keys):
if keys_pressed[left_keys[0]] and spaceship.x - VEL > 0: # LEFT
spaceship.x -= VEL
if keys_pressed[right_keys[0]] and spaceship.x + VEL + spaceship.width < BORDER.x: # RIGHT
spaceship.x += VEL
if keys_pressed[left_keys[1]] and spaceship.y - VEL > 0: # UP
spaceship.y -= VEL
if keys_pressed[right_keys[1]] and spaceship.y + VEL + spaceship.height < HEIGHT - 15: # DOWN
spaceship.y += VEL
def handle_bullets(yellow_bullets, red_bullets, yellow, red):
for bullet in yellow_bullets[:]:
bullet.x += BULLET_VEL
if red.colliderect(bullet):
pygame.event.post(pygame.event.Event(RED_HIT))
yellow_bullets.remove(bullet)
elif bullet.x > WIDTH:
yellow_bullets.remove(bullet)
for bullet in red_bullets[:]:
bullet.x -= BULLET_VEL
if yellow.colliderect(bullet):
pygame.event.post(pygame.event.Event(YELLOW_HIT))
red_bullets.remove(bullet)
elif bullet.x < 0:
red_bullets.remove(bullet)
def draw_winner(text):
draw_text = WINNER_FONT.render(text, 1, WHITE)
WIN.blit(draw_text, (WIDTH / 2 - draw_text.get_width() / 2, HEIGHT / 2 - draw_text.get_height() / 2))
pygame.display.update()
pygame.time.delay(5000)
def main():
red = pygame.Rect(700, 300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT)
yellow = pygame.Rect(100, 300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT)
red_bullets = []
yellow_bullets = []
red_health = 10
yellow_health = 10
clock = pygame.time.Clock()
run = True
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LCTRL and len(yellow_bullets) < MAX_BULLETS:
yellow_bullets.append(pygame.Rect(yellow.x + yellow.width, yellow.y + yellow.height // 2 - 2, 10, 5))
if event.key == pygame.K_RCTRL and len(red_bullets) < MAX_BULLETS:
red_bullets.append(pygame.Rect(red.x, red.y + red.height // 2 - 2, 10, 5))
if event.type == RED_HIT:
red_health -= 1
if event.type == YELLOW_HIT:
yellow_health -= 1
winner_text = ""
if red_health <= 0:
winner_text = "Yellow Wins!"
if yellow_health <= 0:
winner_text = "Red Wins!"
if winner_text != "":
draw_winner(winner_text)
break