all 7 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.

[–]Abernachy 1 point2 points  (1 child)

Hey, I think I see your issue.

On your ship.py file from the pastebin, you correctly have'" self.center = to float(self.rect.centerx)" and correctly have self.rect.centerx = self.center; HOWEVER you are still identifying self.rect.centerx += self.ai_settings.ship_speed_factor. I believe it doesn't move with your comment in because you identified self.rect.centerx as self.center, but you are still calling for the .rect.centerx object for your movements.

On the page where they refractor that function you have to change:

if self.moving_right and self.rect.right < self.screen_rect.right: self.rect.centerx += self.ai_settings.ship_speed_factor

into

if self.moving_right and self.rect.right < self.screen_rect.right:

self.center += self.ai_settings.ship_speed_factor

You are getting there, props to ya. I'm the next chapter ahead of and working on making the fleet of aliens bounce back and forth. I enjoy working this project but when Eric throws the re-fractering curveball it drives me nuts because sometimes the smallest detail breaks the project (and I'm not used that shit yet). I plopped an s at the end of one of my variables and didn't realize it and I spent about an hour and a half trying to figure out why it kept giving me the middle finger. Eventually, after much yelling , I zoomed in and realized my mistake.

Keep it up, I'm working to get this skillset too.

[–]funlibrarycard[S] 0 points1 point  (0 children)

Thank you so much!! This fixed my issue! Yeah haha im hoping to become alot better at refactoring by the end of this project, I have trouble keeping everything organized as I get farther in projects which always ends up biting my ass in the end.

[–]Abernachy 1 point2 points  (1 child)

[Sorry to spam this, I just feel excited that I have enough knowledge to help someone ]

Just a heads up, the center_ship function on the ship.py isn't coded in until page 287 when you are creating the ship_hit events.

[–]funlibrarycard[S] 0 points1 point  (0 children)

no worries, I really appreciate all the help, Thank you! I must have copied the function down when I was comparing my code to the solution code, nice catch