- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试制作一款本地 2 人玩家的 tron 游戏。我的代码如下,但它是一个未完成的产品。我想知道如何做到这一点,以便如果形状敌人和形状玩家相互接触或接触他们创建的线,就会产生结果。就像打印游戏结束和更改背景一样。我不断收到下面评论中写的错误。
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()
最佳答案
你的代码不可能按照给定的方式工作——例如每个击键代码的巨大块位于顶层,它只会运行一次。下面是对您的代码的完整修改。
你需要测试你是否跨越了敌人或你自己划定的任何界限。此代码会跟踪这些段并在每次移动时对其进行测试,从而完全消除意外踩到线上的失败者:
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):
""" https://stackoverflow.com/a/9997374/5771269 """
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?
上面的代码不完整并且有错误——它需要额外的工作才能成为一个完成的游戏。但它基本上可以让你尝试想法:
关于python - Tron 游戏碰撞 - turtle python ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45093430/
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
如果 turtle 在一组坐标之上,我想让 turtle 回到地板上: 像这样: floor = -323 if turtle above floor: turtle.goto(floor)
我正在用Python语言编写一个小的文本库游戏。在使用Turtle函数数字输入和文本输入时,会出现一个文本字段,要求用户输入。当文本输入字段出现时,您可以开始输入,而不需要在输入字段中单击,但对于数字
我试图让 turtle 从程序开始就隐藏起来,但即使在放置 t.hideturtle() 之后也是如此。在我将 turtle 声明为变量的正下方, turtle 似乎仍然出现在绘图的中间。 impor
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我将 turtle 设置为最快,当我单独运行第一个循环时,效果很好,但随着我添加更多,它变得与仅单独执行第一个循环时相当。我不知道这是否只是因为绘图的复杂性,但完成形状需要相当长的时间。我可以做些什么
如何在不显示绘图过程的情况下显示最终绘图?我使用的是Python 3.4,这个项目是创建一个射箭游戏。例如,如果我使用以下代码: import turtle screen = turtle.Scree
Python 2.7 版本中的 turtle 和 Turtle 有何不同? import turtle star = turtle.Turtle() for i in range(50): s
如何告诉 turtle 面向 turtle 图形中的方向?我希望 turtle 能够转动并面向一个方向,无论其原始位置如何,我怎样才能实现这一目标? 最佳答案 我认为 turtle.setheadin
如何让 4 只不同的 turtle 同时移动?另外,如何为 Turtle.shape 方法制作人形?我知道有一个名为 register_shape 的 Screen 方法,但我找不到关于它的任何文档。
我设置了热键并且能够移动 turtle ,但是当我运行代码时,如果我超过 x 和 y 值,则不会发生任何事情..也没有错误。怎么了? if (Alex.xcor()>50 or Alex.xcor()
方向: 我创建了以下函数以允许用户将 turtle 更改为他/她选择的图像,然后随时将其标记到 Canvas 上: def TurtleShape(): try: # Tkin
我用黑色 turtle 创建了一个形状(白色矩形)而不是一条线!然后我移动屏幕底部的白色形状以创建一个必须从左向右移动的桨。我必须保持形状但删除黑色箭头。怎么办? from turtle import
我想创建 SpaceInvaders 游戏,但敌人不会被击落,而是会向玩家射击。我使用 .goto() 方法实现它,如下所示: bullet2.goto(player.xcor(),player.yc
出于教学目的,我需要一个图形默认值列表。这是我现在所拥有的: background white canvas 950W by 800H dot 5 (pixels) fil
这个问题在这里已经有了答案: Importing installed package from script with the same name raises "AttributeError: m
我目前正在上初级编程课,正在完成作业。现在,我必须用模块 turtle build 3 个房子(我完成了): def drawBody(mover): #Rectangle part
我有一些代码如下: # My code here turtle.bye() 在那之后,有什么办法可以重新打开 turtle 窗口。我知道您可以执行 turtle.clearscreen() 但这不会关
这段代码设置了一只 turtle 放置的邮票背景。另一只 turtle (其形状来自导入的图像文件)在背景上移动。但是,只要第二只 turtle 位于第一只 turtle 放置的图章上方,它就不可见。
我的程序中有两只 turtle 。它们碰撞在一起时发生了动画,但我希望一只 turtle 位于另一只 turtle 之上,如下所示: 所以,我的问题是 - 我怎样才能实现这一点 - 是否有一行简单的代
我是一名优秀的程序员,十分优秀!