Tron与Turtle图形中生成的线相撞

发布于 2025-02-09 01:20:31 字数 4832 浏览 1 评论 0原文

因此,我正在做TRON游戏的最后一部分,这是为第二名玩家的Tron自行车撞到第一播放器Tron Bike时发生的碰撞。从那里开始,遇到线路的人输了,另一个人赢了。因此,我的问题是,我不确定如何确定如何编码,但是我有一个想法,我想知道是否有人可以提供帮助。

我在想使自己4个定义:

  • 1对于自行车1 1的X坐标
  • 1 1 1 1的y坐标
  • ,用于自行车2 1的X坐标,
  • 用于自行车2的y坐标

。一定要与所有坐标不断更新列表,有点像这样:

def RedX():
      RedXCordList = [red_player.xcord()]
      while True:
        RedXCordList.append(red_player.xcord())

我不确定这是否是首先工作的方式,但是这个想法是将坐标以某种方式将其纳入列表/数组。

将坐标进入那里后,我必须检查其中一辆自行车位置是否等于列表/数组中的任何坐标,如果有坐标,则会发生碰撞。如果不是,它将继续运行,直到某物发生碰撞为止。

另外,有人可以告诉我如何检测颜色,每当乌龟检测到红色或蓝色时,它将检测到碰撞,一个人会赢,一个人会输。

我几乎没有任何有关如何编码帮助的线索,这将不胜感激。

如果您想查看我的完整代码:

def TronGame():
  #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)

  # Helper function to print end of game message
  def game_over(message):
    TurtleCrash = turtle.Turtle(visible=False)
    TurtleCrash.color("white")
    style = ('Arial', 25, 'italic')
    TurtleCrash.write(f"{message}\nGame over!", font=style, align='center')

  def collisions():  
    x_blue, y_blue = blue_player.xcor(), blue_player.ycor() 
    x_red, y_red  = red_player.xcor(), red_player.ycor()
    message_turtle =  turtle.Turtle(visible=False)
    message_turtle.color("white")
    collision_text = ""
    if( math.isclose(x_blue, x_red, abs_tol=10.0) and
        math.isclose(y_blue, y_red, abs_tol=10.0)):
      collision_text = "Red and Blue Crashed!\nGame over!"
    if x_blue > width/2 or x_blue < -1*width/2:
      collision_text = "Blue went out of bounds./nRed wins!"
    if y_blue > height/2 or y_blue < -1*height/2:
      collision_text = "Blue went out of bounds./nRed wins!"
    if x_red > width/2 or x_red < -1*width/2:
      collision_text = "Red went out of bounds./nBlue wins!"
    if y_red > height/2 or y_red < -1*height/2:
      collision_text = "Red went out of bounds./nBlue wins!"
    if collision_text: 
        message_turtle.write(collision_text,font=('Arial', 25, 'italic'), align='center')
     
    return False

  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(5):
      my_turtle = turtle.Turtle(visible=False)
      my_turtle.color("white")
      style = ('Arial', 25, 'italic')
      my_turtle.write(5-x, font=style, align='center') 
      time.sleep(1)
      my_turtle.undo()
  
    screen.listen()
        
    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")

    def gameloop():
      move_player(red_player, red_direction)
      move_player(blue_player, blue_direction)
      # Check for a collision. End game if there is one
      if collisions(): 
        return
      #Repeat after 16ms (0.016s) (1000ms/16ms = 62.5 FPS)
      screen.ontimer(gameloop, 16)
    
    screen.ontimer(gameloop, 250)
  
    gameloop()
    screen.mainloop()

  MainTron()

TronGame()

So I'm doing the last part of my tron game which is to code the collision that happens when the 2nd players' tron bike hits the line of the 1st players tron bike. From there, whoever ran into the line loses, and the other one wins. So my issue is that I'm not 100% sure on how to code this, but I've got an idea and I'm wondering if anyone can help.

What I was thinking was to make myself 4 definitions:

  • 1 for the x coordinate of bike 1
  • 1 for the y coordinate of bike 1
  • 1 for the x coordinate of bike 2
  • 1 for the y coordinate of bike 2

In each of these definitions there would be a while loop constantly updating a list with all of the coordinates, kind of like this:

def RedX():
      RedXCordList = [red_player.xcord()]
      while True:
        RedXCordList.append(red_player.xcord())

I'm not sure if that's how that works in the first place, but the idea is to get the coordinates into a list/array somehow.

After getting the coordinates into there I'd have to check if one of the bikes positions was equal to any of the coordinates in the list/array, and if it was then there would be a collision. If not the it would continue running until something collided.

Alternatively, Someone can tell me how to detect color and whenever turtle detects the color red or blue it will detect a collision and one will win and one will lose.

I've got barely any clue on how to code that help would be greatly appreciated.

Here's my full code if you want to look at it:

def TronGame():
  #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)

  # Helper function to print end of game message
  def game_over(message):
    TurtleCrash = turtle.Turtle(visible=False)
    TurtleCrash.color("white")
    style = ('Arial', 25, 'italic')
    TurtleCrash.write(f"{message}\nGame over!", font=style, align='center')

  def collisions():  
    x_blue, y_blue = blue_player.xcor(), blue_player.ycor() 
    x_red, y_red  = red_player.xcor(), red_player.ycor()
    message_turtle =  turtle.Turtle(visible=False)
    message_turtle.color("white")
    collision_text = ""
    if( math.isclose(x_blue, x_red, abs_tol=10.0) and
        math.isclose(y_blue, y_red, abs_tol=10.0)):
      collision_text = "Red and Blue Crashed!\nGame over!"
    if x_blue > width/2 or x_blue < -1*width/2:
      collision_text = "Blue went out of bounds./nRed wins!"
    if y_blue > height/2 or y_blue < -1*height/2:
      collision_text = "Blue went out of bounds./nRed wins!"
    if x_red > width/2 or x_red < -1*width/2:
      collision_text = "Red went out of bounds./nBlue wins!"
    if y_red > height/2 or y_red < -1*height/2:
      collision_text = "Red went out of bounds./nBlue wins!"
    if collision_text: 
        message_turtle.write(collision_text,font=('Arial', 25, 'italic'), align='center')
     
    return False

  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(5):
      my_turtle = turtle.Turtle(visible=False)
      my_turtle.color("white")
      style = ('Arial', 25, 'italic')
      my_turtle.write(5-x, font=style, align='center') 
      time.sleep(1)
      my_turtle.undo()
  
    screen.listen()
        
    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")

    def gameloop():
      move_player(red_player, red_direction)
      move_player(blue_player, blue_direction)
      # Check for a collision. End game if there is one
      if collisions(): 
        return
      #Repeat after 16ms (0.016s) (1000ms/16ms = 62.5 FPS)
      screen.ontimer(gameloop, 16)
    
    screen.ontimer(gameloop, 250)
  
    gameloop()
    screen.mainloop()

  MainTron()

TronGame()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别想她 2025-02-16 01:20:32

您已经朝着正确的方向思考,但仍然缺少将代码和许多其他细节放在哪里。

有关最新更新的游戏的整个工作代码,请查看我对您的最初问题的答案(当前三个)的答案(首先是三个),请 https ://stackoverflow.com/a/72694075/7711283

在那里,您可以看到在哪里放置用于收集坐标的代码以及如何评估它们。

You are thinking already in the right direction, but still missing the point where to put the code and many other details.

For the entire working code of the game with newest updates check out my answer to your initial question (first in a series of currently three) at https://stackoverflow.com/a/72694075/7711283 .

There you can see where to put the code for collecting the coordinates and how to evaluate them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文