Push the box is a puzzle game, in childhood we have played
Today, Python implements one of its own, using Python3+ Tkinter (the built-in module), which checks the keyboard movement and controls the position of the character
The running effect is as follows:
The complete code is as follows: (material download address: www.itprojects.cn/detail.html…).
from tkinter import *
from tkinter.messagebox import *
import copy
root = Tk()
root.title("For more on pushing boxes visit www.itprojects.cn")
imgs = [PhotoImage(file='./bmp/Wall.gif'),
PhotoImage(file='./bmp/Worker.gif'),
PhotoImage(file='./bmp/Box.gif'),
PhotoImage(file='./bmp/Passageway.gif'),
PhotoImage(file='./bmp/Destination.gif'),
PhotoImage(file='./bmp/WorkerInDest.gif'),
PhotoImage(file='./bmp/RedBox.gif')]
# 0 for wall, 1 for box, 2 for box, 3 for road, and 4 for destination
# 5 represents the destination, and 6 represents the box placed at the destination
Wall = 0
Worker = 1
Box = 2
Passageway = 3
Destination = 4
WorkerInDest = 5
RedBox = 6
# Original map
myArray1 = [[0.3.1.4.3.3.3],
[0.3.3.2.3.3.0],
[0.0.3.0.3.3.0],
[3.3.2.3.0.0.0],
[3.4.3.3.3.0.0],
[0.0.3.3.3.3.0],
[0.0.0.0.0.0.0]]
Draw the entire game area
def drawGameImage() :
global x, y
for i in range(0.7) :# 0-6
for j in range(0.7) :# 0-6
if myArray[i][j] == Worker:
x = i # Current position of the worker (x,y)
y = j
print("Current position of worker :", x, y)
img1 = imgs[myArray[i][j]]
cv.create_image((i * 32 + 20, j * 32 + 20), image=img1)
cv.pack()
# print (myArray)
def callback(event) : # Key handling
global x, y, myArray
print("Press the key:", event.char)
KeyCode = event.keysym
# Current position of the worker (x,y)
if KeyCode == "Up": # Analyze key messages
# up
x1 = x;
y1 = y - 1;
x2 = x;
y2 = y - 2;
# Enter all locations for judgment and map update
MoveTo(x1, y1, x2, y2);
# down
elif KeyCode == "Down":
x1 = x;
y1 = y + 1;
x2 = x;
y2 = y + 2;
MoveTo(x1, y1, x2, y2);
# to the left
elif KeyCode == "Left":
x1 = x - 1;
y1 = y;
x2 = x - 2;
y2 = y;
MoveTo(x1, y1, x2, y2);
# to the right
elif KeyCode == "Right":
x1 = x + 1;
y1 = y;
x2 = x + 2;
y2 = y;
MoveTo(x1, y1, x2, y2);
elif KeyCode == "space": # the blank space key
print("Press key: Space", event.char)
myArray = copy.deepcopy(myArray1) # Restore the original map
drawGameImage()
# Determine if it is in the game area
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): # Determine if it is in the game area
P1 = myArray[x1][y1];
if IsInGameArea(x2, y2):
P2 = myArray[x2][y2]
if P1 == Passageway: # P1 is the channel
MoveMan(x, y);
x = x1;
y = y1;
myArray[x1][y1] = Worker;
if P1 == Destination: # P1 is the destination
MoveMan(x, y);
x = x1;
y = y1;
myArray[x1][y1] = WorkerInDest;
if P1 == Wall or not IsInGameArea(x1, y1):
# P1 is a wall or out of bounds
return;
if P1 == Box: # P1 is the box
if P2 == Wall or not IsInGameArea(x1, y1) or P2 == Box: ##P2 is the wall or exit
return;
# Below P1 is the box
# P1 is the box, and P2 is the channel
if P1 == Box and P2 == Passageway:
MoveMan(x, y);
x = x1;
y = y1;
myArray[x2][y2] = Box;
myArray[x1][y1] = Worker;
if P1 == Box and P2 == Destination:
MoveMan(x, y);
x = x1;
y = y1;
myArray[x2][y2] = RedBox;
myArray[x1][y1] = Worker;
# P1 is the box placed at the destination, and P2 is the channel
if P1 == RedBox and P2 == Passageway:
MoveMan(x, y);
x = x1;
y = y1;
myArray[x2][y2] = Box;
myArray[x1][y1] = WorkerInDest;
# P1 is the box placed at the destination, and P2 is the destination
if P1 == RedBox and P2 == Destination:
MoveMan(x, y);
x = x1;
y = y1;
myArray[x2][y2] = RedBox;
myArray[x1][y1] = WorkerInDest;
drawGameImage()
# Here to verify whether pass
if IsFinish():
showinfo(title="Tip", message="Congratulations on making it through.")
print("The next level")
def MoveMan(x, y) :
if myArray[x][y] == Worker:
myArray[x][y] = Passageway;
elif myArray[x][y] == WorkerInDest:
myArray[x][y] = Destination;
def IsFinish() : # Verify whether it passes the test
bFinish = True;
for i in range(0.7) :# 0-6
for j in range(0.7) :# 0-6
if (myArray[i][j] == Destination
or myArray[i][j] == WorkerInDest):
bFinish = False;
return bFinish;
def drawQiPan() : # drawing board
for i in range(0.15):
cv.create_line(20.20 + 40 * i, 580.20 + 40 * i, width=2)
for i in range(0.15):
cv.create_line(20 + 40 * i, 20.20 + 40 * i, 580, width=2)
cv.pack()
def print_map() : # output map
for i in range(0.15) :# 0-14
for j in range(0.15) :# 0-14
print(map[i][j], end=' ')
print('w')
cv = Canvas(root, bg='green', width=226, height=226)
# drawQiPan( )
myArray = copy.deepcopy(myArray1)
drawGameImage()
cv.bind("<KeyPress>", callback)
cv.pack()
cv.focus_set() Set focus to CV
root.mainloop()
Copy the code