简体   繁体   中英

How do I make a turtle disapear if touched by another turtle?

Me and my buddy are making a sorta zombie shooting game on Python, we've gotten almost the basic gameplay done except with one issue, we can't find a way to make one turtle disappear after being touched by a different turtle. We have 3 turtles, one for our player model, one for a bullet, and one for a zombie, we're trying to make it so when the bullet turtle touches or is within a close area of the zombie turtle the zombie turtle disappears or at the very least moves location. No matter what we've tried nothing works, if anybody can help it would be greatly appreciated.

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()

We put a clear command for the zombie turtle when the bullet turtle touches it but it doesn't work, we even tried to make it go to random locations and nothing is working.

That code is not working because the code that detects if a turtle touches turtle cannot only use it's own position, because for example if every frame the bullet moves 10 pixels so when it is at the point it needs to hit the zombie the zombie x is 5 and the bullet x is 0 at the next game it is going to 10, so it will not touch the 5. to make a code that detects if they really touches each other you should create a hitbox, this code is a little bit higher level (still beginners level) for example lets make a square and circle the square moves and the game has to detect when they are touching and stop the square:

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM