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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
| from tkinter import Tk, Frame, Canvas,X, Label , StringVar
from numpy import loadtxt, ndenumerate
import Algo_A_V5
MOTIONS = {
'Up': (0, -1),
'Down': (0, 1),
'Left': (-1, 0),
'Right':(1, 0)
}
CASE_SIZE = 20
COIN_SIZE = 5 # rayon
PAC_SIZE = 8 # rayon
LIFE = 3
ENNEMI_SPEED = 300
def cercle(cadre,x, y, r, coul ='black'):
"tracé d'un cercle de centre (x,y) et de rayon r"
return(cadre.create_oval(x-r, y-r, x+r, y+r, fill=coul))
class PacMan:
def __init__(self, root):
self.game_over = False
self.level = 1
self.life = LIFE
self.run_game = True
self.score = 0
self.load_map()
self.init_position()
self.root = root
self.root.title('Pac Man')
self.init_game_graphics()
self.free_case = self.map_game.shape[0]*self.map_game.shape[1]
self.init_terrain()
self.init_boule()
self.boule_motion_launch()
self.ennemi_A = Ennemie(self.can_game,self.map_game)
self.ennemi_A.pacman_position(self.case_row,self.case_column)
self.ennemi_A.path()
self.move()
def relaunch_game(self):
self.game_over = False
self.can_game.delete('all')
self.level = 1
self.life = LIFE
self.run_game = True
self.score = 0
self.load_map()
self.init_position()
self.free_case = self.map_game.shape[0]*self.map_game.shape[1]
self.init_terrain()
self.init_boule()
self.boule_motion_launch()
# self.ennemi_A = Ennemie(self.can_game,self.map_game)
def init_position(self):
for (x,y), value in ndenumerate(self.map_game):
if self.map_game[x,y] == 2:
self.case_row = x
self.case_column = y
break
def init_game_graphics(self):
self.lar = self.map_game.shape[0]*CASE_SIZE
self.hau = self.map_game.shape[1]*CASE_SIZE
self.frame_game = Frame(self.root,bg="light yellow")
self.frame_game.pack()
self.can_game = Canvas(self.frame_game,width=self.lar, height=self.hau, bg="black")
self.can_game.pack(padx =10, pady=(0,10),side=('bottom'))
self.can_game.focus_set()
self.label_var_score = StringVar()
self.label_score = Label(self.frame_game,textvariable=self.label_var_score, bg="light yellow", font='Helvetica 7 bold italic')
self.label_score.pack(side=('bottom'))
self.label_var_score.set('Score ' + str(self.score))
self.label_var_level = StringVar()
self.label_level = Label(self.frame_game,textvariable=self.label_var_level, bg="light yellow", font='Helvetica 7 bold italic')
self.label_level.pack(side=('left'),padx = (20,0))
self.label_var_level.set('Level ' + str(self.level))
self.label_var_life = StringVar()
self.label_life = Label(self.frame_game,textvariable=self.label_var_life, bg="light yellow", font='Helvetica 7 bold italic')
self.label_life.pack(side=('right'), padx=(0,20))
self.label_var_life.set('Life ' + str(self.life))
def load_map(self):
file_name = 'Map/' + str(self.level) + '.txt'
self.map_game = loadtxt(file_name)
def on_arrow(self, event):
self.direction = event.keysym
self.cx, self.cy = MOTIONS[self.direction]
self.move()
def init_boule(self):
self.can_game.delete("pacman")
self.boule = cercle(self.can_game, self.case_column*CASE_SIZE+CASE_SIZE/2,
self.case_row*CASE_SIZE+CASE_SIZE/2, PAC_SIZE,"gold")
self.can_game.addtag_withtag("pacman",self.boule)
self.direction = None
self.cy = 0
self.cx = 0
def restart(self,event):
self.can_game.delete('all')
if self.game_over:
self.relaunch_game()
def boule_motion_launch(self):
for key in MOTIONS:
self.can_game.bind('<%s>' % key, self.on_arrow)
def check_ennemi_pos(self):
ennemi_pos = self.ennemi_A.ennemi_position()
print(ennemi_pos)
row = ennemi_pos[0]
col = ennemi_pos[1]
if self.case_row == row and self.case_column == col:
self.echec()
return False
else:
return True
def echec(self):
print("failure")
self.life -= 1
self.label_var_life.set('Life ' + str(self.life))
if self.life == 0:
self.run_game = False
self.can_game.delete('all')
self.can_game.create_text(self.lar/2, self.hau/2, fill="white",font="Times 8 italic bold",text="Game Over,\n Press enter to restart")
self.game_over = True
self.can_game.bind('<Return>', self.restart)
else:
self.init_position()
self.init_boule()
self.ennemi_A.relaunch()
def move(self):
if self.direction and self.check_terrain() and self.run_game:
self.case_row = self.case_row + self.cy
self.case_column = self.case_column + self.cx
self.ennemi_A.pacman_position(self.case_row,self.case_column)
if self.check_ennemi_pos():
self.can_game.move(self.boule,self.cx*CASE_SIZE,self.cy*CASE_SIZE)
if self.map_game[self.case_row,self.case_column] != 0:
self.object_eater()
self.victory()
#self.can_game.after(150,self.move)
def victory(self):
if self.free_case == 0:
print("victory")
def update_score(self):
if self.map_game[self.case_row,self.case_column]==2:
self.score = self.score+1
elif self.map_game[self.case_row,self.case_column]==3:
self.score = self.score+2
self.label_var_score.set('Score ' + str(self.score))
def object_eater(self):
self.update_score()
object_finder = self.can_game.find_enclosed((self.case_column+1/2)*CASE_SIZE-COIN_SIZE-1,
(self.case_row+1/2)*CASE_SIZE-COIN_SIZE-1,
(self.case_column+1/2)*CASE_SIZE+COIN_SIZE+1,
(self.case_row+1/2)*CASE_SIZE+COIN_SIZE+1)
self.can_game.delete(object_finder[0])
self.map_game[self.case_row,self.case_column] = 0
self.free_case -= 1
def check_terrain(self):
row = self.case_row + self.cy
col = self.case_column + self.cx
if col >= 0 and col < self.map_game.shape[1] and \
row >= 0 and row < self.map_game.shape[0] and \
self.map_game[row,col] !=1:
return True
def init_terrain(self):
for (x,y), value in ndenumerate(self.map_game):
if self.map_game[x,y] == 1:
self.can_game.create_rectangle(y*CASE_SIZE,x*CASE_SIZE,
(y+1)*CASE_SIZE,(x+1)*CASE_SIZE,
outline='blue')
self.free_case -= 1
elif self.map_game[x,y] == 2:
cercle(self.can_game,CASE_SIZE*(y+1/2),CASE_SIZE*(x+1/2),
COIN_SIZE,"yellow")
elif self.map_game[x,y] == 3:
cercle(self.can_game,CASE_SIZE*(y+1/2),CASE_SIZE*(x+1/2),
COIN_SIZE,"green")
class Ennemie():
def __init__(self, can_game,map_game):
self.can_game = can_game
self.map_game = map_game
self.position = self.init_position()
self.draw_boule()
self.pac_man_row = float('inf')
self.pac_man_col = float('inf')
self.counter_motions = 0
self.cx = 0
self.cy = -1
if self.pac_man_row < float('inf'):
source = [self.case_row,self.case_column]
target = [self.pac_man_row,self.pac_man_col]
print(target)
def init_position(self):
for (x,y), value in (ndenumerate(self.map_game)):
if self.map_game[x,y] == 2:
self.case_row = x
self.case_column = y
def draw_boule(self):
self.can_game.delete("ennemi")
self.boule = cercle(self.can_game, self.case_column*CASE_SIZE+CASE_SIZE/2,
self.case_row*CASE_SIZE+CASE_SIZE/2, PAC_SIZE,"red")
self.can_game.addtag_withtag("ennemi",self.boule)
def path(self):
source = [self.case_row,self.case_column]
target = [self.pac_man_row,self.pac_man_col]
self.Algo = Algo_A_V5.Algo_A(source,target,self.map_game)
self.motion_list = self.Algo.return_result()
self.ma_liste_titled=[] # UP => Up, LEFT => Left
[self.ma_liste_titled.append(self.motion_list[i].title()) for i in range(len(self.motion_list))]
self.move()
def move(self):
if self.ma_liste_titled:
self.cx, self.cy = MOTIONS[self.ma_liste_titled.pop(0)]
self.case_row = self.case_row + self.cy
self.case_column = self.case_column + self.cx
self.can_game.move(self.boule,self.cx*CASE_SIZE,self.cy*CASE_SIZE)
self.can_game.after(ENNEMI_SPEED,self.move)
else:
self.path()
def ennemi_position(self):
return (self.case_row,self.case_column)
def pacman_position(self,row,col):
self.pac_man_row = row
self.pac_man_col = col
def relaunch(self):
print("ok")
self.init_position()
self.draw_boule()
if __name__ == '__main__':
root = Tk()
pacMan_launch = PacMan(root)
root.mainloop() |
Partager