Tron 游戏碰撞 - 乌龟 Python
Tron Game Collision - Turtle Python
我正在尝试制作一个本地 2 玩家的 Tron 游戏。我的代码在下面,但它是一个未完成的产品。我想知道我如何才能做到,如果形状敌人和形状玩家相互接触或接触他们创造的线条,就会产生结果。就像打印 Game Over 和更改背景一样。
我在下面的评论中不断收到错误。
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()
乌龟 "position" 方法在这种情况下非常有用:http://interactivepython.org/runestone/static/IntroPythonTurtles/Summary/summary.html
具体来说,你可以这样做
if player.position() == enemy.position():
print "Game Over"
return
对于线发生碰撞的场景,需要将两个玩家走过的路径存储在列表或数组中,然后在类似上面的if语句中判断玩家的移动是否碰上了一个这些空间。
您可以通过创建二维数组或列表来存储路径。如果玩家向同一方向移动,则将玩家已移动的距离范围列表附加到适当的轴。如果玩家向新方向移动,则附加一个新列表,其中包含沿轴移动的距离范围。
playerPath = [[[100],[100]]] #[[[X], [Y]]]
enemyPath = [[[600],[600]]]
previousMove = "na"
if up():
if previousMove != "up":
#Check for other methods
if right():
playerPath[0][0].append(playerPath[0][0][0] + 90)
playerPath.insert(0, (playerPath[0][0][0], playerPath[0][0]
[1])
#repeat for other directions
if previousMove == "up":
playerPath[0][0].append(playerPath[0][0][0])
playerPath[0][1].append(playerPath[0][1][0] + 30)
previousMove = "up"
...
检查是否发生碰撞时,遍历子路径,检查player.position X 和Y 值是否在已经遍历的X 和Y 路径中。
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"
return
您的代码不可能按给定的那样工作——例如每次击键代码的巨大块位于顶层,它只会 运行 一次。以下是您的代码的完整返工。
你需要测试你是否正在越过敌人或你自己生成的任何线。此代码跟踪段并在每次移动时对其进行测试,完全消除意外踩到一条线的失败者:
from turtle import Turtle, Screen
screen = Screen()
screen.bgcolor('black')
def up(who):
global previousMove
turtle, path = players[who]
turtle.setheading(90)
if previousMove != 'up':
path.append(turtle.position())
previousMove = 'up'
turtle.fd(15)
if checkCollision(turtle.position(), path, players[1 - who][PATH]):
collision(turtle)
def right(who):
global previousMove
turtle, path = players[who]
turtle.setheading(0)
if previousMove != 'right':
path.append(turtle.position())
previousMove = 'right'
turtle.fd(15)
if checkCollision(turtle.position(), path, players[1 - who][PATH]):
collision(turtle)
def left(who):
global previousMove
turtle, path = players[who]
turtle.setheading(180)
if previousMove != 'left':
path.append(turtle.position())
previousMove = 'left'
turtle.fd(15)
if checkCollision(turtle.position(), path, players[1 - who][PATH]):
collision(turtle)
def down(who):
global previousMove
turtle, path = players[who]
turtle.setheading(270)
if previousMove != 'down':
path.append(turtle.position())
previousMove = 'down'
turtle.fd(15)
if checkCollision(turtle.position(), path, players[1 - who][PATH]):
collision(turtle)
def collision(turtle):
for key in ('Up', 'Left', 'Right', 'Down', 'w', 'a', 'd', 'x'):
screen.onkey(None, key) # disable game
turtle.clear() # remove the loser from the board!
def checkCollision(position, path1, path2):
if len(path1) > 1:
A, B = position, path1[-1] # only check most recent line segment
if len(path1) > 3: # check for self intersection
for i in range(len(path1) - 3):
C, D = path1[i:i + 2]
if intersect(A, B, C, D):
return True
if len(path2) > 1: # check for intersection with other turtle's path
for i in range(len(path2) - 1):
C, D = path2[i:i + 2]
if intersect(A, B, C, D):
return True
return False
X, Y = 0, 1
def ccw(A, B, C):
""" """
return (C[Y] - A[Y]) * (B[X] - A[X]) > (B[Y] - A[Y]) * (C[X] - A[X])
def intersect(A, B, C, D):
""" Return true if line segments AB and CD intersect """
return ccw(A, C, D) != ccw(B, C, D) and ccw(A, B, C) != ccw(A, B, D)
player = Turtle('circle')
player.shapesize(6 / 20)
player.color('red')
player.pensize(6)
player.speed('fastest')
player.penup()
player.setposition(100, 100)
player.pendown()
enemy = Turtle('circle')
enemy.shapesize(6 / 20)
enemy.color('blue')
enemy.pensize(6)
enemy.speed('fastest')
enemy.penup()
enemy.setposition(-300, -300)
enemy.pendown()
players = [[player, [player.position()]], [enemy, [enemy.position()]]]
PLAYER, ENEMY = 0, 1
TURTLE, PATH = 0, 1
previousMove = None # consolidate moves in same direction into single line segment
screen.onkey(lambda: up(PLAYER), 'Up')
screen.onkey(lambda: left(PLAYER), 'Left')
screen.onkey(lambda: right(PLAYER), 'Right')
screen.onkey(lambda: down(PLAYER), 'Down')
screen.onkey(lambda: up(ENEMY), 'w')
screen.onkey(lambda: left(ENEMY), 'a')
screen.onkey(lambda: right(ENEMY), 'd')
screen.onkey(lambda: down(ENEMY), 'x')
screen.listen()
screen.mainloop()
线段交叉代码取自How can I check if two segments intersect?
以上代码不完整且存在错误 -- 需要额外的工作才能成为完整的游戏。但它基本上可以让您尝试想法:
我正在尝试制作一个本地 2 玩家的 Tron 游戏。我的代码在下面,但它是一个未完成的产品。我想知道我如何才能做到,如果形状敌人和形状玩家相互接触或接触他们创造的线条,就会产生结果。就像打印 Game Over 和更改背景一样。 我在下面的评论中不断收到错误。
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()
乌龟 "position" 方法在这种情况下非常有用:http://interactivepython.org/runestone/static/IntroPythonTurtles/Summary/summary.html
具体来说,你可以这样做
if player.position() == enemy.position():
print "Game Over"
return
对于线发生碰撞的场景,需要将两个玩家走过的路径存储在列表或数组中,然后在类似上面的if语句中判断玩家的移动是否碰上了一个这些空间。
您可以通过创建二维数组或列表来存储路径。如果玩家向同一方向移动,则将玩家已移动的距离范围列表附加到适当的轴。如果玩家向新方向移动,则附加一个新列表,其中包含沿轴移动的距离范围。
playerPath = [[[100],[100]]] #[[[X], [Y]]]
enemyPath = [[[600],[600]]]
previousMove = "na"
if up():
if previousMove != "up":
#Check for other methods
if right():
playerPath[0][0].append(playerPath[0][0][0] + 90)
playerPath.insert(0, (playerPath[0][0][0], playerPath[0][0]
[1])
#repeat for other directions
if previousMove == "up":
playerPath[0][0].append(playerPath[0][0][0])
playerPath[0][1].append(playerPath[0][1][0] + 30)
previousMove = "up"
...
检查是否发生碰撞时,遍历子路径,检查player.position X 和Y 值是否在已经遍历的X 和Y 路径中。
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"
return
您的代码不可能按给定的那样工作——例如每次击键代码的巨大块位于顶层,它只会 运行 一次。以下是您的代码的完整返工。
你需要测试你是否正在越过敌人或你自己生成的任何线。此代码跟踪段并在每次移动时对其进行测试,完全消除意外踩到一条线的失败者:
from turtle import Turtle, Screen
screen = Screen()
screen.bgcolor('black')
def up(who):
global previousMove
turtle, path = players[who]
turtle.setheading(90)
if previousMove != 'up':
path.append(turtle.position())
previousMove = 'up'
turtle.fd(15)
if checkCollision(turtle.position(), path, players[1 - who][PATH]):
collision(turtle)
def right(who):
global previousMove
turtle, path = players[who]
turtle.setheading(0)
if previousMove != 'right':
path.append(turtle.position())
previousMove = 'right'
turtle.fd(15)
if checkCollision(turtle.position(), path, players[1 - who][PATH]):
collision(turtle)
def left(who):
global previousMove
turtle, path = players[who]
turtle.setheading(180)
if previousMove != 'left':
path.append(turtle.position())
previousMove = 'left'
turtle.fd(15)
if checkCollision(turtle.position(), path, players[1 - who][PATH]):
collision(turtle)
def down(who):
global previousMove
turtle, path = players[who]
turtle.setheading(270)
if previousMove != 'down':
path.append(turtle.position())
previousMove = 'down'
turtle.fd(15)
if checkCollision(turtle.position(), path, players[1 - who][PATH]):
collision(turtle)
def collision(turtle):
for key in ('Up', 'Left', 'Right', 'Down', 'w', 'a', 'd', 'x'):
screen.onkey(None, key) # disable game
turtle.clear() # remove the loser from the board!
def checkCollision(position, path1, path2):
if len(path1) > 1:
A, B = position, path1[-1] # only check most recent line segment
if len(path1) > 3: # check for self intersection
for i in range(len(path1) - 3):
C, D = path1[i:i + 2]
if intersect(A, B, C, D):
return True
if len(path2) > 1: # check for intersection with other turtle's path
for i in range(len(path2) - 1):
C, D = path2[i:i + 2]
if intersect(A, B, C, D):
return True
return False
X, Y = 0, 1
def ccw(A, B, C):
""" """
return (C[Y] - A[Y]) * (B[X] - A[X]) > (B[Y] - A[Y]) * (C[X] - A[X])
def intersect(A, B, C, D):
""" Return true if line segments AB and CD intersect """
return ccw(A, C, D) != ccw(B, C, D) and ccw(A, B, C) != ccw(A, B, D)
player = Turtle('circle')
player.shapesize(6 / 20)
player.color('red')
player.pensize(6)
player.speed('fastest')
player.penup()
player.setposition(100, 100)
player.pendown()
enemy = Turtle('circle')
enemy.shapesize(6 / 20)
enemy.color('blue')
enemy.pensize(6)
enemy.speed('fastest')
enemy.penup()
enemy.setposition(-300, -300)
enemy.pendown()
players = [[player, [player.position()]], [enemy, [enemy.position()]]]
PLAYER, ENEMY = 0, 1
TURTLE, PATH = 0, 1
previousMove = None # consolidate moves in same direction into single line segment
screen.onkey(lambda: up(PLAYER), 'Up')
screen.onkey(lambda: left(PLAYER), 'Left')
screen.onkey(lambda: right(PLAYER), 'Right')
screen.onkey(lambda: down(PLAYER), 'Down')
screen.onkey(lambda: up(ENEMY), 'w')
screen.onkey(lambda: left(ENEMY), 'a')
screen.onkey(lambda: right(ENEMY), 'd')
screen.onkey(lambda: down(ENEMY), 'x')
screen.listen()
screen.mainloop()
线段交叉代码取自How can I check if two segments intersect?
以上代码不完整且存在错误 -- 需要额外的工作才能成为完整的游戏。但它基本上可以让您尝试想法: