简体   繁体   English

如何使多个克隆在 python 龟中同时运行

[英]How to make multiple clones run at the same time in python turtle

I am trying to make a code where you can press the spacebar and an object will move forwards constantly.我正在尝试编写一个可以按空格键的代码,并且 object 将不断向前移动。 I am hoping to be able to have multiple of these objects moving at once without having to code hundreds of them separately.我希望能够一次移动多个这样的对象,而不必单独编写数百个。

This is my current code:这是我当前的代码:

Bullet:子弹:

bullet = turtle.Turtle()
bullet.speed(0)
bullet.shape("circle")
bullet.color("red")
bullet.shapesize(stretch_wid=0.5, stretch_len=0.5)
bullet.penup()
bullet.goto(-200, -200)
bullet.hideturtle()

Movement:移动:

def shoot_bullet():
    stop = False
    bullet2 = bullet.clone()
    bullet2.showturtle()
    while stop == False:
        y = bullet2.ycor()
        bullet2.sety(y + 20)
        wn.update()
        time.sleep(0.5)
...

onkeypress(shoot_bullet, "space")

This works until I press space again and the bullet just stops as 'bullet2' has been redefined as the new bullet I create when I press space.这一直有效,直到我再次按下空格并且项目符号停止,因为“bullet2”已被重新定义为我按下空格时创建的新项目符号。 Is there a way to create multiple clones which can run on top of each other?有没有办法创建可以在彼此之上运行的多个克隆?

Your while stop == False: loop and time.sleep(0.5) have no place in an event-driven environment like turtle.您的while stop == False: loop 和time.sleep(0.5)在像 turtle 这样的事件驱动环境中没有位置。 Instead, as we fire each bullet, the below code attaches a timer event that moves it along until it disappears.相反,当我们发射每个子弹时,下面的代码会附加一个计时器事件,该事件会移动它直到它消失。 At which point the bullet is recycled.此时子弹被回收。

This simplified example just shoots bullets in random directions from the center of the screen.这个简化的例子只是从屏幕中心向随机方向发射子弹。 You can keep hitting the space bar to generate simultaneous bullets that all move in their own direction until they get far enough away:您可以继续按空格键来生成同时向自己的方向移动的子弹,直到它们离得足够远:

from turtle import Screen, Turtle
from random import randrange

def move_bullet(bullet):
    bullet.forward(15)

    if bullet.distance((0, 0)) > 400:
        bullet.hideturtle()
        bullets.append(bullet)
    else:
        screen.ontimer(lambda b=bullet: move_bullet(b), 50)

    screen.update()

def shoot_bullet():
    screen.onkey(None, 'space')  # disable handler inside hander

    bullet = bullets.pop() if bullets else bullet_prototype.clone()
    bullet.home()
    bullet.setheading(randrange(0, 360))
    bullet.showturtle()

    move_bullet(bullet)

    screen.onkey(shoot_bullet, 'space')  # reenable handler on exit

bullet_prototype = Turtle('circle')
bullet_prototype.hideturtle()
bullet_prototype.dot(10)  # just for this example, not for actual code
bullet_prototype.shapesize(0.5)
bullet_prototype.color('red')
bullet_prototype.penup()

bullets = []

screen = Screen()
screen.tracer(False)
screen.onkey(shoot_bullet, 'space')
screen.listen()
screen.mainloop()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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