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.
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.
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.
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
Post a Comment