Untitled

mail@pastecode.io avatar
unknown
plain_text
10 months ago
1.2 kB
3
Indexable
Never
import sys
import time
import pygame
from pygame.locals import *

pygame.init()

# screen settings
size = width, height = 640, 480
speed = [2, 2]
black = 0, 0, 0

# screen creation
screen = pygame.display.set_mode(size)
pygame.display.set_caption('Pacman Game')

# loading images
pacman = pygame.image.load('pacman.png')
ghost = pygame.image.load('ghost.png')

# positions
pacman_position = pacman.get_rect().move(320, 240)
ghost_position = ghost.get_rect().move(240, 240)

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    # moving pacman
    keys = pygame.key.get_pressed()
    if keys[K_LEFT]:
        pacman_position.left -= speed[0]
    if keys[K_RIGHT]:
        pacman_position.left += speed[0]
    if keys[K_UP]:
        pacman_position.top -= speed[1]
    if keys[K_DOWN]:
        pacman_position.top += speed[1]

    # checking collisions
    if pacman_position.colliderect(ghost_position):
        print('Game Over')
        pygame.quit()
        sys.exit()

    # drawing on screen
    screen.fill(black)
    screen.blit(pacman, pacman_position)
    screen.blit(ghost, ghost_position)
    pygame.display.flip()

    time.sleep(0.01)
Leave a Comment