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

Mastering SQL for Data Science: Top SQL Interview Questions by Experience Level

Introduction: SQL (Structured Query Language) is a cornerstone of data manipulation and querying in data science. SQL technical rounds are designed to assess a candidate’s ability to work with databases, retrieve, and manipulate data efficiently. This guide provides a comprehensive list of SQL interview questions segmented by experience level—beginner, intermediate, and experienced. For each level, you'll find key questions designed to evaluate the candidate’s proficiency in SQL and their ability to solve data-related problems. The difficulty increases as the experience level rises, and the final section will guide you on how to prepare effectively for these rounds. Beginner (0-2 Years of Experience) At this stage, candidates are expected to know the basics of SQL, common commands, and elementary data manipulation. What is SQL? Explain its importance in data science. Hint: Think about querying, relational databases, and data manipulation. What is the difference between WHERE

What is Bort?

 Introduction: Bort, is the new and more optimized version of BERT; which came out this october from amazon science. I came to know about it today while parsing amazon science's news on facebook about bort. So Bort is the newest addition to the long list of great LM models with extra-ordinary achievements.  Why is Bort important? Bort, is a model of 5.5% effective and 16% total size of the original BERT model; and is 20x faster than BERT, while being able to surpass the BERT model in 20 out of 23 tasks; to quote the abstract of the paper,  ' it obtains performance improvements of between 0 . 3% and 31%, absolute, with respect to BERT-large, on multiple public natural language understanding (NLU) benchmarks. ' So what made this achievement possible? The main idea behind creation of Bort is to go beyond the shallow depth of weight pruning, connection deletion or merely factoring the NN into different matrix factorizations and thus distilling it. While methods like knowle

Spacy errors and their solutions

 Introduction: There are a bunch of errors in spacy, which never makes sense until you get to the depth of it. In this post, we will analyze the attribute error E046 and why it occurs. (1) AttributeError: [E046] Can't retrieve unregistered extension attribute 'tag_name'. Did you forget to call the set_extension method? Let's first understand what the error means on superficial level. There is a tag_name extension in your code. i.e. from a doc object, probably you are calling doc._.tag_name. But spacy suggests to you that probably you forgot to call the set_extension method. So what to do from here? The problem in hand is that your extension is not created where it should have been created. Now in general this means that your pipeline is incorrect at some level.  So how should you solve it? Look into the pipeline of your spacy language object. Chances are that the pipeline component which creates the extension is not included in the pipeline. To check the pipe eleme