A while ago I found Pivot android game, and even though it is very simple, it is quiet good. Plus there are no ads.
So I decided that I try to make the same or similar game. For the programming language I decide to go with Python, and I am going to use Kivy framework, even though I have never used it before.
Plus, I was wondering how much I can do in 2 days.
Plan
Do a simple app in Kivy.
Make UI for Pivot game
Get the ball moving
Get the ball to collect the points
Simple app
I checked out Kivy’s documentation and they have exactly what I was looking for. Pong game tutorial was quick and simple but it had everything I needed. Well, everything to get me started.
Pivot game UI
For the UI, I need to show some borders around my window, some text labels and ball.
So let start with showing the actual game window. This is starting pivot.py file. After running this we should be seeing black window, which is good, it means that our environment is set up correctly.
To manage UI we will create pivot.kv file. In pivot.kv I will create some borders around the window. For that I will create rectangle on each side.
Then I want to have text label in center of window with some kind of instructions, and score label in corner.
So far we should have something like this.
Adding the ball
To add the ball I need to create new PivotBall class in pivot.py file which is extending Widget. Then I’ll add PivotBall to pivot.kvfile. I will also add id to items in pivot.kv file so I can use them from my Python code.
And pivot.py file.
Next thing I need to do is hide text label when I press spacebar and make ball moving. In order to do that I will need to bind key press with method in my Python code. To hide text I decided to just turn opacity to 0.
At this point I need to do different actions at different states of game. When game has just started, I want to see text label but I don’t want to see ball. Then when pressed spacebar, I want to hide text label and I want to see moving ball, so playing state.
To move ball in circle, I use Math.sin() and Math.cos() to calculate x,y coordinates, where I pass angle as argument. So to keep ball moving in circle I’ll just need to increase angle number, and if I want the ball to move in opposite direction, instead of adding to angle I subtract from it.
Here is pivot.py.
Now we should be able to see moving ball in circles and having ball responding to spacebar press.
Collecting points and touching borders
Up to this point, the ball is freely moving around. That’s nice, but little bit boring. So we will write method which will check if ball is touching any of the sides of the window. Then we should write method which will return ball to initial position.
Checking if ball touched the wall, should be in update() method so it is run each time the ball is moved.
Additional code for PivotBall class checking if the ball is touching border and also moving it back to center.
Calling of these methods in update().
You can see that in last bit of the code, we change game state to killed. That causes the ball to stop in the center of screen after it touches the borders. So now we need define what happens when player is killed.
This is the behavior I want for each state:
state started
show text label telling player to press spacebar
hide everything else
state playing
show ball and start moving it
hide everything else except score counter
state killed
show final score
hide everything else
There are two things we need to add, new text label with score which will be shown when player is killed and change actions when spacebar is pressed during different game states.
To add the text label, you can copy the code for already existing text label.
For the Python code, here is update() method where I hide and show different labels, depending on game state.
And here is on_keyboard_down() method.
Now it looks more like the actual game, but the very important part is still missing. And that it the target ball that we want to pick. For that I created new widget PivotTarget, which is very similar to our current ball.
Then we need to do two things, move the target randomly inside the window and check if we touched it. When our ball touches the target then we increase the score.
And got_point() is checking if coordinates of ball are colliding with coordinates of target.
Result
My Pivot game looks currently something like this. There is a window, there is a ball moving and score is being counted. So I think I reached my goal for this 2 day “sprint”.
For the next sprint I am planning to add moving squares as opponents, same as in actual Pivot app.
So far I am quiet happy with Kivy, it is easy enough if you have some experience with Python, but I will definitely need to have more look into it. Two days is just not enough and project is not that complicated.