In this tutorial you will learn how to build the game snake. The game is an arcade game and it has very simple logic, which is why it is an ideal example to demonstrate how to build games with Pygame.
You may wish to be familiar with the basics of the Python 3 programming language, or at least the basics of Programming in general That said, this course begins at a slowish pace, and I do my best to explain everything at least the first time it shows up. PyGame can actually be a wonderful starting. Jun 03, 2018 As this series is called PyGame Tutorial for Beginners, we will cover the installation as well;). But before installing PyGame you also need Python and yes it is something obvious. Installing Python. Go to Python’s Website. And download the Python according to your system. In my case I am using Python 3.7, 64bit windows installer.
The player is represented as snake, which grows if it eats an apple. The goal of the game is to eat as many apples as possible without colliding into yourself. This is very easy in the early phase of the game but is increasingly more difficult as the length of the snake grows.
Related courses:
Getting started: Basic structure and event handling.
We define a class Player which holds the players position on the screen and the speed by which it moves. In addition we define the actions a Player instance can do (movements):
We define a class Player which holds the players position on the screen and the speed by which it moves. In addition we define the actions a Player instance can do (movements):
A player object can be created and variables can be modified using the movement methods.
We link those methods to the events. In Pygame we can get non-blocking keyboard input using this code:
We link those methods to the events. In Pygame we can get non-blocking keyboard input using this code:
The complete code gives us the ability to move the player across the screen:
You can now move the block around the screen with the arrow keys.
<caption id=”attachment_589” align=”alignnone” width=”400”] pygame example. Move the block around the screenBuilding the player (snake)
The player controls a snake which has an initial length. This snake is always moving and changes the direction it moves when pressing an arrow key. To do so, update the player class:
The player controls a snake which has an initial length. This snake is always moving and changes the direction it moves when pressing an arrow key. To do so, update the player class:
and don’t forget to add a delay to the game loop.
This starts to act more as a snake, but it does not have a base length yet. We keep track of the snakes old positions and move the head of the snake. We also moved the drawing method into the snake. Copy the code and you will have a moving snake:
Result:
<caption id=”attachment_594” align=”alignnone” width=”592”] python snakeGame logic
The snake game has some rules:
The snake game has some rules:
- If the snake eats an apple, the apple moves to a new position.
- If the snake eats an apple, the snakes length grows.
- If a snake collapses with itself, game over.
We start by creating a new class that enables us to create apples:
For simplicity sake, we display the apples as green cubes. We have this basic code, but it does not do a lot more than moving the snake and showing the apple:
We have thus to add the game logic. To know if the snakes position matches the apples position, we have to do collision detection. This simply means veryfing that the cordinate of the snake are intersecting with the coordinates of the apple. We create a new method to do that:
It will return True if the coordinates (x1,y1) are intersection with (x2,y2) given its block size bsize. We call this method to determine if the snake collides with the apple. We need to check this for the entire snake and not only for the head, because we do not want the new position of the apple to be somewhere on the snake. We use the same isCollision method to determine if the snake collides with itself (= lose game).
Full source:
Conclusion:
![Python 3 Pygame Tutorial Python 3 Pygame Tutorial](https://i.ytimg.com/vi/msAV50WTef0/maxresdefault.jpg)
Python Game Tutorial
You learned how to create the game snake in Python along with concepts such as collision detection, image loading and event handling. Many things could be added to this little toy game but this serves as a very simple example. :-)