Загрузка данных


import pygame
import random
from random import randint

pygame.init()

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
CYAN = (0, 255, 255)
MAGENTA = (255, 0, 255)
ORANGE = (255, 165, 0)

colors = [WHITE, RED, GREEN, BLUE, YELLOW, CYAN, MAGENTA, ORANGE]

size = (700, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Pong")

try:
    hit_sound = pygame.mixer.Sound('hit.wav')
    score_sound = pygame.mixer.Sound('score.wav')
    pygame.mixer.music.load('bg_music.mp3')
    pygame.mixer.music.set_volume(0.5)
    sound_enabled = True
except:
    sound_enabled = False

class Paddle(pygame.sprite.Sprite):
    def __init__(self, color, width, height):
        super().__init__()
        self.original_color = color
        self.current_color = color
        self.image = pygame.Surface([width, height])
        self.image.fill(BLACK)
        self.image.set_colorkey(BLACK)
        pygame.draw.rect(self.image, self.current_color, [0, 0, width, height])
        self.rect = self.image.get_rect()
        self.width = width
        self.height = height

    def change_color(self, color):
        self.current_color = color
        self.image.fill(BLACK)
        pygame.draw.rect(self.image, self.current_color, [0, 0, self.width, self.height])

    def reset_color(self):
        self.current_color = self.original_color
        self.image.fill(BLACK)
        pygame.draw.rect(self.image, self.current_color, [0, 0, self.width, self.height])

    def moveUp(self, pixels):
        self.rect.y -= pixels
        if self.rect.y < 0:
            self.rect.y = 0

    def moveDown(self, pixels):
        self.rect.y += pixels
        if self.rect.y > 400:
            self.rect.y = 400

class Ball(pygame.sprite.Sprite):
    def __init__(self, color, width, height):
        super().__init__()
        self.original_color = color
        self.current_color = color
        self.image = pygame.Surface([width, height])
        self.image.fill(BLACK)
        self.image.set_colorkey(BLACK)
        pygame.draw.rect(self.image, self.current_color, [0, 0, width, height])
        self.velocity = [randint(4, 8), randint(-8, 8)]
        self.rect = self.image.get_rect()
        self.width = width
        self.height = height

    def change_color(self, color):
        self.current_color = color
        self.image.fill(BLACK)
        pygame.draw.rect(self.image, self.current_color, [0, 0, self.width, self.height])

    def reset_color(self):
        self.current_color = self.original_color
        self.image.fill(BLACK)
        pygame.draw.rect(self.image, self.current_color, [0, 0, self.width, self.height])

    def update(self):
        self.rect.x += self.velocity[0]
        self.rect.y += self.velocity[1]

    def bounce(self):
        self.velocity[0] = -self.velocity[0]
        self.velocity[1] = randint(-8, 8)

def show_text(text, font_size, color, x, y):
    font = pygame.font.Font(None, font_size)
    text_surf = font.render(text, True, color)
    text_rect = text_surf.get_rect(center=(x, y))
    screen.blit(text_surf, text_rect)

def draw_button(text, x, y, w, h, color, text_color):
    pygame.draw.rect(screen, color, (x, y, w, h))
    pygame.draw.rect(screen, WHITE, (x, y, w, h), 2)
    font = pygame.font.Font(None, 36)
    text_surf = font.render(text, True, text_color)
    text_rect = text_surf.get_rect(center=(x + w//2, y + h//2))
    screen.blit(text_surf, text_rect)
    return pygame.Rect(x, y, w, h)

def show_menu():
    selected = 0
    menu_items = ["2 Игрока", "Выход"]
    while True:
        screen.fill(BLACK)
        show_text("PONG", 100, WHITE, size[0]//2, 100)
        for i, item in enumerate(menu_items):
            color = YELLOW if i == selected else WHITE
            show_text(item, 50, color, size[0]//2, 250 + i * 60)
        pygame.display.flip()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return False
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    selected = (selected - 1) % len(menu_items)
                elif event.key == pygame.K_DOWN:
                    selected = (selected + 1) % len(menu_items)
                elif event.key == pygame.K_RETURN:
                    if menu_items[selected] == "Выход":
                        return False
                    else:
                        return True
    return False

def run_pong():
    if sound_enabled:
        pygame.mixer.music.play(-1)

    paddleA = Paddle(WHITE, 10, 100)
    paddleA.rect.x = 20
    paddleA.rect.y = 200

    paddleB = Paddle(WHITE, 10, 100)
    paddleB.rect.x = 670
    paddleB.rect.y = 200

    ball = Ball(WHITE, 10, 10)
    ball.rect.x = 345
    ball.rect.y = 195

    all_sprites_list = pygame.sprite.Group()
    all_sprites_list.add(paddleA)
    all_sprites_list.add(paddleB)
    all_sprites_list.add(ball)

    carryOn = True
    clock = pygame.time.Clock()
    scoreA = 0
    scoreB = 0

    while carryOn:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                carryOn = False
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    carryOn = False

        keys = pygame.key.get_pressed()
        if keys[pygame.K_w]:
            paddleA.moveUp(5)
        if keys[pygame.K_s]:
            paddleA.moveDown(5)
        if keys[pygame.K_UP]:
            paddleB.moveUp(5)
        if keys[pygame.K_DOWN]:
            paddleB.moveDown(5)

        all_sprites_list.update()

        if ball.rect.x >= 690:
            scoreA += 1
            if sound_enabled:
                score_sound.play()
            ball.velocity[0] = -ball.velocity[0]
            new_color = random.choice(colors)
            ball.change_color(new_color)
            paddleA.reset_color()
            paddleB.reset_color()
        if ball.rect.x <= 0:
            scoreB += 1
            if sound_enabled:
                score_sound.play()
            ball.velocity[0] = -ball.velocity[0]
            new_color = random.choice(colors)
            ball.change_color(new_color)
            paddleA.reset_color()
            paddleB.reset_color()
        if ball.rect.y > 490:
            ball.velocity[1] = -ball.velocity[1]
        if ball.rect.y < 0:
            ball.velocity[1] = -ball.velocity[1]

        if pygame.sprite.collide_mask(ball, paddleA) or pygame.sprite.collide_mask(ball, paddleB):
            ball.bounce()
            if sound_enabled:
                hit_sound.play()
            new_color = random.choice(colors)
            ball.change_color(new_color)
            paddleA.change_color(random.choice(colors))
            paddleB.change_color(random.choice(colors))

        screen.fill(BLACK)
        pygame.draw.line(screen, WHITE, [349, 0], [349, 500], 5)
        all_sprites_list.draw(screen)

        font = pygame.font.Font(None, 74)
        text = font.render(str(scoreA), 1, WHITE)
        screen.blit(text, (250, 10))
        text = font.render(str(scoreB), 1, WHITE)
        screen.blit(text, (420, 10))

        show_text(f"ESC - выход", 20, WHITE, size[0]//2, 480)

        pygame.display.flip()
        clock.tick(60)

    if sound_enabled:
        pygame.mixer.music.stop()
    pygame.quit()

if __name__ == "__main__":
    if show_menu():
        run_pong()
    else:
        pygame.quit()