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

Username:   Password: 
 RegisterRegister   
 Pong help
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Travveh




PostPosted: Fri Jun 16, 2006 8:06 am   Post subject: Pong help

Well, I have my simple problem. I made pong, but It occasionally starts in an enless loop. It's 4-player pong on a 700x700 field with 200x200 boxes in each corner. The ball bounces by multiply the direction by -1. Help would be greatly aooreciated, as this class is over in a couple days.
Sponsor
Sponsor
Sponsor
sponsor
Remm




PostPosted: Fri Jun 16, 2006 8:40 am   Post subject: (No subject)

Well, as we say to EVERY person who posts like this Confused :
If you havent, check the turing tutorial about loops and for loops to see if you can find anything odd with your program.
Secondly, you would be best to give your source code or portions of your sorce code so we can have a look at the program too, and see what is wrong with it first-hand.
Also, since you know your program better than anyone, check your own loops and for loops to make sure all the exiting and whatnot is in order.
Clayton




PostPosted: Fri Jun 16, 2006 8:41 am   Post subject: (No subject)

could you post code plz, we cant really help u unless we can see what you are doing, remember to use [code] tags when posting code Very Happy
Travveh




PostPosted: Fri Jun 16, 2006 8:48 am   Post subject: (No subject)

code:
View.Set ('graphics:700;700, position:middle;middle, nobuttonbar, offscreenonly, nocursor')
 
var x, y : int
var mx, my, button : int
var yd : int := -1
var xd : int := -1
var y1, y2 : int := 300
var x1, x2 : int := 300
var P1, P2, P3, P4 : int := 0
var speed : int := 0
var P1Color, P2Color, P3Color, P4Color : int := 0
var hit : int := 0
var key : array char of boolean
var font : int := Font.New ("Viner Hand ITC:30:bold")
var font2 : int := Font.New ("Comic Sans:20")
var font3 : int := Font.New ("Arial:50")
var Player1, Player2, Player3, Player4, again : string
var P1Win, P2Win, P3Win, P4Win, Play : boolean := false
randint (x, 200, maxx - 200)
randint (y, 200, maxy - 200)

procedure drawField
    Draw.FillBox (0, 0, maxx, maxy, green)
    Draw.FillBox (0, maxy, 200, maxy - 200, black)
    Draw.FillBox (maxx, maxy, maxx - 200, maxy - 200, black)
    Draw.FillBox (0, 0, 200, 200, black)
    Draw.FillBox (maxx, 0, maxx - 200, 200, black)
   
    if P1 = 0 then
        Player1 := "0"
    elsif P1 = 1 then
        Player1 := "1"
    elsif P1 = 2 then
        Player1 := "2"
    elsif P1 = 3 then
        Player1 := "3"
    elsif P1 = 4 then
        Player1 := "4"
    elsif P1 = 5 then
        Player1 := "5"
    elsif P1 = 6 then
        Player1 := "6"
    elsif P1 = 7 then
        Player1 := "7"
    elsif P1 = 8 then
        Player1 := "8"
    elsif P1 = 9 then
        Player1 := "9"
    elsif P1 = 10 then
        Player1 := "10"
        P1Win := true
    end if
    if P2 = 0 then
        Player2 := "0"
    elsif P2 = 1 then
        Player2 := "1"
    elsif P2 = 2 then
        Player2 := "2"
    elsif P2 = 3 then
        Player2 := "3"
    elsif P2 = 4 then
        Player2 := "4"
    elsif P2 = 5 then
        Player2 := "5"
    elsif P2 = 6 then
        Player2 := "6"
    elsif P2 = 7 then
        Player2 := "7"
    elsif P2 = 8 then
        Player2 := "8"
    elsif P2 = 9 then
        Player2 := "9"
    elsif P2 = 10 then
        Player2 := "10"
        P2Win := true
    end if
    if P3 = 0 then
        Player3 := "0"
    elsif P3 = 1 then
        Player3 := "1"
    elsif P3 = 2 then
        Player3 := "2"
    elsif P3 = 3 then
        Player3 := "3"
    elsif P3 = 4 then
        Player3 := "4"
    elsif P3 = 5 then
        Player3 := "5"
    elsif P3 = 6 then
        Player3 := "6"
    elsif P3 = 7 then
        Player3 := "7"
    elsif P3 = 8 then
        Player3 := "8"
    elsif P3 = 9 then
        Player3 := "9"
    elsif P3 = 10 then
        Player3 := "10"
        P3Win := true
    end if
    if P4 = 0 then
        Player4 := "0"
    elsif P4 = 1 then
        Player4 := "1"
    elsif P4 = 2 then
        Player4 := "2"
    elsif P4 = 3 then
        Player4 := "3"
    elsif P4 = 4 then
        Player4 := "4"
    elsif P4 = 5 then
        Player4 := "5"
    elsif P4 = 6 then
        Player4 := "6"
    elsif P4 = 7 then
        Player4 := "7"
    elsif P4 = 8 then
        Player4 := "8"
    elsif P4 = 9 then
        Player4 := "9"
    elsif P4 = 10 then
        Player4 := "10"
        P4Win := true
    end if

    Font.Draw (Player1, 110, maxy - 160, font3, P1Color)
    Font.Draw (Player2, maxx - 160, maxy - 160, font3, P2Color)
    Font.Draw (Player3, maxx - 160, 110, font3, P3Color)
    Font.Draw (Player4, 110, 110, font3, P4Color)
    Font.Draw ("Player 1", 20, maxy - 40, font, P1Color)
    Draw.Line (40, maxy - 60, 40, maxy - 180, P1Color)
    Draw.Line (40, maxy - 180, 20, maxy - 150, P1Color)
    Draw.Line (40, maxy - 180, 60, maxy - 150, P1Color)
    Font.Draw ("Player 2", maxx - 180, maxy - 40, font, P2Color)
    Draw.Line (maxx - 20, maxy - 80, maxx - 180, maxy - 80, P2Color)
    Draw.Line (maxx - 180, maxy - 80, maxx - 150, maxy - 60, P2Color)
    Draw.Line (maxx - 180, maxy - 80, maxx - 150, maxy - 100, P2Color)
    Font.Draw ("Player 3", maxx - 180, 30, font, P3Color)
    Draw.Line (maxx - 40, 60, maxx - 40, 180, P3Color)
    Draw.Line (maxx - 40, 180, maxx - 20, 150, P3Color)
    Draw.Line (maxx - 40, 180, maxx - 60, 150, P3Color)
    Font.Draw ("Player 4", 20, 30, font, P4Color)
    Draw.Line (20, 80, 180, 80, P4Color)
    Draw.Line (180, 80, 150, 60, P4Color)
    Draw.Line (180, 80, 150, 100, P4Color)
end drawField


procedure drawPaddel1
    Input.KeyDown (key)
    if key ('w') then
        y2 += 1
    elsif key ('s') then
        y2 -= 1
    end if
    if y2 < 200 then
        y2 := 200
    elsif y2 + 75 > maxy - 200 then
        y2 := maxy - 275
    end if
    drawfillbox (0, y2, 15, y2 + 75, P1Color)
end drawPaddel1


procedure drawPaddel2
    Input.KeyDown (key)
    if key (KEY_RIGHT_ARROW) then
        x1 += 1
    elsif key (KEY_LEFT_ARROW) then
        x1 -= 1
    end if
    if x1 < 200 then
        x1 := 200
    elsif x1 + 75 > maxy - 200 then
        x1 := maxy - 275
    end if
    drawfillbox (x1, maxy, x1 + 75, maxy - 15, P2Color)
end drawPaddel2


procedure drawPaddel3
    Input.KeyDown (key)
    if key ('8') then
        y1 += 1
    elsif key ('2') then
        y1 -= 1
    end if
    if y1 < 200 then
        y1 := 200
    elsif y1 + 75 > maxy - 200 then
        y1 := maxy - 275
    end if
    drawfillbox (maxx - 15, y1, maxx, y1 + 75, P3Color)
end drawPaddel3


procedure drawPaddel4
    Input.KeyDown (key)
    if key ('l') then
        x2 += 1
    elsif key ('k') then
        x2 -= 1
    end if
    if x2 < 200 then
        x2 := 200
    elsif x2 + 75 > maxy - 200 then
        x2 := maxy - 275
    end if
    drawfillbox (x2, 0, x2 + 75, 15, P4Color)
end drawPaddel4


procedure beginning
    color (blue)
    loop
        Input.KeyDown (key)
        Mouse.Where (mx, my, button)
        Font.Draw ("PONG!", maxx div 2 - 75, maxy div 2 + 50, font, 13)
        locate (23, 20)
        put "What speed would you like to play at? (0,1,2,3)"
        locate (24, maxcol div 2)
               
        %% Player 1 %%
        locate (30, 15)
        put "Player 1"..
        Draw.FillBox (200, 222, 210, 232, 12)
        Draw.FillBox (230, 222, 240, 232, 13)
        Draw.FillBox (260, 222, 270, 232, 14)
        Draw.FillBox (290, 222, 300, 232, 11)
        %% Player 2 %%
        locate (32, 15)
        put "Player 2"..
        Draw.FillBox (200, 190, 210, 200, 12)
        Draw.FillBox (230, 190, 240, 200, 13)
        Draw.FillBox (260, 190, 270, 200, 14)
        Draw.FillBox (290, 190, 300, 200, 11)
        %% Player 3 %%
        locate (34, 15)
        put "Player 3"..
        Draw.FillBox (200, 158, 210, 168, 12)
        Draw.FillBox (230, 158, 240, 168, 13)
        Draw.FillBox (260, 158, 270, 168, 14)
        Draw.FillBox (290, 158, 300, 168, 11)
        %% Player 4 %%
        locate (36, 15)
        put "Player 4"..
        Draw.FillBox (200, 126, 210, 136, 12)
        Draw.FillBox (230, 126, 240, 136, 13)
        Draw.FillBox (260, 126, 270, 136, 14)
        Draw.FillBox (290, 126, 300, 136, 11)
        if mx > 200 and mx < 210 and my > 222 and my < 232 and button = 1 then
            P1Color := 12
        elsif  mx > 230 and mx < 240 and my > 222 and my < 232 and button = 1 then
            P1Color := 13
        elsif  mx > 260 and mx < 270 and my > 222 and my < 232 and button = 1 then
            P1Color := 14
        elsif  mx > 290 and mx < 300 and my > 222 and my < 232 and button = 1 then
            P1Color := 11
        end if
       
        if mx > 200 and mx < 210 and my > 190 and my < 200 and button = 1 then
            P2Color := 12
        elsif  mx > 230 and mx < 240 and my > 190 and my < 200 and button = 1 then
            P2Color := 13
        elsif  mx > 260 and mx < 270 and my > 190 and my < 200 and button = 1 then
            P2Color := 14
        elsif  mx > 290 and mx < 300 and my > 190 and my < 200 and button = 1 then
            P2Color := 11
        end if
       
        if mx > 200 and mx < 210 and my > 158 and my < 168 and button = 1 then
            P3Color := 12
        elsif  mx > 230 and mx < 240 and my > 158 and my < 168 and button = 1 then
            P3Color := 13
        elsif  mx > 260 and mx < 270 and my > 158 and my < 168 and button = 1 then
            P3Color := 14
        elsif  mx > 290 and mx < 300 and my > 158 and my < 168 and button = 1 then
            P3Color := 11
        end if
       
        if mx > 200 and mx < 210 and my > 126 and my < 136 and button = 1 then
            P4Color := 12
        elsif  mx > 230 and mx < 240 and my > 126 and my < 136 and button = 1 then
            P4Color := 13
        elsif  mx > 260 and mx < 270 and my > 126 and my < 136 and button = 1 then
            P4Color := 14
        elsif  mx > 290 and mx < 300 and my > 126 and my < 136 and button = 1 then
            P4Color := 11
        end if
        Draw.Box (320, 222, 330, 232, 7)
        Draw.FillBox (321, 223, 329, 231, P1Color)
        Draw.Box (320, 190, 330, 200, 7)
        Draw.FillBox (321, 191, 329, 199, P2Color)
        Draw.Box (320, 158, 330, 168, 7)
        Draw.FillBox (321, 159, 329, 167, P3Color)
        Draw.Box (320, 126, 330, 136, 7)
        Draw.FillBox (321, 127, 329, 135, P4Color)
        locate (38, 15)
        put "Speed      0   1   2   3   "..
        if mx < 210 and mx > 200 and my > 95 and my < 105 and button = 1 then
            speed := 0
            put speed
        elsif mx > 230 and mx < 240 and my > 95 and my < 105 and button = 1 then
            speed := 1
            put speed
        elsif mx > 260 and mx < 272 and my > 95 and my < 105 and button = 1 then
            speed := 2
            put speed
        elsif mx > 295 and mx < 305 and my > 95 and my < 105 and button = 1 then
            speed := 3
            put speed
        end if
       
        Draw.Box (200, 30, 320, 70, 7)
        locate (41,31)
        put "PLAY" ..
        if mx > 200 and mx < 320 and my > 30 and my < 70 and button = 1 then
            Play := true
        end if
        exit when Play = true and speed = 0 or Play = true and speed = 1 or Play = true and speed = 2 or Play = true and speed = 3   
        View.Update
   
    end loop
   
end beginning


beginning

loop
    drawField
    drawPaddel1
    drawPaddel2
    drawPaddel3
    drawPaddel4
    % Reflection
    if x + xd < 200 and y + yd > maxy - 200 or x + xd > maxx - 200 and y + yd > maxy - 200 or x + xd < 200 and y + yd < 200 or x + xd > maxx - 200 and y + yd < 200 or x + xd > maxx - 15 and y + yd < y1 + 75 and y + yd > y1 or x + xd < 15 and y + yd > y2 and y + yd < y2 + 75
            then
        xd *= -1
    end if
    if x + xd > maxx - 200 and y + yd > maxy - 200 or x + xd > maxx - 200 and y + yd < 200 or x + xd < 200 and y + yd > maxy - 200 or x + xd < 200 and y + yd < 200 then
        %if x + xd > maxx - 25 and x + xd < maxx and y + yd < y1 + 150 and y + yd > y1 or x + xd < 25 and x + xd > 0 and y + yd < y2 + 150 and y + yd > y2 then
        xd *= -1
    end if
    x += xd

    if y + yd > maxy - 200 and x + xd > maxx - 200 or y + yd < 200 and x + xd > maxx - 200 or y + yd > maxy - 200 and x + xd < 200 or y + yd < 200 and x + xd < 200 or y + yd > maxy - 15 and x + xd > x1 and x + xd < x1 + 75 or y + yd < 15 and x + xd > x2 and x + xd < x2 + 75
            then
        yd *= -1
    end if
    if y + yd > maxy - 200 and x + xd < 200 or y + yd > maxy - 200 and x + xd > maxx - 200 or y + yd < 200 and x + xd > maxx - 200 or y + yd < 200 and x + xd < 200 then
        yd *= -1
    end if
    y += yd


    if x + xd < 15 and y + yd > y2 and y + yd < y2 + 75 then
        hit := 1
    elsif y + yd > maxy - 15 and x + xd > x1 and x + xd < x1 + 75 then
        hit := 2
    elsif x + xd > maxx - 15 and y + yd < y1 + 75 and y + yd > y1 then
        hit := 3
    elsif y +  yd < 15 and x + xd > x2 and x + xd < x2 + 75 then
        hit := 4
    end if

    % Tells if the ball has left the screen and starts it up again.
    if x + xd < -10 then
        if hit = 1 then
            P2 += 1
            P3 += 1
            P4 += 1
        elsif hit = 2 then
            P2 += 1
        elsif hit = 3 then
            P3 += 1
        elsif hit = 4 then
            P4 += 1
        end if
        hit := 0
        x := 50
        xd := 1
        yd := 1
        randint (y, 200, maxy - 200)
        delay (1000)
    elsif x + xd > maxx + 10 then
    if hit = 1 then
            P1 += 1
        elsif hit = 2 then
            P2 += 1
        elsif hit = 3 then
            P1 += 1
            P2 += 1
            P4 += 1
        elsif hit = 4 then
            P4 += 1
        end if
        hit := 0
        x := maxx - 50
        xd := -1
        yd := -1
        randint (y, 200, maxy - 200)
        delay (1000)
    end if

    if y + yd < -10 then
        if hit = 1 then
            P1 += 1
        elsif hit = 2 then
            P2 += 1
        elsif hit = 3 then
            P3 += 1
        elsif hit = 4 then
            P1 += 1
            P2 += 1
            P3 += 1
        end if
        hit := 0
        y := 50
        xd := 1
        yd := 1
        randint (x, 200, maxx - 200)
        delay (1000)
    elsif y + yd > maxy + 10 then
        if hit = 1 then
            P1 += 1           
        elsif hit = 2 then
            P1 += 1
            P3 += 1
            P4 += 1
        elsif hit = 3 then
            P3 += 1
        elsif hit = 4 then
            P4 += 1
        end if
        hit := 0
        y := maxy - 50
        xd := -1
        yd := -1
        randint (y, 200, maxx - 200)
        delay (1000)
    end if

    drawfilloval (x, y, 5, 5, 7)

    if P1 = 10 then
        P1Win := true
    elsif P2 = 10 then
        P2Win := true
    elsif P3 = 10 then
        P3Win := true
    elsif P4 = 10 then
        P4Win := true
    end if
    View.Update
    delay (speed)
    cls
    exit when P1 = 10 or P2 = 10 or P3 = 10 or P4 = 10
end loop



Draw.FillBox (0, 0, maxx, maxy, blue)
if P1Win = true then
    Font.Draw ("PLAYER 1 WINS!!", 150, 500, font, 0)
elsif P2Win = true then
    Font.Draw ("PLAYER 2 WINS!!", 150, 500, font, 0)
elsif P3Win = true then
    Font.Draw ("PLAYER 3 WINS!!", 150, 500, font, 0)
elsif P4Win = true then
    Font.Draw ("PLAYER 4 WINS!!", 150, 500, font, 0)
end if
Font.Draw ("Play Again? (Press F1)", 200, 250, font2, 0)


Yeah, i didn't post my source code earlier because it was really buggy. It's not a loop problem (bad choice of words on my part...) It's just that it will hit the same spots over and over again, until a paddle misses and starts it again at a random spot.. It's really hard to explain, so just try it out. And there's another glitch that I can't seem to find out: when it scores on player 2, the ball starts too low...
Travveh




PostPosted: Fri Jun 16, 2006 8:56 am   Post subject: (No subject)

I know /why/ it does it, and all that, it's just a matter of figuring out a way to fix it. >.<

And when I said the ball starts too low when scored on Player 2, that only happens occasionally.

Thanks again for any help.
Travveh




PostPosted: Fri Jun 16, 2006 9:00 am   Post subject: (No subject)

Oh, nevermind the Player 2 glitch thingy... On line 440, (randint (y,200,maxy-200)), the y and maxy need to be x's.
Travveh




PostPosted: Thu Jun 22, 2006 8:03 am   Post subject: (No subject)

Well, you guys were helpful. XP
Delos




PostPosted: Thu Jun 22, 2006 8:56 am   Post subject: (No subject)

Don't whine. We don't get paid to help you - we do this of our own volition in our own time.

code:

if x + xd < 200 and y + yd > maxy - 200 or x + xd > maxx - 200 and y + yd > maxy - 200 or x + xd < 200 and y + yd < 200 or x + xd > maxx - 200 and y + yd < 200 or x + xd > maxx - 15 and y + yd < y1 + 75 and y + yd > y1 or x + xd < 15 and y + yd > y2 and y + yd < y2 + 75
            then
        xd *= -1
    end if

I have a feeling your error lies somewhere in this mass of conditions. I notice 2 things here: first, there are several distinct if clauses accomplishing essentially the same thing (2 affect x, 2 affect y). Since they are distinct, all 4 will be checked, which could potentially lead to a problem. For instance, if the conditions are met in clause 1, then xd will invert. However, once that is done, another round of conditions are checked...if these too are met, xd will invert again! Now, given that it has just inverted twice, it's back where it started, and chances are it's going to stay right there ad infinitum.
How to fix this?
This brings me to my second point.
If you look at the above code snippet, you'll notice that there are several parts of the if clause that are repeated. So, instead of having one really long run-on condition, why not nest a few if's and make your debugging life easier?
For instance:
code:

% instead of:
if x > 10 and y <10 or x > 10 and y > 40 or x < 40 and y < 10 or x < 40 and y > 40 then
%...

% do:
if x > 10 then
  if y < 10 then
    %...
  elsif y > 40 then
    %...
elsif x < 40 then
   if y < 10 then
     %...
   elsif y > 40 then
     %...


There are a few more improvements you could add to that, but breaking things up like that makes your life a lot easier, and get's you to the same place.

Now as a general comment - learn to use parameters in your procedures, they will make your life a *lot* easier. Check out the [Turing Walkthrough] for links the relevant tuts.
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: