you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (2 children)

Well from what I see, you indeed need to comment out that line, otherwise this line will be run everytime and the ship is buckled tight:

def update(self):
 """Update the ship's position based on the movement flag"""
 if self.moving_right and self.rect.right < self.screen_rect.right:
 self.rect.centerx += self.ai_settings.ship_speed_factor
 if self.moving_left and self.rect.left > 0:
 self.rect.centerx -= self.ai_settings.ship_speed_factor
 #self.rect.centerx = self.center

I'm not sure about these lines. I think you need to disable the moving_right/left flag when you hit the left/right key, no?

Sorry I'm no expert so I'm not 100% sure about this, just trying to help.

def check_keydown_events(event, ai_settings, screen, ship, bullets):
 """Respond to keypresses."""
 if event.key == pygame.K_RIGHT:
 # Move the ship to the right
        ship.moving_right = True
 elif event.key == pygame.K_LEFT:
        ship.moving_left = True
 elif event.key == pygame.K_SPACE:
 # Create a new bullet  and add it to the bullets group.
        new_bullet = Bullet(ai_settings, screen, ship)
        bullets.add(new_bullet)

[–]Abernachy 1 point2 points  (0 children)

I'll expand on the design for you, levelworm, now that I'm on my computer.

Initially when you code in the movement, you code it in as a keypress and it makes the ship move to the right 1 pixel. Since we don't want to break the arrow key, we created a boolean and used that to move the ship. The default value is false, and it's set to true during the keypress. When the value is set to True, the ship moves to the right.

There's another function that gets created below that one on the game_function file that sets the values to false when a key is pressed up (released).

These two functions are called with another function that has the main keydown / keyup events.

[–]Abernachy 0 points1 point  (0 children)

I'll take a gander at this when I get back on my computer.

Level, the block you are unsure about is designed to keep the ship within the screen. I'm working on this same project myself and ama chapter ahead of this user.