python实现推箱子游戏.doc

上传人:灯火****19 文档编号:134959763 上传时间:2020-06-10 格式:DOC 页数:6 大小:79.02KB
返回 下载 相关 举报
python实现推箱子游戏.doc_第1页
第1页 / 共6页
python实现推箱子游戏.doc_第2页
第2页 / 共6页
python实现推箱子游戏.doc_第3页
第3页 / 共6页
python实现推箱子游戏.doc_第4页
第4页 / 共6页
python实现推箱子游戏.doc_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《python实现推箱子游戏.doc》由会员分享,可在线阅读,更多相关《python实现推箱子游戏.doc(6页珍藏版)》请在金锄头文库上搜索。

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 = Non

2、e, 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) +

3、 .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)

4、#清空画布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

5、后退, 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(sel

6、f.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.ma

7、p.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.s

8、tack) = 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.mapm

9、xmy = 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_yi

10、ndex 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

11、 + 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 (mp

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 幼儿/小学教育 > 幼儿教育

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号