Basic 2-D Game in Python(part-4)

Asish Raz
DataDrivenInvestor
Published in
4 min readOct 22, 2019

--

This is the fourth part of the article, where we will drop the enemy from the top to bottom.

Before going further, if you want to see the original video of this game, please click here. Keith Galli did a wonderful job, do visit his channel, in case you are interested in learning to code through the game.

To drop the enemy from top to bottom, you need to understand simple math here. The enemy block will move from top to bottom, which means it’s y-coordinates are getting increased and it will move till the height of the screen. We need to update only y-coordinates, which is ‘enemy_pos[1]’.

enemy_dropping from top

Here, we will check whether y-coordinates are in the range of game screen and if yes, we are moving its position to 20px units towards down.
And in case, if the block is going beyond the screen, we are updating its values again to zero. So, it will start again from zero.
Once you run this code, you will see something like this:

enemy coming down

(I can’t capture the rapid movement of enemy going from top to down)

The speed of the enemy block is super fast. We can’t win against it. We have to make it a little slow, to see and save our block. For that, we have to decrease the frames per second and for that, we have to use one module of pygame, which is responsible for monitoring the time, ‘pygame.time’.

Then we will create an object for tracking time. For this, we have to use a method: ‘pygame.time.Clock()

>> clock = pygame.time.Clock()

Here, ‘clock’ is an object for tracking time and now we have to update the clock with frame-rate. For that, we need to use a method responsible for updating the time: ‘pygame.time.Clock.tick(framerate)

This method is useful when you want to slow down some functionality in the game. Here, we want to slow down the speed of the enemy block, for that, we will assign some frame-rate and according to that, the block will appear. This method should be called once per frame.

>> clock.tick(30)

The program will never run at more than 30 frames per second.

Write the above code and execute it, you will notice the difference in speed. If you have any doubt or problems, please do let me know.

If you notice, the block is coming from one position only, but we want the block from any random position inside the screen.

What exactly, we have to update, to see the blocks dropping from random positions?

The answer is too simple. We just need to update the x-coordinates of the block, once the block is off the screen. That’s it.

enemy dropping from top

Here, if you will see, whenever the enemy is going beyond the screen(downward direction), we are updating the value of enemy_pos to 1. But we are not mentioning the random x-coordinates. So we have to update x-coordinates too. (Think, what we have to write, I already gave you a hint)

Just one single line and that is:

Every time, the enemy will go off the screen, the x-coordinates will generate some random position for the block. (I told you, it was really simple ;) )

Now we came to the end of the fourth part, till this point we drop the enemy from the top and we were able to slow down the speed of the enemy.

Please click here for the fifth part. In case you still not read the third part, click here. And if you found this article interesting, please give a clap and share it with your friends.

--

--

entrepreneur, moody, moody writer, moody singer, traveller, hangout lover