太空侵略者乌龟python中有多个子弹

时间:2019-11-13 13:20:38

标签: python turtle-graphics

我正在编码一个太空侵略者,我想有多个子弹,但我似乎找不到解决方法。每次尝试新事物时,都会出现错误。我尝试使用turtle.clone()或类或列表,但似乎无法正确处理。 我认为我需要列出“ x”个项目符号列表,并且每当我发射项目符号时,代码都会将我的项目符号列表附加到n + 1。

请帮助

turtle.register_shape("laser.gif")

#CREATE THE BULLET
bullet = turtle.Turtle()
bullet.shape("laser.gif")
bullet.penup()
bullet.speed(0)
bullet.setheading(90)
bullet.shapesize(0.2, 0.8)
bullet.hideturtle()

bulletspeed = 20
bulletstate = "ready" #Define bullet state -- ready = ready to fire -- fire = bullet is firing

def fire_bullet():
#DECLARE BULLETSTATE AS GLOBAL IF IT NEEDS CHANGES
global bulletstate
if bulletstate == "ready":
    os.system("afplay laseraigu.wav&")
    bulletstate = "fire"
    #MOVE BULLET TO JUST ABOVE THE PLAYER
    x = player.xcor()
    y = player.ycor() + 10
    bullet.setposition(x, y)
    bullet.showturtle()



#MOVE BULLET
if bulletstate == "fire":
    y = bullet.ycor()
    y += bulletspeed
    bullet.sety(y) 


#CHECK IF BULLET HAS GONE TO TOP
if bullet.ycor() > 275:
    bullet.hideturtle()
    bulletstate = "ready"

#CHECK COLLISION BULLET-ENEMY
if isCollision(bullet, enemy):
        os.system("afplay explosion.wav&")

#RESET BULLET
bullet.hideturtle()
bulletstate = "ready"
bullet.setposition(0, -400)

#RESET ENEMY
x = randint(-200, 200)
y = randint(100, 250)
enemy.setposition(x,y)

0 个答案:

没有答案