如何在Python中生成克隆?

2024-04-25 08:42:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在制作一个简化版的《太空入侵者》,我正在尝试让子弹从玩家的角色射出。我做了一只海龟作为角色(主船)和一只海龟作为子弹(子弹)。我怎样才能复制子弹(为了射杀他们)?这是我的子弹代码:

`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(main_ship.xcor(), main_ship.ycor())
bullet.hideturtle()`

我还没有试过任何东西,因为我找不到任何解释。你知道吗


Tags: 代码角色main玩家太空海龟入侵者turtle
1条回答
网友
1楼 · 发布于 2024-04-25 08:42:51

既然你的子弹是乌龟,你有没有看过乌龟自己的clone()方法:

Help on function clone in module turtle:

clone()

    No argument.

    Create and return a clone of the turtle with same position, heading
    and turtle properties.

    Example (for a Turtle instance named mick):
    mick = Turtle()
    joe = mick.clone()

由于海龟是有效的全局实体,从不垃圾收集,我建议您不要浪费项目符号,而是保留一个可用项目符号的池(list),您可以根据需要从中提取,并在项目符号不再活动时添加回。只有克隆额外的子弹,从一个专用的原型,当池是空的。你知道吗

相关问题 更多 >

    热门问题