import pygame
import time
# Constants
SCREEN_WIDTH = 128
SCREEN_HEIGHT = 64
PADDLE_WIDTH = 2
PADDLE_HEIGHT = 12
BALL_SIZE = 2
PADDLE_RATE = 64
BALL_RATE = 16
SCORE_LIMIT = 9
# Initialize pygame
pygame.init()
# Set up the display
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Pong Game")
clock = pygame.time.Clock()
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
# Game variables
game_over = False
win = False
player_score = 0
mcu_score = 0
ball_x, ball_y = 53, 26
ball_dir_x, ball_dir_y = 1, 1
ball_update = pygame.time.get_ticks()
paddle_update = ball_update
MCU_X, PLAYER_X = 12, 115
mcu_y, player_y = 16, 16
# Sound functions (dummy for now)
def play_sound(frequency, duration):
pass # pygame lacks built-in sound synthesis, but you can integrate a library like PyGame.mixer for sound.
# Game logic
def update_ball():
global ball_x, ball_y, ball_dir_x, ball_dir_y, player_score, mcu_score, game_over, win
new_x = ball_x + ball_dir_x
new_y = ball_y + ball_dir_y
# Check for wall collisions
if new_x <= 0 or new_x >= SCREEN_WIDTH - BALL_SIZE:
ball_dir_x *= -1
if new_x < SCREEN_WIDTH // 2:
player_score += 1
else:
mcu_score += 1
if player_score == SCORE_LIMIT or mcu_score == SCORE_LIMIT:
win = player_score > mcu_score
game_over = True
if new_y <= 0 or new_y >= SCREEN_HEIGHT - BALL_SIZE:
ball_dir_y *= -1
# Check for paddle collisions
if new_x == MCU_X and mcu_y <= new_y <= mcu_y + PADDLE_HEIGHT:
ball_dir_x *= -1
if new_x == PLAYER_X and player_y <= new_y <= player_y + PADDLE_HEIGHT:
ball_dir_x *= -1
ball_x = new_x
ball_y = new_y
def update_paddles(up_pressed, down_pressed):
global player_y, mcu_y
# CPU paddle logic
if ball_y < mcu_y + PADDLE_HEIGHT // 2:
mcu_y -= 1
if ball_y > mcu_y + PADDLE_HEIGHT // 2:
mcu_y += 1
mcu_y = max(0, min(mcu_y, SCREEN_HEIGHT - PADDLE_HEIGHT))
# Player paddle logic
if up_pressed:
player_y -= 1
if down_pressed:
player_y += 1
player_y = max(0, min(player_y, SCREEN_HEIGHT - PADDLE_HEIGHT))
def draw():
screen.fill(BLACK)
# Draw the court
pygame.draw.rect(screen, WHITE, (0, 0, SCREEN_WIDTH, SCREEN_HEIGHT), 1)
# Draw the paddles
pygame.draw.rect(screen, WHITE, (MCU_X, mcu_y, PADDLE_WIDTH, PADDLE_HEIGHT))
pygame.draw.rect(screen, WHITE, (PLAYER_X, player_y, PADDLE_WIDTH, PADDLE_HEIGHT))
# Draw the ball
pygame.draw.rect(screen, WHITE, (ball_x, ball_y, BALL_SIZE, BALL_SIZE))
# Draw the scores
font = pygame.font.SysFont(None, 24)
player_score_text = font.render(str(player_score), True, WHITE)
mcu_score_text = font.render(str(mcu_score), True, WHITE)
screen.blit(player_score_text, (PLAYER_X - 10, SCREEN_HEIGHT - 20))
screen.blit(mcu_score_text, (MCU_X + 10, SCREEN_HEIGHT - 20))
pygame.display.flip()
def reset_game():
global ball_x, ball_y, ball_dir_x, ball_dir_y, mcu_y, player_y, mcu_score, player_score, game_over
ball_x, ball_y = 53, 26
ball_dir_x, ball_dir_y = 1, 1
mcu_y, player_y = 16, 16
mcu_score = player_score = 0
game_over = False
# Main game loop
running = True
while running:
time_now = pygame.time.get_ticks()
up_pressed = down_pressed = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
up_pressed = True
if event.key == pygame.K_DOWN:
down_pressed = True
if game_over:
screen.fill(BLACK)
font = pygame.font.SysFont(None, 36)
text = "YOU WIN!" if win else "YOU LOSE!"
game_over_text = font.render(text, True, WHITE)
screen.blit(game_over_text, (40, 28))
pygame.display.flip()
time.sleep(3)
reset_game()
if time_now > ball_update:
update_ball()
ball_update += BALL_RATE
if time_now > paddle_update:
update_paddles(up_pressed, down_pressed)
paddle_update += PADDLE_RATE
draw()
clock.tick(60) # Frame rate limiter
pygame.quit()