Simple PONG-game tutorial for Beginner Coders

The place for any Hollywood tutorials

Re: Simple PONG-game tutorial for Beginner Coders

Postby Yasu » Thu Dec 04, 2014 1:30 am

I have just tested everything and it works :) It's not the prettiest Pong game for sure, but it's a really good beginners guide. I will make some changes in order for it to work better as an article.

Is it possible though to lower the screen resolution? Not everyone has such big screens I think.
Yasu
 
Posts: 17
Joined: Mon Jun 30, 2014 3:12 pm

Re: Simple PONG-game tutorial for Beginner Coders

Postby Yasu » Thu Dec 04, 2014 1:31 pm

By the way, I would also like to write your real name for this article.
Yasu
 
Posts: 17
Joined: Mon Jun 30, 2014 3:12 pm

Re: Simple PONG-game tutorial for Beginner Coders

Postby Bugala » Fri Dec 05, 2014 1:35 pm

Yeah, its easy to change the resolution. Resolution is defined in the first line.

Of course you also need to change some numbers to make it work right after that, but those are all at beginning again. You need to change things like the edge numbers, and the size of the sticks, since otherwise those sticks will be quite big, plus the player stick wont even show, since its current x is close to 1980 screen right edge, and if its changed to for example 800x600, then it is about one screen away from showing itself.

However, if you give me the resolution you wish to be used, as example 800x600, i can do that as well.

Basically it doesnt matter even if users dont have that HD resolution, since that first line defines so that the game (or actually - program) opens itself a window which is size of a whole screen.

After that it uses Hollywoods scaling feature to display itself on whatever screen it opened itself at. Meaning that if screen it opened itself is 320x256, then it will scale itself to fit all that stuff in that screen, by otherwords, by shrinking all the graphics.

In case it is opening itself on larger screen, like 3920 x 2160, then it would once again scale itself to fit the whole screen, in this case, by strecthing the graphics.

So regardless what the actual size of the users screen is, it will always look the same. Actually very much the same in this case, since i used only rectangles, hence you wouldnt really notice if some pixel is mixing due to shrinking, nor will any pixel looked like stretched in bigger resolution.

And that also means that there really wasnt any logical reason to using 1920 x 1080, since it doesnt give any real benefit to programs execution or looks. Only reason i used that is since HD is basically current standard, and since i just copy pasted that line from some otehr program of mine, and i most of time use HD resolution in my programs.

And my real name is Samuli Holopainen
Bugala
 
Posts: 573
Joined: Sun Feb 14, 2010 8:11 pm

Re: Simple PONG-game tutorial for Beginner Coders

Postby Yasu » Tue Dec 23, 2014 1:14 am

I'm almost finished with the translation. It has been a pain since sometimes I've had huge problem simply understanding the code, and therefore not been able to understand how to explain it.

But I'm stuck at one place:

Code: Select all
        Repeat
        timepassed = GetTimer(1)
        Until timepassed > 3
        ResetTimer(1)

        timepassed = timepassed / 1000


You claim later that this equals 1 second. But if timepassed = timepassed /1000 and 1000 is a second, and we have defined Until timespassed > 3, doesn't that mean that we wait until more than 3 seconds (4 seconds)? I don't see how this equals to 1 second.

Thankful for a quick answer from anyone.
Yasu
 
Posts: 17
Joined: Mon Jun 30, 2014 3:12 pm

Re: Simple PONG-game tutorial for Beginner Coders

Postby Yasu » Tue Dec 23, 2014 1:46 am

I'm sorry, but the more I try to read the text and understand this part, the less I get it. I understand that this has something to do with time, but I really don't understand what this part actually do or how it calculates time.
Yasu
 
Posts: 17
Joined: Mon Jun 30, 2014 3:12 pm

Re: Simple PONG-game tutorial for Beginner Coders

Postby Bugala » Tue Dec 23, 2014 9:24 am

Does it help if this line:
REPEAT - UNTIL loop is there constantly looping, until at least 4 time units have passed. "timepassed" variable saves the current amount of time passed since clock was started/reseted, and if it isnt at least 4, it keeps continuing to do so, until clock shows at least 4.

is changed into:
REPEAT - UNTIL loop is there constantly looping, until at least 4 time units have passed (notice, this is 4 time units, not seconds, and 1 time units is 1/1000th of a second). "timepassed" variable saves the current amount of time passed since clock was started/reseted, and if it isnt at least 4, it keeps continuing to do so, until clock shows at least 4.


This part of code I guess is the hardest part in the whole tutorial since it has most advanced stuff in it, and I am explaining it very shortly only leaving it in great part for the reader to understand it from the code itself. However, since it gave you so much trouble understanding it, I wonder if I should perhaps make extended version of that time using part where I explain the principles of the whole thing in more detail, hence making it also more understandable.

What do you think, should i rewrite that time using part to explain it in more detail?
Bugala
 
Posts: 573
Joined: Sun Feb 14, 2010 8:11 pm

Re: Simple PONG-game tutorial for Beginner Coders

Postby Yasu » Tue Dec 23, 2014 1:40 pm

I see! I simply thought that since you had defined 1 timepassed as 1000 timeunits, and 1000 timeunits is one second, then 4 timepassed must be 4 seconds long.

So this rather mean that since the REPEAT - LOOP wait for 4 time units, the program gets updated 250 times/second? And from what I understood, without StartTimer, then the program will simply work as fast as it can right? And with StartTimer it will base the program updates on time units? Is it then automatically based on seconds? Because you mentioned that "ballstartspeed = 1" means that it will update the balls movement 1 pixel per second. Or will this "repeat - timepassed" part regulate how often the program is updated? I mean, does this part affect how the program perceives time or just tell the program to update on a regular basis?

I think if I can understand this part, then I can do a sensible translation.

I don't think you need to rewrite this part, as I will most probably rewrite it myself. I have rewritten a lot in order to shorten it down, clarify things and try to make it more comprehensible for non-programmers. It's a very good guide and has given me some basic knowledge of programming.

Thanks for the quick replies!
Yasu
 
Posts: 17
Joined: Mon Jun 30, 2014 3:12 pm

Re: Simple PONG-game tutorial for Beginner Coders

Postby Bugala » Tue Dec 23, 2014 9:50 pm

Yes, you have pretty much figured it right.

However, two things i make clear still.

If you change the program to wait for longer than 4 time units (or less), it wont change the speed by which things move around on screen, it will only affect refresh rate.

What this part of program does, is to make sure that it have took at least that 1/250th of a secong, before it moves forward. Hence, if you have a slow machine, before it enters that loop and checks, there could have already have went 10 time units for example, in which case the program just continues right away forward, and then the refresh rate will be 1/100th of a second.


Reason why I am waiting for 4 time units to pass, is to make it run at same speed on each computer. For using no wait at all, or using 1-2 can cause problems in some cases.

First of all, if you dont wait at all, but simply save the amount of time units that have passed, it can easily happen (and probably would happen while executing this program too), that code is executed once through before even 1 timeunit have passed.

Hence what would constantly happen, would be that when it saves the amount of time passed (=timepassed), it would record 0, and after that it would reset the counter again, effectively making that timer woudl never get to reach even 1 time unit.

And if timepassed is always 0, then when it gets to calculations of for example ballmovement, it would be advancing the ball by speed of speed x 0, resulting in no movement at all, as if you multiply by 0, result is always 0.

To get round this 0 problem, you could simply wait for at least 1 time unit to pass. However, this isnt problem free either.

For suppose there are two computer. Another computer takes half a time unit to go through each loop, while other one takes 2/3rd of a time unit to go through.

If we then look for time to having passed at least 1 time unit, then when we compare these two computers, the other computer would continue program at point when exactly 1 time unit have passed, while the otehr computer would continue the program when 1 1/3rd (2/3rd + 2/3rd) of a time unit have passed.

hence this means that these two programs would actually be running at 33 percent difference in speed, since both would still be using the same "1" as their multiplier, instead of 1 and 1.33 as they should.

By waiting till 4, it removes part of this problem since even loops take different time in different computers, the difference is much less.

For example by waiting for 2, it could be that one computers cycle takes less than 2 and it waits till the 2 is up, while other computers cycle takes 2.5 time units, hence program recording it as 2 time units only, as 1 time units is the minimum measurement.

Even with 4 it can still happen that some machine takes 4.9 time units to go through, hence recording it as 4, and when compared for exact 4, still almost 25 percent difference, but at least it both makes it less likely, as well as the difference less than with waiting just 1 or 2 for example.


And the second thing.

Yes, you understood right, that without this timepassed system, it would work as fast as it can. And it is based upon seconds (pixel per second) since I am using this 1000 as the divider. It could use something different as well by changing the divider number to something else.

But the place where it affects is that for example, lets suppose i want ball to move 20 pixels per second.

Since 1 000 time units is 1 second, hence i keep checking how many time units have passed, as example,100 time units, which means 1/10th of a second.

Now i use this knowledge of 1/10th of a second (100 time units) when i move the ball.

As i know that i want ball to move 20 pixels per second, i will use this info following way:

movementamount = movementspeed * timepassed / second (in time units)
movementamount = 20 * 100 / 1000

when you multiply movementspeed (20) and time units passed (100) you get 2000.

If you divide this 2000 by 1 000 time units (1 second), then the end result is 2.

And 2 is exactly the amount that the ball shoudl move in 1/10th of a second, if it aims to be moving 20 pixels per second.

you can aslo try this that suppose 1 second have passed (1000 time units).

This would mean that movementamount would be 20 * 1000 / 1000 or by other words: 20 * 1, which is 20 pixels.


By using this timepassed variable (which stores the amount of time that have passed, which in fast computers would be 4, but in slower one could be something bigger) we can make sure that regardless of the time that it takes for the computer to go the cycle through, it will still be moving each item the same amount in same amount of time. That if it takes 1 minute for ball to reach one edge of screen, then even if machine is slow or fast, or by otherwords keeps going through the cycle fast or slow and keeps refreshing the screen fast or slow, it will still take that same 1 minute for the ball to mvoe from one edge to other, regardless if cycle speed/refresh rate / Frames per second is 20 or 100.


Did you understand the logic?
Bugala
 
Posts: 573
Joined: Sun Feb 14, 2010 8:11 pm

Re: Simple PONG-game tutorial for Beginner Coders

Postby Bugala » Tue Dec 23, 2014 9:59 pm

Oh, and one more thing.

I forgot that i used one more thing i didnt explain yet.

So that part of timepassed = timepassed / 1000.


The idea with this is that I am doing a little shortcut here.

Instead of having to use in each movement, as example ball movement in way of:

movement = speed * timepassed / second

i can simply use

movement = speed * timepassed

since in that timepassed = timepassed / 1000

I am in practice combining timepassed / second together, and hence i only need to use

speed * timepassed in the future.

But in reality that timepassed in that movement spot is not simply timepassed, but it is the result of timepassed / second. Hence saving me from putting that part each time there is some movement.

As example. suppsoe 100 time units have passed. this makes 1/10th of a second. We do 100 / 1000 and we get 0.1 as result.

Now we can use to ball for example:

ballspeed * timepassed
= 20 * 0.1

and result is 2, as it should.
Bugala
 
Posts: 573
Joined: Sun Feb 14, 2010 8:11 pm

Re: Simple PONG-game tutorial for Beginner Coders

Postby Bugala » Wed Dec 24, 2014 1:27 pm

came to my mind that perhaps it would be easier to understand if we do a little chane to the code:

change:
Code: Select all
timepassed = timepassed / 1000


into

Code: Select all
timemultiplier = timepassed / 1000



and similarly you need to change:

Code: Select all
ballx = ballx - (ballspeed * timepassed)
    bally = bally + (ballspeed * timepassed)
    bally = bally - (ballspeed * timepassed)
    computery = computery + (computerspeed * timepassed)
    computery = computery - (computerspeed * timepassed)


into:

Code: Select all
ballx = ballx - (ballspeed * timemultiplier)
    bally = bally + (ballspeed * timemultiplier)
    bally = bally - (ballspeed * timemultiplier)
    computery = computery + (computerspeed * timemultiplier)
    computery = computery - (computerspeed * timemultiplier)



I have originally simply used timepassed, but it might be hard for beginner to grasp the idea that first timepassed is the time units that have passed, and then suddenly it is changed into multiplier to be used at other parts of code, so perhaps better call the multiplier part with different name for beginner to understand that these work differently at those two different parts of code.
Bugala
 
Posts: 573
Joined: Sun Feb 14, 2010 8:11 pm

PreviousNext

Return to Tutorials

Who is online

Users browsing this forum: No registered users and 1 guest