Untitled

 avatar
unknown
plain_text
a year ago
2.4 kB
6
Indexable
import pygame
import sys
import random

# Initialize Pygame
pygame.init()

# Set up some constants
WIDTH, HEIGHT = 640, 480
SIZE = 20

# Set up some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
YELLOW = (255, 255, 102)
BLUE = (53, 115, 223)
RED = (255, 53, 53)
GREEN = (53, 223, 53)

# Set up the display
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pac-Man")

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

# Set up the player
player = pygame.Rect(WIDTH / 2, HEIGHT / 2, SIZE, SIZE)
player_speed = 4

# Set up the ghosts
ghosts = []
ghost_speeds = [2, 2, 2, 2]
ghost_colors = [BLUE, RED, GREEN, YELLOW]
for i in range(4):
    ghost = pygame.Rect(random.randint(0, WIDTH / SIZE) * SIZE, random.randint(0, HEIGHT / SIZE) * SIZE, SIZE, SIZE)
    ghosts.append(ghost)

# Set up the food
food = pygame.Rect(random.randint(0, WIDTH / SIZE) * SIZE, random.randint(0, HEIGHT / SIZE) * SIZE, SIZE, SIZE)
food_list = [food]

# Set up the direction vectors
dx = 0
dy = 0
dx_g = [0, 0, 0, 0]
dy_g = [0, 0, 0, 0]

# Game loop
while True:
    # Event handling
    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_UP:
                dx = 0
                dy = -player_speed
            elif event.key == pygame.K_DOWN:
                dx = 0
                dy = player_speed
            elif event.key == pygame.K_LEFT:
                dx = -player_speed
                dy = 0
            elif event.key == pygame.K_RIGHT:
                dx = player_speed
                dy = 0

    # Move the player
    player.x += dx
    player.y += dy

    # Check for collision with the walls
    if player.x < 0 or player.x > WIDTH - player.width or player.y < 0 or player.y > HEIGHT - player.height:
        player.x -= dx
        player.y -= dy

    # Move the ghosts
    for i in range(4):
        if random.random() < 0.01:
            dx_g[i] = random.choice([-1, 1, 0])
            dy_g[i] = random.choice([-1, 1, 0])
        ghosts[i].x += dx_g[i]
        ghosts[i].y += dy_g[i]

        # Check for collision with the walls
        if ghosts[i].x < 0 or ghosts[i].x > WIDTH - ghosts[i].width or ghosts[i].y < 0 or ghosts[i].y > HEIGHT - ghosts[i].height:
            ghosts[i].x -= dx_g[
Editor is loading...
Leave a Comment