资源描述:
《python实现推箱子游戏.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、#encoding:utf-8importtkMessageBoxfromTkinterimportTk,Canvas,FramefromPILimportImageTk,Imageimg=[]defload_picture():globalimgforiinrange(0,10):file="F:/source/"+str(i)+".bmp"im=Image.open(file)im=ImageTk.PhotoImage(im)img.append(im)classGrid:def__init__(self,master=None,win
2、dow_width=450,window_height=500,width=30):self.master=masterself.width=widthself.map=[]self.load_map(1)self.canvas=Canvas(master,width=window_width,height=window_height,bg='cyan')self.canvas.pack()self.display_map()defload_map(self,i):file="F:/source/"+str(i)+".txt"f=open(
3、file,"r")lines=f.readlines()forkinrange(0,len(self.map)):self.map.pop()forkinrange(0,len(lines)):mp=[]line=lines[k]forjinrange(0,len(lines)):p=int(line[j])mp.append(p)ifp==6:self.x=kself.y=jself.map.append(mp)defdisplay_map(self):self.canvas.delete('all')#清空画布canvasm=len(s
4、elf.map)n=len(self.map[0])foriinrange(0,m):forjinrange(0,n):self.create_image(i,j)self.display_text()defdisplay_text(self):self.canvas.create_text(210,460,text="pageUp,pageDown切换关卡",font='BOLD',fill='black')self.canvas.create_text(210,480,text="BackSpace后退",font='BOLD',fil
5、l='black')defcreate_image(self,i,j):globalimgx=(2*i+1)*self.width/2y=(2*j+1)*self.width/2pic=self.map[i][j]self.canvas.create_image(y,x,image=img[pic])classGame(Frame):def__init__(self,master=None):Frame.__init__(self,master)self.grid=Grid(self.master)self.direction='Down'
6、self.map=[]self.get_map()#获取地图,用于标志地图中最初箱子放在目标位置self.stack=[1]self.level=1self.dir_x=[-1,1,0,0]self.dir_y=[0,0,-1,1]self.dir={'Up':0,'Down':1,'Left':2,'Right':3}self.bind_all("",self.KeyPress_event)defget_map(self):forkinrange(0,len(self.map)):self.map.pop()forii
7、nself.grid.map:mp=[]forjini:mp.append(j)self.map.append(mp)defKeyPress_event(self,event):key=event.keysymifself.dir.has_key(key):self.direction=keyself.move()elifkey=='Prior':self.prior_level()elifkey=='Next':self.next_level()elifkey=='BackSpace':iflen(self.stack)==1:retur
8、ncur_dir=self.stack.pop()#当前坐标的方向#当前坐标mx=self.stack.pop()my=self.stack.pop()cur_picture=s