forked from deployphp/deployer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode Pac
85 lines (70 loc) · 2.06 KB
/
code Pac
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import pygame
import sys
import random
# Initialize Pygame
pygame.init()
# Constants
WIDTH, HEIGHT = 800, 600
FPS = 60
PACMAN_SIZE = 50
PACMAN_SPEED = 5
# Colors
BLACK = (0, 0, 0)
YELLOW = (255, 255, 0)
# Pacman class
class Pacman(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface((PACMAN_SIZE, PACMAN_SIZE), pygame.SRCALPHA)
pygame.draw.circle(self.image, YELLOW, (PACMAN_SIZE // 2, PACMAN_SIZE // 2), PACMAN_SIZE // 2)
self.rect = self.image.get_rect()
self.rect.center = (WIDTH // 2, HEIGHT // 2)
self.direction = random.choice(['UP', 'DOWN', 'LEFT', 'RIGHT'])
def update(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
self.direction = 'UP'
elif keys[pygame.K_DOWN]:
self.direction = 'DOWN'
elif keys[pygame.K_LEFT]:
self.direction = 'LEFT'
elif keys[pygame.K_RIGHT]:
self.direction = 'RIGHT'
if self.direction == 'UP':
self.rect.y -= PACMAN_SPEED
elif self.direction == 'DOWN':
self.rect.y += PACMAN_SPEED
elif self.direction == 'LEFT':
self.rect.x -= PACMAN_SPEED
elif self.direction == 'RIGHT':
self.rect.x += PACMAN_SPEED
# Wrap around the screen
self.rect.x = (self.rect.x + WIDTH) % WIDTH
self.rect.y = (self.rect.y + HEIGHT) % HEIGHT
# Initialize game window
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pacman Game")
clock = pygame.time.Clock()
# Create sprites
all_sprites = pygame.sprite.Group()
pacman = Pacman()
all_sprites.add(pacman)
# Main game loop
running = True
while running:
# Event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Update
all_sprites.update()
# Draw
screen.fill(BLACK)
all_sprites.draw(screen)
# Refresh screen
pygame.display.flip()
# Cap the frame rate
clock.tick(FPS)
# Quit the game
pygame.quit()
sys.exit()