Step by step guide to writing Tetris in Python with PyGame In this tutorial, we will write a simple Tetris using the PyGame library in Python. The algorithms inside are pretty simple but can be a little challenging for the beginners. We will not concentrate on PyGame mechanics too much, but rather focus on the game logic. If you are too lazy to read all the stuff, you may simply copy and paste the code in the end. Prerequisites
You may experience issues with installing PyGame or Python itself, but this is out of scope here. Please refer to StackOverflow for that :) I personally experienced a problem on Mac with having anything displayed on the screen, and installing some specific version of PyGame solved the problem: The Figure ClassWe start with the Figures class. Our goal is to store the figure types together with the rotations. We could, of course, rotate them using matrix rotation, but that can make it too complex. The main idea of figures representationSo, we simply have a list of lists of figures like that: class Figure: Where the main list contains figure types, and the inner lists contain their rotations. The numbers in each figure represent the positions in a 4x4 matrix where the figure is solid. For instance, the figure [1,5,9,13] represents a line. To better understand that, please refer to the picture above. As an exercise try to add some missing figures here, namely the “z” figures. The class Figure: where we randomly pick a type and a color. And we need to quickly be able to rotate and get the current rotation of a figure, for this we have these two simple methods: class Figure: The Tetris ClassWe first initialize the Game with some variables: class Tetris: where the state tells us if we are still playing a game or not. The We initialize the game with the following simple method: class Tetris: That creates a field with the size Creating a new figure and position it at coordinates (3,0) is simple: class Tetris:
The more interesting function is to check if the currently flying figure intersecting with something fixed on the field. This may happen when the figure is moving left, right, down, or rotating. class Tetris: It is pretty simple: we go and check each cell in the 4x4 matrix of the current Figure, whether it is out of game bounds and whether it is touching some busy game field. We check if Having this function, we can now check if we are allowed to move or rotate the Figure. If it moves down and intersects, then this means we have reached the bottom, so we need to “freeze” the figure on our field: class Tetris: After freezing, we have to check if there are some full horizontal lines that should be destroyed. Then we create a new Figure, and if it already intersects, then game over :) Checking the full lines is relatively simple and straightforward, but pay attention to the fact that destroying a line goes from the bottom to the top: class Tetris: Now, we are missing the moving methods: class Tetris: As you can see, the And in every method, we remember the last position, change the coordinates, and check if there is an intersection. If there is, we return to the previous state. PyGame and the Complete CodeWe are almost done! There is some simple logic left, which is the game loop and the PyGame stuff. So, let’s see at the complete code now: Try to copy and paste it into a And don’t forget to share it with your friends! Level Up CodingThanks for being a part of our community! Level Up is transforming tech recruiting. Find your perfect job at the best companies. |