'Alien fleet falls diagonally
On page 266 of "Python Crash Course" when you're supposed to make the fleet fall one then change direction, my alien fleet falls diagonally. After using print statements I can see that the variable fleetDirection changes from 1 to -1 to 1 and so on. The problem is that it is constantly changing directions so it just falls. What should I do to fix this?
def check_edges(self):
screenRect = self.screen.get_rect()
if self.rect.right >= screenRect.right or self.rect.left <= 0:
return True
def update(self):
self.x += (self.settings.alienSpeed * self.settings.fleetDirection)
self.rect.x = self.x
def check_fleet_edges(self):
for alien in self.aliens.sprites():
if alien.check_edges():
self.change_fleet_direction()
break
def change_fleet_direction(self):
for alien in self.aliens.sprites():
alien.rect.y += self.settings.fleetDropSpeed
self.settings.fleetDirection *= -1
Solution 1:[1]
I figured out the issue. In my run_game method I had self.check_fleet_edges() this made it so that when check_edges returned true then it constantly ran check_fleet_edges(). Thank you for giving me a link to the correct syntax; that helped me find the issue.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jack_AKA_Oscar |
