我想做一个特隆游戏,它是一个本地2玩家。我的代码在下面,但它是一个未完成的产品。我想知道我怎样才能做到,这样,如果形状敌人和形状玩家彼此接触,或者他们创造的线,就会有结果。就像打印游戏和改变背景。 我一直把错误写在下面的评论里。在
def up():
player.fd(15)
def right():
player.fd(15)
def left():
player.fd(15)
def down():
player.fd(15)
playerPath = [[[100],[100]]]
enemyPath = [[[600],[600]]]
previousMove = "na"
#I keep getting this error: TclError: bad event type or keysym "up"
if previousMove != "up":
#Check for other methods
if previousMove == right():
playerPath.append((playerPath[0][0][0] + 90, playerPath[0][0][1]))
if previousMove == left():
playerPath.append((playerPath[0][0][0] - 90, playerPath[0][0][1]))
if previousMove == down():
playerPath.append((playerPath[0][0][0] + 180, playerPath[0][0][1]))
#repeat for other directions
if previousMove == "up":
playerPath[0][0].append(playerPath[0][0][0] + 30)
playerPath[0][1].append(playerPath[0][1][0] + 30)
previousMove = "up"
if previousMove != "right":
#Check for other methods
if previousMove == up():
playerPath.append((playerPath[0][0][0] - 90, playerPath[0][0][1]))
if previousMove == left():
playerPath.append((playerPath[0][0][0] + 180, playerPath[0][0][1]))
if previousMove ==down():
playerPath.append((playerPath[0][0][0] + 90, playerPath[0][0][1]))
#repeat for other directions
if previousMove == "right":
playerPath[0][0].append(playerPath[0][0][0] + 30)
playerPath[0][1].append(playerPath[0][1][0] + 30)
previousMove = "right"
if previousMove != "left":
#Check for other methods
if previousMove == up():
playerPath.append((playerPath[0][0][0] + 90, playerPath[0][0][1]))
if previousMove == right():
playerPath.append((playerPath[0][0][0] + 180, playerPath[0][0][1]))
if previousMove == down():
playerPath.append((playerPath[0][0][0] - 90, playerPath[0][0][1]))
#repeat for other directions
if previousMove == "left":
playerPath[0][0].append(playerPath[0][0][0] + 30)
playerPath[0][1].append(playerPath[0][1][0] + 30)
previousMove = "left"
if previousMove != "down":
#Check for other methods
if previousMove == up():
playerPath.append((playerPath[0][0][0] + 180, playerPath[0][0][1]))
if previousMove == left():
playerPath.append((playerPath[0][0][0] + 90, playerPath[0][0][1]))
if previousMove == right():
playerPath.append((playerPath[0][0][0] - 90, playerPath[0][0][1]))
#repeat for other directions
if previousMove == "down":
playerPath[0][0].append(playerPath[0][0][0] + 30)
playerPath[0][1].append(playerPath[0][1][0] + 30)
previousMove = "down"
#This code gives me this error: IndexError: list index out of range
#for subPath in enemyPath:
# if player.position()[0] in range(subPath[0][0], subPath[0][1]) and player.position()[1] in range(subPath[1][0], subPath[1][1]):
# print("Collision")
onkey(up, "up")
onkey(left, "left")
onkey(right, "right")
onkey(down, "down")
onkey(up1, "w")
onkey(left1, "a")
onkey(right1, "d")
listen()
mainloop()
turtle“position”方法在这个例子中非常有用:http://interactivepython.org/runestone/static/IntroPythonTurtles/Summary/summary.html
具体来说,你可以做到
对于与一条线发生冲突的场景,您需要将两个玩家的移动路径存储在一个列表或数组中,然后在上面类似的if语句中确定玩家的移动是否进入其中一个空间。在
可以通过创建二维数组或列表来存储路径。如果玩家在同一个方向上移动,你可以在相应的轴上附加一个玩家移动距离范围的列表。如果玩家向新方向移动,则附加一个新列表,其中包含沿轴移动的距离范围。在
^{pr2}$当检查是否发生了碰撞时,遍历子路径,检查玩家位置X和Y值在已经遍历的X和Y路径中。在
你的代码不可能像给定的那样工作,比如每次击键都有大量的代码块,如果它只运行一次,那么它就处于顶层。下面是您的代码的一个完整的重做。在
你需要测试你是否越过了敌人或你自己的防线。这段代码跟踪片段,并在每次移动时测试它们,完全消除了不小心踩到一条线的失败者:
线段交叉代码取自How can I check if two segments intersect?
以上代码是不完整的,它需要额外的工作,成为一个完整的游戏。但它基本上可以让你尝试一些想法:
相关问题 更多 >
编程相关推荐