猿问

当我射击时乌龟停止

我遇到了一个问题,即我射击时敌方乌龟会停下来。我对 python 比较陌生,所以我知道我的代码很糟糕。我似乎无法发现为什么会发生这种情况,但我假设它与 while 循环有关。


这是代码:(我添加了注释,因此很容易跳到其中的重要部分)


import turtle

import os

#wn is window

#bp = border

bullet = 'ready'

#screen setup

wn = turtle.Screen()

wn.bgcolor('black')

wn.title('SPACE.INVADERS')



#border

bp = turtle.Turtle()

bp.speed(0)

bp.color('green')

bp.penup()

bp.setposition(-300,-300)

bp.pendown()

count=0

while count != 5:

    count= (count+1)

    bp.fd(600)

    bp.lt(90)

bp.hideturtle()


#player

p = turtle.Turtle()

p.color('red')

p.shape('triangle')

p.penup()

p.speed(0)

p.setposition(0,-250)

p.setheading(90)


#enemy

e = turtle.Turtle()

e.penup()

e.speed(0)

e.shape('square')

e.shapesize(1.25,1.25)

e.color('orange')

e.setpos(-250,250)

e.speed(1)






#p = player

#ps = player speed


ps = 15


#moving left and right

def left_mov():

    x = p.xcor()

    x -= ps

    p.setx(x)


def right_mov():

    x = p.xcor()

    x += ps

    p.setx(x)

#shooting

def shoot():

    global bullet

    if bullet == 'ready':

        bullet = 'fire'

        shot= turtle.Turtle()

        shot.penup()

        shot.speed(0)

        shot.goto(p.pos())

        shot.color('white')

        shot.shape('triangle')

        shot.shapesize(0.5)

        shot.lt(90)

        shot.speed(1)

        shot.fd(550)

        bullet = 'ready'





#bindings

turtle.listen()

turtle.onkey(left_mov, 'Left')

turtle.onkey(right_mov, 'Right')

turtle.onkey(shoot, 'space')

#enemy movement

while True:

    e.fd(500)

    e.rt(90)

    e.fd(25)

    e.rt(90)

    e.fd(500)

    e.lt(90)

    e.fd(25)

    e.lt(90)


MMTTMM
浏览 170回答 1
1回答

潇湘沐

我假设它与 while 循环有关是的,它不应该在那里!在像乌龟这样的事件驱动的世界中,永远不应该有while True:循环。相反,我们需要一个定时事件,它定期触发,对运动中的对象(敌人和子弹)进行增量更新,使它们看起来同时移动。以下是我使用计时器事件重新编写的代码:from turtle import Screen, Turtle# moving left and rightdef left_move():&nbsp; &nbsp; player.setx(player.xcor() - player_speed)def right_move():&nbsp; &nbsp; player.setx(player.xcor() + player_speed)# screen setupscreen = Screen()screen.bgcolor('black')screen.title('SPACE.INVADERS')# borderborder = Turtle(visible=False)border.speed('fastest')border.color('green')border.penup()border.setposition(-300, -300)border.pendown()for _ in range(4):&nbsp; &nbsp; border.forward(600)&nbsp; &nbsp; border.left(90)# playerplayer = Turtle('triangle')player.speed('fastest')player.color('red')player.penup()player.setposition(0, -250)player.setheading(90)player_speed = 15# enemyenemy = Turtle('square')enemy.speed('fast')enemy.shapesize(1.25)enemy.color('orange')enemy.penup()enemy.setpos(-250, 250)enemy_speed = 6&nbsp; # enemy speedenemy_direction = 1&nbsp; # enemy direction# bulletbullet = Turtle('triangle', visible=False)bullet.speed('fastest')bullet.color('white')bullet.shapesize(0.5)bullet.penup()bullet.left(90)bullet_speed = 9&nbsp; # bullet speed# shootingdef shoot():&nbsp; &nbsp; if not bullet.isvisible():&nbsp; &nbsp; &nbsp; &nbsp; bullet.goto(player.position())&nbsp; &nbsp; &nbsp; &nbsp; bullet.showturtle()def move():&nbsp; &nbsp; global enemy_direction&nbsp; &nbsp; if bullet.isvisible():&nbsp; &nbsp; &nbsp; &nbsp; if bullet.ycor() > 275:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bullet.hideturtle()&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bullet.forward(bullet_speed)&nbsp; &nbsp; # enemy movement&nbsp; &nbsp; enemy.forward(enemy_direction * enemy_speed)&nbsp; &nbsp; enemy.settiltangle(enemy.tiltangle() + 3)&nbsp; # just for fun&nbsp; &nbsp; x, y = enemy.position()&nbsp; &nbsp; if x > 250 or x < -250:&nbsp; &nbsp; &nbsp; &nbsp; enemy_direction *= -1&nbsp; &nbsp; &nbsp; &nbsp; enemy.sety(y - 25)&nbsp; &nbsp; screen.ontimer(move, 50)# bindingsscreen.onkey(left_move, 'Left')screen.onkey(right_move, 'Right')screen.onkey(shoot, 'space')screen.listen()move()screen.mainloop()这应该为您提供前进所需的基本功能,并添加碰撞检测(子弹到敌人、敌人到玩家、玩家到墙壁)和得分。另请注意,海龟是全局实体,因此每次需要时都创建一个新项目符号是一个坏主意。即使您最终允许重叠项目符号,也要为此目的保留一组可重复使用的项目符号。
随时随地看视频慕课网APP

相关分类

Python
我要回答