【问题标题】:How do I put multiple actors in a list and have them move - python pygame如何将多个演员放在一个列表中并让他们移动 - python pygame
【发布时间】:2021-05-01 12:49:46
【问题描述】:

如何在 pygame 的列表中移动演员,这是我尝试过的:`

boxes = []
for i in range(10):
    h = random.randint(0,200)
    w = random.randint(0,200)
    boxes.append( Actor("red.png"))
def draw():

    red.draw()
for i in range(len(boxes)):
  boxes[i].center = (-570,250 -50*i)`

但它给了我一个错误,比如红色没有定义。我该如何解决这个问题?

【问题讨论】:

  • 问题是关于Pygame Zero,而不是关于Pygame
  • 有区别吗?
  • 当然可以。例如,Pygame 没有Actor。 Pygame Zero 适合初学者。
  • 很高兴知道!我是新手,所以信息很好!

标签: python python-3.x pgzero


【解决方案1】:

你必须设置Actor对象的xy属性:

boxes = []
for i in range(10):
    actor = Actor("red.png")
    actor.x = random.randint(0, 200)
    actor.y = random.randint(0, 200)
    boxes.append(actor)

如果要移动对象,则需要更改坐标。例如:

for box in boxes:
    box.x += 1

【讨论】:

  • 谢谢,太好了!
猜你喜欢
  • 2020-11-08
  • 2020-03-24
  • 2012-04-04
  • 2016-10-20
  • 2016-01-26
  • 2012-07-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多