-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.py
184 lines (154 loc) · 5.76 KB
/
map.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import pygame, sys, random, time
from pygame.locals import *
pygame.init()
WIDTH = 560
HEIGHT = 620
screen = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("Pacman")
PACMAN_X = WIDTH/2-5
PACMAN_Y = 63
PACMAN_WIDTH = 20
PACMAN_HEIGHT = 20
PACMAN_VEL = 2
GHOST_WIDTH = 20
GHOST_HEIGHT = 20
GHOST1_X = 25
GHOST1_Y = 25
GHOST2_X = 520
GHOST2_Y = 25
GHOST3_X = 520
GHOST3_Y = 580
GHOST4_X = 25
GHOST4_Y = 580
GHOST_VEL = 1
GHOST_UP = True
GHOST_TURN = True
ALIVE = True
YELLOW = (255,255,153)
BLACK = (0, 0, 0)
BLUE = (0, 0, 255)
WHITE = (255,255,255)
BORDER_COLOR = (0, 128, 248, 255)
GHOST_COLOR = (42, 191, 242, 255)
image = pygame.image.load('template.png').convert()
image = pygame.transform.scale(image, (560, 620))
class Ghosts(pygame.sprite.Sprite):
def __init__(self, width, height, x, y, vel):
super(Ghosts, self).__init__()
self.width = width
self.height = height
self.x = x
self.y = y
self.vel = vel
self.image = pygame.image.load('ghost.png').convert_alpha()
self.image = pygame.transform.scale(self.image, (self.width, self.height))
def draw(self, screen):
screen.blit(self.image, self.rect)
def wallStop(self):
if tuple(screen.get_at((self.x, self.y))) == BORDER_COLOR or tuple(screen.get_at((self.x+self.width, self.y+self.height))) == BORDER_COLOR or tuple(screen.get_at((self.x+self.width, self.y))) == BORDER_COLOR or tuple(screen.get_at((self.x, self.y+self.height))) == BORDER_COLOR or tuple(screen.get_at((self.x+self.width/2, self.y))) == BORDER_COLOR or tuple(screen.get_at((self.x, self.y+self.height/2))) == BORDER_COLOR or tuple(screen.get_at((self.x+self.width/2, self.y+self.height))) == BORDER_COLOR or tuple(screen.get_at((self.x+self.width, self.y+self.height/2))) == BORDER_COLOR:
return True
else:
return False
def move(self):
global GHOST_UP
global GHOST_TURN
if self.x<PACMAN_X:
self.x += self.vel
if self.wallStop():
self.x -= self.vel
time.sleep(0.002)
else:
self.x -= self.vel
if self.wallStop():
self.x += self.vel
time.sleep(0.002)
if self.y<PACMAN_Y:
self.y += self.vel
if self.wallStop():
self.y -= self.vel
time.sleep(0.002)
else:
self.y -= self.vel
if self.wallStop():
self.y += self.vel
time.sleep(0.002)
ghost1 = Ghosts(GHOST_WIDTH, GHOST_HEIGHT, GHOST1_X, GHOST1_Y, GHOST_VEL)
ghost2 = Ghosts(GHOST_WIDTH, GHOST_HEIGHT, GHOST2_X, GHOST2_Y, GHOST_VEL)
ghost3 = Ghosts(GHOST_WIDTH, GHOST_HEIGHT, GHOST3_X, GHOST3_Y, GHOST_VEL)
ghost4 = Ghosts(GHOST_WIDTH, GHOST_HEIGHT, GHOST4_X, GHOST4_Y, GHOST_VEL)
def drawMap():
screen.blit(image, [0,0])
x = 35
y = 35
for i in range (0, 12):
pygame.draw.rect (screen, WHITE, [x, y, 5, 5], 0)
x = x + 20
screen.blit (ghost1.image, [ghost1.x, ghost1.y])
screen.blit (ghost2.image, [ghost2.x, ghost2.y])
screen.blit (ghost3.image, [ghost3.x, ghost3.y])
screen.blit (ghost4.image, [ghost4.x, ghost4.y])
def detectCollide():
if tuple(screen.get_at((PACMAN_X, PACMAN_Y))) == BORDER_COLOR or tuple(screen.get_at((PACMAN_X+PACMAN_WIDTH, PACMAN_Y+PACMAN_HEIGHT))) == BORDER_COLOR or tuple(screen.get_at((PACMAN_X+PACMAN_WIDTH, PACMAN_Y))) == BORDER_COLOR or tuple(screen.get_at((PACMAN_X, PACMAN_Y+PACMAN_HEIGHT))) == BORDER_COLOR or tuple(screen.get_at((PACMAN_X+PACMAN_WIDTH/2, PACMAN_Y))) == BORDER_COLOR or tuple(screen.get_at((PACMAN_X, PACMAN_Y+PACMAN_HEIGHT/2))) == BORDER_COLOR or tuple(screen.get_at((PACMAN_X + PACMAN_WIDTH, PACMAN_Y+PACMAN_HEIGHT/2))) == BORDER_COLOR or tuple(screen.get_at((PACMAN_X+PACMAN_WIDTH/2, PACMAN_Y+PACMAN_HEIGHT))) == BORDER_COLOR:
return True
else:
return False
def ghostCollide():
global ALIVE
if tuple(screen.get_at((PACMAN_X, PACMAN_Y))) == GHOST_COLOR or tuple(screen.get_at((PACMAN_X+PACMAN_WIDTH, PACMAN_Y+PACMAN_HEIGHT))) == GHOST_COLOR or tuple(screen.get_at((PACMAN_X+PACMAN_WIDTH, PACMAN_Y))) == GHOST_COLOR or tuple(screen.get_at((PACMAN_X, PACMAN_Y+PACMAN_HEIGHT))) == GHOST_COLOR or tuple(screen.get_at((PACMAN_X+PACMAN_WIDTH/2, PACMAN_Y))) == GHOST_COLOR or tuple(screen.get_at((PACMAN_X, PACMAN_Y+PACMAN_HEIGHT/2))) == GHOST_COLOR or tuple(screen.get_at((PACMAN_X + PACMAN_WIDTH, PACMAN_Y+PACMAN_HEIGHT/2))) == GHOST_COLOR or tuple(screen.get_at((PACMAN_X+PACMAN_WIDTH/2, PACMAN_Y+PACMAN_HEIGHT))) == GHOST_COLOR:
ALIVE = False
else:
ALIVE = True
def sprite_move():
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
global PACMAN_X
global PACMAN_Y
if keys[pygame.K_LEFT]:
PACMAN_X -= PACMAN_VEL
if detectCollide():
PACMAN_X += PACMAN_VEL
time.sleep(0.0006)
if keys[pygame.K_RIGHT]:
PACMAN_X += PACMAN_VEL
if detectCollide():
PACMAN_X -= PACMAN_VEL
time.sleep(0.0006)
if keys[pygame.K_UP] and PACMAN_Y>0:
PACMAN_Y -= PACMAN_VEL
if detectCollide():
PACMAN_Y += PACMAN_VEL
time.sleep(0.0006)
if keys[pygame.K_DOWN] and PACMAN_Y<HEIGHT:
PACMAN_Y += PACMAN_VEL
if detectCollide():
PACMAN_Y -= PACMAN_VEL
time.sleep(0.0006)
if ALIVE:
ghost4.move()
ghost3.move()
ghost2.move()
ghost1.move()
ghostCollide()
drawMap()
pygame.draw.rect(screen, YELLOW, (PACMAN_X, PACMAN_Y, PACMAN_WIDTH, PACMAN_HEIGHT))
pygame.display.update()
else:
pygame.quit()
def main():
if ALIVE == True:
drawMap()
sprite_move()
else:
pygame.quit()
print "Hit ESC to end the program."
while ALIVE: #Animation loop
pygame.event.get()
keys = pygame.key.get_pressed()
if keys[pygame.K_ESCAPE]:
ALIVE = False
main()
pygame.quit()