Saturday, June 21, 2014

32. Pong Game

The source code, for the Pong Game, is on the Kivy website. We will go over this game. The only modifications, here, refer to printouts.




In the Python program, we have some imports. The App class must always be imported as we use two of its methods. All our other classes will be based on the Widget class. Three Kivy property classes are used. The Vector class will be used to write the velocity equation. The Clock is needed for the constant updates to the game. There is one additional import that is not included in the tutorial at the Kivy website. This is the standard Python library time, where we import a function to write time values.




The first widget is PongPaddle. It has a NumericProperty, called score, with initial value of 0. Later, we will have two instances of this class, one for left player and one for the right player. This class has one function or method. This particular function will be called by the periodic update method. The function handles any ball collisions off paddles. Each time the magnitude of velocity increases. Also as the collision is further from paddle center, the ball bounces with even greater speed.




The PongBall class has three properties. Only the first two are absolutely necessary. The Reference List Property groups other properties so changes are automatically updated. We can use velocity in a vector equation and it will change velocity_x and velocity_y accordingly, and also vice versa. The only function of this class is a vector equation to update the position of the ball.




Next, there are three functions. For any property, an on_property function is called by Kivy whenever the value of a Property changes. First, a class variable called time_format is defined so we can have strings with hour, minute, and seconds. In the functions we can refer to it as a self variable, since Python searches the function and then looks up in the hierarchy. The only thing this function does is to print the magnitude of the velocity.




The next two functions added are called by Kivy whenever velocity in the x-direction or y-direction changes. We have a printout of which velocity has changed. The args tuple will have the velocity coordinates.




The root class is PongGame. It defines three ObjectProperties, and which will get their values in the kv file. This class has three functions. The first is serve_ball. This is always called to start a point, or at the beginning of game. This sets the ball initial velocity at 4 in the x-direction, as well as the initial position, in the center.




The update function is periodically called. It first calls the vector equation to move the ball. It then checks for bounces off either paddle by calling the bounce_ball function twice for the two paddles. Next, we check if there are any collisions off the top or bottom. If so, the velocity in y-direction changes sign, so it points back to the center.




Next, we have some logic to update scores. The score is updated for player 1 or player 2 if the ball has gone off the screen. If so, the Property is updated and a new point is started with serve_ball.




The function on touch move is called by Kivy whenever the mouse, on desktop, or touch, on mobile, is clicked, and then dragged. The dragging motion will update the y position of the paddle. We have to be sufficiently close to a particular paddle to change its position.




We have to override the App class's build method with a call to the root class. We also call the serve_ball function and set the Clock to periodically call the update function at a rate of 60 Hz. Finally, in the main code, we create the instance of app and call its run() function.




In the kv file, the first line is a regular comment and the second line is a directive that we are running a Kivy version of at least 1.0.9. The size and shape of the PongBall is specified as a circle.




The PongPaddle is a rectangular shape with a size of 25 by 200.




The three ObjectProperties values are set. The right-hand refers to ids of the widgets. The center vertical divider is drawn.




The Label writes the score of Player one. We have to use root so it refers to PongGame.




Likewise, this updates player2 score. Since score is a Property, the text is only updated when the number changes.




The PongBall and left PongPaddle instances are created. These ids had earlier been used in creating the ObjectProperties. The initial positions are set.




In the Right Paddle, we set the x, the lower-left corner of the rectangle to window width minus the Paddle width.




This is the first group of printouts. As we can see, the initial velocity is set at 4, due to ball_serve function.




In the next call, the velocity is increased by 10%. Its sign also changes indicating now it's going left. Note, we just ran the program and did not alter the position of any paddle. Because of the way score is updated, the velocity will increase so much that the ball will skip the paddle and be a score for one of the players, that is, all without any intervention on our part, other than running the program.




1 comment:

  1. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.

    Python Training in electronic city

    ReplyDelete