10 益智游戏——推箱子游戏

#!/usr/bin/env python
# coding: utf-8

11.3 关键技术

1、前方的几种情况:

    ·通道
    ·围墙或出界
    ·目的地
    ·箱子,需要再次判断箱子前方的情况:
        ·墙或出界
        ·通道
        ·目的地
        ·通道(前一格是目的地的箱子)
        ·目的地(前一格式目的地的箱子)

2、列表复制——深复制



a = [1, 2]
b = a
b[1] = 7
print(a, b)


# 上面应该改为
a = [1, 2]
b = a[:]
b[1] = 7
print(a, b)
# 或者
import copy
b = copy.copy(a)
print(a, b)





# 但是对于列表里面有列表第一招不够用
a = [1, [1, 2]]
b =a[:]
print(b)
a[1].append(3)
print(a)
print(b)





# 所以必须用深复制函数deepcopy()
a = [1, [1, 2]]
b = copy.deepcopy(a)
print(b)
a[1].append(3)
print(a, b)

11.4 源代码

1、设计游戏地图

# In[16]:


# 原始地图(按列存储)
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],]
from tkinter import *


# In[ ]:


Wall = 0
Worker = 1
Box = 2
Passageway = 3
Destination = 4
WorkerInDest = 5
RedBox = 6
# root = Tk()
# cv = Canvas(root)

# 原始地图(按列存储)
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],]

imgs = [PhotoImage(file="10 推箱子\\Wall.gif"),
        PhotoImage(file="10 推箱子\\Worker.gif"),
        PhotoImage(file="10 推箱子\\Box.gif"),
        PhotoImage(file="10 推箱子\\Passageway.gif"),
        PhotoImage(file="10 推箱子\\Destination.gif"),
        PhotoImage(file="10 推箱子\\WorkerInDest.gif"),
        PhotoImage(file="10 推箱子\\RedBox.gif")]

2、绘制整个游戏区域图形

# In[25]:


def drawGameImage():
    global x, y
    for i in range(7):
        for j in range(7):
            if myArray[i][j] == Worker:
                x = i
                y = j
                print('工人当前位置:', x, y)
            img1 = imgs[myArray[i][j]]
            cv.create_image((i*70+35, j*70+35), image = img1)
            cv.pack()


# In[ ]:

3、按键事件处理

# In[19]:


# 用户按完键盘之后的更新!!!
def callback(event):
    global x, y, myArray
    
#     print('按下键:')
    print('按下键:', event.char)
    KeyCode = event.keysym
    
    if KeyCode == 'Up':
        x1 = x
        y1 = y-1
        x2 = x
        y2 = y-2
        MoveTo(x1, y1, x2, y2)
    elif KeyCode == 'Down':
        x1 = x
        y1 = y+1
        x2 = x
        y2 = y+2
        MoveTo(x1, y1, x2, y2)
    elif KeyCode == 'Left':
        x1 = x-1
        y1 = y
        x2 = x-2
        y2 = y
        MoveTo(x1, y1, x2, y2)
    elif KeyCode == 'Right':
        x1 = x+1
        y1 = y
        x2 = x+2
        y2 = y
        MoveTo(x1, y1, x2, y2)
    elif KeyCode == 'r':
        print('按下键:', event.char)
        myArray = copy.deepcopy(myArray1)
        drawGameImage()


# In[4]:


# 判断是否在游戏区域中
def IsInGameArea(row, col):
    return (row >= 0 and row < 7 and col >= 0 and col < 7)


# In[22]:


# 最复杂部分!!!!!
# 实现前面分析的所有规则和算法!!!
def MoveTo(x1, y1, x2, y2):
    global x, y
    P1 = None
    P2 = None
    if IsInGameArea(x1, y1):
        P1 = myArray[x1][y1]
    if IsInGameArea(x2, y2):
        P2 = myArray[x2][y2]
    if P1 == Passageway:
        MoveMan(x, y)
        x = x1
        y = y1
        myArray[x1][y1] = Worker
    if P1 == Destination:
        MoveMan(x, y)
        x = x1
        y = y1
        myArray[x1][y1] = WorkerInDest
    if P1 == Wall or not IsInGameArea(x1, y1):
        return
    if P1 == Box:
        if P2 == Wall or not IsInGameArea(x1, y1) or P2 == Box:
            return

        #     以下P1处为箱子
    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
        
    if P1 == RedBox and P2 == Passageway:
        MoveMan(x, y)
        x = x1
        y = y1
        myArray[x2][y2] = Box
        myArray[x1][y1] = WorkerInDest
        
    if P1 == RedBox and P2 == Destination:
        MoveMan(x, y)
        x = x1
        y = y1
        myArray[x2][y2] = RedBox
        myArray[x1][y1] = WorkerInDest
    drawGameImage()
    
#     验证是否过关?
    if IsFinish():
        showinfo(title = '提示', message = '恭喜你顺利通关!')
        drawGameImage()
        print('下一关!')
        


# In[6]:


# 移走位置为(x,y)的工人,修改格子状态值
def MoveMan(x, y):
    if myArray[x][y] == Worker:
        myArray[x][y] = Passageway
    elif myArray[x][y] == WorkerInDest:
        myArray[x][y] = Destination


# In[7]:


def IsFinish():
    bFinish = True
    for i in range(7):
        for j in range(7):
            if (myArray[i][j] == Destination
                   or myArray[i][j] == WorkerInDest):
                bFinish = False
    return bFinish


# In[ ]:

4、主程序


# In[26]:


import copy
from tkinter.messagebox import showinfo


root = Tk()
root.title('推箱子~李妍')
cv = Canvas(root, bg = 'green', width = 7 * 70, height = 7 * 70)


Wall = 0
Worker = 1
Box = 2
Passageway = 3
Destination = 4
WorkerInDest = 5
RedBox = 6
# root = Tk()
# cv = Canvas(root)

# 原始地图(按列存储)
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],]

imgs = [PhotoImage(file="10 推箱子\\Wall.gif"),
        PhotoImage(file="10 推箱子\\Worker.gif"),
        PhotoImage(file="10 推箱子\\Box.gif"),
        PhotoImage(file="10 推箱子\\Passageway.gif"),
        PhotoImage(file="10 推箱子\\Destination.gif"),
        PhotoImage(file="10 推箱子\\WorkerInDest.gif"),
        PhotoImage(file="10 推箱子\\RedBox.gif")]


# In[27]:




myArray = copy.deepcopy(myArray1)
drawGameImage()
cv.bind('<KeyPress>', callback)
cv.pack()
cv.focus_set() # 将焦点设置到cv上
root.mainloop()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值