Untitled
unknown
plain_text
a year ago
1.9 kB
8
Indexable
import pygame
import sys
# Initialize Pygame
pygame.init()
# Set up some constants
WIDTH, HEIGHT = 640, 480
PACMAN_SIZE = 20
PILL_SIZE = 10
GHOST_SIZE = 20
# Set up some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
YELLOW = (255, 255, 0)
# Set up the display
screen = pygame.display.set_mode((WIDTH, HEIGHT))
# Set up the font
font = pygame.font.Font(None, 36)
# Set up the Pac-Man and ghosts
pacman_x, pacman_y = WIDTH // 2, HEIGHT // 2
ghost_x, ghost_y = WIDTH // 2 + 50, HEIGHT // 2
# Set up the pills
pills = [(100, 100), (200, 200), (300, 300)]
# Game loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Move Pac-Man
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
pacman_y -= 5
if keys[pygame.K_DOWN]:
pacman_y += 5
if keys[pygame.K_LEFT]:
pacman_x -= 5
if keys[pygame.K_RIGHT]:
pacman_x += 5
# Move the ghost
if ghost_x > pacman_x:
ghost_x -= 2
elif ghost_x < pacman_x:
ghost_x += 2
if ghost_y > pacman_y:
ghost_y -= 2
elif ghost_y < pacman_y:
ghost_y += 2
# Draw everything
screen.fill(BLACK)
pygame.draw.circle(screen, YELLOW, (pacman_x, pacman_y), PACMAN_SIZE)
pygame.draw.circle(screen, WHITE, (ghost_x, ghost_y), GHOST_SIZE)
for pill in pills:
pygame.draw.circle(screen, WHITE, pill, PILL_SIZE)
# Check for collisions with pills
for pill in pills:
if ((pacman_x - pill[0]) 2 + (pacman_y - pill[1]) 2) 0.5 < PACMAN_SIZE + PILL_SIZE:
pills.remove(pill)
# Check for collision with ghost
if ((pacman_x - ghost_x) 2 + (pacman_y - ghost_y) 2) 0.5 < PACMAN_SIZE + GHOST_SIZE:
print("Game over!")
pygame.quit()
sys.exit()
# Update the display
pygame.display.flip()
pygame.time.Clock().tick(60)Editor is loading...
Leave a Comment