You are browsing as a guest. Sign up (or log in) to start making projects!

2h 13m 9s logged

I made some pretty simple changes to the overall codebase but now it’s much cleaner.

  • Instead of storing all globals like movement speed inside the single settings.py file, I moved them into attributes of their own classes.
  • Sprites are now loaded in the __init__ method of their classes. Previously, I was under the impression that this was not possible because you needed to run pygame.init() before you used the .convert() (and similar) methods after loading the image. However, I was putting it outside of the class, so it would always run before running pygame.init(). This overall makes the code much cleaner, as now I don’t have to load in the sprites in the main file. Also, it was getting quite challenging to deal with figuring out ways to pass sprites into functions like player.shoot() so that it would spawn in a Bullet correctly.
  • I am now using .convert_alpha() instead of .convert(), which allows the sprites to have transparency (as seen in picture 2). I had been saving all of the sprites as .png files, but they had a black background when overlapping with other sprites (as seen in picture 3). With this, it actually allows for transparency and looks much nicer.
  • I added meteors. This was actually quite easy, just another class like the Bullet class but falling downwards. Pygame has this really nice feature where I can check for all collisions between all sprites from any 2 groups like so. pygame.sprite.groupcollide() takes in 2 sprite groups (pygame.sprite.Group), 2 booleans representing whether to remove the sprite right after, and a callback function. This will be useful later for adding explosions and score. They also spawn at a constant interval regardless of FPS. I did that by using delta-time and adding a counter to get the amount of milliseconds since the last frame. A meteor will spawn every 250 milliseconds. I find that this is a playable amount for now.
  • Shooting now has a cooldown. I do this in the same way as the asteroid spawning. The player can shoot 4 times per second.

Next thing to do is to add lives and subtract them when the player gets hit with a meteorite.

0
10

Comments 0

No comments yet. Be the first!