import pygame
import sys
# Initialize Pygame
pygame.init()
# Define some colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
# Set up some constants
WIDTH, HEIGHT = 800, 600
BALL_RADIUS = 20
PLAYER_SPEED = 5
class Ball:
def __init__(self, x, y, color):
self.x = x
self.y = y
self.color = color
self.radius = BALL_RADIUS
def move(self, dx, dy):
self.x += dx
self.y += dy
def draw(self, screen):
pygame.draw.circle(screen, self.color, (self.x, self.y), self.radius)
class Player:
def __init__(self, x, y, color):
self.ball = Ball(x, y, color)
self.speed_x = 0
self.speed_y = 0
def move(self):
self.ball.move(self.speed_x, self.speed_y)
def set_speed(self, dx, dy):
self.speed_x = dx
self.speed_y = dy
def draw(self, screen):
self.ball.draw(screen)
class Game:
def __init__(self):
self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
self.clock = pygame.time.Clock()
self.player1 = Player(100, 100, (255, 0, 0))
self.player2 = Player(700, 100, (0, 0, 255))
self.score1 = 0
self.score2 = 0
def handle_events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
self.player1.set_speed(0, -PLAYER_SPEED)
elif event.key == pygame.K_s:
self.player1.set_speed(0, PLAYER_SPEED)
elif event.key == pygame.K_a:
self.player1.set_speed(-PLAYER_SPEED, 0)
elif event.key == pygame.K_d:
self.player1.set_speed(PLAYER_SPEED, 0)
elif event.key == pygame.K_UP:
self.player2.set_speed(0, -PLAYER_SPEED)
elif event.key == pygame.K_DOWN:
self.player2.set_speed(0, PLAYER_SPEED)
elif event.key == pygame.K_LEFT:
self.player2.set_speed(-PLAYER_SPEED, 0)
elif event.key == pygame.K_RIGHT:
self.player2.set_speed(PLAYER_SPEED, 0)
def update(self):
self.player1.move()
self.player2.move()
# Collision detection
if self.player1.ball.x - self.player1.ball.radius < 0 or self.player1.ball.x + self.player1.ball.radius > WIDTH:
self.player1.set_speed(-self.player1.speed_x, self.player1.speed_y)
if self.player1.ball.y - self.player1.ball.radius < 0 or self.player1.ball.y + self.player1.ball.radius > HEIGHT:
self.player1.set_speed(self.player1.speed_x, -self.player1.speed_y)
if self.player2.ball.x - self.player2.ball.radius < 0 or self.player2.ball.x + self.player2.ball.radius > WIDTH:
self.player2.set_speed(-self.player2.speed_x, self.player2.speed_y)
if self.player2.ball.y - self.player2.ball.radius < 0 or self.player2.ball.y + self.player2.ball.radius > HEIGHT:
self.player2.set_speed(self.player2.speed_x, -self.player2.speed_y)
# Goal detection
if self.player1.ball.x - self.player1.ball.radius < 0:
self.score2 += 1
self.player1.ball.x = 100
self.player1.ball.y = 100
elif self.player2.ball.x + self.player2.ball.radius > WIDTH:
self.score1 += 1
self.player2.ball.x = 700
self.player2.ball.y = 100
def draw(self):
self.screen.fill(BLACK)
self.player1.draw(self.screen)
self.player2.draw(self.screen)
font = pygame.font.Font(None, 36)
text = font.render(f"{self.score1} - {self.score2}", True, WHITE)
self.screen.blit(text, (WIDTH // 2 - text.get_width() // 2, 10))
pygame.display.flip()
def run(self):
while True:
self.handle_events()
self.update()
self.draw()
self.clock.tick(60)
if __name__ == "__main__":
game = Game()
game.run()