def pop1():
menu2_2.post(win.winfo_x()+60,win.winfo_y()+120)
from tkinter import *
import copy
from tkinter.messagebox import *
win=Tk()
menu1=Menu(win)
menu2_1=Menu(menu1,tearoff=False)
menu1.add_cascade(label=“点击”,menu=menu2_1)
menu2_1.add_command(label=“开始”,command=cv)
menu1.add_command(label=“修改”,command=pop1)
menu2_2=Menu(menu1,tearoff=False)
menu2_2.add_command(label=“添加城市”)
menu2_2.add_command(label=“修改城市”)
menu1.add_command(label=" 退出",command=win.quit)
win.config(menu=menu1)
win.mainloop()
imgs= [PhotoImage(file=‘E:\pythonProjects\bmp\50.gif’),
PhotoImage(file=‘E:\pythonProjects\bmp\53.gif’),
PhotoImage(file=‘E:\pythonProjects\bmp\55.gif’),
PhotoImage(file=‘E:\pythonProjects\bmp\Passageway.gif’),
PhotoImage(file=‘E:\pythonProjects\bmp\Destination.gif’),
PhotoImage(file=‘E:\pythonProjects\bmp\WorkerInDest.gif’),
PhotoImage(file=‘E:\pythonProjects\bmp\RedBox.gif’) ]
Wall=0
Worker=1
Box=2
Passageway=3
Destination=4
WorkerInDest=5
RedBox=6
myArray1 = [[0,3,1,4,3,3,3,0],
[0,3,3,3,2,3,0,0],
[0,0,3,0,3,3,0,0],
[3,3,3,3,3,0,0,0],
[3,4,3,2,0,0,0,0],
[0,0,3,3,2,3,0,0],
[0,0,4,3,3,3,0,0]]
def drawGameImage():
global x,y
for i in range(0,7):
for j in range(0,7):
if myArray[i][j]==Worker:
x=i
y=j
print(“工人当前位置:”,x,y)
img1=imgs[myArray[i][j]]
cv.create_image((i32+20,j32+20),image=img1)
cv.pack()
def callback(event):
global x,y,myArray
print(“按下键:”)
print(“按下键:”,event.char)
KeyCode=event.keysym
if KeyCode==“Up”:
x1=x;
y1=y-1;
x2=x;
y2=y-2;
MoveTo(x1,y1,x2,y2);
elif KeyCode==“Down”:
x1=x
y1=y+1
x2=x
y2=y+2
MoveTo(x1,y1,x2,y2);
elif KeyCode==“Left”:
x1=x-1;
y1=y;
x2=x-2;
y2=y;
MoveTo(x1,y1,x2,y2);
elif KeyCode==“Right”:
x1=x+1;
y1=y;
x2=x+2;
y2=y;
MoveTo(x1,y1,x2,y2);
elif KeyCode==“space”:
print(“按下键:空格”,event.char)
myArray=copy.deepcopy(myArray1)
drawGameImage()
def IsInGameArea(row,col):
return (row>=0 and row<7 and col>=0 and col<7)
def MoveTo(x1,y1,x2,y2):
global x,y
p1=None
p2=None
if IsInGameArea(x1,y1):
p1=myArray[x1][y1];
if IsInGameArea(x2,y2):
p2=myArray[x2][y2];
if p1Passageway:
MoveMan(x,y);
x=x1;
y=y1;
myArray[x1][y1]=Worker;
if p1Destination:
MoveMan(x,y);
x=x1;
y=y1;
myArray[x1][y1]=WorkerInDest;
if p1Wall or not IsInGameArea(x1,y1):
return;
if p1Box:
if p2Wall or not IsInGameArea(x1,y1) or p2Box:
return;
if p1Box and p2Passageway:
MoveMan(x,y);
x=x1;
y=y1;
myArray[x2][y2]=Box;
myArray[x1][y1]=Worker;
if p1Box and p2Destination:
MoveMan(x,y);
x=x1;
y=y1;
myArray[x2][y2]=RedBox;
myArray[x1][y1]=Worker;
if p1RedBox and p2Passageway:
MoveMan(x,y)
x=x1;
y=y1
myArray[x2][y2]=Box;
myArray[x1][y1]=WorkerInDest;
if p1RedBox and p2Destination:
MoveMan(x,y)
x=x1;
y=y1;
myArray[x2][y2]=RedBox;
myArray[x1][y1]=WorkerInDest;
drawGameImage()
if IsFinish():
showinfo(title="提示",message="真棒通关了")
print("下一关")
def MoveMan(x,y) :
if myArray[x][y]==Worker:
myArray[x][y]=Passageway;
elif myArray[x][y]==WorkerInDest:
myArray[x][y]=Destination;
def IsFinish():
bFinish=True;
for i in range(0,7):
for j in range(0,7):
if (myArray[i][j]==Destination
or myArray[i][j]==WorkerInDest):
bFinish=False;
return bFinish
def drawQiPan( ) :
for i in range(0,15) :
cv.create_line(20,20+40i,580,20+40i,width=2)
for i in range(0,15) :
cv.create_line(20+40i,20,20+40i,580,width=2)
cv.pack()
def print_map( ) :
for i in range(0,15) :
for j in range(0,15) :
print (map[i][j],end=’ ')
print (‘w’)
cv = Canvas(win, bg = ‘green’, width = 226, height = 226)
myArray=copy.deepcopy(myArray1)
drawGameImage()
cv.bind("", callback)
cv.pack()
cv.focus_set()
win.mainloop()