检测乌龟/乌龟和乌龟线之间的碰撞
因此,我正在用乌龟编码Tron游戏,而我需要做的最后一部分是在乌龟本身,碰撞的碰撞以及A Turtle和另一只乌龟制造的界线之间进行编码。我对束缚的碰撞进行了编码,但它们没有起作用,我还尝试对玩家进行编码,以防止玩家碰撞也没有起作用。我不知道如何做另一个(当乌龟与另一只乌龟产生的线相撞时,它会输出某些东西)。我想到了以某种方式使用数组,但我不确定这些数组如何工作。谁能帮我吗?
淘汰碰撞的代码:
#Width and height of the screen
width = turtle.window_width()
height = turtle.window_height()
def collisions():
while True:
if(math.isclose(blue_player.xcor(), red_player.xcor(), abs_tol=1e-10) and
math.isclose(blue_player.ycor(), red_player.ycor(), abs_tol=1e-10)):
TurtleCrash = turtle.Turtle(visible=False)
TurtleCrash.color("white")
style = ('Arial', 25, 'italic')
TurtleCrash.write("Red and Blue Crashed!\nGame over!", font=style, align='center')
break
if blue_player.xcor() > width or blue_player.xcor() < -1*width:
BlueTurtleOut = turtle.Turtle(visible=False)
BlueTurtleOut.color("white")
style = ('Arial', 25, 'italic')
BlueTurtleOut.write("Red went out of bounds./nBlue wins!", font=style, align='center')
break
if blue_player.ycor() > height or blue_player.ycor() < -1*height:
BlueTurtleOut2 = turtle.Turtle(visible=False)
BlueTurtleOut2.color("white")
style = ('Arial', 25, 'italic')
BlueTurtleOut2.write("Red went out of bounds./nBlue wins!", font=style, align='center')
break
if red_player.xcor() > width or red_player.xcor() < -1*width:
RedTurtleOut = turtle.Turtle(visible=False)
RedTurtleOut.color("white")
style = ('Arial', 25, 'italic')
RedTurtleOut.write("Red went out of bounds./nBlue wins!", font=style, align='center')
break
if red_player.ycor() > height or red_player.ycor() < -1*height:
RedTurtleOut2 = turtle.Turtle(visible=False)
RedTurtleOut2.color("white")
style = ('Arial', 25, 'italic')
RedTurtleOut2.write("Red went out of bounds./nBlue wins!", font=style, align='center')
break
gameScreen()
GamesCreen是我代码中早期调用的功能,但这不是问题,尽管它可能位于函数中的位置错误。我不知道。
没有人获胜的海龟之间(未遂)碰撞的代码。
collision = turtle.Turtle()
collision.hideturtle()
if blue_player.pos() == red_player.pos():
collision.goto(0,0)
collision.showturtle()
collision.write("You collided with each other!\nIt's a tie!")
这是完整的代码:
#Width and height of the screen
width = turtle.window_width()
height = turtle.window_height()
#Variables for tron game coded later on
red_direction = None
blue_direction = None
def TronGame():
#Border
#box = Turtle()
#box.ht()
#box.color('purple')
#box.speed('fastest')
#box.pensize(10)
# box.pu()
# box.goto(-1*height, -1*width)
# box.pd()
# for i in range(4):
# box.forward(height)
# box.left(90)
# box.forward(width)
# box.left(90)
#Blue Player movements
def blue_up():
global blue_direction
blue_direction = 'up'
def blue_down():
global blue_direction
blue_direction = 'down'
def blue_left():
global blue_direction
blue_direction = 'left'
def blue_right():
global blue_direction
blue_direction = 'right'
#Red player Movemnts
def red_up():
global red_direction
red_direction = 'up'
def red_down():
global red_direction
red_direction = 'down'
def red_left():
global red_direction
red_direction = 'left'
def red_right():
global red_direction
red_direction = 'right'
#Player movements
def move_player(player, direction):
if direction == 'up':
player.setheading(90)
player.forward(5)
elif direction == 'down':
player.setheading(270)
player.forward(5)
elif direction == 'left':
player.setheading(180)
player.forward(5)
elif direction == 'right':
player.setheading(0)
player.forward(5)
def gameloop():
move_player(red_player, red_direction)
move_player(blue_player, blue_direction)
#Repeat after 10ms (0.01s) (1000ms/10ms = 100 FPS)
screen.ontimer(gameloop, 10)
def collisions():
while True:
if(math.isclose(blue_player.xcor(), red_player.xcor(), abs_tol=1e-10) and
math.isclose(blue_player.ycor(), red_player.ycor(), abs_tol=1e-10)):
TurtleCrash = turtle.Turtle(visible=False)
TurtleCrash.color("white")
style = ('Arial', 25, 'italic')
TurtleCrash.write("Red and Blue Crashed!\nGame over!", font=style, align='center')
break
if blue_player.xcor() > width or blue_player.xcor() < -1*width:
BlueTurtleOut = turtle.Turtle(visible=False)
BlueTurtleOut.color("white")
style = ('Arial', 25, 'italic')
BlueTurtleOut.write("Red went out of bounds./nBlue wins!", font=style, align='center')
break
if blue_player.ycor() > height or blue_player.ycor() < -1*height:
BlueTurtleOut2 = turtle.Turtle(visible=False)
BlueTurtleOut2.color("white")
style = ('Arial', 25, 'italic')
BlueTurtleOut2.write("Red went out of bounds./nBlue wins!", font=style, align='center')
break
if red_player.xcor() > width or red_player.xcor() < -1*width:
RedTurtleOut = turtle.Turtle(visible=False)
RedTurtleOut.color("white")
style = ('Arial', 25, 'italic')
RedTurtleOut.write("Red went out of bounds./nBlue wins!", font=style, align='center')
break
if red_player.ycor() > height or red_player.ycor() < -1*height:
RedTurtleOut2 = turtle.Turtle(visible=False)
RedTurtleOut2.color("white")
style = ('Arial', 25, 'italic')
RedTurtleOut2.write("Red went out of bounds./nBlue wins!", font=style, align='center')
break
def MainTron():
global screen
global blue_player
global red_player
screen = turtle.Screen()
screen.setup(width, height)
screen.bgpic('TronBg.png')
screen.bgcolor('black')
screen.addshape('BlueBike.gif')
screen.addshape('RedBike.gif')
blue_player = turtle.Turtle()
blue_player.shape('BlueBike.gif')
blue_player.pencolor("blue")
blue_player.pensize(3)
blue_player.pu()
blue_player.goto(-1*(width)/3, height/8)
blue_player.pd()
red_player = turtle.Turtle()
red_player.shape('RedBike.gif')
red_player.pencolor("red")
red_player.pensize(3)
red_player.pu()
red_player.goto(width/3, height/8)
red_player.pd()
for x in range(10):
my_turtle = turtle.Turtle(visible=False)
my_turtle.color("white")
style = ('Arial', 25, 'italic')
my_turtle.write(10-x, font=style, align='center')
time.sleep(1)
my_turtle.undo()
screen.listen()
#collisions()
screen.onkey(red_up, "w")
screen.onkey(red_down, "s")
screen.onkey(red_left, "a")
screen.onkey(red_right, "d")
screen.onkey(blue_up, "Up")
screen.onkey(blue_down, "Down")
screen.onkey(blue_left, "Left")
screen.onkey(blue_right, "Right")
screen.ontimer(gameloop, 250)
#collision = turtle.Turtle()
#collision.hideturtle()
#if blue_player.pos() == red_player.pos():
# collision.goto(0,0)
# collision.showturtle()
# collision.write("You collided with each other!\nIt's a tie!")
screen.mainloop()
MainTron()
So I'm coding a tron game in turtle and the last part I need to do is to code the collisions between the turtles themselves, out of bound collisions, and collisions between the line made by the a turtle and the other turtle. I coded the out of bound collisions and they didn't work, and I also tried coding the player against player collisions which didn't work either. I've got no clue how to do the other one (where it outputs something when the turtle collides with the line generated by the other turtle). I thought of using an array in some way but I'm not sure how those work. Can anyone help me out?
Code for the out of bound collisions:
#Width and height of the screen
width = turtle.window_width()
height = turtle.window_height()
def collisions():
while True:
if(math.isclose(blue_player.xcor(), red_player.xcor(), abs_tol=1e-10) and
math.isclose(blue_player.ycor(), red_player.ycor(), abs_tol=1e-10)):
TurtleCrash = turtle.Turtle(visible=False)
TurtleCrash.color("white")
style = ('Arial', 25, 'italic')
TurtleCrash.write("Red and Blue Crashed!\nGame over!", font=style, align='center')
break
if blue_player.xcor() > width or blue_player.xcor() < -1*width:
BlueTurtleOut = turtle.Turtle(visible=False)
BlueTurtleOut.color("white")
style = ('Arial', 25, 'italic')
BlueTurtleOut.write("Red went out of bounds./nBlue wins!", font=style, align='center')
break
if blue_player.ycor() > height or blue_player.ycor() < -1*height:
BlueTurtleOut2 = turtle.Turtle(visible=False)
BlueTurtleOut2.color("white")
style = ('Arial', 25, 'italic')
BlueTurtleOut2.write("Red went out of bounds./nBlue wins!", font=style, align='center')
break
if red_player.xcor() > width or red_player.xcor() < -1*width:
RedTurtleOut = turtle.Turtle(visible=False)
RedTurtleOut.color("white")
style = ('Arial', 25, 'italic')
RedTurtleOut.write("Red went out of bounds./nBlue wins!", font=style, align='center')
break
if red_player.ycor() > height or red_player.ycor() < -1*height:
RedTurtleOut2 = turtle.Turtle(visible=False)
RedTurtleOut2.color("white")
style = ('Arial', 25, 'italic')
RedTurtleOut2.write("Red went out of bounds./nBlue wins!", font=style, align='center')
break
gameScreen()
gameScreen is a function called earlier in my code by the way, it's not the issue, although it could be in the wrong place in the function. I'm not sure.
Code for (attempted) collision between the turtles where nobody wins.
collision = turtle.Turtle()
collision.hideturtle()
if blue_player.pos() == red_player.pos():
collision.goto(0,0)
collision.showturtle()
collision.write("You collided with each other!\nIt's a tie!")
Here's the full code:
#Width and height of the screen
width = turtle.window_width()
height = turtle.window_height()
#Variables for tron game coded later on
red_direction = None
blue_direction = None
def TronGame():
#Border
#box = Turtle()
#box.ht()
#box.color('purple')
#box.speed('fastest')
#box.pensize(10)
# box.pu()
# box.goto(-1*height, -1*width)
# box.pd()
# for i in range(4):
# box.forward(height)
# box.left(90)
# box.forward(width)
# box.left(90)
#Blue Player movements
def blue_up():
global blue_direction
blue_direction = 'up'
def blue_down():
global blue_direction
blue_direction = 'down'
def blue_left():
global blue_direction
blue_direction = 'left'
def blue_right():
global blue_direction
blue_direction = 'right'
#Red player Movemnts
def red_up():
global red_direction
red_direction = 'up'
def red_down():
global red_direction
red_direction = 'down'
def red_left():
global red_direction
red_direction = 'left'
def red_right():
global red_direction
red_direction = 'right'
#Player movements
def move_player(player, direction):
if direction == 'up':
player.setheading(90)
player.forward(5)
elif direction == 'down':
player.setheading(270)
player.forward(5)
elif direction == 'left':
player.setheading(180)
player.forward(5)
elif direction == 'right':
player.setheading(0)
player.forward(5)
def gameloop():
move_player(red_player, red_direction)
move_player(blue_player, blue_direction)
#Repeat after 10ms (0.01s) (1000ms/10ms = 100 FPS)
screen.ontimer(gameloop, 10)
def collisions():
while True:
if(math.isclose(blue_player.xcor(), red_player.xcor(), abs_tol=1e-10) and
math.isclose(blue_player.ycor(), red_player.ycor(), abs_tol=1e-10)):
TurtleCrash = turtle.Turtle(visible=False)
TurtleCrash.color("white")
style = ('Arial', 25, 'italic')
TurtleCrash.write("Red and Blue Crashed!\nGame over!", font=style, align='center')
break
if blue_player.xcor() > width or blue_player.xcor() < -1*width:
BlueTurtleOut = turtle.Turtle(visible=False)
BlueTurtleOut.color("white")
style = ('Arial', 25, 'italic')
BlueTurtleOut.write("Red went out of bounds./nBlue wins!", font=style, align='center')
break
if blue_player.ycor() > height or blue_player.ycor() < -1*height:
BlueTurtleOut2 = turtle.Turtle(visible=False)
BlueTurtleOut2.color("white")
style = ('Arial', 25, 'italic')
BlueTurtleOut2.write("Red went out of bounds./nBlue wins!", font=style, align='center')
break
if red_player.xcor() > width or red_player.xcor() < -1*width:
RedTurtleOut = turtle.Turtle(visible=False)
RedTurtleOut.color("white")
style = ('Arial', 25, 'italic')
RedTurtleOut.write("Red went out of bounds./nBlue wins!", font=style, align='center')
break
if red_player.ycor() > height or red_player.ycor() < -1*height:
RedTurtleOut2 = turtle.Turtle(visible=False)
RedTurtleOut2.color("white")
style = ('Arial', 25, 'italic')
RedTurtleOut2.write("Red went out of bounds./nBlue wins!", font=style, align='center')
break
def MainTron():
global screen
global blue_player
global red_player
screen = turtle.Screen()
screen.setup(width, height)
screen.bgpic('TronBg.png')
screen.bgcolor('black')
screen.addshape('BlueBike.gif')
screen.addshape('RedBike.gif')
blue_player = turtle.Turtle()
blue_player.shape('BlueBike.gif')
blue_player.pencolor("blue")
blue_player.pensize(3)
blue_player.pu()
blue_player.goto(-1*(width)/3, height/8)
blue_player.pd()
red_player = turtle.Turtle()
red_player.shape('RedBike.gif')
red_player.pencolor("red")
red_player.pensize(3)
red_player.pu()
red_player.goto(width/3, height/8)
red_player.pd()
for x in range(10):
my_turtle = turtle.Turtle(visible=False)
my_turtle.color("white")
style = ('Arial', 25, 'italic')
my_turtle.write(10-x, font=style, align='center')
time.sleep(1)
my_turtle.undo()
screen.listen()
#collisions()
screen.onkey(red_up, "w")
screen.onkey(red_down, "s")
screen.onkey(red_left, "a")
screen.onkey(red_right, "d")
screen.onkey(blue_up, "Up")
screen.onkey(blue_down, "Down")
screen.onkey(blue_left, "Left")
screen.onkey(blue_right, "Right")
screen.ontimer(gameloop, 250)
#collision = turtle.Turtle()
#collision.hideturtle()
#if blue_player.pos() == red_player.pos():
# collision.goto(0,0)
# collision.showturtle()
# collision.write("You collided with each other!\nIt's a tie!")
screen.mainloop()
MainTron()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如约翰尼·莫普(Johnny Mopp)已经提到的那样,有很多改进代码的地方。在一个肯定的不完整列表下,仅描述了对原始代码进行的许多更改的一部分以及简短的说明以及对python功能的描述的变化如何改善这些更改:
bool(“”)是false
)。这允许将字符串用作指示min&lt ; x&lt;最大
而不是min&lt; x和x&lt; Max
我决定将其用于检测碰撞,但不是作为检查播放器是否在屏幕区域之外的检查(因为表达式max&lt; x&lt; min不起作用),但如果他在内部。如果玩家不再内部,则会发生碰撞。.distance()
功能来检测玩家海龟之间的碰撞,而不是评估其坐标。增加了通过按“ 碰撞检测部分中的代码通过另一种存储乌龟路径的克隆来替换乌龟的克隆,而另一种检测与路径下方的碰撞的方式
。代码正在运行:
As Johnny Mopp already mentioned, there are lot of places to improve the code. Below a for sure incomplete list describing only part of the many changes done to the original code along with a short explanation what do the changes improve along with description of Python features making these changes possible:
bool("") is False
). This allows to use the string as a flag indicating found collisionmin < x < max
instead ofmin < x and x < max
I decided to use it in detecting collisions, but not as a check if the player is outside of the screen area (because the expression max < x < min doesn't work) but if he is within. If the player is no more within, there is a collision..distance()
function for detecting collision between the player turtles instead of evaluation of their coordinates.What could be further done to this code in order to improve it even more? The most urgent improvement would be to speed up the code in the collision detection part by replacing cloning of the turtles by another way of storing the turtle path and another way of detecting collision with the path.
Below the background image the after the image following code will try to load from the same directory from which the code is running:
首先,您应该在
gameloop()
函数中调用collionis()
,以便每次碰撞玩家移动时检查碰撞。执行此操作后,将从函数中删除。另外,如果有碰撞以结束游戏,请返回true
。至于与一条线相交,我的最初建议是在乌龟移动到的位置上阅读像素,如果不是黑色,则会发生碰撞。不幸的是,据我所知,没有简单的方法可以阅读带有Turtle的像素。因此,您需要保留玩家访问的所有职位的列表并使用。或者也许使用了更先进的引擎,例如Pygame。
有很多改进代码的地方。如果您想获得一些反馈,请在
First, you should call
collisions()
in thegameloop()
function so it checks for a collision each time the players move. After doing this, remove thewhile
from the function. Also, have the function returnTrue
if there is a collision to end the game.As far as intersecting with a line, my initial suggestion would be to read the pixel at the position the turtle is moving to and if it is not black, then there is a collision. Unfortunately, as far as I can see, there's no easy way to read a pixel with Turtle. So you need to keep a list of all the positions a player has visited and use that. Or maybe used a more advanced engine like PyGame.
There are lots of places to improve the code. If you want to get some feedback, post the code over at https://codereview.stackexchange.com/.