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

Tinder bio generation with OpenAI GPT-3 API

Introduction: Recently I got access to OpenAI API beta. After a few simple experiments, I set on creating a simple test project. In this project, I will try to create good tinder bio for a specific person.  The abc of openai API playground: In the OpenAI API playground, you get a prompt, and then you can write instructions or specific text to trigger a response from the gpt-3 models. There are also a number of preset templates which loads a specific kind of prompt and let's you generate pre-prepared results. What are the models available? There are 4 models which are stable. These are: (1) curie (2) babbage (3) ada (4) da-vinci da-vinci is the strongest of them all and can perform all downstream tasks which other models can do. There are 2 other new models which openai introduced this year (2021) named da-vinci-instruct-beta and curie-instruct-beta. These instruction models are specifically built for taking in instructions. As OpenAI blog explains and also you will see in our

Can we write codes automatically with GPT-3?

 Introduction: OpenAI created and released the first versions of GPT-3 back in 2021 beginning. We wrote a few text generation articles that time and tested how to create tinder bio using GPT-3 . If you are interested to know more on what is GPT-3 or what is openai, how the server look, then read the tinder bio article. In this article, we will explore Code generation with OpenAI models.  It has been noted already in multiple blogs and exploration work, that GPT-3 can even solve leetcode problems. We will try to explore how good the OpenAI model can "code" and whether prompt tuning will improve or change those performances. Basic coding: We will try to see a few data structure coding performance by GPT-3. (a) Merge sort with python:  First with 200 words limit, it couldn't complete the Write sample code for merge sort in python.   def merge(arr, l, m, r):     n1 = m - l + 1     n2 = r- m       # create temp arrays     L = [0] * (n1)     R = [0] * (n

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