-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpacman.py
More file actions
54 lines (45 loc) · 1.33 KB
/
pacman.py
File metadata and controls
54 lines (45 loc) · 1.33 KB
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
# import the libraries
import sys, pygame
# initialize pygame
pygame.init()
# set size of the window
size = width, height = 1024, 720
# initially pacman does not move
speed = [0,0]
# windows color
black = 0, 0, 0
screen = pygame.display.set_mode(size)
# pacman image object
pacman = pygame.image.load('pacman.gif')
# rectangle around pacman
pacmanrect = pacman.get_rect()
# pacman not moving initially
moving = False
# the game loop
while 1:
# check all the current events
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
moving = True
speed = [-8, 0]
if event.key == pygame.K_RIGHT:
moving = True
speed = [8, 0]
if event.key == pygame.K_UP:
moving = True
speed = [0, -8]
if event.key == pygame.K_DOWN:
moving = True
speed = [0, 8]
if moving:
pacmanrect = pacmanrect.move(speed)
if pacmanrect.left < 0 or pacmanrect.right > width:
moving = False
if pacmanrect.top < 0 or pacmanrect.bottom > height:
moving = False
screen.fill(black)
screen.blit(pacman, pacmanrect)
pygame.display.flip()
pygame.display.update()