如何使用 python 和 pygame 创建一个对象的多个唯一实例并将它们添加到列表中?

发布于 2024-10-21 21:33:54 字数 472 浏览 1 评论 0原文

这是 Train 类的代码:

class Train(pygame.sprite.Sprite):
def __init__(self):
    pygame.sprite.Sprite.__init__(self) #call Sprite initializer
    self.image, self.rect = load_image('train.bmp', -1)
    ...

这是我在主循环中创建单独的火车实例的地方:

trains = pygame.sprite.Group()
while True:
    for event in pygame.event.get():
        if event ...:
            train = Train()
            train.add(trains)

如何使火车的每个实例都是唯一的?

Here is the code for the Train class:

class Train(pygame.sprite.Sprite):
def __init__(self):
    pygame.sprite.Sprite.__init__(self) #call Sprite initializer
    self.image, self.rect = load_image('train.bmp', -1)
    ...

And here is where I create the individual train instances, within the main loop:

trains = pygame.sprite.Group()
while True:
    for event in pygame.event.get():
        if event ...:
            train = Train()
            train.add(trains)

How can I make each instance of train unique?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伴随着你 2024-10-28 21:33:54

您的代码创建了一个变量 train,但从不使用 train*s*。我将变量重命名为 train_list 以使其更清晰。

train_list = pygame.sprite.Group()
done = False # any code can toggle this , to request a nice quit. Ie ESC

while not done:
    for event in pygame.event.get():
        if event ...:
            train = Train()
            train_list.add(train)

阅读 文档:pygame.sprite.Group
以及 piman 的 sprite pygame 教程

<块引用>

如果我想更改其中一列列车的变量怎么办?

# modify a variable on one train
train_list[2].gas = 500

# kill train when out of gas
for cur in train_list:
    # this will call Sprite.kill , removing that one train from the group.
    if cur.gas <= 0:
        cur.kill()

<块引用>

如何更新所有列车?

您可以定义 Train.update 以使用 Gas,因为 Sprite.update 由 SpriteGroup.update 调用

Train(pygame.sprite.Sprite):
    def __init__(self):
        super(Train, self).__init__()
        self.gas = 100
    def update(self)
        self.gas -= 1 # or you can use a variable from the caller

Your code creates a variable train, but never uses train*s*. I renamed the variable to train_list to make it clearer.

train_list = pygame.sprite.Group()
done = False # any code can toggle this , to request a nice quit. Ie ESC

while not done:
    for event in pygame.event.get():
        if event ...:
            train = Train()
            train_list.add(train)

Read docs: pygame.sprite.Group
And piman's sprite pygame tutorial

What if I want to change a variable of one of the trains?

# modify a variable on one train
train_list[2].gas = 500

# kill train when out of gas
for cur in train_list:
    # this will call Sprite.kill , removing that one train from the group.
    if cur.gas <= 0:
        cur.kill()

How do I update all trains?

You can define Train.update, to use gas, since Sprite.update is called by SpriteGroup.update

Train(pygame.sprite.Sprite):
    def __init__(self):
        super(Train, self).__init__()
        self.gas = 100
    def update(self)
        self.gas -= 1 # or you can use a variable from the caller
违心° 2024-10-28 21:33:54

实际上,如果您需要识别一列火车,您可以为每个火车添加一个 ID,如下所示:

class Train(pygame.sprite.Sprite):
    def __init__(self, ID = None):
        # train goes here, maybe even a counter that changes "ID" to something if not user-defined.

然后循环它们:

for train in trains:
    if ID == "whatever you like":
        #Do something

!伪代码 !

Actually, if you need to identify a train you could add a ID to every single one of them something like this:

class Train(pygame.sprite.Sprite):
    def __init__(self, ID = None):
        # train goes here, maybe even a counter that changes "ID" to something if not user-defined.

and then loop them over:

for train in trains:
    if ID == "whatever you like":
        #Do something

! Pseudo code !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文