推箱子是一款益智的小游戏,在小时候大家都玩过
今天用Python自己实现一个,它是使用Python3+tkinter(内置模块)实现的,检查了键盘的移动,然后控制人物的位置
运行效果如下:
完整代码如下:(素材下载地址:https://www.itprojects.cn/web/material/details.html?id=18)
1 from tkinter import * 2 from tkinter.messagebox import * 3 import copy 4 5 root = Tk() 6 root.title("推箱子 更多请访问www.itprojects.cn") 7 8 imgs = [PhotoImage(file='./bmp/Wall.gif'), 9 PhotoImage(file='./bmp/Worker.gif'), 10 PhotoImage(file='./bmp/Box.gif'), 11 PhotoImage(file='./bmp/Passageway.gif'), 12 PhotoImage(file='./bmp/Destination.gif'), 13 PhotoImage(file='./bmp/WorkerInDest.gif'), 14 PhotoImage(file='./bmp/RedBox.gif')] 15 # 0代表墙,1代表人,2代表箱子,3代表路,4代表目的地 16 # 5代表人在目的地,6代表放到目的地的箱子 17 Wall = 0 18 Worker = 1 19 Box = 2 20 Passageway = 3 21 Destination = 4 22 WorkerInDest = 5 23 RedBox = 6 24 # 原始地图 25 myArray1 = [[0, 3, 1, 4, 3, 3, 3], 26 [0, 3, 3, 2, 3, 3, 0], 27 [0, 0, 3, 0, 3, 3, 0], 28 [3, 3, 2, 3, 0, 0, 0], 29 [3, 4, 3, 3, 3, 0, 0], 30 [0, 0, 3, 3, 3, 3, 0], 31 [0, 0, 0, 0, 0, 0, 0]] 32 33 34 # 绘制整个游戏区域图形 35 def drawGameImage(): 36 global x, y 37 38 for i in range(0, 7): # 0--6 39 for j in range(0, 7): # 0--6 40 if myArray[i][j] == Worker: 41 x = i # 工人当前位置(x,y) 42 y = j 43 print("工人当前位置:", x, y) 44 img1 = imgs[myArray[i][j]] 45 cv.create_image((i * 32 + 20, j * 32 + 20), image=img1) 46 cv.pack() 47 # print (myArray) 48 49 50 def callback(event): # 按键处理 51 global x, y, myArray 52 print("按下键:", event.char) 53 KeyCode = event.keysym 54 # 工人当前位置(x,y) 55 if KeyCode == "Up": # 分析按键消息 56 # 向上 57 x1 = x; 58 y1 = y - 1; 59 x2 = x; 60 y2 = y - 2; 61 # 将所有位置输入以判断并作地图更新 62 MoveTo(x1, y1, x2, y2); 63 # 向下 64 elif KeyCode == "Down": 65 x1 = x; 66 y1 = y + 1; 67 x2 = x; 68 y2 = y + 2; 69 MoveTo(x1, y1, x2, y2); 70 # 向左 71 elif KeyCode == "Left": 72 x1 = x - 1; 73 y1 = y; 74 x2 = x - 2; 75 y2 = y; 76 MoveTo(x1, y1, x2, y2); 77 # 向右 78 elif KeyCode == "Right": 79 x1 = x + 1; 80 y1 = y; 81 x2 = x + 2; 82 y2 = y; 83 MoveTo(x1, y1, x2, y2); 84 elif KeyCode == "space": # 空格键 85 print("按下键:空格", event.char) 86 myArray = copy.deepcopy(myArray1) # 恢复原始地图 87 drawGameImage() 88 89 90 # 判断是否在游戏区域 91 def IsInGameArea(row, col): 92 return (row >= 0 and row < 7 and col >= 0 and col < 7) 93 94 95 def MoveTo(x1, y1, x2, y2): 96 global x, y 97 P1 = None 98 P2 = None 99 if IsInGameArea(x1, y1): # 判断是否在游戏区域 100 P1 = myArray[x1][y1]; 101 if IsInGameArea(x2, y2): 102 P2 = myArray[x2][y2] 103 if P1 == Passageway: # P1处为通道 104 MoveMan(x, y); 105 x = x1; 106 y = y1; 107 myArray[x1][y1] = Worker; 108 if P1 == Destination: # P1处为目的地 109 MoveMan(x, y); 110 x = x1; 111 y = y1; 112 myArray[x1][y1] = WorkerInDest; 113 if P1 == Wall or not IsInGameArea(x1, y1): 114 # P1处为墙或出界 115 return; 116 if P1 == Box: # P1处为箱子 117 if P2 == Wall or not IsInGameArea(x1, y1) or P2 == Box: ##P2处为墙或出界 118 return; 119 # 以下P1处为箱子 120 121 # P1处为箱子,P2处为通道 122 if P1 == Box and P2 == Passageway: 123 MoveMan(x, y); 124 x = x1; 125 y = y1; 126 myArray[x2][y2] = Box; 127 myArray[x1][y1] = Worker; 128 if P1 == Box and P2 == Destination: 129 MoveMan(x, y); 130 x = x1; 131 y = y1; 132 myArray[x2][y2] = RedBox; 133 myArray[x1][y1] = Worker; 134 # P1处为放到目的地的箱子,P2处为通道 135 if P1 == RedBox and P2 == Passageway: 136 MoveMan(x, y); 137 x = x1; 138 y = y1; 139 myArray[x2][y2] = Box; 140 myArray[x1][y1] = WorkerInDest; 141 # P1处为放到目的地的箱子,P2处为目的地 142 if P1 == RedBox and P2 == Destination: 143 MoveMan(x, y); 144 x = x1; 145 y = y1; 146 myArray[x2][y2] = RedBox; 147 myArray[x1][y1] = WorkerInDest; 148 drawGameImage() 149 # 这里要验证是否过关 150 if IsFinish(): 151 showinfo(title="提示", message=" 恭喜你顺利过关") 152 print("下一关") 153 154 155 def MoveMan(x, y): 156 if myArray[x][y] == Worker: 157 myArray[x][y] = Passageway; 158 elif myArray[x][y] == WorkerInDest: 159 myArray[x][y] = Destination; 160 161 162 def IsFinish(): # 验证是否过关 163 bFinish = True; 164 for i in range(0, 7): # 0--6 165 for j in range(0, 7): # 0--6 166 if (myArray[i][j] == Destination 167 or myArray[i][j] == WorkerInDest): 168 bFinish = False; 169 return bFinish; 170 171 172 def drawQiPan(): # 画棋盘 173 for i in range(0, 15): 174 cv.create_line(20, 20 + 40 * i, 580, 20 + 40 * i, width=2) 175 for i in range(0, 15): 176 cv.create_line(20 + 40 * i, 20, 20 + 40 * i, 580, width=2) 177 cv.pack() 178 179 180 def print_map(): # 输出map地图 181 for i in range(0, 15): # 0--14 182 for j in range(0, 15): # 0--14 183 print(map[i][j], end=' ') 184 print('w') 185 186 187 cv = Canvas(root, bg='green', width=226, height=226) 188 # drawQiPan( ) 189 myArray = copy.deepcopy(myArray1) 190 drawGameImage() 191 192 cv.bind("<KeyPress>", callback) 193 cv.pack() 194 cv.focus_set() # 将焦点设置到cv上 195 root.mainloop()