The above gif is my very first game. In this blog post, I will introduce a very simple game created using pygame.
What is this game about?¶
In this game, you are a "princess" and the goal is to rescue "price". But be careful, there is a snake monster that could hit you!
I used pygame.¶
This pygame library is really REALLY amazing for creating a game. I originally thought that creating game needs a "serious" computer science degree, and it is out of my reach. Creating graphical user interface seemed very complicated! How can my script accepts keyboard input, do calculation and then update the game screen accodingly? No worry, pygame will take care of such things, so you can focus on creating game logics (e.g., how to score points, what decides "game over"). In fact the game above requires only about 400 lines.
Where did I start?¶
Did I take any online courses? Well, yes and no. All I did was follow this great youtube series from thenewboston. This series has many tutorial videos. I watched the first 40 (40!). Each tutorial is nicely modularized and takes only about 5 - 10 minutes. I would love to watch the rest later in my life!
If you are interested in learning pygame from scratch, you should go to thenewboston. Here, I will show some features of my game, and how I did them.
The keyboad input are extracted in less than 20 lines.¶
This is not really features of my game but I want to say where this is defined. Its defined
The game is 400 x 400 pixcels¶
You can specify the number of pixcels by specifying npix_x and npix_y inputs of the SavePrince object. But the game display is really defined in the line:
The snake monster hunts you down.¶
I added this functionality to make the game more challenging. It will always move toward you. For example, if you are on the right of the snake monster and above it, then the snake monster will go up or right. The decision of going up or going right is based on randomness.
The snake monster appears around prince.¶
This is another fuctionality to make the game more challenging. While you want to go closer to prince, you need to avoid the snake that is around the prince.
The speed of the snake monster increases when it moves toward the same direction consequtively.¶
Even if the snake seems to be far away from you, it will get you really quick if the snake is on the same row or on the same column.
How to play the game?¶
To run the game, download the whole SavePrinceGame repository, pip install pygame, and then within the SavePrinceGame repository and on your terminal, type: