import pygame
import requests
from urllib.request import urlopen
from io import BytesIO
# Initialize pygame
pygame.init()
# Set the width and height of the screen
width = 640
height = 480
# Create a pygame window
screen = pygame.display.set_mode((width, height))
# Create the maze
maze = [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1],
[1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
# Create a pacman character
class Pacman:
def __init__(self, x, y, direction):
self.x = x
self.y = y
self.direction = direction
class Ghost:
def __init__(self, x, y, color=(255, 0, 0)):
self.x = x
self.y = y
self.color = color
pacman = Pacman(1, 1, "right")
ghost = Ghost(9, 9)
# Download the pacman image
url = "https://i.ibb.co/RQJvwQ6/png-transparent-yellow-pacman-pac-man-computer-icons-pac-man-angle-video-game-smiley.png"
response = urlopen(url)
image_file = BytesIO(response.read())
pacman_image = pygame.image.load(image_file)
# Download the ghost image
url = "https://www.giantbomb.com/a/uploads/scale_small/8/87790/2469740-blinky.png"
response = urlopen(url)
image_file = BytesIO(response.read())
ghost_image = pygame.image.load(image_file)
# Download the dot image
url = "https://i.ibb.co/L0Jv7SN/550-5501141-pacman-dot-png-royalty-free-library-yellow-circle.png"
response = urlopen(url)
image_file = BytesIO(response.read())
dot_image = pygame.image.load(image_file)
# Smoothly scale the image to a smaller size
pacman_image = pygame.transform.smoothscale(pacman_image, (16, 16))
# Smoothly scale the image to a smaller size
ghost_image = pygame.transform.smoothscale(ghost_image, (16, 16))
# Smoothly scale the image to a smaller size
dot_image = pygame.transform.smoothscale(dot_image, (16, 16))
# Create a list of dot locations
dot_locations = []
# Add the dots to the maze
for i in range(1, len(maze) - 1):
for j in range(1, len(maze[0]) - 1):
if maze[i][j] == 0:
dot_locations.append((i, j))
# Main game loop
running = True
pacman.direction = "none"
pacman_dy = 0
pacman_dx = 0
ghost_dy = 0
ghost_dx = 0
while running:
# Process events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Check for keypresses
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
pacman.direction = "left"
elif event.key == pygame.K_RIGHT:
pacman.direction = "right"
elif event.key == pygame.K_UP:
pacman.direction = "up"
elif event.key == pygame.K_DOWN:
pacman.direction = "down"
# Move pacman in the current direction
if pacman.direction == "left":
if maze[round(pacman.y)][round(pacman.x - 1)] == 0:
pacman_dx = -0.001
pacman_dy = 0
elif pacman.direction == "right":
if maze[round(pacman.y)][round(pacman.x + 1)] == 0:
pacman_dx = 0.001
pacman_dy = 0
elif pacman.direction == "up":
if maze[round(pacman.y - 1)][round(pacman.x)] == 0:
pacman_dx = 0
pacman_dy = -0.001
elif pacman.direction == "down":
if maze[round(pacman.y + 1)][round(pacman.x)] == 0:
pacman_dx = 0
pacman_dy = 0.001
# Check if Pacman has touched a dot
for dot in dot_locations:
# Create a pygame.Rect object for the dot
dot_rect = pygame.Rect(dot[0] * 16, dot[1] * 16, 16, 16)
# Create a pygame.Rect object for Pacman
pacman_rect = pygame.Rect(pacman.x * 16, pacman.y * 16, 16, 16)
# Check if the two rectangles are colliding
if dot_rect.colliderect(pacman_rect):
# Remove the dot from the list of dot locations
dot_locations.remove(dot)
if pacman.direction == "left" or "right" or "up" or "down":
# Update movement variables for pacman
pacman.x += pacman_dx
pacman.y += pacman_dy
pacman.direction = "none"
#Check which direction brings the ghost closer to pacman
if pacman.x < ghost.x:
if pacman.y < ghost.y:
# Pacman is to the left and above the ghost
if maze[round(ghost.y - 1)][round(ghost.x)] == 0:
# Ghost can move up
ghost.direction = "up"
elif maze[round(ghost.y)][round(ghost.x - 1)] == 0:
# Ghost can move left
ghost.direction = "left"
elif pacman.y > ghost.y:
# Pacman is to the left and below the ghost
if maze[round(ghost.y + 1)][round(ghost.x)] == 0:
# Ghost can move down
ghost.direction = "down"
elif maze[round(ghost.y)][round(ghost.x - 1)] == 0:
# Ghost can move left
ghost.direction = "left"
else:
# Pacman is to the left and at the same height as the ghost
if maze[round(ghost.y)][round(ghost.x - 1)] == 0:
# Ghost can move left
ghost.direction = "left"
elif pacman.x > ghost.x:
if pacman.y < ghost.y:
# Pacman is to the right and above the ghost
if maze[round(ghost.y - 1)][round(ghost.x)] == 0:
# Ghost can move up
ghost.direction = "up"
elif maze[round(ghost.y)][round(ghost.x + 1)] == 0:
# Ghost can move right
ghost.direction = "right"
elif pacman.y > ghost.y:
# Pacman is to the right and below the ghost
if maze[round(ghost.y + 1)][round(ghost.x)] == 0:
# Ghost can move down
ghost.direction = "down"
elif maze[round(ghost.y)][round(ghost.x + 1)] == 0:
# Ghost can move right
ghost.direction = "right"
else:
# Pacman is to the right and at the same height as the ghost
if maze[round(ghost.y)][round(ghost.x + 1)] == 0:
# Ghost can move right
ghost.direction = "right"
else:
if pacman.y < ghost.y:
# Pacman is at the same x-coordinate and above the ghost
if maze[round(ghost.y - 1)][round(ghost.x)] == 0:
# Ghost can move up
ghost.direction = "up"
elif pacman.y > ghost.y:
# Pacman is at the same x-coordinate and below the ghost
if maze[round(ghost.y + 1)][round(ghost.x)] == 0:
# Ghost can move down
ghost.direction = "down"
# Move the ghost in the chosen direction
if ghost.direction == "up":
ghost_dx = 0
ghost_dy = -0.001
elif ghost.direction == "down":
ghost_dx = 0
ghost_dy = 0.001
elif ghost.direction == "left":
ghost_dx = -0.001
ghost_dy = 0
elif ghost.direction == "right":
ghost_dx += 0.001
ghost_dy = 0
# Update movement variables
ghost.x += ghost_dx
ghost.y += ghost_dy
# Clear the screen
screen.fill((0, 0, 0))
# Draw the maze
for i in range(len(maze)):
for j in range(len(maze[i])):
if maze[i][j] == 1:
color = (255, 255, 255)
else:
color = (0, 0, 0)
pygame.draw.rect(screen, color, (j * 32, i * 32, 32, 32))
# Loop through each element in the maze
for i in range(len(maze)):
for j in range(len(maze[0])):
# If the maze value is 2, then draw a dot
if maze[i][j] == 2:
pygame.draw.rect(screen, (255, 255, 0), (j * 16, i * 16, 16, 16))
# Draw pacman at its current position
screen.blit(pacman_image, (pacman.x * 32, pacman.y * 32))
# Set the dot color
dot_color = (255, 255, 0) # yellow
# Set the size of the dots
dot_size = (8, 8)
# Draw the dots on the screen
for dot_location in dot_locations:
i, j = dot_location
x = j * 32
y = i * 32
screen.blit(dot_image, (x, y))
# Draw the ghost at its current position
screen.blit(ghost_image, (ghost.x * 32, ghost.y * 32))
# Update the screen
pygame.display.flip()
# Clean up
pygame.quit()