Computer Science Canada

Help on Pong Program

Author:  Comp_Expert777 [ Thu Dec 25, 2003 10:10 am ]
Post subject:  Help on Pong Program

I need help on my pong game. I cant figure out how to make the ball bounce off the paddles on my pong program. Can someone help me?


Here is the code:

drawfillbox (0,0,maxx,maxy,black)

var font1, font2, font3 : int
font1:= Font.New ("serif:220:bold")
font2 := Font.New ("sans serif:15:bold")
font3 := Font.New ("sans serif:15:bold")
Font.Draw ("Pong", 5,150,font1,white)

Font.Draw ("Start Game", 265,100,font2,white)
Font.Draw ("Quit Game", 265,50,font3,white)
drawfillbox (50,20,150,30,brightblue)
drawfillbox (405,375,500,385,brightred)

var ch : string (1)
var x, y, btnnum, btnupdown : int
loop
if buttonmoved ("down") then
buttonwait ("down", x,y,btnnum,btnupdown)
locate (1,1)
for i : 1..100
%Variables
var y1 : int
var x1 : int
var c : int
if x > 256 and x < 400 then
if y > 100 and y < 300 then
View.Set ("offscreenonly")
View.Update
cls
%Second Page
View.Set ("offscreenonly")
var chars : array char of boolean
var x2,y2,x3,y3 : int := 0
var row : int := 630
var column : int :=2
loop

var rowchange, columnchange := 1
loop
drawfillbox (0,0,maxx,maxy,black)
if row = 630 or row= 0 then
rowchange := -rowchange
end if
if column= 394 or column = 0 then
columnchange := -columnchange
end if
locatexy (row,column)
drawfilloval (row,column,10,10,brightgreen)
row :=row + rowchange
column:= column+columnchange

Input.KeyDown (chars)
locate (1,1)
drawfillbox (x2+5,y2,x2+15,y2+70,brightblue)
if chars (KEY_UP_ARROW) then
y2 := y2+2
end if
if chars (KEY_DOWN_ARROW) then
y2 := y2-2
end if
if y2<0 then
y2:=0
end if
if y2>328 then
y2:=328
end if

Input.KeyDown (chars)
locate (1,1)
drawfillbox (x3+635,y3,x3+625,y3+70,brightblue)
if chars ('w') then
y3 := y3+2
end if
if chars ('s') then
y3 := y3-2
end if
if y3<0 then
y3:=0
end if
if y3>328 then
y3:=328
end if
drawline(maxx div 2,0,maxx div 2,maxy,brightgreen)
View.Update
cls
drawfillbox (0,0,maxx,maxy,black)
end loop
end loop
end if
end if
end for
end if
end loop

Author:  DanShadow [ Thu Dec 25, 2003 11:15 am ]
Post subject: 

An easier way, (but a little worse) is this way. Take a look:
code:

Draw.FillBox (0, 0, maxx, maxy, 255)
var ballx, bally, px, py, xstep, ystep, score : int := 0
var key : string (1) := ""
ballx := 230
bally := 200
xstep := 1
ystep := 1
px := 10
py := 200
loop
    if hasch then
        getch (key)
    end if
    Draw.FillOval (ballx, bally, 10, 10, 255)
    Draw.FillBox (px - 8, py - 20, px, py + 20, 1)
    delay (5)
    ballx := ballx + xstep
    bally := bally + ystep
    Draw.FillOval (ballx, bally, 10, 10, 12)
    Draw.FillBox (px - 20, py - 50, px, py + 50, 255)
    if ballx + 10 >= maxx then
        xstep := -1
    elsif ballx - 10 <= 0 then
        xstep := 1
    end if
    if bally + 10 >= maxy then
        ystep := -1
    elsif bally - 10 <= 0 then
        ystep := 1
    end if
    if key = (KEY_DOWN_ARROW) then
        py := py - 1
    elsif key = (KEY_UP_ARROW) then
        py := py + 1
    end if
    if py - 20 <= 0 then
        py := py + 1
        key := ""
    elsif py + 20 >= maxy then
        py := py - 1
        key := ""
    end if
    if whatdotcolor (ballx - 10, bally) = 1 then
        xstep := 1
        for i : 1 .. 10
            ballx := ballx + xstep
            bally := bally + ystep
        end for
    end if
    if ballx - 11 <= 0 then
        score := score + 1
        xstep := 1
        for i : 1 .. 5
            ballx := ballx + xstep
            bally := bally + ystep
        end for
    end if

    locate (12, 25)
    colorback (255)
    color (0)
    put "Ball Got Passed ", score, " times."
end loop

Author:  Comp_Expert777 [ Thu Dec 25, 2003 12:42 pm ]
Post subject: 

Where can I put that in my code? I am sorry for asking that question. I still have a little trouble with Turing so I can get some help plz but thanks for posting the code.

Author:  Comp_Expert777 [ Thu Jan 15, 2004 10:34 am ]
Post subject: 

Thanks I got the ball bounce off the paddle now but I am trying to make a score board but I dont know y it doesnt work. Here is my code take a look at it. Can u plz help me this is a final project and i only have 3 three more days left.

code:

%Pong Game

%Background Colour
drawfillbox (0,0,maxx,maxy,black)

var font1, font2, font3 : int
font1:= Font.New ("serif:220:bold")
font2 := Font.New ("sans serif:15:bold")
font3 := Font.New ("sans serif:15:bold")
Font.Draw ("Pong", 5,150,font1,white)

Font.Draw ("Start Game", 265,100,font2,white)
Font.Draw ("Quit Game", 265,50,font3,white)
drawfillbox (50,20,150,30,brightblue)
drawfillbox (405,375,500,385,brightred)

var ch : string (1)
var x, y, btnnum, btnupdown : int
loop
if buttonmoved ("down") then
buttonwait ("down", x,y,btnnum,btnupdown)
locate (1,1)
for i : 1..100
%Variables
var y1 : int
var x1 : int
var c : int
if x  > 256 and x < 400 then
if y > 100 and y < 300 then
View.Set ("offscreenonly")
View.Update
cls
%Second Page
View.Set ("offscreenonly")
var chars : array char of boolean
var x2,y2,x3,y3 : int := 0
var a : int
a := 200
var p2,p1:int
p2:=0
p1:=0
loop

View.Set ("offscreenonly")
var ballx,bally : int :=200
var xstep,ystep : int := 1
loop
ballx:=ballx+xstep
bally:=bally+ystep
drawfilloval(ballx, bally, 10, 10, 0)
delay(5)
drawfilloval(ballx, bally, 10, 10, 12)
if ballx+15>=maxx then
xstep:=-1
elsif ballx-15<=0 then
xstep:=+1
elsif bally+15>=maxy then
ystep:=-1
elsif bally-15<=0 then
ystep:=+1
end if


put "P1:",p1
put "P2:",p2
if a-15<=0 then
p2:=p2+1
elsif a+15>=maxx then
p1:=p1+1
end if


Input.KeyDown (chars)
locate (1,1)
drawfillbox (x2+5,y2,x2+15,y2+70,brightblue)
if chars (KEY_UP_ARROW) then
y2 := y2+2
end if
if chars (KEY_DOWN_ARROW) then
y2 := y2-2
end if
if y2<0 then
y2:=0
end if
if y2>328 then
y2:=328
end if

Input.KeyDown (chars)
locate (1,1)
drawfillbox (x3+635,y3,x3+625,y3+70,brightblue)
if chars ('w') then
y3 := y3+2
end if
if chars ('s') then
y3 := y3-2
end if
if y3<0 then
y3:=0
end if
if y3>328 then
y3:=328
end if
drawline(maxx div 2,0,maxx div 2,maxy,brightgreen)
View.Update
cls
drawfillbox (0,0,maxx,maxy,black)
end loop
end loop
end if
end if
end for
end if
end loop

Author:  DanShadow [ Thu Jan 15, 2004 1:29 pm ]
Post subject: 

Well try this scoreboard, but change variable names to suit your program:
code:

var p1_score,p2_score:int:=0
proc scoreboard
Draw.Box(220,150,420,250,255)
locate(11,25)
put "Player 1 Score: ",p1_score
locate(13,25)
put "Player 2 Score: ",p2_score
end scoreboard

%Main Program
scoreboard

Yes, the "locate" command is locating the text in the wrong area, but I dont have Turing on this computer, so try and fix it yourself. Wink
But the basic design of that should work for your program.

Author:  Comp_Expert777 [ Thu Jan 15, 2004 10:14 pm ]
Post subject: 

Their is a problem on your code and I can figure out wut it means. When I run the program it says 'procedure's may only be declared at the program, module, or monitor level and it highlights proc on ur code and says that. Can u plz help me?

Author:  Thuged_Out_G [ Fri Jan 16, 2004 9:59 am ]
Post subject: 

you have to declare procedures at the top of your program....with the rest of your procedures

Author:  Comp_Expert777 [ Sat Jan 17, 2004 1:23 pm ]
Post subject: 

I dont have any procedures in my program because I dont really get procedures that much so can you plz help me.

Author:  Thuged_Out_G [ Sat Jan 17, 2004 1:44 pm ]
Post subject: 

that scoreboard you were giving is a procedure

Author:  Comp_Expert777 [ Mon Jan 19, 2004 5:02 pm ]
Post subject: 

I got the scoreboard to work but when the ball hits the paddle too it counts as a point too so can u help me on it here is the code:

code:


drawfillbox (0,0,maxx,maxy,black)

var font1, font2, font3 : int
font1:= Font.New ("serif:220:bold")
font2 := Font.New ("sans serif:15:bold")
font3 := Font.New ("sans serif:15:bold")
Font.Draw ("Pong", 5,150,font1,white)

Font.Draw ("Start Game", 265,100,font2,white)
Font.Draw ("Quit Game", 265,50,font3,white)
drawfillbox (50,20,150,30,brightblue)
drawfillbox (405,375,500,385,brightred)

var ch : string (1)
var x, y, btnnum, btnupdown : int
loop
if buttonmoved ("down") then
buttonwait ("down", x,y,btnnum,btnupdown)
locate (1,1)
for i : 1..100
%Variables
var y1 : int
var x1 : int
var c : int
if x  > 256 and x < 400 then
if y > 100 and y < 300 then
View.Set ("offscreenonly")
View.Update
cls
%Second Page
View.Set ("offscreenonly")
var chars : array char of boolean
var x2,y2,x3,y3 : int := 0
var a : int
a := 200
var p2,p1:int
p2:=0
p1:=0
loop

View.Set ("offscreenonly")
var ballx,bally : int := 200
var xstep,ystep : int := 1
loop
ballx:=ballx+xstep
bally:=bally+ystep
drawfilloval(ballx, bally, 10, 10, brightred)
if ballx+15>=maxx then
xstep:=-1
elsif ballx-15<=0 then
xstep:=+1
elsif bally+15>=maxy then
ystep:=-1
elsif bally-15<=0 then
ystep:=+1
end if


put "P1:",p1
put "P2:",p2
if ballx+15>=maxx then
p1:=p1+1
elsif ballx-15<=0 then
p2:=p2+1
end if


Input.KeyDown (chars)
locate (1,1)
drawfillbox (x2+5,y2,x2+15,y2+70,brightblue)
if chars ('w') then
y2 := y2+5
end if
if chars ('s') then
y2 := y2-5
end if
if y2<0 then
y2:=0
end if
if y2>328 then
y2:=328
end if

Input.KeyDown (chars)
locate (1,1)
drawfillbox (x3+635,y3,x3+625,y3+70,brightblue)
if chars (KEY_UP_ARROW) then
y3 := y3+5
end if
if chars (KEY_DOWN_ARROW) then
y3 := y3-5
end if
if y3<0 then
y3:=0
end if
if y3>328 then
y3:=328
end if
drawline(maxx div 2,0,maxx div 2,maxy,brightgreen)
View.Update
cls
drawfillbox (0,0,maxx,maxy,black)
end loop
end loop
end if
end if
end for
end if
end loop

Author:  Eulogy [ Mon Mar 29, 2004 6:03 pm ]
Post subject:  Bump

Bump. Same problem with the scoreboard suggested. Any one know of a solution?

Author:  Tony [ Mon Mar 29, 2004 6:23 pm ]
Post subject: 

umm... Confused That's because the paddles don't deflect the ball Laughing it goes right though them and hits the score line


: