Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Help me with my pong game
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
shoobyman




PostPosted: Mon Jan 16, 2006 9:22 am   Post subject: Help me with my pong game

Hey, i need help with my program cuz this is my final exam!
I need to make the ball bounce of the both platforms but i cant pick the right code :/ . PLS help! ty...


code:
var x, y, b, t, r, x1, y1 : int
var chars : array char of boolean
var x3, y3, button : int
var objectVX, objectVY, x2, y2 : int
objectVY := 10
objectVX := 10
x2 := maxx div 2
y2 := maxy div 2
x := 100
y := 200
x1 := 300
y1 := 400
setscreen ("graphics:500;500")
colorback (green)
setscreen ("offscreenonly")
loop
    Input.KeyDown (chars)
    drawfillbox (x, 100, y, 120, black)
    if chars (KEY_RIGHT_ARROW) then
        y := y + 15
        x := x + 15
    end if
    if chars (KEY_LEFT_ARROW) then
        x := x - 15
        y := y - 15
    end if

    drawfillbox (x1, 400, y1, 380, black)
    if chars ('d') then
        y1 := y1 + 15
        x1 := x1 + 15
    end if
    if chars ('a') then
        x1 := x1 - 15
        y1 := y1 - 15
    end if


    drawline (150, 70, 150, 1, black)
    drawline (300, 70, 300, 1, black)
    drawline (300, 500, 300, 430, black)
    drawline (450, 500, 450, 430, black)



    const ballRad : int := 7
    mousewhere (x3, y3, button)
    drawfilloval (x2, y2, ballRad, ballRad, black)
    x2 += objectVX
    y2 += objectVY
    delay (15)
    if x2 > maxx - 5 then
        objectVX := -objectVX + 2
    elsif x2 < 5 then
        objectVX := -objectVX + 1
    elsif y2 > maxx - 5 then
        objectVY := -objectVY + 1
    elsif y2 < 5 then
        objectVY := -objectVY + 1
    elsif x2<=x and y2<=y and x2<=400 and y2>=380 then
    objectVY:= -objectVY+1

    end if

   
   
    View.Update
    delay (5)
    cls
end loop
Sponsor
Sponsor
Sponsor
sponsor
Albrecd




PostPosted: Mon Jan 16, 2006 10:06 am   Post subject: (No subject)

Quote:
code:
if chars (KEY_RIGHT_ARROW) then
        y := y + 15
        x := x + 15


First of all, you can do this much more simply by using:

code:
y += 15

(It means the same thing)

Second of all, why do you have a mousewhere? You don't use it...

And third of all, in order to make the ball bounce, you need to put something along the lines of:

code:
if x2 >= x and x2 <= y and y2 < 120 then
    %change the ball direction


Then do the same for the other paddle. Note that when I said < 120, you could put = 120 or something else to make the impact look better (or change 120 to 130 or something). The reason I put < 120 is that, since the ball does not move one pixel each frame, it will not nessessarily appear at 120 Y.

(And Fourth of all... Constants should be entirely capitals... Ex: BALLRAD... but that's just me being picky...)
Delos




PostPosted: Mon Jan 16, 2006 2:53 pm   Post subject: (No subject)

Albrecd wrote:
(And Fourth of all... Constants should be entirely capitals... Ex: BALLRAD... but that's just me being picky...)


Eh? No, that's likely just your teacher's own paradigm. No such rule, perhaps a suggestion (similar to "Reserve variable names with captial first letters for Classes only"). Perhaps a way of easing legibility.
But not a rule.
shoobyman




PostPosted: Tue Jan 17, 2006 8:47 am   Post subject: (No subject)

thnx a lot Smile
Albrecd




PostPosted: Tue Jan 17, 2006 8:52 am   Post subject: (No subject)

Quote:
No, that's likely just your teacher's own paradigm. No such rule


Like I said, that's just me being picky.
shoobyman




PostPosted: Tue Jan 17, 2006 9:38 am   Post subject: (No subject)

Dont get why it doesnt bounce and shakes every time? I thought i got the coordinated right but still... PLS help again Smile

code:
var x, y, b, t, r, x1, y1 : int
var chars : array char of boolean
var x3, y3, button : int
var objectVX, objectVY, x2, y2 : int
objectVY := 10
objectVX := 10
x2 := maxx div 2
y2 := maxy div 2
x := 100
y := 200
x1 := 300
y1 := 400
setscreen ("graphics:500;500")
colorback (green)
setscreen ("offscreenonly")
loop
    Input.KeyDown (chars)
    drawfillbox (x, 0, y, 20, black)
    if chars (KEY_RIGHT_ARROW) then
        y := y + 15
        x := x + 15
    end if
    if chars (KEY_LEFT_ARROW) then
        x := x - 15
        y := y - 15
    end if

    drawfillbox (x1, 499, y1, 479, black)
    if chars ('d') then
        y1 := y1 + 15
        x1 := x1 + 15
    end if
    if chars ('a') then
        x1 := x1 - 15
        y1 := y1 - 15
    end if

    const ballRad : int := 7
    drawfilloval (x2, y2, ballRad, ballRad, black)
    x2 += objectVX
    y2 += objectVY
    delay (15)
    if x2 > maxx - 5 then
        objectVX := -objectVX
    elsif x2 < 5 then
        objectVX := -objectVX
    elsif y2 > maxx - 5 then
        objectVY := -objectVY
    elsif y2 < 5 then
        objectVY := -objectVY
    elsif x2 >= x and x2 <= y and y2 < 20 then
        objectVY := -objectVY
    elsif x2 >= x1 and x2 <= y1 and y2 < 479 then
        objectVY := -objectVY
    end if

    View.Update
    delay (5)
    cls
end loop
pavol




PostPosted: Tue Jan 17, 2006 1:40 pm   Post subject: (No subject)

first, i think that you shouldn't have : if...elsif...elsif etc. because this way only one of the conditions may be true, so put each of the conditions in a seperate if statement. second, your collision detection for the paddle looks wrong.
you have
code:
    elsif x2 >= x and x2 <= y and y2 < 20 then
        objectVY := -objectVY

you should have something like:
code:
if x2>= x- (paddlewidth div 2) and x2 <= x+(paddlewidth div 2) and y2 <= 20 then
    objectVY := -objectVY
end if

do that for the other paddle as well and it should help at least a bit
Clayton




PostPosted: Tue Jan 17, 2006 10:38 pm   Post subject: (No subject)

i got your ball to bounce properly now you have to figure it out your self, u should have something like this

code:

if x2 - radius <= 0 then
        objectVX := -objectVX
    end if
    if x2 + radius >= maxx then
        objectVX := -objectVX
    end if
    if y2 + radius >= maxy - 21 and x2 - radius >= x and x2 + radius <= y then
        objectVY := -objectVY
    end if
    if y2 - radius <= 21 and x2 - radius >= x1 and x2 + radius <= y1 then
        objectVY := -objectVY
    end if
    if y2 + radius >= maxy or y2 - radius <= 0 then
        objectVY := 0
        objectVX := 0
    end if

something like that eliminates the really weird jumpy movement of the ball, but you need to figure out how to bounce off of the paddle
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: