猿问

有没有办法在 python 中生成不同的对象实例?

我正在用 python 制作游戏(使用街机库)。我做了一个“武器”类,我可以在其中制作不同的武器实例(具有不同的精灵、伤害统计等)。我想要它,所以当某个变量为真时,枪会创建一个类的新实例(子弹),但我不想将每个实例分配给一个新变量等。有没有办法做到这一点?并且请不要太复杂,因为我在 Python 方面还没有走得太远。


class Weapon(object):

    def __init__(self, sprite, size):

        self.weapon = arcade.Sprite(sprite,size)

        self.attacking = True


    def draw(self):

        self.weapon.draw()

        if self.attacking:

            # Creates an instance of Bullet Class with initialized variables

            # Every single time this variable is turned into True


Smart猫小萌
浏览 152回答 2
2回答

陪伴而非守候

我不会那样做,我会创建一个包含 draw() 逻辑的 Sprite 类,其中一个 Player 和一个 Weapon 类将继承玩家将包含武器并在包含你所有的 Game 类中实例化逻辑class Sprite:&nbsp; &nbsp; def draw(self):&nbsp; &nbsp; &nbsp; &nbsp; # Draw logic, maybe have an image sprite member variableclass Bullet(Sprite):&nbsp; &nbsp; def __init__(self, posX, posY, angle, velocity, game):&nbsp; &nbsp; &nbsp; &nbsp; self.posX = posX&nbsp; &nbsp; &nbsp; &nbsp; self.posY = posY&nbsp; &nbsp; &nbsp; &nbsp; self.angle = angle&nbsp; &nbsp; &nbsp; &nbsp; self.velocity = velocity&nbsp; &nbsp; &nbsp; &nbsp; self.game = game&nbsp; &nbsp; &nbsp; &nbsp; self.game.bullets.append(self)&nbsp; &nbsp; def fly(self):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# Add some trigonometry here using the angle and velocity and implement some collision algorithmclass Weapon(Sprite):&nbsp; &nbsp; posX = 0&nbsp; &nbsp; posY = 0 #Probably gonna need to keep track of the position, you should init them in constructor&nbsp; &nbsp; angle = PI&nbsp; &nbsp; bullets = []&nbsp; &nbsp; def shoot(self):&nbsp; &nbsp; &nbsp; &nbsp; if len(self.bullets) <= 10: # maybe you don't want too many bullets&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.bullets.append(Bullet(self.posX, self.posY, self.angle, 100))class Player(Sprite):&nbsp; &nbsp; posX = 0&nbsp; &nbsp; posY = 0&nbsp; &nbsp; def __init__(self, game):&nbsp; &nbsp; &nbsp; &nbsp; self.game = game&nbsp; &nbsp; &nbsp; &nbsp; self.weapon = Weapon()&nbsp; &nbsp; def shoot(self):&nbsp; &nbsp; &nbsp; &nbsp; self.weapon.shoot()class Game:&nbsp; &nbsp; players = []&nbsp; &nbsp; bullets = []&nbsp; &nbsp; def __init__(self, player_count=1):&nbsp; &nbsp; &nbsp; &nbsp; for i in range(player_count):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.players.append(Player(self))&nbsp; &nbsp; def draw(self):&nbsp; &nbsp; &nbsp; &nbsp; for player in self.players:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; player.draw()&nbsp; &nbsp; &nbsp; &nbsp; for bullet in self.bullets:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bullet.fly()def main():&nbsp; &nbsp; game = Game(1)&nbsp; &nbsp; game.start() # need to implement start logic, rerendering every few MS, etc.if __name__ == "__main__":&nbsp; &nbsp; main()

慕村9548890

您可以为此使用设置器class Weapon(object):&nbsp; &nbsp; ...&nbsp; &nbsp; @property&nbsp; &nbsp; def attacking(self):&nbsp; &nbsp; &nbsp; &nbsp; return self._attacking&nbsp; &nbsp; @attacking.setter&nbsp; &nbsp; def attacking(self, value):&nbsp; &nbsp; &nbsp; &nbsp; if value and not self._attacking:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # logic for when attacking become true&nbsp; &nbsp; &nbsp; &nbsp; self._attacking = value然后,当您somewep.attacking = True对代码的其他部分执行操作时,setter 将运行。关于保存 Bullet 实例。如果实例化Bullet具有它需要的所有副作用,那么你不需要保存它,Bullet(parameters, ...)你会没事的。如果Bullet框架正在绘制,那么它有一个对它的引用,只要框架引用它,GC就不应该收集它
随时随地看视频慕课网APP

相关分类

Python
我要回答