Skip to main content

Pygame tutorial

Before I introduce:


I have a long desire to develop some games I can play. And recently, I have been able to learn the first few steps of that. I have created a prototype of the famous and one of the most ancient games, pong. My version of this game currently do not contain score boards, or starting screen. The game somehow looks like this:
Here the green dashes are the players, the name of the game is two player pong, and the white square is the box. I will write a thorough tutorial on the code, which is on my github, about how to develop an easy game like this. You can check my github page for the game here
Hope to see you soon, on the hands on tutorial session to be uploaded here on this blog.

Introduction:

First things first. I have developed the game using pygame in a jupyter notebook. So, first consider the imports as below:
I have used the template created by chris of Kidscancode. However, there are multiple changes. In this case, I have imported pygame and random module. 
It is good to define the global constants like this in the top portion of the code. This will help you to avoid any kind of mishap due to scopes and other reasons. Here, I have defined the width, height and fps; and 4 of the main colors. 
If you do not know fps; fps is the frequency at which the screen updates during a game or a animation. In general fps are set to 30,60 or 120. fps too fast may lead to lag, while if fps is slower, then the game may turn boring. This is an important concept to keep in mind while developing any game.
This is not a important fact; but when the pygame module is imported correctly, this message comes up.
First I will describe some of the important factors of the game template. Consider this following block. I will describe the important factors in the game template. First look at the code block below:

First we initialize the pygame and a module called mixer from pygame. pygame.init() initializes the library. In other hand, mixer is a library, which helps to run audio in the game or any pygame application. As most of the games include audio, you should also initialize the mixer. Now, I will give you some "gyan" or par say "knowledge". 

Basic concepts:

A game will have three basic actions, which will run mainly in programs. One is update, draw or render, and the most important one is accepting the inputs from the player. If you have to write a game, you will have to write
(1) accepting input 
(2) updating code blocks
(3) draw all the game objects properly in the updated condition
(4) define all the game objects, properties of the objects as their methods
(5) define how the objects will interact, by defining corresponding methods

Here, a important fact is that, drawing all the objects in the screen can take really long time. These incidents happen each looping of the game. That's why, the general method used is first the drawing is done, then a Flip is performed. This is like, first the drawing is done in the other side of the screen, and after completing the flip, using flip function, this screen is flipped. This flipping updates all the objects in the screen. 
In the above codeblock, this is done by all_sprites.draw(screen) where screen is where the game will be shown. Also, pygame.display.flip() is used to do the flipping I discussed above. Now, I will discuss another concept, called sprite.

Sprite: 

In case of a game, you have to introduce a lot many objects, like the players, weapons etc. In general this is called Sprite. This is purely a object oriented programming work. First things first. 
The most general object in this case is pygame.sprite.Sprite. This is just like in general object oriented programming, a parent class called object is used. Also, as pygame.sprite.Sprite is not actually the most general class, for the definition method, we have to first initiate the initiate method from the pygame.sprite.Sprite program. In a general sprite construction, the code will look something like below:
Now, I will describe a bunch of used codes from pygame. Clearly, the surface attribute helps to create a surface in the pygame window. The surface formed in this way, is a rectangle. Also clearly, the fill command is used to fill the object in a color.
You may think that why the set_colorkey is there although the color is already set? The answer is that, when a object is formed, and a rectangle is formed around it. We generally get that rectangle using get_rect(). But the problem is that, when the background gets a color which is not black as the rectangle gets; it clearly shows that a rectangle is running around with the object it is assigned with. That's why, it is wise to set the color of the background as black, i.e. in pygame, as invisible. This makes the rectangle invisible. This makes the actions smoother.

For each sprite, you have to construct a update function. The update function will be used to update the object in each loop of the game; therefore you have to write all the interactions of your object with time and the basic frame in the update.
One other concept in gaming is collision.
I will discuss about collision next time. 

Collision:

In a gaming environment, collision is one of the most important concepts. Let us assume you are creating a shooting game. Now you must have a event that after striking with a bullet, your players as well as his/her opponent will lose life or get vanished. So, each time a player object collides with a bullet, there needs a event to be assessed. There comes the collision event.
Now, your collisions can be largely two types. One with the gaming environment and different constructs of the game, like the walls , mazes, ground, roofs etc. The other can be with the objects of the game like bullets, stones whatever is moving, or with the players of the objects.
Now we generally handle these two types of collisions differently.
Generally, the gravity, collisions with constant non moving staffs of the games are incorporated in the class constructs of the sprite objects. While the other collisions are more written down as a collision event, given events and others.

For understanding these staffs more using the code, now please refer to my github code at https://github.com/shyamcody/pong-game. I will create more games and then provide update about them later on.




Comments

Popular posts from this blog

20 Must-Know Math Puzzles for Data Science Interviews: Test Your Problem-Solving Skills

Introduction:   When preparing for a data science interview, brushing up on your coding and statistical knowledge is crucial—but math puzzles also play a significant role. Many interviewers use puzzles to assess how candidates approach complex problems, test their logical reasoning, and gauge their problem-solving efficiency. These puzzles are often designed to test not only your knowledge of math but also your ability to think critically and creatively. Here, we've compiled 20 challenging yet exciting math puzzles to help you prepare for data science interviews. We’ll walk you through each puzzle, followed by an explanation of the solution. 1. The Missing Dollar Puzzle Puzzle: Three friends check into a hotel room that costs $30. They each contribute $10. Later, the hotel realizes there was an error and the room actually costs $25. The hotel gives $5 back to the bellboy to return to the friends, but the bellboy, being dishonest, pockets $2 and gives $1 back to each friend. No...

GAM model : PyGAM package details Analysis and possible issue resolving

Introduction:                  picture credit to peter laurinec. I have been studying about PyGAM package for last couple of days. Now, I am planning to thoroughly analyze the code of PyGAM package with necessary description of GAM model and sources whenever necessary. This is going to be a long post and very much technical in nature. Pre-requisites: For understanding the coding part of PyGAM package, first you have to learn what is a GAM model. GAM stands for generalized additive model, i.e. it is a type of statistical modeling where a target variable Y is roughly represented by additive combination of set of different functions. In formula it can be written as: g(E[Y]) = f 1 (x 1 ) + f 2 (x 2 ) + f 3 (x 3 ,x 4 )+...etc where g is called a link function and f are different types of functions. In technical terms, in GAM model, theoretically expectation of the link transformed target variable is assume...

Pyarabic: python package for Arabic language

 Introduction:  In languages which are non-english and non-european as well, NLP work has progressed slowly in the last few decades because of the lesser number of scholars working on them as well as a lack of global interest in them. But now the time has changed and people from all over the world are collaborating on these lesser explored libraries and they are building resources for working on these languages with the same ease with that of english.  Pyarabic is a package created from such a similar effort which deals with the intricate details of the arabic language and helps processing all kinds of arabic texts. While trying to learn it, being from a non-arab background, I couldn't read lots of parts of the main readthedocs site and had to work my around it. So in this blog post, I will summarize my learnings in english language, so that you can learn it and use the package with much more ease than me. [Credit where credit is due: this article heavily uses the ac...