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