问题描述
所以我在我的程序中已经到了需要为一些玩家可以碰撞而不会死亡的精灵创建一个组的地步(就像我在屏幕上可能有的其他精灵一样).
So I've gotten to the point in my program where I need to create a group for some sprites that the player can collide with without dying (like some other sprites I may have on screen).
我已经搜索过谷歌,但官方 pygame 文档似乎无用和/或难以理解.我正在寻求任何对此有所了解的人的帮助.
I've scoured Google but it appears that the official pygame documentation is useless and/or hard to comprehend. I'm looking for just a wee bit of help from anyone who knows a bit about this.
首先,我需要了解如何创建组.它会在初始游戏设置中使用吗?
First, I need to find out how to create a group. Does it go in the initial game setup?
然后在创建时将精灵添加到组中.pygame 网站对这个主题有这样的说法:
Then adding a sprite to a group upon its creation. The pygame site has this to say on the subject:
Sprite.add(*groups)
那么...如何使用它?假设我有一个名为 gem 的精灵.我需要将 gem 添加到 gems 组中.是吗:
So... how does one use this? Let's say I have an sprite named gem. I need to add gem to the gems group. Is it:
gem = Sprite.add(gems)
我对此表示怀疑,但网站上没有任何示例可供参考,我不知所措.
I doubt it, but without any examples to go off of on the site, I am at a loss.
此外,我希望能够编辑某个组的属性.这是通过像我一样定义一个组来完成的吗?或者它是我在现有精灵的定义中定义的,但带有if sprite in group"?
Furthermore, I would love to be able to edit attributes for a certain group. Is this done by defining a group like I would a class? Or is it something I define within the definition for the existing sprite, but with an 'if sprite in group'?
推荐答案
回答你的第一个问题;要创建一个组,您可以执行以下操作:
To answer your first question; to create a group you would do something like this:
gems = pygame.sprite.Group()
然后添加一个精灵:
gems.add(gem)
关于您要编辑的组的属性取决于它们是什么.例如,您可以定义如下内容来指示组的方向:
Regarding the attributes for the group you'd like to edit it depends what they are. For example you could define something like this to indicate the direction of the group:
gems.direction = 'up'
这篇关于如何在pygame中使用精灵组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!