python/pygame question. Wondering what I can do to optimize this code so it doesn't freeze upon alien/ship collisions. github link, the game executes from alien_invasion.py https://github.com/PockyPepero/Python-Exercises/tree/master/alien_invasion
Serious help appreciated. Spam/harassment will be removed.
I'm not sure your code has optimization issues per say. I've identified a list of things that I think are bugs that once changed have the functionality I expect you desire (based on my own experience playing alien invasion) 1. Line 193 of alien_invasion.py contains sleep(0.5). This line stalls the code for 0.5 seconds when a collision between ship and alien is detected, so the game doesn't immediately restart when you lose a life preventing you from being able to react it. Adjust the timing for how long you want before things restart. 2. Line 52 of ship.py in "center_ship()" You set self.x but not the self.y variable. This means your ship is getting centered in the x direction, but it's not moved to the bottom of the screen so when the game restarts the ship immediately collides with the aliens and gets stalled for the sleep time. Notice that because you don't have any logic handling what happens when you run out of lives, the game just continues to infinitely stall. 3. Line 182 of alien_invasion.py in "_ship_hit()" You probably meanself.stats.ships_left -=1 and not to set the ship left count to -1. Let me know if you have any questions. Nice color scheme :)
To clarify point 1 isn't a bug, I just have it in there to explain what's going on in your code to make it behave the way it does.
thanks c: i've implemented the changes you suggested and it's working as intended
Join our real-time social learning platform and learn together with your friends!