At last I was able to finished this “Pong Game” – one of the hardest mini-projects of my Python Programming course. For other mini projects I built please check my github account, though I haven’t added my last 2 projects.
We are now on our Week 5 of the course and it’s getting really really harder! Not the the programming itself but the mix of Physics and Calculus! I mean I forgotten these stuff already and studied these like 5-6 years ago! So I have to review the formula and theorems, and the 3-Dimensional Theories again so I can fully understand the Pong concept and can build this Pong Game. I know! A very easy concept game but very hard to build and requires lots of Math and stuff! I don’t hate Math but if your brain is already full of Website Development, Database Manipulation and some IT stuff that you need to learn in order to survive in this industry and you yes I forgot about almost all the stuff I learned in College!
For this project, it introduced us the list functionality, keyboard controls and the Math and Physics specifically in Motions, Collision and Reflection.
Without further ado, here’s the program – http://www.codeskulptor.org/#user38_KhAYOChSIbDGkgx_0.py will upload this on my github soon that will work on machine that has Python installed
Here’s the summary:
– First is the initialisation: width is 600 pixels, Height is 400 pixels, Ball Radius is 20 px, Two Paddles are 8 x 80 pixels, ball velocity which is 0,0 (x,y), accelerator is 5, and Ball position 600/2, 400/2 (x,y) should be in the middle.
– The game controllers are: Up and Down for Right Players and W and S for Left players.
– Create a frame or the canvas of the pong which is by default the background color is black
– Then the Draw Handler or the content inside the canvas. On this program: We draw 3 lines – 1 middle and 2 gutters(left and right), 2 Paddles for the 2 players, circle as the ball and the 2 scores for the 2 players.
– Then the Event Handlers. we have 3 event handlers here first the Restart button which calls the new game function, Then the keydown event and they keyup event.
– Then to start the program, it calls the New_game() function. In the new game function it initialise the ball and paddles to the default position then call the spawn_ball() function.
– Spawn_ball() function will call which direction the ball will throw. By default the Right player is the first one. On this function we will assign data to the ball velocity. If right player, then (+x,-y) else if left player then velocity is (-x,-y). we use random numbers on this case.
– The keydown function, that decide which direction the paddles will move. The statements are: if key is ‘UP’ the paddle(right) velocity = paddle(right) velocity – accelerator and if key is ‘w’ the paddle(left) velocity = paddle(left) velocity – accelerator while if key is ‘Down’ then paddle(right) velocity = paddle(right) velocity + accelerator and paddle(left) velocity = paddle(left) velocity + accelerator.
– For the keyup function it will be opposite of the keydown statements.
– the draw functionality. Here’s all the actions takes place. aside from adding the circles and lines, Here also we manipulate the paddles, the ball and the scores of each players.
First: In updating the paddle vertical position and limit it only on the height of the screen.
Second: The Collision.Code. This involves what velocity of the ball when it hits the paddles. Please check the formula. This is the hardest part.
Third: When the ball hits the top and bottom wall it should bounce.
and that’s it! Our little pong game. Thanks to Simplegui plugin and codeskulptor editor, everything becomes easier 😉 And ofcourse Coursera- best online tutorial ever!