As I mentioned in one of my previous posts I will code the game snake for my first Python project. I just started to properly think about the how, why and so on a few minutes ago. I already googled one or two things and then thought, why don’t I document all the steps so you can see 🙂 So here we go!
How to start
I have never before been coding a game in Python before. Maybe there is a correct way how to start the coding process. Maybe there isn’t. Anyhow, I start by thinking about how the game could work – from a code perspective.
The game snake
In the game snake, you have to steer a snake through a field. In this field, you have apples, which can be eaten by the snake. For each apple the snake eats, it will get longer. The challenge is to eat all the apples and not let the snake crash into itself, the borders or other objects on the field. Once all apples on the field are eaten, a gate will open through which the snake will get to the next level.
I want to start with the field. Meaning the window we see while playing. How can I create a window on my computer, draw the objects, the apples and the snake?
Also, as the snake should be moving and should be steered by the player, the picture has to update according to the player’s commands. So how do I update the field and then “reload” it?
I know that there is a library called “pygame” which is said to be helpful to do games with python. As the first step in my program, I import pygame.
I just googled pygame GUI and window to get an idea on how to draw the field. The internet provided me with this page which explained to me how to draw a window. Right now my code looks like this:
Jay! And when I run the code it actually draws me a white window. This might not sound very exciting – but still 🙂
Obstacles, apples and pieces of snake
But of course, I want a little more than “just” a white window. So I kept on reading on the page I mentioned before, as the next chapter was on drawing elements. And I have to say, Peter is quite good at explaining. I adapted it and drew an apple, an obstacle and a mini snake. the code for this looked like this
and the result like this
During this part, I have learned that everything you want to do on the screen has to come before the flip() statement. Is it called statement? I am not even sure about it.
Close the window
While I have been “researching” the pygame.draw.rect() I have discovered this page which I also liked. Right now my window is not closing when I click on the X in the upper right corner. The only thing the clicking does is stopping the program from running. According to close the window I would need to import “sys” to make it close properly.
Of course, I updated my little program to this very fancy closing the window standard. I only had to add one line of code here (line 2)
and adjust the last line to this
I hope you are still following 😀
You got to move it!
This has been very nice. I now know the basics on how to draw. Which I did not a while ago. But I do not want the screen to stand still. The snake has to move. How do I tell Python to move it for me?
The logic behind the movement (as I imagen) is to move the objects a bit and then display them again (and at the same time delete the old display).
By default I want the snake to go up on the screen. Meaning as long as the player is not doing anything, the snake will go up.
I read on Peters page how he moves particles and then adjusted it to my game. I introduced the variable dy = -1, added the move() function to the class snake and then inserted the my_first_snake.move() at the right position as you can see in the screen shots below.
At this point, I realized that I have to change my snake object a little. Because if I just move self.y by dy I will not get much further than the -1. I have to store the value y somehow. Damn no!
Because I feel tired I am going to think about this tomorrow (procrastination for the win haha)…
I hope you have enjoyed this piece, let me know if you want more of this kind!
Thank you for reading!