Today I followed Coding With Russ‘s tutorial to make a fighting game in Python using Pygame. I just set up the game, create a window, draw the background image into the window, and create two instances of fighters and make it move left to right.
First we create a file called “main.py”. Next is setting up a game, creating a window with screen width is 1000 and screen height is 600. And set caption of the window is “Brawler”.
After you run the code, you will get a window like below.
And now we load the background image and create a function to draw it into the screen game. And call that function in while loop.
You will get a window with a background like the image below.
But this is not how the background we want. We need the background like the image below.
To fix this problem, we just create a variable that uses pygame.transform.scale to make it the background is fit with our window. Like the image below.
Next we make fighters appear on the window. Create a new file called “fighter.py”, and set Fighter class. In Fighter class, we have a draw fighter function by using pygame.draw.rect. It will draw two rectangles on the window. Next we import the Fighter class in the “main.py” file. And create two instances of fighters and draw them in the while loop.
The code above is in the “fighter.py” file. And the code below is in the “main.py” file.
Two fighters appear on the window.
We create a new function called move() in the “fighter.py”file. And call it in the while loop on the “main.py” file. After you run the code, fighters will move left to right.
But fighters are moving too fast. We want it to move smoother. To fix that, we just set a pygame.time.clock with FPS is 60 to make it move smoother.
You can see how it moves at 17:55 on the Coding With Russ’s tutorial below. Because I cannot make gif and post it on the blog to show you how it moves. I will try to make that next time.
No Responses