python实现推箱子游戏.doc_第1页
python实现推箱子游戏.doc_第2页
python实现推箱子游戏.doc_第3页
python实现推箱子游戏.doc_第4页
python实现推箱子游戏.doc_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

#encoding:utf-8import tkMessageBoxfrom Tkinter import Tk, Canvas, Framefrom PIL import ImageTk, Imageimg = def load_picture(): global img for i in range(0, 10): file = F:/source/ + str(i) + .bmp im = Image.open(file) im = ImageTk.PhotoImage(im) img.append(im)class Grid: def _init_(self, master = None, window_width = 450, window_height = 500, width = 30): self.master = master self.width = width self.map = self.load_map(1) self.canvas = Canvas(master, width = window_width, height = window_height, bg = cyan) self.canvas.pack() self.display_map() def load_map(self, i): file = F:/source/ + str(i) + .txt f = open(file, r) lines = f.readlines() for k in range(0, len(self.map): self.map.pop() for k in range(0, len(lines): mp = line = linesk for j in range(0, len(lines): p = int(linej) mp.append(p) if p = 6: self.x = k self.y = j self.map.append(mp) def display_map(self): self.canvas.delete(all) #清空画布canvas m = len(self.map) n = len(self.map0) for i in range(0, m): for j in range(0, n): self.create_image(i, j) self.display_text() def display_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, fill = black) def create_image(self, i, j): global img x = (2 * i + 1) * self.width / 2 y = (2 * j + 1) * self.width / 2 pic = self.mapij self.canvas.create_image(y, x, image = imgpic)class Game(Frame): def _init_(self, master = None): Frame._init_(self, master) self.grid = Grid(self.master) self.direction = Down self.map = self.get_map() # 获取地图,用于标志地图中最初箱子放在目标位置 self.stack = 1 self.level = 1 self.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) def get_map(self): for k in range(0, len(self.map): self.map.pop() for i in self.grid.map: mp = for j in i: mp.append(j) self.map.append(mp) def KeyPress_event(self, event): key = event.keysym if self.dir.has_key(key): self.direction = key self.move() elif key = Prior: self.prior_level() elif key = Next: self.next_level() elif key = BackSpace: if len(self.stack) = 1: return cur_dir = self.stack.pop() # 当前坐标的方向 # 当前坐标 mx = self.stack.pop() my = self.stack.pop() cur_picture = self.stack.pop() # 当前坐标cur_dir方向的图片 next_picture = self.stack.pop() # 前一个坐标以前图片 pre_dir = self.stacklen(self.stack) - 1 dx = self.dir_xcur_dir dy = self.dir_ycur_dir self.grid.mapmxmy = cur_picture self.grid.mapmx + dxmy + dy = next_picture self.grid.mapmx - dxmy - dy = pre_dir + 5 self.grid.x = mx - dx self.grid.y = my - dy self.grid.display_map() def move(self): index = self.dirself.direction x = self.grid.x # 原来的位置 y = self.grid.y dx = self.dir_xindex # 位移 dy = self.dir_yindex mp = self.grid.map self.grid.x += dx self.grid.y += dy manX = self.grid.x manY = self.grid.y origin_picture = mpmanXmanY next_picture = mpmanX + dxmanY + dy # dir方向是箱子,箱子dir方向是墙或箱子 if (mpmanXmanY = 4 or mpmanXmanY = 3) and (mpmanX + dxmanY + dy = 2 or mpmanX + dxmanY + dy = 3 or mpmanX + dxmanY + dy = 4): self.grid.x -= dx self.grid.y -= dy return # dir方向是墙 if mpmanXmanY = 2: self.grid.x -= dx self.grid.y -= dy return # dir方向是草地或者目的地 if mpmanXmanY = 1 or mpmanXmanY = 9: mpmanXmanY = index + 5 # self.grid.create_image(manX, manY) if self.mapxy = 9: # 原来位置是目的地 mpxy = 9 else: mpxy = 1 # 原来位置是草地 # self.grid.create_image(x, y) # dir方向是箱子,箱子dir方向是草地 if (mpmanXmanY = 4 or mpmanXmanY = 3) and mpmanX + dxmanY + dy = 1: mpmanXmanY = index + 5 # self.grid.create_image(manX, manY) if self.mapxy = 9: # 原来位置是目的地 mpxy = 9 else: # 原来位置是草地 mpxy = 1 # self.grid.create_image(x, y) mpmanX + dxmanY + dy = 4 # 草地位置变箱子 # self.grid.create_image(manX + dx, manY + dy) # dir方向是箱子,箱子dir方向是目的地 if (mpmanXmanY = 4 or mpmanXmanY = 3) and mpmanX + dxmanY + dy = 9: mpmanXmanY = index + 5 # self.grid.create_image(manX, manY) if self.mapxy = 9: # 原来位置是目的地 mpxy = 9 else: # 原来位置是草地 mpxy = 1 # self.grid.create_image(x, y) mpmanX + dxmanY + dy = 3 # 目的地位置变箱子 # self.grid.create_image(manX + dx, manY + dy) self.grid.display_map() # 重绘画布 if self.is_pass(): message = tkMessageBox.showinfo(提示, 恭喜过关!) if message = ok: self.next_level() # 每走一步保存当前位置的坐标、以前的图片 self.stack.append(next_picture) self.stack.append(origin_picture) self.stack.append(manY) self.stack.append(manX) self.stack.append(index) def destroy_stack(self): for i in range(0, len(self.stack) - 1): self.stack.pop() def prior_level(self): if self.level = 1: return self.level -= 1 self.grid.load_map(self.level) self.grid.display_map() self.destroy() self.get_map() def next_level(self): if self.level = 13: return self.level += 1 self.grid.load_map(self.level) self.grid.display_map() self.dest

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论