我怎么能写一组生命的数量,并使一个特定的gif消失时,它发生在Python龟?

w46czmvw  于 2023-04-28  发布在  Python
关注(0)|答案(2)|浏览(84)

我做了一个简单的Python龟游戏,玩家必须收集屏幕上随机生成的物品,但每当它击中一个受限制的,我希望它失去一条生命。我为第一次生活做好了准备,但我不太确定如何让它为其他两次生活工作。这是我为第一个所做的:

# there's 3 lives in total

live3 = trtl.Turtle()
live3.penup()
window.addshape('live3.gif') # picture of  a heart representing one life
live3.shape('heart2.gif')
live3.goto(0, -50)

def find_collosion_with_restricted():
  global playersize
  between_distance = 5
  px, py = player.pos()
  rx, ry = restricted_point.pos() # coords of the item the player can't touch
  distance = math.sqrt((px - rx)**2 + (py - ry)**2) # finds distance between to see if they touch
  if distance < between_distance:
    live3.hideturtle()

我怎样才能使它这样,如果它击中另一个限制值,第二个心脏重生,然后为第三个生活以及。

3phpmpom

3phpmpom1#

您可以:

lives = (life1, life2, life3)
n_of_lives = len(lives)

然后:

if collided_with_restricted:
    n_of_lives -= 1
    lives[n_of_lives].hideturtle()
jobtbby3

jobtbby32#

# there are 3 lives in total
lives = []

for i in range(3):
  life = trtl.Turtle()
  life.penup()
  window.addshape('heart2.gif') # picture of a heart representing one life
  life.shape('heart2.gif')
  life.goto((-i * 30), -50)
  lives.append(life)

def find_collision_with_restricted():
  global playersize
  between_distance = 5
  px, py = player.pos()
  for point in restricted_points: # list of coordinates of the restricted points
    rx, ry = point
    distance = math.sqrt((px - rx)**2 + (py - ry)**2) # finds distance between to see if they touch
    if distance < between_distance:
      life = lives.pop()
      life.hideturtle()
      if not lives:
        print("Game over") # display game over message when all lives are lost

相关问题