Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.7 kB
2
Indexable
Never
import pygame
import sys

# Initialize Pygame
pygame.init()

# Set up the game window
WINDOW_WIDTH = 640
WINDOW_HEIGHT = 480
window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("Pac-Man")

# Set up the game clock
clock = pygame.time.Clock()

# Load the game resources
pacman_image = pygame.image.load("pacman.png").convert_alpha()

# Set up the game objects
class Pacman:
    def __init__(self):
        self.image = pacman_image
        self.rect = self.image.get_rect()
        self.rect.center = (WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2)
        self.speed = 5

    def update(self, keys):
        if keys[pygame.K_UP]:
            self.rect.y -= self.speed
        if keys[pygame.K_DOWN]:
            self.rect.y += self.speed
        if keys[pygame.K_LEFT]:
            self.rect.x -= self.speed
        if keys[pygame.K_RIGHT]:
            self.rect.x += self.speed

        # Check for screen wraparound
        if self.rect.left > WINDOW_WIDTH:
            self.rect.right = 0
        if self.rect.right < 0:
            self.rect.left = WINDOW_WIDTH
        if self.rect.top > WINDOW_HEIGHT:
            self.rect.bottom = 0
        if self.rect.bottom < 0:
            self.rect.top = WINDOW_HEIGHT

    def draw(self, surface):
        surface.blit(self.image, self.rect)

pacman = Pacman()

# Main game loop
while True:
    # Handle events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    # Update game objects
    keys = pygame.key.get_pressed()
    pacman.update(keys)

    # Draw game objects
    window.fill((0, 0, 0))
    pacman.draw(window)
    pygame.display.flip()

    # Set the game speed
    clock.tick(60)