Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.7 kB
0
Indexable
Never
import pygame
import random

# Initialize Pygame
pygame.init()

# Set screen dimensions
screen_width = 640
screen_height = 480

# Create game window
screen = pygame.display.set_mode((screen_width, screen_height))

# Set game caption
pygame.display.set_caption("Pac-Man")

# Load game images
pacman_image = pygame.image.load("pacman.png").convert_alpha()
dot_image = pygame.image.load("dot.png").convert_alpha()

# Set game variables
pacman_x = screen_width // 2
pacman_y = screen_height // 2
pacman_speed = 5
dots = []
for i in range(50):
    x = random.randint(0, screen_width - 10)
    y = random.randint(0, screen_height - 10)
    dots.append(pygame.Rect(x, y, 10, 10))
score = 0

# Set game loop
running = True
while running:
    # Handle events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    
    # Move Pac-Man
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        pacman_x -= pacman_speed
    elif keys[pygame.K_RIGHT]:
        pacman_x += pacman_speed
    elif keys[pygame.K_UP]:
        pacman_y -= pacman_speed
    elif keys[pygame.K_DOWN]:
        pacman_y += pacman_speed
    
    # Check for collision with dots
    for dot in dots:
        if dot.colliderect(pygame.Rect(pacman_x, pacman_y, 32, 32)):
            dots.remove(dot)
            score += 10
    
    # Draw game objects
    screen.fill((0, 0, 0))
    for dot in dots:
        screen.blit(dot_image, dot)
    screen.blit(pacman_image, (pacman_x, pacman_y))
    font = pygame.font.Font(None, 30)
    score_text = font.render("Score: " + str(score), True, (255, 255, 255))
    screen.blit(score_text, (10, 10))
    pygame.display.flip()

# Quit Pygame
pygame.quit()