-
Notifications
You must be signed in to change notification settings - Fork 31
/
pacman.py
143 lines (121 loc) · 4.27 KB
/
pacman.py
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import sys
import pygame
from pygame.locals import *
from math import floor
import random
def init_window():
pygame.init()
pygame.display.set_mode((512, 512))
pygame.display.set_caption('Pacman')
def draw_background(scr, img=None):
if img:
scr.blit(img, (0, 0))
else:
bg = pygame.Surface(scr.get_size())
bg.fill((128, 128, 128))
scr.blit(bg, (0, 0))
class GameObject(pygame.sprite.Sprite):
def __init__(self, img, x, y, tile_size, map_size):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(img)
self.screen_rect = None
self.x = 0
self.y = 0
self.tick = 0
self.tile_size = tile_size
self.map_size = map_size
self.set_coord(x, y)
def set_coord(self, x, y):
self.x = x
self.y = y
self.screen_rect = Rect(floor(x) * self.tile_size, floor(y) * self.tile_size, self.tile_size, self.tile_size )
def game_tick(self):
self.tick += 1
def draw(self, scr):
scr.blit(self.image, (self.screen_rect.x, self.screen_rect.y))
class Ghost(GameObject):
def __init__(self, x, y, tile_size, map_size):
GameObject.__init__(self, './resources/ghost.png', x, y, tile_size, map_size)
self.direction = 0
self.velocity = 4.0 / 10.0
def game_tick(self):
super(Ghost, self).game_tick()
if self.tick % 20 == 0 or self.direction == 0:
self.direction = random.randint(1, 4)
if self.direction == 1:
self.x += self.velocity
if self.x >= self.map_size-1:
self.x = self.map_size-1
self.direction = random.randint(1, 4)
elif self.direction == 2:
self.y += self.velocity
if self.y >= self.map_size-1:
self.y = self.map_size-1
self.direction = random.randint(1, 4)
elif self.direction == 3:
self.x -= self.velocity
if self.x <= 0:
self.x = 0
self.direction = random.randint(1, 4)
elif self.direction == 4:
self.y -= self.velocity
if self.y <= 0:
self.y = 0
self.direction = random.randint(1, 4)
self.set_coord(self.x, self.y)
class Pacman(GameObject):
def __init__(self, x, y, tile_size, map_size):
GameObject.__init__(self, './resources/pacman.png', x, y, tile_size, map_size)
self.direction = 0
self.velocity = 4.0 / 10.0
def game_tick(self):
super(Pacman, self).game_tick()
if self.direction == 1:
self.x += self.velocity
if self.x >= self.map_size-1:
self.x = self.map_size-1
elif self.direction == 2:
self.y += self.velocity
if self.y >= self.map_size-1:
self.y = self.map_size-1
elif self.direction == 3:
self.x -= self.velocity
if self.x <= 0:
self.x = 0
elif self.direction == 4:
self.y -= self.velocity
if self.y <= 0:
self.y = 0
self.set_coord(self.x, self.y)
def process_events(events, packman):
for event in events:
if (event.type == QUIT) or (event.type == KEYDOWN and event.key == K_ESCAPE):
sys.exit(0)
elif event.type == KEYDOWN:
if event.key == K_LEFT:
packman.direction = 3
elif event.key == K_RIGHT:
packman.direction = 1
elif event.key == K_UP:
packman.direction = 4
elif event.key == K_DOWN:
packman.direction = 2
elif event.key == K_SPACE:
packman.direction = 0
if __name__ == '__main__':
init_window()
tile_size = 32
map_size = 16
ghost = Ghost(0, 0, tile_size, map_size)
pacman = Pacman(5, 5, tile_size, map_size)
background = None #pygame.image.load("./resources/background.png")
screen = pygame.display.get_surface()
while True:
process_events(pygame.event.get(), pacman)
pygame.time.delay(100)
ghost.game_tick()
pacman.game_tick()
draw_background(screen, background)
pacman.draw(screen)
ghost.draw(screen)
pygame.display.update()