Computer Science Canada

pong:paddles

Author:  gilbamy [ Thu May 18, 2006 1:22 pm ]
Post subject:  pong:paddles

alright what i need to do is have a program called paddle and i have to have to paddle s move but my first paddle won't move
code:
%Amy Gilbank
%May 15 2006
%paddle.t
%A paddle program
var winMain : int := Window.Open ("graphics :800;600")
var key : string (1)
var y : int
y := 100

loop
        drawfillbox (0, 0, 20, 100, 7)
        getch (key)
        cls
        if key = "S" or key = "s" then
        y := y - 90
    elsif key = "W" or key = "w" then
        y := y + 90
    end if
end loop


Author:  MysticVegeta [ Thu May 18, 2006 1:31 pm ]
Post subject: 

Take a look at your drawfilbox, shouldnt it have a coordinate y in there? Also, do not use getch use something else such as Input.KeyDown, look it up in the turing refercne

Author:  gilbamy [ Thu May 18, 2006 1:34 pm ]
Post subject: 

but i have to use getch though oh and here is my new code and i got it to move but it changes sizes and things so i need help
code:

%Amy Gilbank
%May 15 2006
%paddle.t
%A paddle program
var winMain : int := Window.Open ("graphics :800;600")
var key : string (1)
var y ,x,x2,y2: int
y := 0
x:=20

loop
        drawfillbox (x, y, 0, 100, 7)
        getch (key)
        cls
        if key = "S" or key = "s" then
        y := y - 90
    elsif key = "W" or key = "w" then
        y := y + 90
    end if
end loop

Author:  TheOneTrueGod [ Thu May 18, 2006 2:08 pm ]
Post subject: 

First of all, you need to understand what drawfillbox does.

code:
drawfillbox(x1,y1,x2,y2,colour)


drawfillbox will draw a filled in box from the co-ordinate (x1,y1) to the co-ordinate (x2,y2).

If you tell it to draw the box from x,y to 0,100 then obviously its going to do just that. Think about it for a second. Before, you wanted to draw the box from 0,0 to 20,100. If you split that up into X and Y components, then you'll figure out that you want the box to be 20 wide, and 100 tall.

So, if you have the position (x,y), and you want the second co-ordinate to be 20 greater along the x, and 100 greater along the y, what should you do?

(I'll let you try and figure that out. To anyone else who thinks they know the answer, don't post it. He won't learn anything that way.)

Author:  Clayton [ Thu May 18, 2006 3:18 pm ]
Post subject: 

if you are going to use getch use it with hasch as well ex.:

code:

if hasch then
    getch (a)
end if
[/code]

Author:  gilbamy [ Thu May 18, 2006 7:24 pm ]
Post subject: 

i have decided to screw getch and use that input thing

Author:  gilbamy [ Thu May 18, 2006 7:34 pm ]
Post subject: 

never mind my last comment okay well i got the input thing to work but now its just a line when i hit W for up so yeah here is new code aain sorr
code:
%Amy Gilbank
%May 15 2006
%paddle.t
%A paddle program
var winMain : int := Window.Open ("graphics :800;600")
var key : array char of boolean
var y, x, x2, y2 : int
y2 := 1
x := 20

loop
    drawfillbox (x, y2, 0, 100, 7)
    Input.KeyDown (key)
    if key ('S') or key ('s') then
        y2 := y2 - 10
    elsif key ('W') or key ('w') then
        y2 := y2 + 10
    end if
end loop

Author:  TheOneTrueGod [ Thu May 18, 2006 7:43 pm ]
Post subject: 

Turing doesn't erase the screen every iteration of the loop, you have to tell it when to do that. Also, keep in mind that the code you have there iterates every roughly 12 milliseconds, so y2 is decreasing by 10 every 12 milliseconds that you hold the key down. There are three commands you want to use.

Number one, cls
Number two, delay
Number three, View.Update

Visit the Turing Walkthrough to find info on these.

Author:  gilbamy [ Thu May 18, 2006 7:52 pm ]
Post subject: 

alright now it won't move at all
code:
%Amy Gilbank
%May 15 2006
%paddle.t
%A paddle program
var winMain : int := Window.Open ("graphics :800;600")
var key : array char of boolean
var y, x, x2, y2 : int
y2 := 1
x := 20

loop
View.Update
    drawfillbox (x, y2, 0, 100, 7)
    delay(100)
    Input.KeyDown (key)
    if key ('S') or key ('s') then
        y2 := y2 - 10
    elsif key ('W') or key ('w') then
        y2 := y2 + 10
    end if
end loop

Author:  MysticVegeta [ Thu May 18, 2006 8:43 pm ]
Post subject: 

Did you pay attention to true gods post? He explaned drawfillbox, as you can clearly see your y starts at 1 and you increment it every 10, and the other y coordinate is 100! so it will draw a box with y1 = 1 and y2 = 100, so you have to press w 10 times for the y1 to go to 100 right? Now, Why is the second y coordinate a constant too? Do you not want the other end of the paddle to move too?! Its not memorizing, its logic, you have to think about it, start with knowing how drawfillbox operates.

Author:  gilbamy [ Fri May 19, 2006 12:11 pm ]
Post subject: 

why must everyone explan eveything so difficultly arg!! i hate pong now oh and why won't oit still move and why won't my second one appear blast this computer stupid school comp can't even install everything okay here is my new code but yeah here
code:

%Amy Gilbank
%May 15 2006
%paddle.t
%A paddle program
var winMain : int := Window.Open ("graphics :800;600")
var key : array char of boolean
var y, x, x2, y2 : int
y2 := 1
x := 20

loop
    drawfillbox (x, y2, 0, 100, 7)
       Input.KeyDown (key)
       if key ('S') or key ('s') then
        y2 := y2 - 2
    elsif key ('W') or key ('w') then
        y2 := y2 + 1
    end if
        end loop
loop
drawfillbox(800,0,780,100,9)
    delay(250)
    cls
    Input.KeyDown(key)
    if  key  ('O') or key ('o') then
    y2:=y2+1
    elsif key ('L') or key ('l') then
    y2:=y2-2
    end if
end loop

Author:  gilbamy [ Thu May 25, 2006 6:25 pm ]
Post subject: 

i need help

Author:  MysticVegeta [ Thu May 25, 2006 6:36 pm ]
Post subject: 

code:
loop
    Input.KeyDown (key)
    if key ('S') or key ('s') then
        y2 := y2 - 1
    elsif key ('W') or key ('w') then
        y2 := y2 + 1
    end if
    cls
    drawfillbox (x, y2, 0, y2 - 80, 7)
    delay (3)
end loop


try to understand this code, and before you say " I need help " why not look at how drawfillbox works in the first place??? Also, to move your second paddle you need to put it in the same loop, not a different one because as you clearly see it wont enter the second loop until it finishes exiting the first one, and it is clear you dont have an exit condition on the first loop.

Author:  gilbamy [ Fri May 26, 2006 11:08 am ]
Post subject: 

well guess what i got my 1st paddle to move but my second one on't appear in the same loop or in a different loop oh why why ! here is my code
code:
%Amy Gilbank
%May 15 2006
%paddle.t
%A paddle program
var winMain : int := Window.Open ("graphics :800;600")
var key, key2 : array char of boolean
var y, y2, y3, y4 : int
y2 := 100
y := 20
y3 := 20
y4 := 100
process pad
    loop
        drawfillbox (20, y, 0, y2, 7)
        delay (3)
        Input.KeyDown (key)
        drawfillbox (20, y, 0, y2, 0)
        if key ('S') or key ('s') then
            y := y - 1
            y2 := y2 - 1
        end if
        if key ('W') or key ('w') then
            y := y + 1
            y2 := y2 + 1
        end if
    end loop
end pad
%2nd pad
process pad2
    loop
        drawfillbox (780, y3, 780, y4, 7)
        delay (3)
        Input.KeyDown (key2)
        drawfillbox (780, y3, 800, y4, 0)
        if key2 (KEY_UP_ARROW)  then
            y3 := y3 + 1
            y4 := y4 + 1
        end if
        if key2 (KEY_DOWN_ARROW)  then
            y3 := y3 - 1
            y4 := y4 - 1
        end if
    end loop
end pad2

fork pad
fork pad2

Author:  NikG [ Fri May 26, 2006 11:44 am ]
Post subject: 

First off, don't use processes. They don't work reliably.

Second, you don't need 2 loops for the movement. You need one main loop, and in that loop you can ask for input for both paddles. Here's some sample code:
code:
Input.KeyDown (key)
if key ('S') or key ('s') then
    Paddle1y := Paddle1y - 1 %can also be written as Paddle1y-=1
elsif key ('W') or key ('w') then
    Paddle1y := Paddle1y + 1 %can also be written as Paddle1y+=1
end if
if key (KEY_UP_ARROW)  then
    Paddle2y += 1
elsif key (KEY_DOWN_ARROW)  then
    Paddle2y -= 1
end if

Hope that helped.

Author:  gilbamy [ Fri May 26, 2006 11:57 am ]
Post subject: 

okay but it still does not appear and now my first paddle is flickering like mad

Author:  MysticVegeta [ Fri May 26, 2006 1:37 pm ]
Post subject: 

care to post the code?

Author:  gilbamy [ Tue May 30, 2006 8:39 am ]
Post subject: 

oops sorry
code:
%Amy Gilbank
%May 15 2006
%paddle.t
%A paddle program
var winMain : int := Window.Open ("graphics :800;600")
var key, key2 : array char of boolean
var y, y2, y3, y4 : int
y2 := 100
y := 20
y3 := 20
y4 := 100
process pad
    loop
        drawfillbox (20, y, 0, y2, 7)
        delay (3)
        Input.KeyDown (key)
        drawfillbox (20, y, 0, y2, 0)
        if key ('S') or key ('s') then
            y := y - 1
            y2 := y2 - 1
        end if
        if key ('W') or key ('w') then
            y := y + 1
            y2 := y2 + 1
        end if
    end loop
end pad
%2nd pad
process pad2
    loop
        drawfillbox (780, y3, 800, y4, 7)
        delay (3)
        Input.KeyDown (key2)
        drawfillbox (780, y3, 800, y4, 0)
        if key2 (KEY_UP_ARROW)  then
            y3 := y3 + 1
            y4 := y4 + 1
        end if
        if key2 (KEY_DOWN_ARROW)  then
            y3 := y3 - 1
            y4 := y4 - 1
        end if
    end loop
end pad2

fork pad
fork pad2

Author:  HellblazerX [ Tue May 30, 2006 10:18 am ]
Post subject: 

Arghhhh. Don't use processes. Read Cervantes' tutorial on why you shouldn't use processes. You should only use one main loop for your program. Secondly, you'll need to use View.Set and View.Update in your program. View.Set and View.Update allows you draw in a double buffered mode which reduces the flickering. View.Set should be outside your main loop, and you should have only one View.Update inside the main loop.

Author:  gilbamy [ Tue May 30, 2006 10:49 am ]
Post subject: 

yeah that did not work so now i tried puting it all in one loop like this right
code:
%Amy Gilbank
%May 15 2006
%paddle.t
%A paddle program
var winMain : int := Window.Open ("graphics :800;600")
var key, key2 : array char of boolean
var y, y2, y3, y4 : int
y2 := 100
y := 20
y3 := 20
y4 := 100

    loop
    View.Update
        drawfillbox (20, y, 0, y2, 7)
        delay (3)
        Input.KeyDown (key)
        drawfillbox (20, y, 0, y2, 0)
         drawfillbox (780, y3, 800, y4, 7)
        delay (3)
        Input.KeyDown (key2)
        drawfillbox (780, y3, 800, y4, 0)
        if key ('S') or key ('s') then
            y := y - 1
            y2 := y2 - 1
        end if
        if key ('W') or key ('w') then
            y := y + 1
            y2 := y2 + 1
        end if
          if key2 (KEY_UP_ARROW)  then
            y3 := y3 + 1
            y4 := y4 + 1
        end if
        if key2 (KEY_DOWN_ARROW)  then
            y3 := y3 - 1
            y4 := y4 - 1
        end if
    end loop
View.Set

Author:  HellblazerX [ Tue May 30, 2006 4:44 pm ]
Post subject: 

Sry, I guess I didn't make it clear. The View.Set should be just before the loop, not after it. And you need to enter "offscreenonly" as the parameter in order to change the mode. So this is what it should look like:

code:
View.Set ("offscreenonly")
loop
    //rest of your program
end loop

Author:  gilbamy [ Tue May 30, 2006 9:06 pm ]
Post subject: 

i' am kinda new at this stuff

Author:  Stigmata [ Tue May 30, 2006 10:37 pm ]
Post subject: 

I'll see if I can help.

code:
var winMain : int := Window.Open ("graphics :800;600")
var key, key2 : array char of boolean
var y, y2, y3, y4 : int
y2 := 100
y := 20
y3 := 20
y4 := 100

Okay, right here I would change a few things. First, you don't need four y variables for two paddles. Since the paddles don't change height, you can always set the second y coordinates for each paddle to be 80 pixels above the first. Second, you don't need two char arrays for input, one will do just fine (at least in my experience). When you get the input, it tracks all the keyboard input at once, for you to use however you like. I'll explain this in a sec.

code:
        drawfillbox (20, y, 0, y2, 7)
        delay (3)
        Input.KeyDown (key)
        drawfillbox (20, y, 0, y2, 0)

Alright, here is where you can get rid of two of the four y variables. Since the heights of the paddles never changes, you don't need to change all the variables independently. Instead, you can just set the Draw procedure to something like this:
code:
Draw.FillBox(0,y,20,y+80,7)
and it will work exactly the same, only with far less chance of you being able to accidentally screw up your paddles when you change code.

code:
         drawfillbox (780, y3, 800, y4, 7)
        delay (3)
        Input.KeyDown (key2)
        drawfillbox (780, y3, 800, y4, 0)

What you've done here is doubled both the delay and the Input command. Generally, this is unnecessary. As this is a game, you want to get rid of any delays that could adversely affect the flow of the code; in this case, you're putting a delay between both Input commands, meaning that if players 1 and 2 both press their "down" key when player 2's input call comes, only player 2's paddle will move. Also, you don't need to call the input procedure twice for this kind of input. Since the command tells you all of the keyboard input at any one instant, you can tell if both player's keys are being pressed at the same time, so one will do just fine.

code:
        if key ('S') or key ('s') then
            y := y - 1
            y2 := y2 - 1
        end if
        if key ('W') or key ('w') then
            y := y + 1
            y2 := y2 + 1
        end if
          if key2 (KEY_UP_ARROW)  then
            y3 := y3 + 1
            y4 := y4 + 1
        end if
        if key2 (KEY_DOWN_ARROW)  then
            y3 := y3 - 1
            y4 := y4 - 1
        end if
    end loop
View.Set

Considering what I've said above, you can get rid of y2 and y4 in these procedures. Additionally, you could consider making a "paddle_height" integer constant to use in place of the "80" in the draw commands, so if you want to change the paddle heights, all you need to do is change one value at the start of the program instead of going to both (or more) draw procedures and changing each value by hand.

Additionally, you should reverse the order, so to speak, of the "input" and "draw" portions of your code. Essentially, something like this:
code:
loop
    <get input>
    <draw things>
    <delay goes here>
    cls
end loop

This sort of organization is more orderly and efficient, especially if you're looking to add more things to your game. The way this works is as such: First, your game gets any input you might be inputting. So, it changes where the paddles are going to be draw. Then, it moves on to actually drawing the paddles in their new positions. Once that's done, it delays the code execution, to ensure the game doesn't run as fast as computerly possible, which would end with you hitting the Up arrow once and having the paddle shoot immediately off the screen. Then, after it delays, it clears the screen, leaving a clean slate for your paddles to be re-drawn in the next loop.

Hope this helps Smile

Author:  gilbamy [ Thu Jun 01, 2006 11:53 am ]
Post subject: 

but i thought i need the two y's so they won't continue (-) it from the first y value and i thought i needed the delay's for the speed of my paddle

Author:  gilbamy [ Thu Jun 01, 2006 12:00 pm ]
Post subject: 

okay i tried it all and the paddle is lines now though so how do i fix that and you so lost ne there with the y corridnates

Author:  Stigmata [ Thu Jun 01, 2006 8:47 pm ]
Post subject: 

You're right, you do need the delay to control the game's speed. But you have to understand, with games, you want to get all the "behind the scenes" operations out of the way before you draw things to the screen. The reason for this is probably not immediately apparent to you, but trust me, it's almost always better if you draw everything at once and have a single delay to control your game's speed.

And about the Y coordinates:

Think of a rectangle of a specific size, any size. It has four points; the bottom-left, bottom-right, top-left, and top-right. Now let's say, without changing the rectangle's size, you put it on a graph. Now, Turing draws rectangles using the bottom-left and top-right corners, so we'll say the bottom-left corner is (x1,y1) and the top-right is (x2,y2).

Now, try moving that rectangle around on the graph, without changing its size. You see that every time you move it left or right, both the x-coordinates of the corners will change by the amount you moved the rectangle. If I move it right by two units, the coordinates will be (x1+2, y1) and (x2+2, y1). The same thing happens when you move it up and down; If I move it up 2 units, the coordinates would be (x1, y1+2) and (x2, y1+2).

So you can see that when you move the rectangle along either of those directions, the distances between the x-coordinates never changes. x1 and x2 are always (x2-x1) units apart. The same goes for the y-coordinates, as y1 and y2 are always (y2-y1) units apart.

So, with that in mind, you can say that x2 is dependent on x1. That is, the position of x2 depends on the position of x1, as they are always the same distance apart. Considering this, in the coordinates, you could replace x2 with x1 + (the distance between x1 and x2). And, naturally, the same goes for the y-coordinates.

So now, back to your pong-paddles game. In that game, you draw your paddles using two y-coordinates each, for both corners of both paddles. But, since the size of the paddles never changes, you can replace y2 with y1 + (the distance between y1 and y2). Now, when you increase y1 by, say, 10 pixels, y2 + (the distance between y1 and y2) will also increase by 10 pixels.

(The x-coordinates don't ever change, since the paddles don't move left or right, so you can just leave those as they are.)

Now, to make this a little simpler, you can make an integer variable called paddle_height. And say you want your paddles to be 100 pixels high. So, you set paddle_height equal to 100, like so:
code:
var paddle_height : int := 100
So now, whenever you need the distance between the bottom and the top of the paddle (i.e. the difference between y1 and y2), you just replace (distance between y1 and y2) with paddle_height. In the end, your code should look a bit like this:
code:
Draw.Box(20,y1,40,y1 + paddle_height,7)
Now, whenever you need to move the paddle up or down, you only need to change one variable, and the rest of the rectangle will take care of itself when it gets drawn.

Did that help at all?[/code]


: