-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdatedMap.py
118 lines (94 loc) · 3.69 KB
/
updatedMap.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
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 = 25
PACMAN_Y = 25
PACMAN_WIDTH = 20
PACMAN_HEIGHT = 20
PACMAN_VEL = 1
YELLOW = (255,255,153)
BLACK = (0,0,0)
BLUE = (0,0,255)
WHITE = (255,255,255)
BORDER_COLOR = (0,128,248,255)
image = pygame.image.load("template.png").convert()
image = pygame.transform.scale(image, (560, 620))
def drawMap():
screen.blit(image, [0,0])
def drawDots():
x = 35
y = 35
for i in range (0, 12):
pygame.draw.rect (screen, WHITE, [x, y, 5, 5], 0)
x = x + 20
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 detectDots():
if tuple(screen.get_at((PACMAN_X, PACMAN_Y))) == WHITE or tuple(screen.get_at((PACMAN_X+PACMAN_WIDTH, PACMAN_Y+PACMAN_HEIGHT))) == WHITE or tuple(screen.get_at((PACMAN_X+PACMAN_WIDTH, PACMAN_Y))) == WHITE or tuple(screen.get_at((PACMAN_X, PACMAN_Y+PACMAN_HEIGHT))) == WHITE or tuple(screen.get_at((PACMAN_X+PACMAN_WIDTH/2, PACMAN_Y))) == WHITE or tuple(screen.get_at((PACMAN_X, PACMAN_Y+PACMAN_HEIGHT/2))) == WHITE or tuple(screen.get_at((PACMAN_X + PACMAN_WIDTH, PACMAN_Y+PACMAN_HEIGHT/2))) == WHITE or tuple(screen.get_at((PACMAN_X+PACMAN_WIDTH/2, PACMAN_Y+PACMAN_HEIGHT))) == WHITE:
return True
else:
return False
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 PACMAN_X<0:
PACMAN_X = WIDTH-PACMAN_WIDTH
if detectCollide():
PACMAN_X += PACMAN_VEL
time.sleep(0.002)
if detectDots():
pygame.draw.rect (screen, BLACK, [PACMAN_X, PACMAN_Y, 5, 5], 0)
if keys[pygame.K_RIGHT]:
PACMAN_X += PACMAN_VEL
if PACMAN_X > WIDTH-PACMAN_WIDTH:
PACMAN_X = 0
if detectCollide():
PACMAN_X -= PACMAN_VEL
time.sleep(0.002)
if detectDots():
pygame.draw.rect (screen, BLACK, [PACMAN_X, PACMAN_Y, 5, 5], 0)
if keys[pygame.K_UP] and PACMAN_Y>0:
PACMAN_Y -= PACMAN_VEL
if detectCollide():
PACMAN_Y += PACMAN_VEL
time.sleep(0.002)
if detectDots():
pygame.draw.rect (screen, BLACK, [PACMAN_X, PACMAN_Y, 5, 5], 0)
if keys[pygame.K_DOWN] and PACMAN_Y<HEIGHT:
PACMAN_Y += PACMAN_VEL
if detectCollide():
PACMAN_Y -= PACMAN_VEL
time.sleep(0.002)
if detectDots():
pygame.draw.rect (screen, BLACK, [PACMAN_X, PACMAN_Y, 5, 5], 0)
drawMap()
pygame.draw.rect(screen, YELLOW, (PACMAN_X, PACMAN_Y, PACMAN_WIDTH, PACMAN_HEIGHT))
pygame.display.update()
def main():
drawMap()
drawDots()
sprite_move()
inPlay = True
print "Hit ESC to end the program."
while inPlay: #Animation loop
pygame.event.get()
keys = pygame.key.get_pressed()
if keys[pygame.K_ESCAPE]:
inPlay = False
main()
pygame.quit()