我在練習做Pygame的一個Android應用程序,並決定做你的典型的泡沫射擊。儘管玩家發射的彈丸不夠準確,但我遇到了一個問題。Python 2.7版 - pygame的 - move_ip()過於詳盡
def move (self, time_passed):
x = (math.sin(self.angle) * self.speed) * time_passed
y = (math.cos(self.angle) * self.speed) * time_passed
c = self.rect.center
self.rect.center = (c[0]+x, c[1]+y)
def update(self, screen, time_passed):
if self.launched:
if not self.mpos:
self.mpos = pygame.mouse.get_pos()
diffx, diffy = (self.mpos[0]-self.rect.center[0],
self.mpos[1]-self.rect.center[1])
self.angle = math.atan2(diffx, diffy)
self.move(time_passed)
的screen
和time_passed
參數從主迴路通過,是顯示和返回的值的時鐘類的tick(60)/1000.0
,爲清楚起見。
如果我從move()
打印x和y的值,它非常準確:x: 1.0017224084 y: -21.9771825359
或類似的東西取決於鼠標的位置。但是,似乎pygame或move_ip只能使用整數。我之間會有什麼1.00000001和1.9999999點擊鼠標將在準確的位置拍攝,爲1(參考x座標)。這對我的比賽不利。有沒有解決方案,以便我可以讓我的對象非常精確地移動?
編輯:我意識到我錯了代碼粘貼。 self.rect.center = (c[0]+x, c[1]+y)
是self.rect.move_ip(x, y)
應該在哪裏。但是,兩種方法都會得到相同的結果。
我從來沒有與pygame的工作之前,但我感到困惑的是什麼'move_ip()'是和做。在你的代碼示例中,你有一個名爲'move()'的函數,那和'move_ip()'有什麼區別? – Dennis 2013-05-10 05:09:12
@ Dennis啊,我複製了錯誤的版本。 'self.rect.center =(C [0] + X,C [1] + y)的'通常會'self.rect.move_ip(X,Y)'爲了做我嘗試剛剛更新rect.center琥珀建議如下,但兩種方法都有相同的結果。 – jtsmith1287 2013-05-10 05:37:58