
-----------------------------------
uknowhoiam
Fri Mar 13, 2009 4:39 pm

I need help with pong
-----------------------------------
I have alot of questions about this program but the main one is that how to make my ball bounce back when it hits the bar, i've tried everything but nothing works and my flickering will NOT STOP omg my head hurts... its been 2 weeks since i've been trying to fix it

here's my code, can some one plz help me


View.Set ("graphics:650,400;offscreenonly")
Mouse.ButtonChoose ("multibutton")

var xpos, ypos, ypos1, ypos2, ypos3, ypos4 : int := 1
var x : int := maxx div 2
var y : int := maxy div 2
var y1 : int := maxy div 2 - 35
var y2 : int := maxy div 2 + 35
var y3 : int := maxy div 2 - 35
var y4 : int := maxy div 2 + 35
var key : string (1)
var x5, y5, button : int
var xpos1 : int := 1
var xmov : int := maxx div 2
var x1mov : int := maxy div 2 - 35
var x2mov : int := maxy div 2 + 35


colourback (black)

cls

procedure leftbar
    drawfillbox (0, y1, 5, y2, white)
    View.Update
end leftbar

procedure rightbar
    drawfillbox (maxx - 5, y3, maxx, y4, white)
    View.Update
end rightbar

procedure keyboard

    %player 1
    drawfillbox (0, y1, 5, y2, white)
    View.Update
    delay (2)
    cls

    if ord (key) = 203 then
        y1 := y1 + 10
        y2 := y2 + 10
        x1mov := x1mov + 1
        x2mov := x2mov + 1
    elsif ord (key) = 205 then
        y1 := y1 - 10
        y2 := y2 - 10
        xpos1 := -xpos1
        x1mov := x1mov - 1
        x2mov := x2mov - 1
    end if
end keyboard

procedure mouse
    %player 2
    drawfillbox (maxx - 5, y3, maxx, y4, white)

    View.Update
    delay (5)
    cls

    if button = 1 then
        y3 := y3 - 5
        y4 := y4 - 5
    elsif button = 100 then
        y3 := y3 + 5
        y4 := y4 + 5
    end if
end mouse

procedure ball

    %ball
    drawfilloval (x, y, 5, 5, white)

    delay (5)

    if y < maxy div 2 then
        xmov := xmov - 1
    elsif y > maxy div 2 then
        xmov := xmov + 1
    end if

    if xmov >= x1mov or xmov = maxx - 5 then
        xpos := -xpos
    end if

    if y < 5 or y >= maxy - 5 then
        ypos := -ypos
    end if

end ball

loop
    ball
    leftbar
    rightbar

    x := x + xpos
    y := y + ypos


    Mouse.Where (x5, y5, button)
    if Mouse.ButtonMoved ("Down") then
        mouse
    end if

    if hasch then
        getch (key)
        keyboard
    end if
    
    if x = 5 or x = maxx - 5 then
        exit
    end if
    
    cls
end loop

Mod Edit: Remember to use syntax tags! Thanks :) Code Here

-----------------------------------
corriep
Fri Mar 13, 2009 9:51 pm

Re: I need help with pong
-----------------------------------
First off, code tags
% You have:
View.Set ("graphics:650,400;offscreenonly")
% It should be
View.Set ("graphics:650;400, offscreenonly")
% always use commas to separate different commands in View.Set

Third, always try to organize your main loop like this:Mod Edit: Code tags are for the general Pseudo code :)

-----------------------------------
uknowhoiam
Fri Mar 13, 2009 11:02 pm

RE:I need help with pong
-----------------------------------
k i dont understand the syntax turing thing.. can you tell me how to use that and about the program what i did was 

if y >= y1 or y = y3 or y  0 then
        y1 := y1 - 10
        y2 := y2 - 10
    end if
end keyboard

procedure mouse
    %player 2
    drawfillbox (maxx - 5, y3, maxx, y4, white)

    View.Update
    delay (5)
    cls

    if button = 1 and y3 > 0 then
        y3 := y3 - 5
        y4 := y4 - 5
    elsif button = 100 and y4 < maxy then
        y3 := y3 + 5
        y4 := y4 + 5
    end if
end mouse

procedure ball

    %ball
    drawfilloval (x, y, 5, 5, white)

    if counter = 100 then
        speed := speed - 1
    end if

    if counter = 200 then
        speed := speed - 1
    end if

    if counter = 300 then
        speed := speed - 1
    end if

    if counter = 400 then
        speed := speed - 1
    end if

    if counter = 500 then
        speed := speed - 1
    end if

    delay (speed)

    if x = 10 and y >= y1 and y = y3 and y = maxx - 5 then
        xpos := -xpos
    end if

    if y < 5 or y >= maxy - 5 then
        ypos := -ypos
    end if

end ball

procedure loc_ball
    x := maxx div 2
    y := maxy div 2
end loc_ball

procedure score
    if x = 5 then
        score1 := score1 + 1
    elsif x = maxx - 5 then
        score2 := score2 + 1
    end if

    locate (1, 1)
    colour (white)
    put score2

end score

loop
    ball
    leftbar
    rightbar
    score
    
    View.Update
    x := x + xpos
    y := y + ypos

    counter := counter + 1

    Mouse.Where (x5, y5, button)

    mouse


    if hasch then
        Input.KeyDown (key)
        keyboard
    end if

    if x = 5 then

        delay (200)
        loc_ball

    elsif x = maxx - 5 then

        delay (200)
        loc_ball
    end if

    cls
end loop




-----------------------------------
BigBear
Sat Mar 14, 2009 6:10 pm

RE:I need help with pong
-----------------------------------
The score is updating, you just arn't displaying both score1 and score2.

-----------------------------------
corriep
Sat Mar 14, 2009 6:17 pm

RE:I need help with pong
-----------------------------------
Quick tip: Getting rid of if hasch then
% code
end if but keeping the code inside of it will eliminate the need to constantly tap the arrow keys ;)

-----------------------------------
BigBear
Sat Mar 14, 2009 6:28 pm

Re: RE:I need help with pong
-----------------------------------
Quick tip: Getting rid of if hasch then
% code
end if but keeping the code inside of it will eliminate the need to constantly tap the arrow keys ;)

Actually what that does it make it more senetive, it is able to get more inputs in a shorter time. if you were to use

 if hasch then getch(key)
end if
 and not asign key := "" every iteration of the loop it will act as you are holding down the key because it will keep the same value of key untill hasch or a key is pressed.

-----------------------------------
uknowhoiam
Sat Mar 14, 2009 6:31 pm

RE:I need help with pong
-----------------------------------
i dont get it, my score still wont update this is what i did

procedure score
    if x = 5 then
        locate (1, maxcol - 1)
        put "  "                 %Erasing the code
        score1 := score1 + 1
    elsif x = maxx - 5 then
        locate (1, 1)
        put " "
        score2 := score2 + 1
    end if

    locate (1, 1)
    colour (white)
    put score2

    locate (1, maxcol)
    colour (white)
    put score1
end score

-----------------------------------
uknowhoiam
Sat Mar 14, 2009 11:07 pm

RE:I need help with pong
-----------------------------------
NVM i got it work
Here's my code

View.Set ("graphics:650;400,offscreenonly")
Mouse.ButtonChoose ("multibutton")

var xpos, ypos, ypos1, ypos2, ypos3, ypos4 : int := 1
var x : int := maxx div 2
var y : int := maxy div 2
var y1 : int := maxy div 2 - 35
var y2 : int := maxy div 2 + 35
var y3 : int := maxy div 2 - 35
var y4 : int := maxy div 2 + 35
var key : array char of boolean
var x5, y5, button : int
var speed : int := 5
var counter : int := 0
var score1, score2 := 0

colourback (black)

cls

procedure leftbar
    drawfillbox (0, y1, 5, y2, white)

end leftbar

procedure rightbar
    drawfillbox (maxx - 5, y3, maxx, y4, white)
end rightbar

procedure keyboard

    %player 1
    drawfillbox (0, y1, 5, y2, white)

    delay (2)
    cls

    if key (KEY_LEFT_ARROW) and y2 < maxy then
        y1 := y1 + 10
        y2 := y2 + 10

    elsif key (KEY_RIGHT_ARROW) and y1 > 0 then
        y1 := y1 - 10
        y2 := y2 - 10
    end if
end keyboard

procedure mouse
    %player 2
    drawfillbox (maxx - 5, y3, maxx, y4, white)

    View.Update
    delay (5)
    cls

    if button = 1 and y3 > 0 then
        y3 := y3 - 5
        y4 := y4 - 5
    elsif button = 100 and y4 < maxy then
        y3 := y3 + 5
        y4 := y4 + 5
    end if
end mouse

procedure ball

    %ball
    drawfilloval (x, y, 5, 5, white)

    if counter = 100 then
        speed := speed - 1
    end if

    if counter = 200 then
        speed := speed - 1
    end if

    if counter = 300 then
        speed := speed - 1
    end if

    if counter = 400 then
        speed := speed - 1
    end if

    if counter = 500 then
        speed := speed - 1
    end if

    delay (speed)

    if x = 10 and y >= y1 and y = y3 and y = maxx - 5 then
        xpos := -xpos
    end if

    if y < 5 or y >= maxy - 5 then
        ypos := -ypos
    end if

end ball

procedure loc_ball
    x := maxx div 2
    y := maxy div 2
end loc_ball

procedure score

end score

loop
    colour (yellow)
    put repeat ("%", maxcol)
    exit
end loop


loop
    ball
    leftbar
    rightbar


    View.Update
    x := x + xpos
    y := y + ypos

    counter := counter + 1

    Mouse.Where (x5, y5, button)

    mouse


    if hasch then
        Input.KeyDown (key)
        keyboard
    end if

    if x = 5 then

        delay (200)
        loc_ball

    elsif x = maxx - 5 then

        delay (200)
        loc_ball
    end if

    if x = maxx - 6 then

        score1 := score1 + 1
    end if

    locate (1, 1)
    colour (white)
    put score1

    locate (1, maxcol)
    colour (white)
    put score2

    if score1 = 5 then
        locate (maxrow div 2, maxcol div 2)
        colour (white)
        put "Player 1 WON!"
        delay (500)
        exit
    elsif score2 = 5 then
        locate (maxrow div 2, maxcol div 2)
        colour (white)
        put "Player 2 WON!"
        delay (500)
        exit
    end if

end loop




for the score this is what i did

if x = maxx - 6 then

        score1 := score1 + 1
    end if

    locate (1, 1)
    colour (white)
    put score1

    locate (1, maxcol)
    colour (white)
    put score2

-----------------------------------
uknowhoiam
Sun Mar 15, 2009 1:15 am

RE:I need help with pong
-----------------------------------
Sorry im having too much fun, the keyboard lag is out

View.Set ("graphics:650;400,offscreenonly")
Mouse.ButtonChoose ("multibutton")

var xpos, ypos, ypos1, ypos2, ypos3, ypos4 : int := 1
var x : int := maxx div 2
var y : int := maxy div 2
var y1 : int := maxy div 2 - 35
var y2 : int := maxy div 2 + 35
var y3 : int := maxy div 2 - 35
var y4 : int := maxy div 2 + 35
var key : array char of boolean
var x5, y5, button : int
var speed : int := 5
var counter : int := 0
var score1, score2 := 0

colourback (black)

cls

procedure leftbar
    drawfillbox (0, y1, 5, y2, white)

end leftbar

procedure rightbar
    drawfillbox (maxx - 5, y3, maxx, y4, white)
end rightbar

procedure keyboard
    %player 1
    drawfillbox (0, y1, 5, y2, white)
    delay (2)
    cls
    y1 := y1 + 5
    y2 := y2 + 5
end keyboard

procedure keyboard2
    %player 1
    drawfillbox (0, y1, 5, y2, white)
    delay (2)
    cls
    y1 := y1 - 5
    y2 := y2 - 5
end keyboard2

procedure mouse
    %player 2
    drawfillbox (maxx - 5, y3, maxx, y4, white)

    View.Update
    delay (5)
    cls

    if button = 1 and y3 > 0 then
        y3 := y3 - 5
        y4 := y4 - 5
    elsif button = 100 and y4 < maxy then
        y3 := y3 + 5
        y4 := y4 + 5
    end if
end mouse

procedure ball

    %ball
    drawfilloval (x, y, 5, 5, white)

    if counter = 100 then
        speed := speed - 1
    end if

    if counter = 200 then
        speed := speed - 1
    end if

    if counter = 300 then
        speed := speed - 1
    end if

    if counter = 400 then
        speed := speed - 1
    end if

    if counter = 500 then
        speed := speed - 1
    end if

    delay (speed)

    if x = 10 and y >= y1 and y = y3 and y = maxx - 5 then
        xpos := -xpos
    end if

    if y < 5 or y >= maxy - 5 then
        ypos := -ypos
    end if

end ball

procedure loc_ball
    x := maxx div 2
    y := maxy div 2
end loc_ball

procedure score

end score

loop
    colour (yellow)
    put repeat ("%", maxcol)
    exit
end loop


loop
    ball
    leftbar
    rightbar


    View.Update
    x := x + xpos
    y := y + ypos

    counter := counter + 1

    Mouse.Where (x5, y5, button)

    mouse

    Input.KeyDown (key)
    if key (KEY_LEFT_ARROW) and y2 < maxy then
        keyboard
    elsif key (KEY_RIGHT_ARROW) and y1 > 0 then
        keyboard2
    end if

    if x = 5 then

        delay (200)
        loc_ball

    elsif x = maxx - 5 then

        delay (200)
        loc_ball
    end if

    if x = maxx - 6 then

        score1 := score1 + 1
    end if

    locate (1, 1)
    colour (white)
    put score1

    locate (1, maxcol)
    colour (white)
    put score2

    if score1 = 5 then
        locate (maxrow div 2, maxcol div 2)
        colour (white)
        put "Player 1 WON!"
        delay (500)
        exit
    elsif score2 = 5 then
        locate (maxrow div 2, maxcol div 2)
        colour (white)
        put "Player 2 WON!"
        delay (500)
        exit
    end if

end loop



