You are on page 1of 2

import pygame

import random

# Initialize pygame
pygame.init()

# Set up the game window


width = 640
height = 480
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Pacman Clone")

# Define colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)
BLUE = (0, 0, 255)

# Define game variables


pacman_position = [width // 2, height // 2]
pacman_speed = 5
pacman_direction = 0 # 0 = right, 1 = down, 2 = left, 3 = up
ghost_position = [random.randint(0, width), random.randint(0, height)]
ghost_speed = 3
food_list = []
for i in range(20):
food_list.append([random.randint(0, width), random.randint(0, height)])

# Define game functions


def draw_pacman():
if pacman_direction == 0:
pygame.draw.circle(screen, YELLOW, pacman_position, 20)
pygame.draw.circle(screen, BLACK, pacman_position, 10)
pygame.draw.rect(screen, BLACK, (pacman_position[0], pacman_position[1] -
10, 20, 20))
elif pacman_direction == 1:
pygame.draw.circle(screen, YELLOW, pacman_position, 20)
pygame.draw.circle(screen, BLACK, pacman_position, 10)
pygame.draw.rect(screen, BLACK, (pacman_position[0] - 10,
pacman_position[1], 20, 20))
elif pacman_direction == 2:
pygame.draw.circle(screen, YELLOW, pacman_position, 20)
pygame.draw.circle(screen, BLACK, pacman_position, 10)
pygame.draw.rect(screen, BLACK, (pacman_position[0] - 20,
pacman_position[1] - 10, 20, 20))
elif pacman_direction == 3:
pygame.draw.circle(screen, YELLOW, pacman_position, 20)
pygame.draw.circle(screen, BLACK, pacman_position, 10)
pygame.draw.rect(screen, BLACK, (pacman_position[0] - 10,
pacman_position[1] - 20, 20, 20))

def draw_ghost():
pygame.draw.circle(screen, BLUE, ghost_position, 20)
pygame.draw.rect(screen, BLUE, (ghost_position[0] - 20, ghost_position[1] - 20,
40, 40))

def draw_food():
for food_position in food_list:
pygame.draw.circle(screen, RED, food_position, 5)

def move_pacman():
global pacman_position, pacman_direction
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT]:
pacman_position[0] += pacman_speed
pacman_direction = 0
elif keys[pygame.K_DOWN]:
pacman_position[1] += pacman_speed
pacman_direction = 1
elif keys[pygame.K_LEFT]:
pacman_position[0] -= pacman_speed
pacman_direction = 2
elif keys[pygame.K

You might also like