A basic Ping Pong
Writing in QB64
This post will not be about how to write a really good ping pong, but how I wrote it.
Mine was about a mediocre strategy considering that I wrote the whole game in pen and paper without ones trying it on the computer.
I would prefer using QB64 as the language for writing the first game, as since the game will most probably be simple, QB64 is very easy to learn and code in.
This was my first program of more than say 50 lines, so the code is messy and bad and I don't expect anyone to read and understand anything, I myself can't sometimes. So this will only be helpful to those who want to write some kind of game, but don't know much about programming.
Overall Plan
So first let's forget about the language specifics ... what is the most basic way of writing a ping pong. Well, the first thing that comes to mind is that the ball will be , well, a circle, with a certain radius.
Secondly, the two players will be just rectangles on which the ball will bounce.
This is enough to create the game.
I represented the two bars as two variables, one keeping the upper co-ordinate and the other keeping the lower co-ordinate. First, imagine it as a line, making it a rectangle later will be very easy, I assure you.
The ball also has two variables, its center and its radius.
Also, the ball will have a 2D direction where, one will store its speed ( pixels per iteration) horizontally and vertically. Using signs will be easier than making direction flags for all the directions as then we can just add the speeds to the current co-ordinate and we will be done as if it is going left, speed will be positive and on right it will be negative and similarly for the horizontal direction.When it collides with something, like the bars or the upper walls, the speed will just be the negative of the previous speed. For example, if it hits the upper or lower wall, speedY = - speedY
For better gameplay( :-) ) you can make the speed be randomly incremented or decremented every collision.
This brings us to the next topic, how to do collision detection.This I would suggest that you find out yourself. Hint is that for the bars, use the upper and lower Y co-ordinates and the balls center co-ordinate and radius to find if a portion of the ball lies within the area of the bar and if it is sufficiently close, based on X co-ordinates.
Now just imagine the ceiling as a extra long bar.
If the ball hits the sidewalls( you can check this in the same way) then one of the sides lose points.
Now , it isn't interesting for two humans to play, so one of the sides should be AI controlled.
Now, I used a very simple algorithm(too simple to be even called an algorithm) : Just make the AI bar go in the direction of the ball, but with different speeds ( which are always lower than the balls speed) according to the level, so that the player finds it progressively harder to beat the machine.
Now according to how to actually put all this into practice, I personally used the sound command for controlling speed of the loop.If you want to use PCOPY and DISPLAY, you can find the examples in the other QB64 games that I wrote.
It can be built using very simple graphics.
What I did was I used all the variables explicitly, (because I did not know arrays at that time) but that can lead to bugs.
For reference, you could always see my code, but it is not that great.(All the code can be found in the post "The Whole Lot".
Comments
Post a Comment