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)
a = [ 1 , [ 1 , 2 ] ]
b = copy. deepcopy( a)
print ( b)
a[ 1 ] . append( 3 )
print ( a, b)
11.4 源代码
1、设计游戏地图
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 *
Wall = 0
Worker = 1
Box = 2
Passageway = 3
Destination = 4
WorkerInDest = 5
RedBox = 6
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、绘制整个游戏区域图形
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( )
3、按键事件处理
def callback ( event) :
global x, y, myArray
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( )
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) :
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
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 ( '下一关!' )
def MoveMan ( x, y) :
if myArray[ x] [ y] == Worker:
myArray[ x] [ y] = Passageway
elif myArray[ x] [ y] == WorkerInDest:
myArray[ x] [ y] = Destination
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
4、主程序
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
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" ) ]
myArray = copy. deepcopy( myArray1)
drawGameImage( )
cv. bind( '<KeyPress>' , callback)
cv. pack( )
cv. focus_set( )
root. mainloop( )