title: Eric Matthes alien invasion project, ship will not stay on screen
body: <p>I am working on the alien invasion project 1 and attempting to write the code for restricting the ships movement so that it stays on the screen. However, this is not happening. I can get the program to work with zero errors but the ship will not stay on the screen. The section that specifically limits the ships movement is under <code>def update(self):</code></p>
  
  <p>Any help would be great, thanks everyone!</p>
  
  <pre><code>import pygame 
  
  class Ship():
  
      def __init__(self, ai_settings, screen):
          # initialize the ship and set its starting position
          self.screen = screen
          self.ai_settings = ai_settings
  
          # load the ship image and get its rect
          self.image = pygame.image.load('alien_invasion/ship.bmp')
          self.rect = self.image.get_rect()
          self.screen_rect = screen.get_rect()
  
          self.rect.centerx = self.screen_rect.centerx
          self.rect.bottom = self.screen_rect.bottom
  
          # start each new ship at the center of the bottom of the screen
          self.center = float(self.rect.centerx)
  
          # movement flag 
          self.moving_right = False
          self.moving_left = False
  
      def update(self):
          # update the ships position based on the movement flag
          # update the ships center value, not the rect
          if self.moving_right and self.rect.right &lt; self.screen_rect.right:
              self.center += self.ai_settings.ship_speed_factor
          if self.moving_left and self.rect.left &gt; 0:
              self.center -= self.ai_settings.ship_speed_factor
  
          # update rect object from self.center
          self.rect.centerx = self.center
  
      def blitme(self):
          # draw ship at current location
          self.screen.blit(self.image, self.rect)
  </code></pre>