【发布时间】:2020-05-15 12:56:13
【问题描述】:
我只是想不通为什么我的子弹不起作用。我做了一个子弹类,它是:
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.bullets,然后 game.shoot_bullet 会计算玩家和鼠标之间的角度并相应地射击子弹在鼠标的方向。然而,结果是一团糟,子弹实际上不旋转也不移动。它们被生成并奇怪地移动到屏幕的左侧。不知道是不是我搞砸了,还是我使用的方法完全错误。
【问题讨论】:
-
这可能是三角函数在不同象限中产生负面结果吗? - teachoo.com/7240/1406/… 鼠标在右上方是否有效?
-
我打印了 sin 和 cos 值,似乎是这样(看起来 sin 在第二象限是负数)。我该如何解决这个问题?谢谢
-
右上 sin 为正,cos 为负
-
使用极坐标怎么样?参考:stackoverflow.com/questions/6775897/…