为什么这艘船向右移动但向左,上下移动(不优化的方式)?
class Ship():
def __init__(self, ai_setting, screen):
self.screen = screen
self.ai_setting = ai_setting
self.image = pygame.image.load("images/ship.bmp")
self.image = pygame.transform.scale(self.image, (200, 200))
self.image_rect = self.image.get_rect()
self.screen_rect = self.screen.get_rect()
self.image_rect.bottom = self.screen_rect.bottom
self.image_rect.centerx = self.screen_rect.centerx
self.center_xaxis =float( self.image_rect.centerx)
#move_flags
self.moving_right = False
self.moving_left = False
self.moving_up = False
self.moving_down = False
def update(self):
if self.moving_right:
self.center_xaxis += self.ai_setting.ship_speed_factor
if self.moving_left:
self.image_rect.centerx -= 10
if self.moving_up:
self.image_rect.centery -=10
if self.moving_down:
self.image_rect.centery +=10
def blitme(self):
self.screen.blit(self.image, self.image_rect)
我尝试打印self.center_xaxis
和self.image_rect.centerx
的值 但是self.center_xaxis
并不相同。
我需要self.center
的值来通过我在我制作的设置文件中控制的因素进行更改。 game_function
文件根据按键将更改的移动标志处理为true
或返回false
。
class Ship():
def __init__(self, ai_setting, screen):
self.screen = screen
self.ai_setting = ai_setting
self.image = pygame.image.load("images/ship.bmp")
self.image = pygame.transform.scale(self.image, (200, 200))
self.image_rect = self.image.get_rect()
self.screen_rect = self.screen.get_rect()
self.image_rect.bottom = self.screen_rect.bottom
self.image_rect.centerx = self.screen_rect.centerx
self.center_xaxis =float( self.image_rect.centerx)
#move_flags
self.moving_right = False
self.moving_left = False
self.moving_up = False
self.moving_down = False
def update(self):
if self.moving_right:
self.center_xaxis += self.ai_setting.ship_speed_factor
if self.moving_left:
self.image_rect.centerx -= 10
if self.moving_up:
self.image_rect.centery -=10
if self.moving_down:
self.image_rect.centery +=10
def blitme(self):
self.screen.blit(self.image, self.image_rect)
I tried printing the value of self.center_xaxis
and self.image_rect.centerx
but self.center_xaxis
isn't the same.
I need the value of self.center
to change by a factor I'll control in the settings file I made. The game_function
file handles the changing movement flags according to the keypress to True
or back to False
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能想以浮点精度移动对象。因为
pygame.Rect
应该代表一个屏幕上的区域,pygame.Rect
对象只能存储积分数据。参见pygame.Rect
:如果要以浮点精度存储对象位置,则必须将对象的位置分别存储在单独的变量属性中,并同步
pygame.Rect.Rect
对象。round> rounh
坐标并将其分配给矩形的位置:You probably want to move the object with floating point precision. Since
pygame.Rect
is supposed to represent an area on the screen, apygame.Rect
object can only store integral data. Seepygame.Rect
:If you want to store object positions with floating point accuracy, you have to store the location of the object in separate variables respectively attributes and to synchronize the
pygame.Rect
object.round
the coordinates and assign it to the location of the rectangle: