python中不能存储小数吗那这样绕了一圈最后self.rect.centerx不是还只能取self.center的整数值吗?有什么意义?importpygameclassShip():def__init__(self,ai_settings,screen):"""In...
python中不能存储小数吗那这样绕了一圈最后self.rect.centerx不是还只能取self.center的整数值吗?有什么意义?
import pygameclass Ship(): def __init__(self, ai_settings, screen): """Initialize the spaceship image and set its original position.""" self.screen = screen self.ai_settings = ai_settings # Load the image and obtain its bounding rectangle. self.image = pygame.image.load( r'D:\python_work\alien_invasion\images\ship.bmp') self.rect = self.image.get_rect() self.screen_rect = screen.get_rect() # Place the spaceship in the center of the bottom of the screen. self.rect.centerx = self.screen_rect.centerx self.rect.bottom = self.screen_rect.bottom # Store the decimals in the property center. self.center = float(self.rect.centerx) # The moving sign self.moving_right = False self.moving_left = False def update(self): """Adjust the position of the ship based on the sign.""" # Update the center of the ship instead of the rect. if self.moving_right and self.rect.right < self.screen_rect.right: self.center += self.ai_settings.ship_speed_factor if self.moving_left and self.rect.left > 0: self.center -= self.ai_settings.ship_speed_factor # Update rect based on self.center self.rect.centerx = self.center def blitme(self): """Place the spaceship in the designated position.""" self.screen.blit(self.image, self.rect)
展开