Computer Science Canada

Help me comprehend some (SIMPLE) Pong coding

Author:  Aange10 [ Sat Sep 03, 2011 5:10 pm ]
Post subject:  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)

Turing:


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 [Space] To Start", 255, 25, fontID, black)
            Font.Free (fontID)
        end if
        fs := fs + 1
        if fs = 2 then
            fs := 0
        end if
        delay (450)
        var chars6 : array char of boolean
        Input.KeyDown (chars6)
        if chars6 (' ') then
            ip := 0
            drawfillbox (10, 10, 500, 390, 0)
        end if
    end if
    if ip = 1 then
        locate (11, 25)
        put "Game Paused Press [Space] to continue"
        if ip = 1 then
            locate (12, 25)
            locate (13, 25)
            put "Times Ape-Shit'd : ", s
            locate (14, 25)
            put "Time Elapsed : ", m, ":", t div 200
            var chars5 : array char of boolean
            Input.KeyDown (chars5)
            if chars5 (' ') then
                ip := 0
                drawfillbox (10, 10, 500, 390, 0)
            end if
        end if
    end if
    if ip = 0 then
        drawfilloval (x, y, 10, 10, 0)
        drawoval (x, y, 10, 10, 0)
        drawfillbox (gx, gy, gx + 58, gy + 10, 0)
        x := x + xdir
        y := y + ydir
        if x >= 640 then
            xdir := -1
        end if
        if y < 0 then
            y := 200
            x := 300
            randint (xdir, -2, 2)
            randint (ydir, -2, 2)
            if ydir = 0 then
                ydir := -1
            end if
            if xdir = 0 then
                xdir := -1
            end if
            s := s + 1
        end if
        if y > 410 then
            ydir := -1
        end if
        if x < 0 then
            xdir := 1
        end if
        if y < 10 then
            if gx - 5 < x then
                if gx + 5 + 60 > x then
                    ydir := 1
                    sound (550, 100)
                    sound (1, 1)
                end if
            end if
        end if
       
        var chars : array char of boolean
        Input.KeyDown (chars)
        if chars (KEY_LEFT_ARROW) then
            if gx > 20 then
                gx := gx - 3
            end if
        end if
        if chars (KEY_RIGHT_ARROW) then
            if gx < 600 - 20 then
                gx := gx + 3
            end if
        end if
        if chars ('p') then
            ip := 1
        end if

        drawfilloval (x, y, 10, 10, 14)
        drawoval (x, y, 10, 10, 8)
        drawfillbox (gx, gy, gx + 58, gy + 10, 12)
        drawbox (gx, gy, gx + 58, gy + 10, 255)
        if chars (KEY_ESC) then
            x := 1
            y := 1
            gx := 295
            gy := 1
            xdir := 1
            ydir := 1
            s := 0
            ip := 2
            fs := 0
            drawfillbox (0, 0, 700, 400, 0)
        end if
        delay (5)
        t := t + 1
        if t div 200 = 60 then
            m := m + 1
            t := 0
        end if
    end if
    View.Update
end loop




Please specify what version of Turing you are using
This coding works with 4.1.1 (newest version)

Author:  Insectoid [ Sat Sep 03, 2011 5:26 pm ]
Post subject:  RE:Help me comprehend some (SIMPLE) Pong coding

Quote:
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.

Author:  Aange10 [ Sat Sep 03, 2011 5:41 pm ]
Post subject:  Re: Help me comprehend some (SIMPLE) Pong coding

Quote:
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.

Author:  Raknarg [ Sat Sep 03, 2011 5:45 pm ]
Post subject:  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.

Author:  Insectoid [ Sat Sep 03, 2011 6:29 pm ]
Post subject:  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.

Author:  Zren [ Sat Sep 03, 2011 6:36 pm ]
Post subject:  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:

Turing:


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.

Author:  Aange10 [ Sat Sep 03, 2011 6:51 pm ]
Post subject:  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".

Author:  Zren [ Sat Sep 03, 2011 7:09 pm ]
Post subject:  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:
Turing:

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.

Author:  Aange10 [ Sat Sep 03, 2011 9:54 pm ]
Post subject:  Re: Help me comprehend some (SIMPLE) Pong coding

Done! Smile ... 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!
Turing:

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 <= 0 then %  x cord direction inverse
        oval_directionx := oval_directionx * -1
    end if
        loop % Loop to de bug the continue system
            if oval_y1 <= 20 then %  if y cord hits <= 20 it asks if you'd like to continue (reseting score)
                cls
                put "Would you like to continue? y/n: " ..
                get continue   % if you'd like to continue..
            else
                exit
            end if
            if continue = "y" or continue = "n" then
                cls % Clears the screen after you type in a valid answer
                oval_x1 := 328
                oval_y1 := 228
                oval_directionx := 1
                oval_directiony := 1 % Reset the direction & placement of the ball
                score := 0 % Resets the score
                exit
            else
                put "Invalid answer."
                delay (2000)
                cls
            end if
        end loop % End the debugging loop
    if continue = "n" then
        put "Good Bye!" %Says goodbye and closes if you chose not to keep going
        delay (2000)
        break
    end if
    oval_x1 += (oval_directionx * oval_speed) % adds the direction *speed to the x cord of the ball
    oval_y1 += (oval_directiony * oval_speed) % " but with the y cord
    drawfilloval (oval_x1,oval_y1,oval_xr1,oval_yr1,yellow) %draw the ball
        Input.KeyDown (chars)
            if chars (KEY_RIGHT_ARROW) then %Right key move the paddle to the right
                box1_x1 := box1_x1 + 5
                box1_x2 := box1_x2 + 5
            end if
            if chars ('d') then % " but d
                box1_x1 := box1_x1 + 5
                box1_x2 := box1_x2 + 5
            end if
            if chars (KEY_LEFT_ARROW) then % left key move paddle to the left
                box1_x2 := box1_x2 - 5
                box1_x1 := box1_x1 - 5
            end if
            if chars ('a') then % " but a
                box1_x2 := box1_x2 - 5
                box1_x1 := box1_x1 - 5
            end if
        if box1_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 <= 0 or box1_x2 <= 15 + 115 then %Sets the near x bounds for the paddle
            box1_x1 := box1_x1 + 5
            box1_x2 := box1_x2 + 5
        end if
    drawfillbox (box1_x1, box1_y1, box1_x2, box1_y2, red) %Draw the changes to the box
        if oval_x1 <= box1_x2 & oval_y1 <= box1_y2 & oval_x1 >= box1_x1 & oval_y1 >= box1_y1 then %If collision occurs, the ball moves in the oposite direction
            oval_directiony := oval_directiony * -1
            score := score + 1
        end if
    put "Score:", score
    View.Update % Supposed to fix screen flickering
    delay (10)
end loop


If you guys would.. I'd like to know 2 things.

1) How can i make my timer only tell me the integer the second is in, not the real number? (No decimals)
2) Can you help me fix my ball disappearing at the top (Because the put lines are coloring the yellow white)?

Also if you wanted to help me with an annoyance I have found...
My "Main Menu Debugging System" makes my menu tell you that you entered an incorrect key if you choose a key other than "b". When it does this all it does is put a statement, and then exits the loop and continues on through the main loop. (Which when you get back to go through it a second time, mainmenu will still be set to true since you never hit "b", making you go back through the main menu. Which is how the debugging works.) However, the "annoyance" is that it flickers the pong ball/paddle screen before it takes you back. Can you think of a way to prevent this?

Author:  Insectoid [ Sat Sep 03, 2011 10:39 pm ]
Post subject:  RE:Help me comprehend some (SIMPLE) Pong coding

1. round() is your friend.
2. colorback() will change the color of those lines. Don't draw a box around the screen. Just use colorback().

Annoyance: Look at your code.
code:

else
    cls
    put "Invalid key."
    delay (2000)
    exit
end if


Remember that you're in offscreenonly mode.

Author:  Aange10 [ Sat Sep 03, 2011 11:17 pm ]
Post subject:  RE:Help me comprehend some (SIMPLE) Pong coding

Thankyou for the help! It all works! Anything that I should improve on/ add in?

Author:  Aange10 [ Mon Sep 05, 2011 1:05 am ]
Post subject:  Re: Help me comprehend some (SIMPLE) Pong coding

At this section in my coding i'd like to make my time reset. How would I go about doing that? I've tried a couple different ways, but none of them worked. Some steering in the right direction would be greatly appreciated! Smile

This is the section i need to reset the time at.
Turing:

if continue = "y" or continue = "n" then
                cls % Clears the screen after you type in a valid answer
                oval_x1 := 328
                oval_y1 := 228
                oval_directionx := 1
                oval_directiony := 1 % Reset the direction & placement of the ball
                score := 0 % Resets the score
                exit
            else


This is the full coding.
Turing:

View.Set("offscreenonly, 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
var time1 : real % Trying to reset timer...
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
      Input.KeyDown (chars)
        loop % main menu debugging loop
            if mainmenu = true then
            View.Set("nooffscreenonly, graphics:maxx;maxxy")
                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
            View.Set("offscreenonly, graphics:maxx;maxxy")
            else
                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."
                View.Update
                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
    time1 := timestart
    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 <= 0 then %  x cord direction inverse
        oval_directionx := oval_directionx * -1
    end if
        loop % Loop to de bug the continue system
            if oval_y1 <= 20 then %  if y cord hits <= 20 it asks if you'd like to continue (reseting score)
                cls
                put "Would you like to continue? y/n: " ..
                get continue   % if you'd like to continue..
            else
                exit
            end if
            if continue = "y" or continue = "n" then
                cls % Clears the screen after you type in a valid answer
                oval_x1 := 328
                oval_y1 := 228
                oval_directionx := 1
                oval_directiony := 1 % Reset the direction & placement of the ball
                score := 0 % Resets the score
                exit
            else
                put "Invalid answer."
                delay (2000)
                cls
            end if
        end loop % End the debugging loop
    if continue = "n" then
        put "Good Bye!" %Says goodbye and closes if you chose not to keep going
        delay (2000)
        break
    end if
    oval_x1 += (oval_directionx * oval_speed) % adds the direction *speed to the x cord of the ball
    oval_y1 += (oval_directiony * oval_speed) % " but with the y cord
            if chars (KEY_RIGHT_ARROW) then %Right key move the paddle to the right
                box1_x1 := box1_x1 + 5
                box1_x2 := box1_x2 + 5
            end if
            if chars ('d') then % " but d
                box1_x1 := box1_x1 + 5
                box1_x2 := box1_x2 + 5
            end if
            if chars (KEY_LEFT_ARROW) then % left key move paddle to the left
                box1_x2 := box1_x2 - 5
                box1_x1 := box1_x1 - 5
            end if
            if chars ('a') then % " but a
                box1_x2 := box1_x2 - 5
                box1_x1 := box1_x1 - 5
            end if
        if box1_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 <= 0 or box1_x2 <= 15 + 115 then %Sets the near x bounds for the paddle
            box1_x1 := box1_x1 + 5
            box1_x2 := box1_x2 + 5
        end if
        if oval_x1 <= box1_x2 & oval_y1 <= box1_y2 & oval_x1 >= box1_x1 & oval_y1 >= box1_y1 then %If collision occurs, the ball moves in the oposite direction
            oval_directiony := oval_directiony * -1
            score := score + 1
        end if
    colourback(0) put "Score:", score
        colourback(0) put "Time elapsed:", round(time1 / 1000) % How much time has elapsed since exiting the menu
        drawfillbox (box1_x1, box1_y1, box1_x2, box1_y2, red) %Draw the changes to the box
        drawfilloval (oval_x1,oval_y1,oval_xr1,oval_yr1,yellow) %draw the ball
    View.Update % Supposed to fix screen flickering
    delay (5)
end loop
Mr. Green

Author:  Insectoid [ Mon Sep 05, 2011 1:19 am ]
Post subject:  RE:Help me comprehend some (SIMPLE) Pong coding

I'm sure you can figure it out. Remember Time.Elapsed()? Big hint right there.

Author:  Aange10 [ Mon Sep 05, 2011 11:43 am ]
Post subject:  Re: Help me comprehend some (SIMPLE) Pong coding

I've read all the help section on the turing window, and i've searched the forums for "Time.Elapsed()" "Time Resetting" "Resetting time", and spent about half an hour trying to find out the syntax of Time.Elapsed ... The help menu for turing didn't say what the () was for, and Time.Elapsed() seems to be the same as Time.Elapsed.

I've tried codes like
code:

timestart := Time.Elapsed() - timestart
% And
timestart := 0
% And
timestart := Time.Elapsed() * 0

I've tried a lot of other things that the turing said was inapproriate.
Turing:

Time.Elapsed := 0 % Tells me time elapsed isn't a variable..
timestart := Time.Elapsed(0) % Tells me the parameters for Time.Elapsed are inappropriate

Is there something i'm missing here? Is there not a way to say Time.Elapsed (or timestart since it's my variable) := 0 ...... And it work?

Author:  Insectoid [ Mon Sep 05, 2011 12:24 pm ]
Post subject:  RE:Help me comprehend some (SIMPLE) Pong coding

The () just specifies that it's a function. That's right, it's not a variable. It *always* returns the time in milliseconds since the program began execution. You can't change what Time.Elapsed() returns. You can, however, assign the return value to a variable.

int i = Time.Elapsed()

You don't need any extra commands. You need math.

You have got a few things to work with.
-You can get the time since the program began (Time.Elapsed())
-You can assign that to a variable
-Because 'time' is just an integer, you can apply any math function to it (addition, subtraction, etc). Big hint: You need subtraction.

Author:  Aange10 [ Mon Sep 05, 2011 2:59 pm ]
Post subject:  Re: Help me comprehend some (SIMPLE) Pong coding

I've spent half of last night and nearly all day today trying to figure out why my timer isn't working. I've taken what you've said, and took in to account what the guy a couple posts above me said, and I came up with this code.
Turing:

View.Set("offscreenonly, graphics:maxx;maxxy, nocursor")
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
var timesincethen, timestart, timenow : real % Trying to reset timer...
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
timestart := Time.Elapsed
loop % Main loop
    cls
      Input.KeyDown (chars)
        loop % main menu debugging loop
            if mainmenu = true then
            View.Set("nooffscreenonly, graphics:maxx;maxxy")
                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
            View.Set("offscreenonly, graphics:maxx;maxxy, nocursor")
            else
                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."
                View.Update
                delay (2000)
                exit
            end if
        end loop % Ends the main menu de bugging loop
         timenow := Time.Elapsed() % So I can measure the time elapsed from this point
         timesincethen := timenow - timestart
    cls
    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 <= 0 then %  x cord direction inverse
        oval_directionx := oval_directionx * -1
    end if
        loop % Loop to de bug the continue system
            if oval_y1 <= 20 then %  if y cord hits <= 20 it asks if you'd like to continue (reseting score)
                cls
                put "Would you like to continue? y/n: " ..
                get continue   % if you'd like to continue..
            else
                exit
            end if
            if continue = "y" or continue = "n" then
                cls % Clears the screen after you type in a valid answer
                oval_x1 := 328
                oval_y1 := 228
                oval_directionx := 1
                oval_directiony := 1 % Reset the direction & placement of the ball
                score := 0 % Resets the score
                timenow := 0
                exit
            else
                put "Invalid answer."
                delay (2000)
                cls
            end if
        end loop % End the debugging loop
    if continue = "n" then
        put "Good Bye!" %Says goodbye and closes if you chose not to keep going
        delay (2000)
        break
    end if
    oval_x1 += (oval_directionx * oval_speed) % adds the direction *speed to the x cord of the ball
    oval_y1 += (oval_directiony * oval_speed) % " but with the y cord
            if chars (KEY_RIGHT_ARROW) then %Right key move the paddle to the right
                box1_x1 := box1_x1 + 3
                box1_x2 := box1_x2 + 3
            end if
            if chars ('d') then % " but d
                box1_x1 := box1_x1 + 3
                box1_x2 := box1_x2 + 3
            end if
            if chars (KEY_LEFT_ARROW) then % left key move paddle to the left
                box1_x2 := box1_x2 - 3
                box1_x1 := box1_x1 - 3
            end if
            if chars ('a') then % " but a
                box1_x2 := box1_x2 - 3
                box1_x1 := box1_x1 - 3
            end if
        if box1_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 <= 0 or box1_x2 <= 15 + 115 then %Sets the near x bounds for the paddle
            box1_x1 := box1_x1 + 5
            box1_x2 := box1_x2 + 5
        end if
        if oval_x1 <= box1_x2 & oval_y1 <= box1_y2 & oval_x1 >= box1_x1 & oval_y1 >= box1_y1 then %If collision occurs, the ball moves in the oposite direction
            oval_directiony := oval_directiony * -1
            score := score + 1
        end if
    colourback(0) put "Score:", score
        colourback(0) put "Time elapsed:", round(timesincethen / 1000) % How much time has elapsed since exiting the menu
        drawfillbox (box1_x1, box1_y1, box1_x2, box1_y2, red) %Draw the changes to the box
        drawfilloval (oval_x1,oval_y1,oval_xr1,oval_yr1,yellow) %draw the ball
    View.Update % Supposed to fix screen flickering
    delay (5)
end loop


I really enjoy this game and I plan on turning it into a brick breaker game, but it is absolutely bugging me that my timing doesn't work! I just can't figure out why. I've done just about everything I could think of, and iIve searched on here for somebody with the same problem for hours. The example I was giving earlier just doesn't seem to be working. No matter what variable i set to 0 or what variables i subtract from one another the time never restarts. The most I've done is destroy the time all together =( Please help <3

Author:  Insectoid [ Mon Sep 05, 2011 3:27 pm ]
Post subject:  RE:Help me comprehend some (SIMPLE) Pong coding

You never need set anything to 0.

Since you seem to be putting in a decent effort I'll help you out in more than hints.

timeSinceReset = Time.Elapsed() - startTime. startTime is taken when you 'reset' the timer. Whenever you want to display the time, use this formula. If you want to reset, do startTime := Time.Elapsed. For the sake of clean code, put startTime := Time.Elapsed at the top of the main loop. You *Do NOT* want to put this in your "If continue = 'y' or continue = 'n'. DO NOT DO THIS. Honestly, that if statement doesn't even have to be there- all that stuff can be put in the top of the loop.

Play around with this if you still don't get it:

code:

var startTime : int
var reset_flag : char
startTime := Time.Elapsed()
loop
    put Time.Elapsed() - startTime
    put "Reset? y/n: "..
    get reset_flag
    if reset_flag = "y" then
        startTime := Time.Elapsed()
    end if
end loop


Note that I don't have a copy of Turing available atm, so I haven't been able to test that code, so I can't guarantee it's error-free.

Author:  Aange10 [ Tue Sep 06, 2011 4:42 pm ]
Post subject:  RE:Help me comprehend some (SIMPLE) Pong coding

Oh my goodness. Thank you so much Insectoid! It works! Ahh. You should have seen the expersion on my face when my timer went from "25" to "0"!!!

Thanks a ton!!

How do I get bits? I'd like to donate <3

Author:  Insectoid [ Tue Sep 06, 2011 5:45 pm ]
Post subject:  RE:Help me comprehend some (SIMPLE) Pong coding

No need to donate (bits come from posting). Karma's cool though!

Author:  Aange10 [ Tue Sep 06, 2011 6:47 pm ]
Post subject:  RE:Help me comprehend some (SIMPLE) Pong coding

Hehe when I get 8 more posts, I will be allowed to karma, which I shall do!
(and look, this post goes 1 towards that.. 7 left!)

I'm very much enjoying this game, and I absolutely love watching this expand. (If you knew me personally, you'd know that seeing progress is just absolutely fantastic to me.)

I've come to a minor problem (doesn't bother me nearly as bad as the timer) that I'm not sure how to fix. My problem is with the collision of my ball and paddle. My ball will occasionally go into one of the sides of my paddle.

Before I attempted to fix the problem, I decided I needed to actually learn (not follow a formula) what my collision equation was saying. So I drew a box and a square on a piece of paper and found out exactly what my program was saying. Now the reason I am confused and unable to fix this, is because what i found was that the = signs in my formula drew the border of my box, and the > and < signs "filled it in". So with that being said I have no idea how my ball is going inside my paddle. (Goes in one end comes out the other)

So I decided instead of just coming and asking, I would go to your tutorial on how to make a game and re-read the collision section. After re-reading it, I felt that my hypothesis of how my formula worked was true, but I didn't find the answer to my problem. Though I got two links!

After reading the collision section of your tutorial I decided to go back over the Turing Walkthrough and re-read their collision tutorial. I read it and it honestly didn't teach me anything. I've read the thing a bunch of times and I didn't really feel like it told me anything relevant to my problem. However, I noticed it told about a problem very similar to mine. Which happened to be with ball collision.

So long story short, I went and read the advanced guide. To be honest I'm very happy to say I comprehended the majority of what he was saying. I'm not going to lie; I didn't grasp every bit of it, especially considering I only read it once and then skimmed it once more, but I got the gist.

Where all this is leading to, and the reason I am here now is that I still haven't found a solution to my problem. To be honest I doubt my problem will need anything to do with advanced circular collision (though I will do it, if need be). But it still leaves me with one question: What's not right?

Current coding for collision detection (full coding is a couple posts up...)

Turing:

if oval_x1 <= box1_x2 & oval_y1 <= box1_y2 & oval_x1 >= box1_x1 & oval_y1 >= box1_y1 then %If collision occurs, the ball moves in the opposite direction
            oval_directiony := oval_directiony * -1
            score := score + 1
        end if

Author:  Aange10 [ Wed Sep 07, 2011 7:07 pm ]
Post subject:  RE:Help me comprehend some (SIMPLE) Pong coding

Also this is my current coding for a pause. Obviously, I ran into the problem of not being able to use 'p' for both chars statements, so i used v for one. However this is bugging me, and I was wondering if I could get some help =/. I went to the turing help section and searched "pausing" and read all 20 forums that came up but... well I'm still here Sad


Turing:

var gameState : boolean% If gameState true then it is paused
var fontTNR : int % A to allow me to make a font style into a variable
var chars : array char of boolean
gameState := false
procedure pausing
        if gameState = true then
            cls
            fontTNR := Font.New ("Courier:14")
                    Font.Draw ("Screen Paused. Press V to unpause.", maxx div 2 -150, maxy div 2, fontTNR, purple)
                loop
                    Input.KeyDown (chars)
                    if chars ('v') | chars ('V') then
                        gameState := false
                    end if
                    exit when gameState = false
               end loop
               end if
    end pausing
loop
    cls
    Input.KeyDown (chars)   
    if chars ('p') | chars ('P') then
             gameState := true
            end if
    put "It's not paused!"
    put "gameState:", gameState
    pausing
    delay (1000)
end loop

Author:  DemonWasp [ Wed Sep 07, 2011 7:18 pm ]
Post subject:  RE:Help me comprehend some (SIMPLE) Pong coding

Your first problem is that you should be using or, not a vertical bar.

Author:  Aange10 [ Wed Sep 07, 2011 8:26 pm ]
Post subject:  RE:Help me comprehend some (SIMPLE) Pong coding

What is a verticle bar...?

Author:  DemonWasp [ Wed Sep 07, 2011 8:30 pm ]
Post subject:  Re: RE:Help me comprehend some (SIMPLE) Pong coding

Turing:

...
    if chars ('p') | chars ('P') then

                   ^ that guy

Author:  Aange10 [ Wed Sep 07, 2011 9:17 pm ]
Post subject:  RE:Help me comprehend some (SIMPLE) Pong coding

Okay, thankyou. I changed them all.... But that didn't fix the problem. What do I need to correct?

Author:  DemonWasp [ Wed Sep 07, 2011 10:19 pm ]
Post subject:  RE:Help me comprehend some (SIMPLE) Pong coding

Oh! If you were meaning the problem where you can't use 'p' for both pause and unpause, that's because you need to detect the key-down event, not the key-down state.

The state of key-down ( 'p' ) is whether or not that key is down right now.

The event of key-down ( 'p' ) is whether or not that key was just pressed down now. That is, whether 'p' is pressed now, but not on the last iteration.

The most straightforward way to detect this event is to keep the old keys array around and record into a new one on each iteration. Then, if new_keys_down ( 'p' ) and not old_keys_down ( 'p' ), then you know that p was pressed just this instant.

Looks something like this:
Turing:

var paused : boolean := false
var new_keys_down, old_keys_down : array char of boolean

% Need initialized values!
Input.KeyDown ( old_keys_down )

loop
    Input.KeyDown ( new_keys_down )

    if new_keys_down ( 'p' ) and not old_keys_down ( 'p' ) then
        % Either pause or unpause, depending on whether we were already paused:
        paused := not paused  % 'not' here switches true to false and false to true...or paused to unpaused and vice-versa
    end if

    if not paused then
        % game stuff here -- call some methods, whatever.
    end if

    % Do NOT forget this bit, or you'll have some weird bugs
    old_keys_down := new_keys_down
end loop

Author:  Aange10 [ Sat Sep 10, 2011 1:50 pm ]
Post subject:  Re: Help me comprehend some (SIMPLE) Pong coding

Alright, so I've been busy latley, but for the past 3 or 4 days i've been working on making the computer generate bricks. I'm kind of (as you can assume) lost.

My coding so far has been
Turing:

var brick_x1 :array 1 .. 100 of int %Arrays
var brick_x2 :array 1 .. 100 of int
var brick_y1 :array 1 .. 100 of int
var brick_y2 :array 1 .. 100 of int
var brick_color :array 1 .. 100 of int
brick_x2(1) := 629 % far right x
brick_y2(1) := 399 % top
for i : 1 .. 1
brick_x1(i) := Rand.Int(brick_x2(i) - 115, brick_x2(i) - 20) % Random int to make the width of the brick

brick_color(i) := Rand.Int (1,10) % random color for the brick
end for
brick_y1(1) := brick_y2(1) - 20 % width (constant)
for e : 2 .. 10
end for
put "brick_x1:", brick_x1(1)
put "brick_x2:", brick_x2(1)
put "brick_y1:", brick_y1(1)
put "brick_y2:", brick_y2(1)
%for b : 1 .. 2
drawfillbox (brick_x1(1),brick_y1(1),brick_x2(1),brick_y2(1),brick_color(1)) % draw da box, all the (1) will be % when the %for statement is made
%end for

...
Pretty much all i was trying to make it do what generate bricks starting from the top right corner (629, 399) and having the computer generate a width (the x2) & color (brick_color). After it did that, it would generate another brick starting from where the brick behind it had ended.

Obviously I didn't do too well, and I was wondering how I should go about doing this. Is 4 arrays bad? Is multiple fors bad? Do I need a loop?

Author:  Zren [ Sat Sep 10, 2011 3:23 pm ]
Post subject:  RE:Help me comprehend some (SIMPLE) Pong coding

How would you draw a grid?
How would you draw a bunch of points spaced n pixels apart?

Start with a new program, and try drawing a grid from the bottom left (0,0). After you can, it's easy to translate it to the top.

Tip: Think of it as iterating through each column in a row.

Using a different array for x1, x2 isn't bad, you can't change that until you've learnt how to make your own data types (records/classes).

maxx and maxy are useful variables to find the maximum coordinates.

Author:  Aange10 [ Mon Sep 12, 2011 7:23 pm ]
Post subject:  RE:Help me comprehend some (SIMPLE) Pong coding

Ok but why do i need a grid? I can find any cords i need with my Mouse.Where.. Not to mention i already found where i wanted my brick to start

Author:  Zren [ Tue Sep 13, 2011 12:26 am ]
Post subject:  RE:Help me comprehend some (SIMPLE) Pong coding

Because the behaviour you were describing is similar to a grid. As your eventual output of bricks would look like it has columns and rows.

It's also very easy to draw a grid.
Turing:


var startX := 0
var startY := maxy div 2
var cellW := 40
var cellH := 10
var w := cellW - 1
var h := cellH - 1
for y : 0 .. 100 by cellH
    for x : 0 .. maxx by cellW
        Draw.Dot (x, y, black) %Starting point per brick
        %Draw.Box (x, y, x + cellW, y + cellH, black) % Overlap!
        %Draw.Box (x, y, x + w, y + h, black) % No more overlap
        Draw.FillBox (startX + x, startY + y, startX + x + w, startY + y + h, Rand.Int (0, maxcolor))
    end for
end for

% Alternatively:
for y : 0 .. 19
    for x : 0 .. 4
        %Draw.Dot (x * cellW, y * cellH, black)
    end for
end for


I might have misinterpreted. I thought you were doing the game breakout where all the bricks are a fixed width. You seem to have some randomness in there. If you know how to make a for loop into a regular loop, then you can do a non fixed increment to the counter.

Turing:

var x := 0
loop
    exit when x > maxx
    Draw.Line(x, 0, x, maxy, black)
    x += Rand.Int (5, 20)
end loop

Author:  Aange10 [ Tue Sep 13, 2011 8:05 pm ]
Post subject:  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

Turing:

for x : 0 .. maxx by cellW

but i don't know what
Turing:

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
Turing:

var cell_Width := 40
var cell_Height := 10
var w := cell_Width - 1
var h := cell_Height - 1

in it's relation to
Turing:

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?

Author:  Aange10 [ Tue Sep 13, 2011 8:26 pm ]
Post subject:  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
Turing:

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 [dunno why not 40]

%startY + y + h is doing the same as startY (positioning it) and adding
% a height to it so that the points are drawn apart
% StartY + y + h is StartY + y + 9 [dunno why not 39]

Author:  Zren [ Tue Sep 13, 2011 10:04 pm ]
Post subject:  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 <= x <= 39
0 <= x <= 40

The latter has 41 numbers.
if you still don't understand, try this:
Turing:

View.Set ("text")
var count : int

proc countRange (minN, maxN : int)
    count := 0
    for i : minN .. maxN
        put "i: ", i
        count += 1
    end for
    put skip, "count: ", count, skip
end countRange

countRange (0, 39)
countRange (0, 40)
countRange (40, 79)

Author:  Aange10 [ Wed Sep 14, 2011 6:45 pm ]
Post subject:  RE:Help me comprehend some (SIMPLE) Pong coding

I see I see, thankyou!(:

Author:  Aange10 [ Wed Sep 14, 2011 8:51 pm ]
Post subject:  Re: Help me comprehend some (SIMPLE) Pong coding

Alright, cool. So I've developed a program to randomly generate the blocks with a random width. Now I ask two questions.

1) Is there something I could use to make this very long line shorter?
Turing:

if color_1 + 10 = color_2 or color_1 + 9 = color_2 or color_1 + 8 = color_2 or color_1 + 7 = color_2 or color_1 + 6 = color_2 or color_1 + 5 = color_2 or color_1 + 4 = color_2 or color_1 + 3 = color_2 or color_1 + 2 = color_2 or color_1 + 1 = color_2 or color_1 = color_2 or color_1 - 1 = color_2 or color_1 - 2 = color_2 or color_1 - 3 = color_2 or color_1 - 4 = color_2 or color_1 - 5 = color_2 or color_1 - 6 = color_2 or color_1 - 7 = color_2 or color_1 - 8 = color_2 or color_1 - 9 = color_2 or color_1 - 10 = color_2 then


My goal here is to say that if any integer within 10 numbers of color_1 is = color_2 then it will execute the if statement.


2) I have my blocks being generated. But now how will I set up collision on these blocks (My ball colliding on them)? Also, once i do set up collision, how do i make the block disappear?

Thanks!


EDIT: And I'm also still having the same problem as before; My ball is going into the sides of my paddle and my code isn't exactly showing me why.
Turing:

if oval_x1 <= box1_x2 & oval_y1 <= box1_y2 & oval_x1 >= box1_x1 & oval_y1 >= box1_y1 then

Author:  Zren [ Thu Sep 15, 2011 1:16 am ]
Post subject:  RE:Help me comprehend some (SIMPLE) Pong coding

Please tell me you're not using whatdotcolor colision.

As for Q1: isn't that just a-10 <= b <= a+10 ?

Q2: Either remove it from the flexible array, or have a 'flag' variable for if it exists anymore.

Q3: You're only checking if the center point is within the rectangle.

Author:  Aange10 [ Thu Sep 15, 2011 5:14 pm ]
Post subject:  RE:Help me comprehend some (SIMPLE) Pong coding

Q1: Fixed, thankyou Zren!

and for Q3: I'll go and re read some of the tutorials to get the entire ball to be detected, thanks!

Q2: Well I'm not using an array (my codes at the bottom), so I guess i'll have to go with a flag variable. But how would I program collision on these random width blocks, and how would i make the flag detect if the blocks are still there?


Turing:

var start_x, start_y, box_width, box_height, color_1, bh, bw, color_2, color_3 : int
start_x := 0
start_y := 100
box_height := 20
box_width := Rand.Int (33, 120)
bh := box_height - 1
bw := box_width - 1
color_1 := Rand.Int (1, 255)
color_2 := color_1 + 11

View.Set ("Offscreenonly,graphics:maxx;maxxy")
procedure Draw_Brick
    box_width := Rand.Int (33, 120)
    for height_a : 0 .. 100 by box_height
        for width_a : 0 .. maxx by box_width
            loop
                color_1 := Rand.Int (1, 55)
                if color_1 - 25 <= color_2 and color_2 <= color_1 + 25 then
                else
                    exit
                end if
            end loop
            color_2 := color_1
            drawfillbox (start_x + width_a, start_y + height_a, start_x + width_a + bw, start_y + height_a + bh, color_2)
            locatexy (0, maxy)
            put "Height_A:", height_a, " Width_A:", width_a
            delay (50)
            View.Update
        end for
    end for
end Draw_Brick
%%%%%Main Loop
Draw_Brick


: