Creating Space Invaders clone with PyGame in 100 lines of code or less

If you ever wanted to make Space Invaders in Python, but were too lazy to do it, here’s a short tutorial with GIFS!

>>basic settings

First I create the Game class that will control the game. Then I initialize pygame instance and the alien list. I set the refresh rate to 60 and run the game loop.

>>enemies

I create a new Alien class, that holds coordinates, color, and size and will draw the object to the screen. I set the “falling down” effect with the decreasing y value by 5% of a pixel per frame.
(the un-indented blocks of code are not going to work as you see them— alien = … and alien.draw need a “game” instance reference instead of self. Same goes for Hero and Generator below. For complete code see the end for a github link)

Visit Now

Tags: Code Less