Untitled

mail@pastecode.io avatar
unknown
plain_text
8 months ago
1.4 kB
1
Indexable
Never
import pygame
import sys

# Initialize Pygame
pygame.init()

# Constants
WIDTH, HEIGHT = 800, 600
FPS = 60
WHITE = (255, 255, 255)
YELLOW = (255, 255, 0)
PACMAN_RADIUS = 30

# Create the game window
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Simple Pac-Man Game")

# Initialize clock
clock = pygame.time.Clock()

# Pac-Man properties
pacman_pos = [WIDTH // 2, HEIGHT // 2]
pacman_speed = 5
pacman_angle = 0

# Main game loop
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # Move Pac-Man
    keys = pygame.key.get_pressed()
    pacman_pos[0] += keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]
    pacman_pos[1] += keys[pygame.K_DOWN] - keys[pygame.K_UP]

    # Draw background
    screen.fill(WHITE)

    # Draw Pac-Man
    pygame.draw.circle(screen, YELLOW, (int(pacman_pos[0]), int(pacman_pos[1])), PACMAN_RADIUS)
    mouth_rect = pygame.Rect(pacman_pos[0] - PACMAN_RADIUS, pacman_pos[1] - PACMAN_RADIUS,
                             2 * PACMAN_RADIUS, 2 * PACMAN_RADIUS)
    pygame.draw.pie(screen, WHITE, mouth_rect, pacman_angle, pacman_angle + 180)

    # Update Pac-Man angle for mouth animation
    pacman_angle += 5

    # Update the display
    pygame.display.flip()

    # Cap the frame rate
    clock.tick(FPS)

# Quit Pygame
pygame.quit()
sys.exit()
Leave a Comment