鱼C论坛

 找回密码
 立即注册
查看: 1986|回复: 2

[原创] 推箱子

[复制链接]
发表于 2020-12-26 03:11:24 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x

  1. from typing import List
  2. import os

  3. def SearchElement(Map):
  4.     Row = 0;Column = 0
  5.     Wall = [];Box = [];Player = [];Endplace = []
  6.     for i in Map:
  7.         for j in i:
  8.             if (j.upper() == "W"):
  9.                 Wall.append((Row,Column))
  10.             elif (j.upper() == "B"):
  11.                 Box.append((Row,Column))
  12.             elif (j.upper() == "P"):
  13.                 Player.append(Row)
  14.                 Player.append(Column)
  15.             elif (j.upper() == "E"):
  16.                 Endplace.append((Row,Column))
  17.             Column += 1
  18.         Row += 1;Column = 0
  19.     return Wall,Box,Player,Endplace

  20. def PrintMap(Map):
  21.     for i in Map:
  22.         for j in i:
  23.             print(j,end='')
  24.         print()

  25. def PushBox(Player,Oprate,Element):
  26.     Box = Element[1];Wall = Element[0]
  27.     if (Oprate == "W"):
  28.         if (Player[0]-1,Player[1]) not in Box and (Player[0]-1,Player[1]) not in Wall:
  29.             Box[Box.index(Player)] = (Player[0]-1,Player[1])
  30.             return True,Box
  31.         else:
  32.             return False,Box
  33.     elif (Oprate == "S"):
  34.         if (Player[0]+1,Player[1]) not in Box and (Player[0]+1,Player[1]) not in Wall:
  35.             Box[Box.index(Player)] = (Player[0]+1,Player[1])
  36.             return True,Box
  37.         else:
  38.             return False,Box
  39.     elif (Oprate == "A"):
  40.         if (Player[0],Player[1]-1) not in Box and (Player[0],Player[1]-1) not in Wall:
  41.             Box[Box.index(Player)] = (Player[0],Player[1]-1)
  42.             return True,Box
  43.         else:
  44.             return False,Box
  45.     elif (Oprate == "D"):
  46.         if (Player[0],Player[1]+1) not in Box and (Player[0],Player[1]+1) not in Wall:
  47.             Box[Box.index(Player)] = (Player[0],Player[1]+1)
  48.             return True,Box
  49.         else:
  50.             return False,Box

  51. def PlayerMove(Player,Oprate,Element):
  52.     Wall = Element[0];Box = Element[1]
  53.     if (Player in Wall):
  54.         return False,Box
  55.     elif (Player in Box):
  56.         PushBoxResult = PushBox(Player,Oprate,Element)
  57.         if PushBoxResult[0]:
  58.             Box = PushBoxResult[1]
  59.             return True,Box
  60.         else:
  61.             return False,Box
  62.     else:
  63.         return True,Box

  64. def main():
  65.     # W is Wall/B is Box/P is Player/E is Endplace
  66.     Map = [["W","W","W","W","W","W","W","W","W","W","W"],
  67.            ["W"," "," "," "," ","W"," "," "," "," ","W"],
  68.            ["W"," ","W"," "," ","B"," ","B"," "," ","W"],
  69.            ["W","E"," "," "," ","W"," ","W"," "," ","W"],
  70.            ["W","E"," "," "," "," "," ","W"," ","P","W"],
  71.            ["W","W","W","W","W","W","W","W","W","W","W"]]
  72.     Element = list(SearchElement(Map))#Wall is Element[0]/Box is Element[1]/Endplace is Element[3]
  73.     Player = Element[2]
  74.     PrintMap(Map)
  75.     Oprate = input("How do you want to move:").upper()
  76.     while(True):
  77.         Map[Player[0]][Player[1]] = " "
  78.         if (Oprate == "W"):
  79.             MoveAndBox = PlayerMove(Player=(Player[0]-1,Player[1]),Oprate=Oprate,Element=Element)
  80.             if MoveAndBox[0]:
  81.                 Player[0] -= 1
  82.                 Element[1] = MoveAndBox[1]
  83.         elif (Oprate == "S"):
  84.             MoveAndBox = PlayerMove(Player=(Player[0]+1,Player[1]),Oprate=Oprate,Element=Element)
  85.             if MoveAndBox[0]:
  86.                 Player[0] += 1
  87.                 Element[1] = MoveAndBox[1]
  88.         elif (Oprate == "A"):
  89.             MoveAndBox = PlayerMove(Player=(Player[0],Player[1]-1),Oprate=Oprate,Element=Element)
  90.             if MoveAndBox[0]:
  91.                 Player[1] -= 1
  92.                 Element[1] = MoveAndBox[1]
  93.         elif (Oprate == "D"):
  94.             MoveAndBox = PlayerMove(Player=(Player[0],Player[1]+1),Oprate=Oprate,Element=Element)
  95.             if MoveAndBox[0]:
  96.                 Player[1] += 1
  97.                 Element[1] = MoveAndBox[1]
  98.         elif (Oprate == "B"):
  99.             main()
  100.         else:
  101.             print("Error Input.")
  102.         for i in Element[3]:
  103.             Map[i[0]][i[1]] = "E"
  104.         for i in Element[1]:
  105.             Map[i[0]][i[1]] = "B"
  106.         Map[Player[0]][Player[1]] = "P"
  107.         os.system('cls')
  108.         PrintMap(Map)
  109.         if sorted(Element[1]) == sorted(Element[3]):
  110.             if (input("Do you want to play it again? Y/n").upper() == "Y"):
  111.                 main()
  112.             else:
  113.                 break
  114.         else:
  115.             Oprate = input("How do you want to move:").upper()

  116. if __name__ == "__main__":
  117.     main()

复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-12-26 17:31:25 | 显示全部楼层
能说明一下咋玩吗?
我有点没看懂
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-12-27 19:58:14 | 显示全部楼层
你这个角色(P)退箱子(B)的时候会卡住,麻烦楼主改一下!!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-17 11:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表