-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpacman1.py
More file actions
142 lines (128 loc) · 4.19 KB
/
pacman1.py
File metadata and controls
142 lines (128 loc) · 4.19 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
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
import pgzrun
import gameinput
import gamemaps
from random import randint
from datetime import datetime
WIDTH = 600
HEIGHT = 660
player = Actor("pacman_o") # Load in the player Actor image
SPEED = 3
def draw(): # Pygame Zero draw function
global pacDots, player
screen.blit('header', (0, 0))
screen.blit('colourmap', (0, 80))
pacDotsLeft = 0
for a in range(len(pacDots)):
if pacDots[a].status == 0:
pacDots[a].draw()
pacDotsLeft += 1
if pacDots[a].collidepoint((player.x, player.y)):
pacDots[a].status = 1
if pacDotsLeft == 0:
player.status = 2
drawGhosts()
getPlayerImage()
player.draw()
if player.status == 1:
screen.draw.text("GAME OVER" , center=(300, 434), owidth=0.5, ocolor=(255,255,255), color=(255,64,0) , fontsize=40)
if player.status == 2:
screen.draw.text("YOU WIN!" , center=(300, 434), owidth=0.5, ocolor=(255,255,255), color=(255,64,0) , fontsize=40)
def update(): # Pygame Zero update function
global player, moveGhostsFlag, ghosts
if player.status == 0:
if moveGhostsFlag == 4: moveGhosts()
for g in range(len(ghosts)):
if ghosts[g].collidepoint((player.x, player.y)):
player.status = 1
pass
if player.inputActive:
gameinput.checkInput(player)
gamemaps.checkMovePoint(player)
if player.movex or player.movey:
inputLock()
animate(player, pos=(player.x + player.movex, player.y + player.movey), duration=1/SPEED, tween='linear', on_finished=inputUnLock)
def init():
global player
initDots()
initGhosts()
player.x = 290
player.y = 570
player.status = 0
inputUnLock()
def getPlayerImage():
global player
dt = datetime.now()
a = player.angle
tc = dt.microsecond%(500000/SPEED)/(100000/SPEED)
if tc > 2.5 and (player.movex != 0 or player.movey !=0):
if a != 180:
player.image = "pacman_c"
else:
player.image = "pacman_cr"
else:
if a != 180:
player.image = "pacman_o"
else:
player.image = "pacman_or"
player.angle = a
def drawGhosts():
for g in range(len(ghosts)):
if ghosts[g].x > player.x:
ghosts[g].image = "ghost"+str(g+1)+"r"
else:
ghosts[g].image = "ghost"+str(g+1)
ghosts[g].draw()
def moveGhosts():
global moveGhostsFlag
dmoves = [(1,0),(0,1),(-1,0),(0,-1)]
moveGhostsFlag = 0
for g in range(len(ghosts)):
dirs = gamemaps.getPossibleDirection(ghosts[g])
if ghostCollided(ghosts[g],g) and randint(0,3) == 0: ghosts[g].dir = 3
if (dirs[ghosts[g].dir] == 0 or randint(0,50) == 0):
d = -1
while d == -1:
rd = randint(0,3)
if dirs[rd] == 1:
d = rd
ghosts[g].dir = d
animate(ghosts[g], pos=(ghosts[g].x + dmoves[ghosts[g].dir][0]*20, ghosts[g].y + dmoves[ghosts[g].dir][1]*20), duration=1/SPEED, tween='linear', on_finished=flagMoveGhosts)
def flagMoveGhosts():
global moveGhostsFlag
moveGhostsFlag += 1
def ghostCollided(ga,gn):
for g in range(len(ghosts)):
if ghosts[g].colliderect(ga) and g != gn:
return True
return False
def initDots():
global pacDots
pacDots = []
a = x = 0
while x < 30:
y = 0
while y < 29:
if gamemaps.checkDotPoint(10+x*20, 10+y*20):
pacDots.append(Actor("dot",(10+x*20, 90+y*20)))
pacDots[a].status = 0
a += 1
y += 1
x += 1
def initGhosts():
global ghosts, moveGhostsFlag
moveGhostsFlag = 4
ghosts = []
g = 0
while g < 4:
ghosts.append(Actor("ghost"+str(g+1),(270+(g*20), 370)))
ghosts[g].dir = randint(0, 3)
g += 1
def inputLock():
global player
player.inputActive = False
def inputUnLock():
global player
player.movex = player.movey = 0
player.inputActive = True
init()
pgzrun.go()