"""
迷宮房間類遊戲,這裡提供一個雛形,方便學習。
"""
from sprites import *
def keyscheck():
if akey.down():
while akey.down():screen.update() # 等待鬆開a鍵
x = r.xcor() - tile_width
y = r.ycor()
if cors[(x,y)] == "0" : r.setx(x)
elif dkey.down():
while dkey.down():screen.update() # 等待鬆開d鍵
x = r.xcor() + tile_width
y = r.ycor()
if cors[(x,y)] == "0" : r.setx(x)
elif wkey.down():
while wkey.down():screen.update()
x = r.xcor()
y = r.ycor() + tile_height
if cors[(x,y)] == "0" : r.sety(y)
elif skey.down():
while skey.down():screen.update()
x = r.xcor()
y = r.ycor() - tile_height
if cors[(x,y)] == "0" : r.sety(y)
maze1 = ["1111111111",
"1000000001",
"1001110101",
"1000010101",
"1000010001",
"1001001001",
"1000100001",
"1000000001",
"1011100111",
"1111111111"]
tile_width = tile_height = 80 # 磚塊寬度和高度
rows = len(maze1[0]) # 行的數量
cols = len(maze1) # 列的數量
maze_height = cols * tile_height # 總共的寬度
maze_width = rows * tile_width # 總共的高度
screen = Screen() # 新建螢幕
screen.setup(maze_width,maze_height)
screen.title('迷宮房間類遊戲,請按awsd鍵')
# 左上角起始鋪磚點
startx = -maze_width//2 + tile_width//2
starty = maze_height//2 - tile_height//2
cors = {} # 記錄每個鋪磚點是否有磚
t = Sprite(shape='tile.png')
for i in range(rows):
for j in range(cols):
flag = maze1[i][j]
x = startx + j * tile_width
y = starty - i * tile_height
# 記錄中心點處是否有磚
cors[(x,y)] = flag
if int(flag):
t.goto(x,y)
t.stamp()
# 下面是新建一個人,它只能在沒有磚的地方移動
r = Sprite(shape='ren.png',visible=False)
# 下面是找個沒有磚塊的地方出現
while True:
x,y = random.choice(list(cors.keys()))
if cors[(x,y)] == '0':
r.goto(x,y)
r.show()
break
akey = Key('a')
dkey = Key('d')
wkey = Key('w')
skey = Key('s')
screen.listen()
while 1:
keyscheck()
screen.update()
本作品採用《CC 協議》,轉載必須註明作者和本文連結