我做了一个简单的蟒蛇乌龟游戏,玩家必须收集屏幕上随机生成的物品,但每当它碰到一个受限制的物品时,我希望它失去一条生命。我为第一次直播设置了它,但我不太确定如何让它在其他两次直播中发挥作用。这是我为第一个所做的:
# 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()
我怎么才能做到这一点,如果它达到另一个限制值,第二颗心会重生,然后第三次生命也会重生。
你可以这样做:
lives = (life1, life2, life3)
n_of_lives = len(lives)
然后:
if collided_with_restricted:
n_of_lives -= 1
lives[n_of_lives].hideturtle()
# 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