我只是想不通为什么我的子弹不起作用。我做了一个子弹课,这里是:
class Bullet:
def __init__(self):
self.x = player.x
self.y = player.y
self.height = 7
self.width = 2
self.bullet = pygame.Surface((self.width, self.height))
self.bullet.fill((255, 255, 255))
现在我在我的游戏类中添加了几个函数,这是新代码:
class Game:
def __init__(self):
self.bullets = []
def shoot_bullet(self):
if self.bullets:
for bullet in self.bullets:
rise = mouse.y - player.y
run = mouse.x - player.x
angle = math.atan2(rise, run)
bullet.x += math.cos(angle)
bullet.y += math.sin(angle)
pygame.transform.rotate(bullet.bullet, -math.degrees(angle))
D.blit(bullet.bullet, (bullet.x, bullet.y))
def generate_bullet(self):
if mouse.is_pressed():
self.bullets.append(Bullet())
我期望代码做的是每次按下鼠标按钮时Bullet()都会添加一个,然后计算玩家和鼠标之间的角度并相应地朝鼠标的方向射击子弹。然而,结果是一团糟,子弹实际上不旋转也不移动。它们被生成并奇怪地移动到屏幕的左侧。我不确定我是否搞砸了,或者我使用的方法完全错误。game.bulletsgame.shoot_bullet
弑天下
相关分类