
-----------------------------------
Aange10
Sat Sep 03, 2011 5:10 pm

Help me comprehend some (SIMPLE) Pong coding
-----------------------------------
What is it you are trying to achieve?
One of my friends had made this super simple pong game, and gave me the source code to play around with. I'm trying to learn how to code in turing, and I know that this is one of the best ways to go about it. I've read a lot of the walkthroughs, but everything is easier, and frankly better hands on. So I was wondering if somebody could help me understand this SIMPLE pong coding.

So I'm asking, to whoever finds time to help me out, if he/she would go through this short coding and put % comments, telling me what the computer is "doing now".

If you don't have time, I understand. Just please don't flame.

What is the problem you are having?
I'm not comprehending all of the coding.


Describe what you have tried to solve this problem
I've tried going through and taking mental notes of what i do know, but I run into gaps that pretty much make any information after it "come from nowhere" (because I don't know what had just happened).


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)



View.Set ("offscreenonly")
var m, t, fontID, x, y, gx, gy, s, xdir, ydir, ip, fs, timeRunning : int

var ch : string (1)

var ps : string (24)

t := 0
m := 0
timeRunning := Time.Elapsed
x := 1
y := 1
gx := 295
gy := 1
xdir := 1
ydir := 1
s := 0
ip := 2
fs := 0

loop  %Main Loop
    if ip = 2 then
        fontID := Font.New ("Courier:14:bold, italic")
        Font.Draw ("Pong : A Lonely Existance", 190, 250, fontID, black)
        Font.Free (fontID)
        locate (13, 39)
        put "Controls:"
        locate (15, 10)
        put "Red Paddle Left / Right  -:- Left Arrow / Right Arrow"
        locate (16, 10)
        put "Pause                    -:- P"
        locate (17, 10)
        put "Quit                     -:- ESC"
        locate (24, 33)
        drawfillbox (100, 0, 500, 40, 0)
        if fs = 0 then
            fontID := Font.New ("fixedsys:10")
            Font.Draw ("Hold 

Please specify what version of Turing you are using
This coding works with 4.1.1 (newest version)

-----------------------------------
Insectoid
Sat Sep 03, 2011 5:26 pm

RE:Help me comprehend some (SIMPLE) Pong coding
-----------------------------------
I'm trying to learn how to code in turing, and I know that this is one of the best ways to go about it. 

I'm not a fan of this method of 'learning'. It's like saying taking apart your car is the best way to become a mechanic. 

There are several tutorials that explain how to create a game in Turing from scratch. There's also the Turing Walkthrough, which will teach you enough to start using the language. 

Nobody is gonna go through this code an explain it to you. It's not worth our time. If you really want to learn, put some effort in. Pasting code on a forum and asking us to explain it isn't effort.

-----------------------------------
Aange10
Sat Sep 03, 2011 5:41 pm

Re: Help me comprehend some (SIMPLE) Pong coding
-----------------------------------
Nobody is gonna go through this code an explain it to you. It's not worth our time. If you really want to learn, put some effort in. Pasting code on a forum and asking us to explain it isn't effort.
If you don't want to do it, then don't. But don't flame me. For example, telling me that I am not trying to "put some effort in" is very unproductive, and I consider that flaming. Because you are insulting me when you have no idea what I've been doing. Also, I said very nicely if you didn't want to take the time to do it, then that is fine. But telling me that asking somebody to explain it to me isn't "effort" is ignorance on your part. I'm using this stuff to learn. I'm not being lazy OR lacking effort.

Simply putting it, your teacher explains a worksheet to you before you do it. Or the paper has directions at the top before you start working. I am just asking for somebody kind enough to help me, and ask those who don't want to NOT to flame me.

I'm currently working on coding a game based off the same code this is using. Sadly I don't think it's worth my time to copy and paste and just change some words... I'm trying to take this code and pretty much re-write the game, in my own taste.

I've read the walkthrough, and I use it as a reference everytime I need help. I said that I've read the walkthroughs in my post. It's just hard to read text straight from the book, and use it in practice. Which is why I said that hands on is better.

-----------------------------------
Raknarg
Sat Sep 03, 2011 5:45 pm

RE:Help me comprehend some (SIMPLE) Pong coding
-----------------------------------
If your friend is going to give you this as source code, he should comment it to make it easier for others to read.

Also he should start using variable with a little more meaning. Eventually once you get into larger and more complx games, you cannot have a hoarde of random letter variables. Not to mention its a pain in the ass to read.

-----------------------------------
Insectoid
Sat Sep 03, 2011 6:29 pm

RE:Help me comprehend some (SIMPLE) Pong coding
-----------------------------------
Ah, I missed the line where you said you read some walkthroughs. 

However, that doesn't change my opinion. How is looking at someone else's code any more hands-on than reading a tutorial or textbook? They have examples with comments in them! I'd not be surprised if there was a specific Pong tutorial. Yes, I'm flaming. I admit it. But now I'm gonna help you out.

So, what is Pong? It's essentially two paddles, each of which moves up and down. Do you know how to take keyboard input and use it to make something move? If you don't, look up Input.KeyDown.

There's also a ball, which moves in many directions. For simplicity let's limit it to 4 (up right, down right, up left, down left). You'll want x and y direction variables, which are either 1 or -1. The ball moves at ball_speed*x_direction and ball_speed*y_direction (where ball_speed is the number of pixels the ball moves in the X or Y direction per frame). Changing the direction of the ball is as simple as multiplying the x_direction or y_direction by -1. For example, if your ball hits the roof, multiply the direction by -1 to change direction. If you don't know how to do basic rectangular collision (we can treat the ball as a rectangle) look it up. There's tons of tutorials.

As for score- maintain an integer variable for each player that represents the score. 

If you want sounds, you can just play them whenever say, a collision happens (by the time you even think about sounds the game should be functional).

If you want the ball to move in more than 4 directions based on the speed of the paddle, look up 360 degree movement (there's a tutorial dedicated to it). If, say, the 'up' key is pressed when the ball collides with a paddle, the ball gains Y velocity and loses X velocity (the formula itself is up to you). 

Oh, and you may run into issues with your ball or paddle 'sticking' to the walls. This is because the ball may be 5 pixels from the roof in one frame, and then it moves 10 pixels up so it's 5 pixels above the roof. If this happens the game will just keep flipping the y_direction variable so the ball will never really move. To remedy this, whenever the ball collides with the roof, set the y position to just under the roof (adapt for other walls). 

tl;dr- things you need to know:
    -Input.KeyDown
    -Collision detection
    -movement (basic or advanced. Up to you)
    -Making things happen at the same time (I didn't mention this earlier)
    -View.Update (I didn't mention this earlier either)
    
Thats it. That's all you need to know to build a functional game of Pong.

-----------------------------------
Zren
Sat Sep 03, 2011 6:36 pm

Re: Help me comprehend some (SIMPLE) Pong coding
-----------------------------------
These variables could be better.

ip = isPaused, but it really should be gameState if your using it as an integer instead of a boolean (as you use it for the main menu as well (values 2, 3, ... as well).

gx, gy = bottom left point of paddle. The size of the paddle are hardcoded constants (bad). Hardcoded constants make it hard to change sizes on the fly.

Right now its a 59x11 box. If we were to store then in paddleWidth and paddleHeight, then we could later on change the size of it easier (powerups etc).

x, y are the coordinates of the ball. You should specify it! Especially when you're dealing with more than one object with coordinates (which is nearly everytime).

xdir, ydir is the displacement in their respective planes each frame. When it hits a wall, exteremely simplified (and incorrect) collision detection is done.

Basically when you hit the left wall (x < 0) you reverse your direction in the x plane.

He screwed up since he used the range (-2, 2), so when it hit that wall:
        if x < 0 then 
            xdir := 1 
        end if 
Boom, the speed decreased.

Use:  xDir := -xDir as -(-n) = +n

t represents the frameCount. t is usually short for time, so in this case he could probably get away with it.

m = minutes
delay(5) is 5 milliseconds.
1000 / 5 = 200 framesPerSecond

Since t = frameCount ...
frameCount / framesPerSecond = seconds
t div 200 = 60 is one minute.

s is a score -> put "Times Ape-Shit'd : ", s 

fs is used to count frames. Specifically for the little blinking "hold space" message. It's one of the more attrocious variables here as your already counting the frames and could just do:

if frameCount % 3 = 0 then blink!

% is the modulus operator btw, it's to calculate the remainder in division. Useful for seeing if a number is even or odd (n % 2 = 0 -> even).

Ignore the fact he created 3 different arrays to capture keyboard states.

var chars : array char of boolean 
var chars5 : array char of boolean 
var chars6 : array char of boolean

Each section of block of code has it's own scope. At the end of that block of code, all local variables (those defined in the block of code) are forgotten).
Eg, This is valid code:



if false then
    var n := 0
elsif false then
    var n := 0
else
    var n := 0
end if

var n := 0


... Okay, this turned out longer than I thought.

-----------------------------------
Aange10
Sat Sep 03, 2011 6:51 pm

RE:Help me comprehend some (SIMPLE) Pong coding
-----------------------------------
Thank you guys both for the help! I'm going to go begin a new coding, and start it from scratch. Shouldn't take me too long. I'll post the results here.

and
*ALSO* I'm very confused on how he made the "hold space" instead of "press space".

-----------------------------------
Zren
Sat Sep 03, 2011 7:09 pm

RE:Help me comprehend some (SIMPLE) Pong coding
-----------------------------------
I should probably mention the right ways to do things he did badly too... Mainly because there's the chance you'll try to copy this guy instead of thinking for yourself or searching around as you're probably ignorant to realizing there's better ways.

To calculate your timePlayed:

var timeAtStart := Time.Elapsed
loop
    % The difference between now an then in milliseconds
    % Eg: Started at 7Pm, it's now 8Pm, it's been 1 hour.
    var timeNow := Time.Elapsed
    var timeSinceThen := timeNow - timeAtStart
    put timeSinceThen
end loop


I mentioned you could use the same variable name between different blocks of code (so long as they're locally declared variables), but you could just get array with one variable to begin with. I'm talking about the boolean array for keyboard input. The only reason you'd have two arrays is if you wanted to store what you pressed the last frame. Which is particularly useful if you wanted to find out if a key was released or pressed down from the last frame.

The "hold" thing works because he drew the text, then delayed for half a second before checking the keyboard input and reacting to it. If you released before the delay was up, it wouldn't register. It works, but only when nothing else is being drawn/calculated.

-----------------------------------
Aange10
Sat Sep 03, 2011 9:54 pm

Re: Help me comprehend some (SIMPLE) Pong coding
-----------------------------------
Done! :) ... Thankyou guys for helping me. I really appreciate it. It helps a lot to learn this stuff, and i'm glad you guys took time out of your day for me!(:
..
And to prove i'm not just effortlessly sitting around, I've done my best at programming a paddle ball game (I call that pong, sorry) and keeping it nice and neat.

PLEASE critique!

View.Set("Offscreen, graphics:maxx;maxxy")
var mainmenu : boolean % Using this varriable to tell wether or not to pull up the main menu.
var fontTNR : int % A to allow me to make a font style into a variable
var mainmenuexit : string % The varriable i am using to exit the main meny/ skip past it
var oval_x1,oval_y1,oval_xr1,oval_yr1 : int % I made the drawoval syntax as variables
var oval_speed : int %This is to set my oval's speed
var oval_directionx, oval_directiony : int % To set my ovals trajectory direction
var box1_x1, box1_x2, box1_y1, box1_y2 : int
var chars : array char of boolean % Not sure what this is, just know you have to have it to use keys
var continue : string % Get the y/n asking if the player would like to continue
var score : int % Keeps track of the score
oval_x1 := 70      % the x cord for the ball
oval_y1 := 70      % the y cord for the ball
oval_xr1 := 10     % the radius x for the ball
oval_yr1 := 10     % the radius y for the ball
oval_speed := 2    % the speed of the ball pfp (Pixles per frame ;) )
oval_directionx := 1 % Direction of the x cord of the oval (1 = right, -1 = left)
oval_directiony := 1  % direction of the y cord of the oval ( 1 = up, -1 = down)
box1_x1 := 85 % Box x1 cord
box1_x2 := 200 % box x2 cord
box1_y1 := 20 % box y1 cord
box1_y2 := 40 % box y2 cord
mainmenu := true %a varriable to control the main menu
continue := "y" %a varriable to tell wether or not to continue
score := 0
loop % Main loop
    cls
        loop % main menu debugging loop
            if mainmenu = true then
                fontTNR := Font.New ("TimeNewRoman:14")
                Font.Draw ("Game O' Paddle Ball!", maxx div 2, maxy div 2, fontTNR, green)
                delay (1000)
                fontTNR := Font.New ("Courier:8")
                Font.Draw ("Use AD or Arrow Keys to move! Press b to begin!", maxx div 2 - 100, maxy div 2 - 20, fontTNR, red)
                score := 0 % Just to prevent people from spamming the debug system to gain points
                get mainmenuexit % It asks the user for input
            else % If mainmenu is not true then it exits this loop, and never brings up the main menu!
                exit
            end if
            if mainmenuexit = "b" then %When user inputs b, it changes mainmenu to false so that the loop will skip the main menu if statement
                mainmenu := false
            else
                cls
                put "Invalid key."
                delay (2000)
                exit
            end if
        end loop % Ends the main menu de bugging loop
    cls
    var timestart := Time.Elapsed % So I can measure the time elapsed from this point
    put "Time elapsed:", timestart / 1000 % How much time has elapsed since exiting the menu
    if oval_y1 >= 400 then %if y cord of oval goes >=400 then it inverses the direction of the y
        oval_directiony := oval_directiony * -1
    end if
    if oval_x1 >= 630 then %if the x cord of the oval goes >= 630 then it inverses the direction of the x
        oval_directionx := oval_directionx * -1
    end if
    if oval_x1 = 630 - 115 or box1_x2 >= 630 then %Sets the far x bounds for the paddle
            box1_x1 := box1_x1 - 5
            box1_x2 := box1_x2 - 5
        end if
        if box1_x1  maxx
    Draw.Line(x, 0, x, maxy, black)
    x += Rand.Int (5, 20)
end loop


-----------------------------------
Aange10
Tue Sep 13, 2011 8:05 pm

RE:Help me comprehend some (SIMPLE) Pong coding
-----------------------------------
I'm trying to decipher this (so I can use it in my own programs) and I'm tracing the execution to see how it is working. But what it is doing is when it hits the right edge it ends the x for and adds 1 to the y for, and repeats. ... I don't know what is telling it to do that! I think it has to do with


for x : 0 .. maxx by cellW 

but i don't know what

maxx by cellW

means. Could you please explain the "by cellW"? ... also what is making the y jump up by 10 and not 1? 


Thanks a ton for the code, it's a ton of help!(:

EDIT: I just realized that the "by cellW" is just saying to move it by increments of CellW! xD I'm very happy to have figure that out.

However I'm a bit confused with 

var cell_Width := 40
var cell_Height := 10
var w := cell_Width - 1
var h := cell_Height - 1

in it's relation to

Draw.FillBox (startX + x, startY + y, startX + x + w, startY + y + h, Rand.Int (0, maxcolor))

...
What is the point of making it 1 less than cell_width?

-----------------------------------
Aange10
Tue Sep 13, 2011 8:26 pm

RE:Help me comprehend some (SIMPLE) Pong coding
-----------------------------------
Also, I hate to double post.. But this is my deciphered version of the code. If there is any concept i missed/ incorrectly analyzed, please tell me!(: Thanks

var startX := 0 % Bottom left
var startY := maxy div 2 % Mid 
var cell_Width := 40 % width
var cell_Height := 10 % height
var w := cell_Width - 1 
var h := cell_Height - 1 
for y : 0 .. 100 by cell_Height % Goes by cell_height (AKA 10)
    for x : 0 .. maxx by cell_Width % Goes by cell width (AKA 40)
                locatexy (maxx * 0, maxy)
        colorback(0) put "For y :", y, " For x :",x
        Draw.FillBox (startX + x, startY + y, startX + x + w, startY + y + h, Rand.Int (0, maxcolor))
        delay (100)
    end for
end for
%startX + x is adding the box number (because x is going to make box 1
% = to 40, box 2 = 80, etc.) to the start x. So if it's on box 2 it will
% go 80 away from the start point.

%startY + y is the same idea; everytime the x reaches maxx the y is
%increased by 10 (from the first for) which is making the seperate
%rows.

%startX + x + w is doing the same as StartX + x (finding the position)
%and then adding the a width to it (so that the points are drawn apart)
% StartX + x + w is StartX + x + 39 

-----------------------------------
Zren
Tue Sep 13, 2011 10:04 pm

RE:Help me comprehend some (SIMPLE) Pong coding
-----------------------------------
We don't want the boxes to overlap. Drawing (0 .. 40) and then (40 .. 80) would have the second box draw over (a small) bit of the previous box.

Also:

The range of numbers 0 .. 39, or 40 .. 79 is exactly 40, or the target width we desire.

0 