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

Username:   Password: 
 RegisterRegister   
 In my pong game, why does my left paddle work but not right?
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
s_climax




PostPosted: Sat Apr 24, 2004 5:08 pm   Post subject: In my pong game, why does my left paddle work but not right?

My right paddle in my game works rather flawlessly, but for some reason my left does not. Sometimes the left paddle makes the shot. Other times it passes right through it. In my eyes, the left and right paddle's codes are identical. What is wrong with my coding?

Another question that you may find easy to solve is how to I reduce the flickering?

code:

setscreen ("graphics:vga;offscreenonly")
var chars : array char of boolean
var maxscore, ballcol, timer, ballsize, speed, ballx, bally, lpadx, lpady, rpadx, rpady, dx, dy, lscore, rscore : int
timer := 1
ballsize := 5
speed := 2
lscore := 0
rscore := 0
ballx := maxx div 2
bally := maxy div 2
lpadx := 10
lpady := maxy div 2
rpadx := maxx - 10
rpady := maxy div 2
ballcol := 7
colorback (green)
cls

randint (dx, -2, 2)
randint (dy, -2, 2)
loop
    randint (dx, -2, 2)
    randint (dy, -2, 2)
    exit when dx ~= 0 and dy ~= 0
end loop
put "Enter the number of points you would like to play until."
get maxscore
cls
loop
    timer += 1
    %Right Paddle Controls
    Input.KeyDown (chars)
    if chars (KEY_UP_ARROW) and rpady + 20 < maxy then
        rpady += 5
    end if
    if chars (KEY_DOWN_ARROW) and rpady - 20 > 0 then
        rpady -= 5
    end if

    %Left Paddle Controls

    if chars ('a') and lpady + 20 < maxy then
        lpady += 5
    end if
    if chars ('z') and lpady - 20 > 0 then
        lpady -= 5
    end if


    if ballx + dx < 10 or ballx + dx > maxx - 10 - ballsize then
        dx := -dx
    end if
    if bally + dy < 10 or bally + dy > maxy - 10 - ballsize then
        dy := -dy
    end if
    ballx += dx * speed
    bally += dy * speed


    %Right Paddle Collisions
    if bally >= rpady - 20 and bally <= rpady + 20 then
        if ballx > maxx - 15 - (dx * speed) then
            ballcol := 1
            if bally > rpady and timer mod 3 = 0 then
                dy += 1
            else
                dy -= 1
            end if
        end if
    else
        if ballx >= maxx - 15 - (dx * speed) then
            ballsize := 5
            lscore += 1
            ballx := maxx div 2
            bally := maxy div 2
            loop
                randint (dx, -2, 2)
                randint (dy, -2, 2)
                exit when dx ~= 0 and dy ~= 0
            end loop
            ballcol := 7
            locatexy (maxx div 2, maxy div 2)
            put "Red point"
            delay (1000)
            cls
            if dx = 0 then
                dx := 1
            end if
        end if
    end if
    %Left Paddle Collisions
    if bally >= lpady - 20 and bally <= lpady + 20 then
        if ballx < (15 + (dx * speed)) then
            ballcol := 4
            if bally > lpady and timer mod 3 = 0 then
                dy += 1
            else
                dy -= 1
            end if
        end if
    else
        if ballx <= 0 + 15 + (dx * speed) then
            ballsize := 5
            rscore += 1
            ballx := maxx div 2
            bally := maxy div 2
            loop
                randint (dx, -2, 2)
                randint (dy, -2, 2)
                exit when dx ~= 0 and dy ~= 0
            end loop
            ballcol := 7
            locatexy (maxx div 2, maxy div 2)
            put "Blue point"
            delay (1000)
            cls
            if dx = 0 then
                dx := 1
            end if
        end if
    end if
    %Keep the ball moving relatively horizontal
    if dy > 3 then
        dy -= 1
    elsif dy < -3 then
        dy += 1
    end if
    %Drawing
    drawbox (10, 0, maxx - 10, maxy, 7)
    drawfilloval (ballx, bally, ballsize, ballsize, ballcol)
    drawfillbox (rpadx + 5, rpady - 20, rpadx, rpady + 20, 1)
    drawfillbox (lpadx - 5, lpady - 20, lpadx, lpady + 20, 4)
    drawline (maxx div 2, 0, maxx div 2, maxy, 7)
    View.Update

    %Draw the background
    /*
     drawoval (maxx div 2, maxy div 2, 50, 50, 7)
     drawfilloval (maxx div 2, maxy div 2, 5, 5, 7)
     drawoval (90, maxy div 4, 40, 40, 7)
     drawoval (maxx - 90, maxy div 4, 40, 40, 7)
     drawoval (90, maxy - maxy div 4, 40, 40, 7)
     drawoval (maxx - 90, maxy - maxy div 4, 40, 40, 7)
     drawfilloval (90, maxy div 4, 5, 5, 7)
     drawfilloval (maxx - 90, maxy div 4, 5, 5, 7)
     drawfilloval (90, maxy - maxy div 4, 5, 5, 7)
     drawfilloval (maxx - 90, maxy - maxy div 4, 5, 5, 7)


     drawline (maxx div 4 + 15, 0, maxx div 4 + 15, maxy, 7)
     drawline (maxx div 4 - 15 + maxx div 2, 0, maxx div 4 - 15 + maxx div 2, maxy, 7)

     */


    delay (0)
    cls
    put lscore, "/", maxscore
    locatexy (maxx - 20, maxy)
    put rscore, "/", maxscore

    %Check if the game is over
    if lscore >= maxscore then
        cls
        put "Red Player Wins!"
    elsif rscore >= maxscore then
        cls
        put "Blue Player Wins!"
    end if
    exit when rscore >= maxscore or lscore >= maxscore
end loop


Sponsor
Sponsor
Sponsor
sponsor
Paul




PostPosted: Sat Apr 24, 2004 5:19 pm   Post subject: (No subject)

Well, it was flashing so I took off vga and graphics in setscreen, dun think u need them anyway, and it stopped flashing.
I like the color change in the ball.
Well I'm too lazy to go thru all that code so Im just going to give u what I think.
When I look at ur code, it looks like the other team gets a point if the ball hits the line. Your paddle is behind the line. It might be the random slope the ball gets everytime it hits (I really hate the random thing), so the increments bypass the checking. Say the paddle is at 5, the ball was at 8, it moves to 6, then it moves to 4, therefore passing the 5. So I dunno if u already have it, but make it so that it bounces off, if the ball is INSIDE the paddle. Another reason why this might not work is because of the line which ur checking from. The ball stops when it hits the line, since the line and the paddle are so close, the ball might bypass the paddle and hit the line, as I explained above. So try making the part of the code which checks if the ball hit a back wall bugproof by making the line and checking coordinates further away from the paddle.
s_climax




PostPosted: Sat Apr 24, 2004 6:02 pm   Post subject: (No subject)

Paul Bian wrote:
Well, it was flashing so I took off vga and graphics in setscreen, dun think u need them anyway, and it stopped flashing.


Didn't work for me. Still flashed despite removing the 1st line.
Paul




PostPosted: Sat Apr 24, 2004 6:35 pm   Post subject: (No subject)

no, I didn't take off "offscreenonly"
so its setscreen ("offscreenonly")
s_climax




PostPosted: Sat Apr 24, 2004 8:01 pm   Post subject: (No subject)

That worked. Thanks
s_climax




PostPosted: Tue Apr 27, 2004 7:14 pm   Post subject: (No subject)

Okay. I think I figured out the problems.

code:

    if ballx + dx < 10 or ballx + dx > maxx - 10 - ballsize then
        dx := -dx
    end if
    if bally + dy < 10 or bally + dy > maxy - 10 - ballsize then
        dy := -dy
    end if


should be

code:

    if ballx + dx < 10 + ballsize or ballx + dx > maxx - 10 - ballsize then
        dx := -dx
    end if
    if bally + dy < 10 + ballsize or bally + dy > maxy - 10 - ballsize then
        dy := -dy
    end if


also, this:

code:

    %Left Paddle Collisions
    if bally >= lpady - 20 and bally <= lpady + 20 then
        if ballx < (15 + (dx * speed)) then
            ballcol := 4
            if bally > lpady and timer mod 3 = 0 then
                dy += 1
            else
                dy -= 1
            end if
        end if
    else


should be this:

code:

    %Left Paddle Collisions
    if bally >= lpady - 20 and bally <= lpady + 20 then
        if ballx < (15 + (-dx * speed)) then
            ballcol := 4
            if bally > lpady and timer mod 3 = 0 then
                dy += 1
            else
                dy -= 1
            end if
        end if
    else


It now works fine after those changes. Thanks anyways. I jsut had to think it over a bit and pore through my code and I found those two.
TheFreshPrince




PostPosted: Wed May 05, 2004 4:04 pm   Post subject: (No subject)

from the original old program, the left paddle worked with the a and z keys
s_climax




PostPosted: Wed May 05, 2004 6:26 pm   Post subject: (No subject)

I know that. I wrote the code. My problem was that the collisions were different with the left paddle thatn with the right.
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: