Untitled
unknown
plain_text
3 years ago
2.0 kB
7
Indexable
# Import the pygame library
import pygame
# Initialize the game
pygame.init()
# Set the screen size
screen = pygame.display.set_mode((800, 600))
# Set the title of the game window
pygame.display.set_caption("Pac-Man")
# Load the Pac-Man and ghost images
pacman_image = pygame.image.load("pacman.png")
ghost_image = pygame.image.load("ghost.png")
# Set the position of Pac-Man on the screen
pacman_x = 350
pacman_y = 250
# Set the initial position of the ghost
ghost_x = 300
ghost_y = 200
# Set the initial score to 0
score = 0
# Set the game loop to run indefinitely
while True:
# Get the list of all the events in the game
events = pygame.event.get()
# Iterate over the events
for event in events:
# If the event is the QUIT event, exit the game loop
if event.type == pygame.QUIT:
pygame.quit()
quit()
# If the event is a KEYDOWN event, check if the key pressed is an arrow key
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
pacman_x -= 5
elif event.key == pygame.K_RIGHT:
pacman_x += 5
elif event.key == pygame.K_UP:
pacman_y -= 5
elif event.key == pygame.K_DOWN:
pacman_y += 5
# Check if Pac-Man and the ghost have collided
if (pacman_x < ghost_x + 50 and pacman_x + 50 > ghost_x) and (pacman_y < ghost_y + 50 and pacman_y + 50 > ghost_y):
print("Pac-Man and the ghost have collided!")
# Check if Pac-Man has eaten a dot
if (pacman_x < 300 and pacman_x + 50 > 300) and (pacman_y < 200 and pacman_y + 50 > 200):
print("Pac-Man has eaten a dot!")
score += 10
# Fill the screen with white color
screen.fill((255, 255, 255))
# Draw the Pac-Man, ghost, and dot on the screen
screen.blit(pacman_image, (pacman_x, pacman_y))
screen.blit(ghost_image, (ghost_x, ghost_y))
pygame.draw.circle(screen, (0, 0, 0), (300, 200), 5)
# Update the display
pygame.display.update()Editor is loading...