【问题标题】:How do I make a turtle disapear if touched by another turtle?如果一只乌龟碰到另一只乌龟,我如何让它消失?
【发布时间】:2022-11-19 17:38:17
【问题描述】:

我和我的伙伴正在用 Python 制作一个类似僵尸的射击游戏,我们几乎已经完成了基本的游戏玩法,除了一个问题,我们无法找到一种方法让一只乌龟在被另一只乌龟触碰后消失。我们有 3 只乌龟,一只用于我们的玩家模型,一只用于子弹,一只用于僵尸,我们正在努力做到这一点,以便当子弹龟接触到僵尸乌龟或在僵尸乌龟的近距离区域内时,僵尸乌龟消失或至少移动位置。无论我们尝试了什么都没有用,如果有人可以提供帮助,我们将不胜感激。

import turtle as trtl

wn = trtl.Screen()

p= trtl.Turtle()
g= trtl.Turtle()
z= trtl.Turtle()
b = trtl.Turtle()


counter=trtl.Turtle()
font_setup = ("Arial", 20, "normal")
p.penup()

b.penup()
b.hideturtle()

pSpeed = 30
bSpeed = 30
trtl.register_shape("appleleft.gif")

trtl.register_shape("appleright.gif")

trtl.register_shape("mario.gif")

trtl.register_shape("mario2.gif")

trtl.register_shape("bullet.gif")

trtl.register_shape("bulletleft.gif")

trtl.register_shape("bosszombie.gif")

p.shape("mario.gif")

b.shape("bullet.gif")

z.shape("bosszombie.gif")

z.goto(200,0)

zx = z.xcor()
zy = z.ycor()

bx = b.xcor()
by = b.ycor()


wn.bgpic("mars.gif")


def shoot():
  b.goto(p.position())
  b.showturtle()
  b.forward(400)
  b.hideturtle()
  b.goto(p.position())

if b.xcor() == z.xcor():
  z.clear()

  
def move_left():
  x = p.xcor() - pSpeed
  if x < -280:
    x= -280
  p.setx(x)
  p.shape("mario2.gif")
  b.shape("bulletleft.gif")
  b.setheading(180)



def move_up():
  y = p.ycor() + pSpeed
  if y > 280:
    y=280
  p.sety(y)

def move_down():
  y = p.ycor() - pSpeed
  if y < -280:
    y= -280
  p.sety(y)

def move_right():
  x = p.xcor() + pSpeed
  if x > 280:
    x=280
  p.setx(x)
  p.shape("mario.gif")
  b.shape("bullet.gif")
  b.setheading(0)

wn.onkeypress(move_left, "a")
wn.onkeypress(move_up, "w")
wn.onkeypress(move_down, "s")
wn.onkeypress(move_right, "d")
wn.onkeypress(shoot, "l")


wn.listen()


wn.mainloop()

当子弹龟碰到它时,我们为僵尸龟发出了明确的命令,但它不起作用,我们甚至试图让它去随机位置,但没有任何效果。

【问题讨论】:

    标签: python


    【解决方案1】:

    该代码不起作用,因为检测乌龟是否接触到乌龟的代码不能只使用它自己的位置,因为例如,如果子弹每帧移动 10 个像素,那么当它到达需要击中僵尸的点时,僵尸 x 是5 并且子弹 x 在下一场比赛中为 0,它将变为 10,因此它不会碰到 5。要编写代码来检测它们是否真的相互接触,您应该创建一个碰撞框,此代码有点高水平(还是初学者水平) 例如,让我们制作一个正方形和圆形,正方形移动并且游戏必须检测它们何时触摸并停止正方形:

    import turtle as t
    wn = t.Screen()
    wn.setup(0.7, 0.7)
    
    s = t.Turtle()
    s.pu() # you can use it instead of pen up
    s.shape('square')
    s.shapesize(3, 3)
    s.setx(-100)
    
    c = t.Turtle()
    c.pu()
    c.shape('circle')
    c.shapesize(2, 2)
    c.setx(100)
    
    # now create the function that detects if they touch each other:
    def StouchingC():
        if s.xcor() + 3 * 20.1 / 2 > c.xcor() - 2 * 20.1 / 2: # thats because the scale between turtle size to one pixel is 20.1 means that one turtle size = 20.1 pixels
            return (True)
    
    while not StouchingC():
        s.setx(s.xcor() + 5)
        wn.update()
    
    print('You see!')
    wn.mainloop()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-09
      • 1970-01-01
      相关资源
      最近更新 更多