Computer Science Canada

Pong.

Author:  eNc [ Fri Jan 21, 2005 9:54 am ]
Post subject:  Pong.

Hi umm yea I've made paddles for a ping game except they don't show up at the same time
keys are w, s up and down arrows

This is what i have so far

code:
setscreen ("nocursor")
var uy, uy2 : int := 190
var key, key2 : string (1)
procedure boxes
    loop
        drawfillbox (20, 40 + uy, 40, uy, blue)
        getch (key)
        if key = chr (200) then
            uy := uy + 10
        elsif key = chr (208) then
            uy := uy - 10
        end if
        drawfillbox (maxx - 50, 40 + uy2, maxx-70, uy2, blue)
        getch (key2)
        if key2 = 'w' then
            uy2 := uy2 + 10
        elsif key2 = 's' then
            uy2 := uy2 - 10
        end if
        cls
    end loop
end boxes
loop
    boxes
end loop


I don't know how I can make them visible at the same time.

Author:  Tony [ Fri Jan 21, 2005 12:41 pm ]
Post subject: 

because your pesudocode looks like
pseudocode:

loop
     wait for getch
     draw box
     wait for another getch
     draw another box
end loop


Not only is that horribly bad, but getch doesnt work for two player games because of key jamming (if I was to hold down a button, you would not be able to move)

Use Input.KeyDown() instead

Author:  basketball4ever [ Fri Jan 21, 2005 5:54 pm ]
Post subject: 

i would suggest more like this

code:

loop
drawbox1
drawbox2
getch (key)
if arrow key = up/down then
move box1 up/down
elsif arrow key = w/s then
move box2 up/down
end loop

Author:  Leftover [ Fri Jan 21, 2005 8:02 pm ]
Post subject: 

tony wrote:
Not only is that horribly bad, but getch doesnt work for two player games because of key jamming (if I was to hold down a button, you would not be able to move) Use Input.KeyDown() instead


Did you not catch that part? Getch won't work if there are 2 people pressing keys at same time. It's only meant to get 1 key. A simple program like:
code:

var move : string (1)

getch (move)

put ord (move)


will only take input of 1 key and if you run it you will see. An AI or using InputKeyDown like tony said really is the only option for 2 players like that.

Author:  Unreal_Origin [ Fri Jan 21, 2005 8:09 pm ]
Post subject: 

hey how is it going, your problem is that you have two loops one at the start and one in the procedure

i mod it a bit but here it is

code:

setscreen ("nocursor")
var uy, uy2 : int := 190
var key, key2 : string (1)
procedure boxes (var uy,uy2:int)
        getch (key)
        if key = chr (200) then
            uy := uy + 10
        elsif key = chr (208) then
            uy := uy - 10
        end if
        getch (key2)
        if key2 = 'w' then
            uy2 := uy2 + 10
        elsif key2 = 's' then
            uy2 := uy2 - 10
        end if
        cls
end boxes
loop
    drawfillbox (20, 40 + uy, 40, uy, blue)
    drawfillbox (maxx - 50, 40 + uy2, maxx-70, uy2, blue)
    boxes (uy,uy2)
end loop




that is if you want to keep getch

if you want a better way then this is what i would suggest

code:


setscreen ("nocursor")
setscreen ("offscreenonly")
var uy, uy2 : int := 190

var chars : array char of boolean


procedure boxes (var uy, uy2 : int)
    Input.KeyDown (chars)
    delay (50)
    if chars (KEY_UP_ARROW) then
        uy := uy + 10
        cls
        drawfillbox (20, 40 + uy, 40, uy, blue)
        drawfillbox (maxx - 50, 40 + uy2, maxx - 70, uy2, blue)
        View.Update
    elsif chars (KEY_DOWN_ARROW) then
        uy := uy - 10
        cls
        drawfillbox (20, 40 + uy, 40, uy, blue)
        drawfillbox (maxx - 50, 40 + uy2, maxx - 70, uy2, blue)
        View.Update
    end if

    if chars ('w') then
        uy2 := uy2 + 10
        cls
        drawfillbox (maxx - 50, 40 + uy2, maxx - 70, uy2, blue)
        drawfillbox (20, 40 + uy, 40, uy, blue)
        View.Update
    elsif chars ('s') then
        uy2 := uy2 - 10
        cls
        drawfillbox (maxx - 50, 40 + uy2, maxx - 70, uy2, blue)
        drawfillbox (20, 40 + uy, 40, uy, blue)
        View.Update
    end if
end boxes

drawfillbox (20, 40 + uy, 40, uy, blue)
drawfillbox (maxx - 50, 40 + uy2, maxx - 70, uy2, blue)

loop
    boxes (uy, uy2)
end loop


Author:  eNc [ Sat Jan 22, 2005 10:57 am ]
Post subject: 

ohh ok thanks 10 bits to u

Author:  eNc [ Sat Jan 22, 2005 11:42 am ]
Post subject: 

Sorry Double Post Can't Delete

Author:  eNc [ Sat Jan 22, 2005 11:47 am ]
Post subject: 

Check out the added code, is there anyway I can improve this?

code:

setscreen ("nocursor")
%setscreen ("offscreenonly")
var uy, uy2 : int := 190

var chars : array char of boolean
var iFont, count : int
count := 0
var start, info : string (1)
procedure intro
    iFont := Font.New ("Arial Black:24:bold,italic")
    Font.Draw ("Welcome to Pong", 150, 300, iFont, blue)
    locate (19, 32)
    put "Press Enter to Begin"
    locate (20, 30)
    put "Press I for instructions"
end intro

procedure boxes (var uy, uy2 : int)
    Input.KeyDown (chars)
    delay (50)
    if chars ('w') then
        if uy >= maxy - 40 then
            uy := maxy - 40
        elsif uy < maxy - 40 then
            uy := uy + 10
            cls
            drawfillbox (20, 40 + uy, 40, uy, blue)
            drawfillbox (maxx - 40, 40 + uy2, maxx - 60, uy2, blue)
            %View.Update
        end if
    elsif chars ('s') then
        if uy <= 0 then
            uy := 0
        elsif uy > 0 then
            uy := uy - 10
            cls
            drawfillbox (20, 40 + uy, 40, uy, blue)
            drawfillbox (maxx - 40, 40 + uy2, maxx - 60, uy2, blue)
            %View.Update
        end if
    end if
    if chars (KEY_UP_ARROW) then
        if uy2 >= maxy - 40 then
            uy2 := maxy - 40
        elsif uy2 < maxy - 40 then
            uy2 := uy2 + 10
            cls
            drawfillbox (maxx - 40, 40 + uy2, maxx - 60, uy2, blue)
            drawfillbox (20, 40 + uy, 40, uy, blue)
            %View.Update
        end if
    elsif chars (KEY_DOWN_ARROW) then
        if uy2 <= 0 then
            uy2 := 0
        elsif uy2 > 0 then
            uy2 := uy2 - 10
            cls
            drawfillbox (maxx - 40, 40 + uy2, maxx - 60, uy2, blue)
            drawfillbox (20, 40 + uy, 40, uy, blue)
            %View.Update
        end if
    end if
end boxes

procedure inmation
    locate (9, 35)
    put "Instructions"
    locate (12, 1)
    color (red)
    put "Left Paddle"
    color (black)
    put ""
    put "Press w to move paddle up."
    put "Press s to move paddle down."
    locate (12, 69)
    color (red)
    put "Right Paddle"
    color (black)
    locate (13, 70)
    put ""
    locate (14, 48)
    put "Press up arrow to move paddle up."
    locate (15, 44)
    put "Press down arrow to move paddle down."
    getch (info)
    if info = (KEY_ENTER) then
        cls
    end if
end inmation

drawfillbox (20, 40 + uy, 40, uy, blue)
drawfillbox (maxx - 40, 40 + uy2, maxx - 60, uy2, blue)

loop
    count := count + 1
    if count = 1 then
        cls
        intro
        getch (start)
        if start = KEY_ENTER then
            cls
            boxes (uy, uy2)
        elsif start = 'i' or start = 'I' then
            inmation
        end if
    else
        boxes (uy, uy2)
    end if

end loop

Author:  cool dude [ Sat Jan 22, 2005 12:17 pm ]
Post subject: 

looking good so far. change the background colour, add a ball, make score, make a high score list, but so far good. also when u press enter to play the game the paddles don't show up until u hit the key. thats easy to fix. all u have to do is give a starting point to the paddles so draw them first.

Author:  eNc [ Sat Jan 22, 2005 1:04 pm ]
Post subject: 

cool dude wrote:
looking good so far. change the background colour, add a ball, make score, make a high score list, but so far good. also when u press enter to play the game the paddles don't show up until u hit the key. thats easy to fix. all u have to do is give a starting point to the paddles so draw them first.



thanks

Author:  eNc [ Sat Jan 22, 2005 1:16 pm ]
Post subject: 

I've created the ball, however now the paddles won't move and I'm not sure how to do hit detection of the paddles, here's the code

code:

setscreen ("nocursor")
%setscreen ("offscreenonly")
var uy, uy2 : int := 190

var chars : array char of boolean
var iFont, count : int
count := 0
var start, info : string (1)
procedure intro
    iFont := Font.New ("Arial Black:24:bold,italic")
    Font.Draw ("Welcome to Pong", 150, 300, iFont, blue)
    locate (19, 32)
    put "Press Enter to Begin"
    locate (20, 30)
    put "Press I for instructions"
end intro

procedure boxes (var uy, uy2 : int)
    Input.KeyDown (chars)
    delay (50)
    if chars ('w') then
        if uy >= maxy - 40 then
            uy := maxy - 40
        elsif uy < maxy - 40 then
            uy := uy + 10
            cls
            drawfillbox (20, 40 + uy, 40, uy, blue)
            drawfillbox (maxx - 30, 40 + uy2, maxx - 50, uy2, blue)
            View.Update
        end if
    elsif chars ('s') then
        if uy <= 0 then
            uy := 0
        elsif uy > 0 then
            uy := uy - 10
            cls
            drawfillbox (20, 40 + uy, 40, uy, blue)
            drawfillbox (maxx - 30, 40 + uy2, maxx - 50, uy2, blue)
            View.Update
        end if
    end if
    if chars (KEY_UP_ARROW) then
        if uy2 >= maxy - 40 then
            uy2 := maxy - 40
        elsif uy2 < maxy - 40 then
            uy2 := uy2 + 10
            cls
            drawfillbox (maxx - 30, 40 + uy2, maxx - 50, uy2, blue)
            drawfillbox (20, 40 + uy, 40, uy, blue)
            View.Update
        end if
    elsif chars (KEY_DOWN_ARROW) then
        if uy2 <= 0 then
            uy2 := 0
        elsif uy2 > 0 then
            uy2 := uy2 - 10
            cls
            drawfillbox (maxx - 30, 40 + uy2, maxx - 50, uy2, blue)
            drawfillbox (20, 40 + uy, 40, uy, blue)
            View.Update
        end if
    end if
end boxes

procedure inmation
    locate (9, 35)
    put "Instructions"
    locate (12, 1)
    color (red)
    put "Left Paddle"
    color (black)
    put ""
    put "Press w to move paddle up."
    put "Press s to move paddle down."
    locate (12, 69)
    color (red)
    put "Right Paddle"
    color (black)
    locate (13, 70)
    put ""
    locate (14, 48)
    put "Press up arrow to move paddle up."
    locate (15, 44)
    put "Press down arrow to move paddle down."
    getch (info)
    if info = (KEY_ENTER) then
        cls
    end if
end inmation

procedure ball
    var dx, dy : int := 300
    var xc : int := 0
    loop
        dx := dx + 10
        dy := dy + 10
        if dx + 5 >= maxx - 50 and uy2 = dx - 10 or uy2 = dx + 10 then
            dx := dx - 10
        elsif dx + 5 <= 40 and uy = dx - 10 or uy = dx + 10 then
            dx := dx + 10
        end if
        if dy + 5 >= maxy - 5 then
            dy := dy - 10
        elsif dy + 5 <= 5 then
            dy := dy + 10
        end if
        drawfilloval (dx, dy, 5, 5, red)
        delay (10)
        drawfilloval (dx, dy, 5, 5, white)
    end loop
end ball

loop
    count := count + 1
    if count = 1 then
        cls
        intro
        getch (start)
        if start = KEY_ENTER then
            cls
            drawfillbox (20, 40 + uy, 40, uy, blue)
            drawfillbox (maxx - 30, 40 + uy2, maxx - 50, uy2, blue)

            boxes (uy, uy2)
            ball
        elsif start = 'i' or start = 'I' then
            inmation
        end if
    else

        boxes (uy, uy2)
        ball
    end if

end loop


Author:  basketball4ever [ Sat Jan 22, 2005 1:18 pm ]
Post subject: 

maybe add a little bit of difficulty??? like possibly... how to complete a level. or like a random "point" thing comes out and you have to get it in a certain amount of time.... too much boring pong games out Very Happy make urs fun Very Happy

Author:  eNc [ Sat Jan 22, 2005 1:21 pm ]
Post subject: 

basketball4ever wrote:
maybe add a little bit of difficulty??? like possibly... how to complete a level. or like a random "point" thing comes out and you have to get it in a certain amount of time.... too much boring pong games out Very Happy make urs fun Very Happy



lol yea but first things first i have to get it to work
Razz

Author:  eNc [ Sat Jan 22, 2005 1:22 pm ]
Post subject: 

now i have some hit dection with the boxes, but they won't move...

Author:  Bacchus [ Sat Jan 22, 2005 4:29 pm ]
Post subject: 

they wont move b/c you call the ball proc and then theres a loop in there, so it just keeps repeating that proc over and over. i sugest taking everything out of that proc and just stick it in ur main program

Author:  Unreal_Origin [ Sat Jan 22, 2005 5:04 pm ]
Post subject:  giveing my assistance again

here you go this should fix that up

code:

setscreen ("nocursor")
%setscreen ("offscreenonly")
var uy, uy2 : int := 190

var dx, dy : int := 300
var change_in_x, change_in_y := 2

var chars : array char of boolean
var iFont, count : int
count := 0
var start, info : string (1)
procedure intro
    iFont := Font.New ("Arial Black:24:bold,italic")
    Font.Draw ("Welcome to Pong", 150, 300, iFont, blue)
    locate (19, 32)
    put "Press Enter to Begin"
    locate (20, 30)
    put "Press I for instructions"
end intro

procedure boxes (var uy, uy2 : int)
    Input.KeyDown (chars)
    delay (10)
    if chars ('w') then
        if uy >= maxy - 40 then
            uy := maxy - 40
        elsif uy < maxy - 40 then
            uy := uy + 10
            cls
            drawfillbox (20, 40 + uy, 40, uy, blue)
            drawfillbox (maxx - 30, 40 + uy2, maxx - 50, uy2, blue)
            View.Update
        end if
    elsif chars ('s') then
        if uy <= 0 then
            uy := 0
        elsif uy > 0 then
            uy := uy - 10
            cls
            drawfillbox (20, 40 + uy, 40, uy, blue)
            drawfillbox (maxx - 30, 40 + uy2, maxx - 50, uy2, blue)
            View.Update
        end if
    end if
    if chars (KEY_UP_ARROW) then
        if uy2 >= maxy - 40 then
            uy2 := maxy - 40
        elsif uy2 < maxy - 40 then
            uy2 := uy2 + 10
            cls
            drawfillbox (maxx - 30, 40 + uy2, maxx - 50, uy2, blue)
            drawfillbox (20, 40 + uy, 40, uy, blue)
            View.Update
        end if
    elsif chars (KEY_DOWN_ARROW) then
        if uy2 <= 0 then
            uy2 := 0
        elsif uy2 > 0 then
            uy2 := uy2 - 10
            cls
            drawfillbox (maxx - 30, 40 + uy2, maxx - 50, uy2, blue)
            drawfillbox (20, 40 + uy, 40, uy, blue)
            View.Update
        end if
    end if
end boxes

procedure inmation
    locate (9, 35)
    put "Instructions"
    locate (12, 1)
    color (red)
    put "Left Paddle"
    color (black)
    put ""
    put "Press w to move paddle up."
    put "Press s to move paddle down."
    locate (12, 69)
    color (red)
    put "Right Paddle"
    color (black)
    locate (13, 70)
    put ""
    locate (14, 48)
    put "Press up arrow to move paddle up."
    locate (15, 44)
    put "Press down arrow to move paddle down."
    getch (info)
    if info = (KEY_ENTER) then
        cls
    end if
end inmation

procedure ball (var dx, dy : int, var change_in_x, change_in_y : int)
    drawfilloval (dx, dy, 5, 5, white)
    dx := dx + change_in_x
    dy := dy + change_in_y

    if dy > maxy - 10 or dy < 10 then
        change_in_y := change_in_y * -1
    elsif dx > maxx - 10 or dx < 10 then
        change_in_x := change_in_x * -1
    end if

    drawfilloval (dx, dy, 5, 5, red)
    View.Update
end ball
dx := 300
dy := 300
change_in_x := 2
change_in_y := 2

loop
    count := count + 1
    if count = 1 then
        cls
        intro
        getch (start)
        if start = KEY_ENTER then
            cls
            drawfillbox (20, 40 + uy, 40, uy, blue)
            drawfillbox (maxx - 30, 40 + uy2, maxx - 50, uy2, blue)

            boxes (uy, uy2)
            ball (dx, dy, change_in_x, change_in_y)
        elsif start = 'i' or start = 'I' then
            inmation
        end if
    else

        boxes (uy, uy2)
        ball (dx, dy, change_in_x, change_in_y)
    end if

end loop


Author:  eNc [ Sat Jan 22, 2005 5:18 pm ]
Post subject: 

Here's my code it check to see if it bounces, but it doesn't toally work


code:

setscreen ("nocursor")
%setscreen ("offscreenonly")
var uy, uy2 : int := 190
var chars : array char of boolean
var iFont, count : int
var mem := 0
var dx, dy : int := 300
count := 0
var start, info : string (1)
procedure intro
    iFont := Font.New ("Arial Black:24:bold,italic")
    Font.Draw ("Welcome to Pong", 150, 300, iFont, blue)
    locate (19, 32)
    put "Press Enter to Begin"
    locate (20, 30)
    put "Press I for instructions"
end intro


procedure ball
    var xc : int := 0
    loop
        xc += 1
        if dx >= (maxx - 50) or dy >= (maxy - 5) or dy <= (0) then
            dx := dx - 10
            dy := dy - 10
            mem := 1
        elsif dx <= (50) or dy >= (maxy - 5) or dy <= (0) then
            dx := dx + 10
            dy := dy + 10
            mem := 0
        end if
        if dx <= maxx - 50 and dx >= 50 and dy < maxy - 5 and dy > 0 and mem = 1 then
            dx += 10
            dy += 10
        elsif dx <= maxx - 50 and dx >= 50 and dy < maxy - 5 and dy > 0 and mem = 0 then
            dx -= 10
            dy -= 10
        end if
        drawfilloval (dx, dy, 5, 5, red)
        delay (10)
        cls
        exit when xc = 1
    end loop
end ball

procedure boxes (var uy, uy2 : int)
    Input.KeyDown (chars)
    delay (50)
    if chars ('w') then
        if uy >= maxy - 40 then
            uy := maxy - 40
        elsif uy < maxy - 40 then
            uy := uy + 10
            cls
            drawfillbox (20, 40 + uy, 40, uy, blue)
            drawfillbox (maxx - 30, 40 + uy2, maxx - 50, uy2, blue)
            View.Update
        end if
    elsif chars ('s') then
        if uy <= 0 then
            uy := 0
        elsif uy > 0 then
            uy := uy - 10
            cls
            drawfillbox (20, 40 + uy, 40, uy, blue)
            drawfillbox (maxx - 30, 40 + uy2, maxx - 50, uy2, blue)
            View.Update
        end if
    end if
    if chars (KEY_UP_ARROW) then
        if uy2 >= maxy - 40 then
            uy2 := maxy - 40
        elsif uy2 < maxy - 40 then
            uy2 := uy2 + 10
            cls
            drawfillbox (maxx - 30, 40 + uy2, maxx - 50, uy2, blue)
            drawfillbox (20, 40 + uy, 40, uy, blue)
            View.Update
        end if
    elsif chars (KEY_DOWN_ARROW) then
        if uy2 <= 0 then
            uy2 := 0
        elsif uy2 > 0 then
            uy2 := uy2 - 10
            cls
            drawfillbox (maxx - 30, 40 + uy2, maxx - 50, uy2, blue)
            drawfillbox (20, 40 + uy, 40, uy, blue)
            View.Update
        end if
    end if

end boxes

procedure inmation
    locate (9, 35)
    put "Instructions"
    locate (12, 1)
    color (red)
    put "Left Paddle"
    color (black)
    put ""
    put "Press w to move paddle up."
    put "Press s to move paddle down."
    locate (12, 69)
    color (red)
    put "Right Paddle"
    color (black)
    locate (13, 70)
    put ""
    locate (14, 48)
    put "Press up arrow to move paddle up."
    locate (15, 44)
    put "Press down arrow to move paddle down."
    getch (info)
    if info = (KEY_ENTER) then
        cls
    end if
end inmation

dy += 10
dx += 10
loop
    %count := count + 1
    %if count = 1 then
    %cls
    %intro
    %getch (start)
    %if start = KEY_ENTER then
    %cls
    drawfillbox (20, 40 + uy, 40, uy, blue)
    drawfillbox (maxx - 30, 40 + uy2, maxx - 50, uy2, blue)

    boxes (uy, uy2)
    ball
    %elsif start = 'i' or start = 'I' then
    %   inmation
    %end if
    %else
    %boxes (uy, uy2)
    %ball (dx, dy)
    %end if

end loop

Author:  Unreal_Origin [ Sat Jan 22, 2005 6:30 pm ]
Post subject:  or you could use what i gave you?

yo i already posted you something that works.

All you have to do is, redo the x coordinates so that it redraws the ball at the start and then point (x= 300, y = 300) then add a point to the other team, BUT if the paddle hits the ball it should bounce off. i don't want to program that for you, b/c i am to lazy

Author:  Unreal_Origin [ Sat Jan 22, 2005 6:31 pm ]
Post subject:  or you could use what i gave you?

sry double post i it b/c of dial up

Author:  Kyle [ Sat Jan 22, 2005 7:21 pm ]
Post subject: 

true the ball blinks and there is lag. I dont no if the first pong laged. i think my computer is also better then the old ones. you both need to fix some collision detection, but all in all there good. Very Happy

Author:  Unreal_Origin [ Sat Jan 22, 2005 7:31 pm ]
Post subject:  if you actually read the posts

hey if you read the posts you would see that i am letting them do it since i am to lazy to actually do it, also if you look at the one at the top their is no flicking

anywho later

Author:  eNc [ Sun Jan 23, 2005 2:37 pm ]
Post subject: 

Ok here's my code, I doesn't do collision detection for the left paddle, I'm not sure why

code:

var window : int
window := Window.Open ("title:Pong,nocursor")
var uy, uy2 : int := 190
var dx, dy : int := 300
var cx, cy := 2
var chars : array char of boolean
var again : array char of boolean
var iFont, count, rpoint, lpoint, check, amount, udef : int
count := 0
amount := 0
udef := 0
rpoint := 0
lpoint := 0
check := 0
var start, info : string (1)
procedure intro
    iFont := Font.New ("Arial Black:24:bold,italic")
    Font.Draw ("Welcome to Pong", 155, 300, iFont, blue)
    locate (19, 30)
    put "Press Enter to Begin"
    locate (20, 28)
    put "Press i for instructions"
    locate (21, 33)
    put "Press q to quit"
end intro

procedure boxdraw
    drawfillbox (20, 40 + uy, 40, uy, blue)
    drawfillbox (maxx - 30, 40 + uy2, maxx - 50, uy2, blue)
end boxdraw


procedure boxes (var uy, uy2 : int)
    Input.KeyDown (chars)
    delay (10)
    if chars ('w') then
        if uy >= maxy - 40 then
            uy := maxy - 40
        elsif uy < maxy - 40 then
            uy := uy + 7
            cls
            boxdraw
        end if
    elsif chars ('s') then
        if uy <= 0 then
            uy := 0
        elsif uy > 0 then
            uy := uy - 7
            cls
            boxdraw
        end if
    end if
    if chars (KEY_UP_ARROW) then
        if uy2 >= maxy - 40 then
            uy2 := maxy - 40
        elsif uy2 < maxy - 40 then
            uy2 := uy2 + 7
            cls
            boxdraw
        end if
    elsif chars (KEY_DOWN_ARROW) then
        if uy2 <= 0 then
            uy2 := 0
        elsif uy2 > 0 then
            uy2 := uy2 - 7
            cls
            boxdraw
        end if
    end if
end boxes

procedure inmation
    locate (9, 35)
    put "Instructions"
    locate (12, 1)
    color (red)
    put "Left Paddle"
    color (black)
    put ""
    put "Press w to move paddle up."
    put "Press s to move paddle down."
    locate (12, 69)
    color (red)
    put "Right Paddle"
    color (black)
    locate (13, 70)
    put ""
    locate (14, 48)
    put "Press up arrow to move paddle up."
    locate (15, 44)
    put "Press down arrow to move paddle down."
    getch (info)
    if info = (KEY_ENTER) then
        cls
        delay (20)
        intro
    else
        Window.Close (window)
    end if
end inmation

procedure lpoints
    locate (1, 1)
    put "", lpoint, "" ..
end lpoints

procedure rpoints
    locate (1, 79)
    put "", rpoint, "" ..
end rpoints

%Collision Detection
procedure ball (var dx, dy : int, var cx, cy : int)
    cls
    boxdraw
    rpoints
    lpoints
    dx := dx + cx
    dy := dy + cy
    if dy > maxy - 5 then
        cy := cy * -1
    elsif dy < 5 then
        cy := cy * 1
    end if
    if (dx >= maxx - 60) then
        for i : 0 .. 20
            if (dy = uy2 + i) then
                cy := cy * -1
                cx := cx * -1
            end if
        end for
    elsif (dx = 60) then
        for i : 0 .. 20
            if (dy = uy + i) then
                cy := cy * 1
                cx := cx * 1
            end if
        end for
    end if

    drawfilloval (dx, dy, 5, 5, red)
end ball

%Controls start position of ball
dx := 300
dy := 300

%controls speed of ball
cx := 2
cy := 2

procedure game
    delay (5)
    drawfillbox (20, 40 + uy, 40, uy, blue)
    drawfillbox (maxx - 30, 40 + uy2, maxx - 50, uy2, blue)
    ball (dx, dy, cx, cy)
    if dx > maxx - 5 and amount >= udef then
        check := 1
    elsif dx < 5 and amount >= udef then
        check := 1
    elsif dx > maxx - 5 and amount <= udef then
        lpoint := lpoint + 1
        amount := amount + 1
        delay (10)
        cls
        dx := 300
        dy := 300
        drawfillbox (20, 40 + uy, 40, uy, blue)
        drawfillbox (maxx - 30, 40 + uy2, maxx - 50, uy2, blue)
        delay (100)
        ball (dx, dy, cx, cy)
        boxes (uy, uy2)
    elsif dx < 5 and amount <= udef then
        rpoint := rpoint + 1
        amount := amount + 1
        delay (10)
        cls
        dx := 300
        dy := 300
        drawfillbox (20, 40 + uy, 40, uy, blue)
        drawfillbox (maxx - 30, 40 + uy2, maxx - 50, uy2, blue)
        delay (100)
        ball (dx, dy, cx, cy)
        boxes (uy, uy2)
    end if
    boxes (uy, uy2)
    delay (10)
end game

procedure trapudef
    if udef <= 200 and udef > 2 then
        game
    else
        locate (24, 23)
        put "Error, Enter a number from 3 to 200"
        locate (25, 35)
        put "Restarting.."
        delay (3000)
        cls
        intro
    end if
end trapudef

procedure redraw
    cls
    dx := 300
    dy := 300
    drawfillbox (20, 40 + uy, 40, uy, blue)
    drawfillbox (maxx - 30, 40 + uy2, maxx - 50, uy2, blue)
    delay (30)
    ball (dx, dy, cx, cy)
    boxes (uy, uy2)
end redraw

procedure main (var check : int)
    count := count + 1
    if count = 1 then
        cls
        intro
        getch (start)
        if start = KEY_ENTER then
            locate (22, 20)
            put "Enter amount of rounds you wish to play"
            locate (23, 40)
            get udef
            trapudef
        elsif start = 'i' or start = 'I' then
            inmation
        elsif start = 'q' or start = 'Q' then
            Window.Close (window)
        end if
    else
        ball (dx, dy, cx, cy)
        if dx > maxx - 5 and amount >= udef then
            check := 1
        elsif dx < 5 and amount >= udef then
            check := 1
        elsif dx > maxx - 5 and amount <= udef then
            lpoint := lpoint + 1
            amount := amount + 1
            redraw
        elsif dx < 5 and amount <= udef then
            rpoint := rpoint + 1
            amount := amount + 1
            cls
            redraw
        else
        end if
        boxes (uy, uy2)
    end if
end main
main (check)
amount := amount + 1

procedure resetvars
    check := 0
    dx := 300
    dy := 300
    rpoint := 0
    lpoint := 0
    count := 0
end resetvars

loop
    for c : 1 .. udef
        if check not= 1 then
            main (check)
            lpoints
            rpoints
        end if
        if check = 1 and amount >= udef then
            drawfillbox (0, 0, maxx, maxy, white)
            cls
            locate (12, 37)
            put "Play Again?"
            Input.KeyDown (again)
            if again ('y') or again ('Y') or again (KEY_ENTER) then
                cls
                resetvars
                redraw
                main (check)
            elsif again ('n') or again ('N') then
                intro
            end if
            delay (100)

        end if
    end for
end loop

Author:  Unreal_Origin [ Tue Jan 25, 2005 8:44 pm ]
Post subject:  here you go colision detection

hey so here it is

code:

var window : int
window := Window.Open ("title:Pong,nocursor")

const PADDLE_LENGTH := 80

var uy, uy2 : int := 190
var dx, dy : int := 300
var cx, cy := 2
var chars : array char of boolean
var again : array char of boolean
var iFont, count, rpoint, lpoint, check, amount, udef : int
count := 0
amount := 0
udef := 0
rpoint := 0
lpoint := 0
check := 0
var start, info : string (1)
procedure intro
    iFont := Font.New ("Arial Black:24:bold,italic")
    Font.Draw ("Welcome to Pong", 155, 300, iFont, blue)
    locate (19, 30)
    put "Press Enter to Begin"
    locate (20, 28)
    put "Press i for instructions"
    locate (21, 33)
    put "Press q to quit"
end intro

procedure boxdraw
    drawfillbox (20, PADDLE_LENGTH + uy, 40, uy, blue)
    drawfillbox (maxx - 30, PADDLE_LENGTH + uy2, maxx - 50, uy2, blue)
end boxdraw


procedure boxes (var uy, uy2 : int)
    Input.KeyDown (chars)
    delay (5)
    if chars ('w') then
        if uy >= maxy - PADDLE_LENGTH then
            uy := maxy - PADDLE_LENGTH
        elsif uy < maxy - PADDLE_LENGTH then
            uy := uy + 7
            cls
            boxdraw
        end if
    elsif chars ('s') then
        if uy <= 0 then
            uy := 0
        elsif uy > 0 then
            uy := uy - 7
            cls
            boxdraw
        end if
    end if
    if chars (KEY_UP_ARROW) then
        if uy2 >= maxy - PADDLE_LENGTH then
            uy2 := maxy - PADDLE_LENGTH
        elsif uy2 < maxy - PADDLE_LENGTH then
            uy2 := uy2 + 7
            cls
            boxdraw
        end if
    elsif chars (KEY_DOWN_ARROW) then
        if uy2 <= 0 then
            uy2 := 0
        elsif uy2 > 0 then
            uy2 := uy2 - 7
            cls
            boxdraw
        end if
    end if
end boxes

procedure inmation
    locate (9, 35)
    put "Instructions"
    locate (12, 1)
    color (red)
    put "Left Paddle"
    color (black)
    put ""
    put "Press w to move paddle up."
    put "Press s to move paddle down."
    locate (12, 69)
    color (red)
    put "Right Paddle"
    color (black)
    locate (13, 70)
    put ""
    locate (14, 48)
    put "Press up arrow to move paddle up."
    locate (15, 44)
    put "Press down arrow to move paddle down."
    getch (info)
    if info = (KEY_ENTER) then
        cls
        delay (20)
        intro
    else
        Window.Close (window)
    end if
end inmation

procedure lpoints
    locate (1, 1)
    put "", lpoint, "" ..
end lpoints

procedure rpoints
    locate (1, 79)
    put "", rpoint, "" ..
end rpoints

%Collision Detection
procedure ball (var dx, dy : int, var cx, cy : int)
    cls
    boxdraw
    rpoints
    lpoints
    dx := dx + cx
    dy := dy + cy
    if dy > maxy - 5 then
        cy := cy * -1
    elsif dy < 5 then
        cy := cy * -1
    end if

    if dx = 45 then
        if dy > uy and dy < uy + PADDLE_LENGTH then
            cx := cx * -1
        end if
    elsif dx = maxx - 55 then
        if dy > uy2 and dy < uy2 + PADDLE_LENGTH then
            cx := cx * -1
        end if
    end if

    % if (dx >= maxx - 60) then
    %     for i : 0 .. 20
    %         if (dy = uy2 + i) then
    %             cy := cy * -1
    %             cx := cx * -1
    %         end if
    %     end for
    % elsif (dx = 60) then
    %     for i : 0 .. 20
    %         if (dy = uy + i) then
    %             cy := cy * 1
    %             cx := cx * 1
    %         end if
    %     end for
    % end if

    drawfilloval (dx, dy, 5, 5, red)
end ball

%Controls start position of ball
dx := 300
dy := 300

%controls speed of ball
cx := 1
cy := 1

procedure game
    delay (5)
    drawfillbox (20, 40 + uy, 40, uy, blue)
    drawfillbox (maxx - 30, 40 + uy2, maxx - 50, uy2, blue)
    ball (dx, dy, cx, cy)
    if dx > maxx - 5 and amount >= udef then
        check := 1
    elsif dx < 5 and amount >= udef then
        check := 1
    elsif dx > maxx - 5 and amount <= udef then
        lpoint := lpoint + 1
        amount := amount + 1
        delay (10)
        cls
        dx := 300
        dy := 300
        drawfillbox (20, 40 + uy, 40, uy, blue)
        drawfillbox (maxx - 30, 40 + uy2, maxx - 50, uy2, blue)
        delay (100)
        ball (dx, dy, cx, cy)
        boxes (uy, uy2)
    elsif dx < 5 and amount <= udef then
        rpoint := rpoint + 1
        amount := amount + 1
        delay (10)
        cls
        dx := 300
        dy := 300
        drawfillbox (20, 40 + uy, 40, uy, blue)
        drawfillbox (maxx - 30, 40 + uy2, maxx - 50, uy2, blue)
        delay (100)
        ball (dx, dy, cx, cy)
        boxes (uy, uy2)
    end if
    boxes (uy, uy2)
    delay (10)
end game

procedure trapudef
    if udef <= 200 and udef > 2 then
        game
    else
        locate (24, 23)
        put "Error, Enter a number from 3 to 200"
        locate (25, 35)
        put "Restarting.."
        delay (3000)
        cls
        intro
    end if
end trapudef

procedure redraw
    cls
    dx := 300
    dy := 300
    drawfillbox (20, 40 + uy, 40, uy, blue)
    drawfillbox (maxx - 30, 40 + uy2, maxx - 50, uy2, blue)
    delay (30)
    ball (dx, dy, cx, cy)
    boxes (uy, uy2)
end redraw

procedure main (var check : int)
    count := count + 1
    if count = 1 then
        cls
        intro
        getch (start)
        if start = KEY_ENTER then
            locate (22, 20)
            put "Enter amount of rounds you wish to play"
            locate (23, 40)
            get udef
            trapudef
        elsif start = 'i' or start = 'I' then
            inmation
        elsif start = 'q' or start = 'Q' then
            Window.Close (window)
        end if
    else
        ball (dx, dy, cx, cy)
        if dx > maxx - 5 and amount >= udef then
            check := 1
        elsif dx < 5 and amount >= udef then
            check := 1
        elsif dx > maxx - 5 and amount <= udef then
            lpoint := lpoint + 1
            amount := amount + 1
            redraw
        elsif dx < 5 and amount <= udef then
            rpoint := rpoint + 1
            amount := amount + 1
            cls
            redraw
        else
        end if
        boxes (uy, uy2)
    end if
end main
main (check)
amount := amount + 1

procedure resetvars
    check := 0
    dx := 300
    dy := 300
    rpoint := 0
    lpoint := 0
    count := 0
end resetvars

loop
    for c : 1 .. udef
        if check not= 1 then
            main (check)
            lpoints
            rpoints
        end if
        if check = 1 and amount >= udef then
            drawfillbox (0, 0, maxx, maxy, white)
            cls
            locate (12, 37)
            put "Play Again?"
            Input.KeyDown (again)
            if again ('y') or again ('Y') or again (KEY_ENTER) then
                cls
                resetvars
                redraw
                main (check)
            elsif again ('n') or again ('N') then
                intro
            end if
            delay (100)

        end if
    end for
end loop


i would highly suggest using view.update since it flickers so much

Author:  Pip_Jr [ Thu Feb 10, 2005 12:45 pm ]
Post subject: 

I Tryed your game... I couldn't move pattels and the ball moved to fast Confused
This got annoying after the 1000th time lol


: