
-----------------------------------
Salman
Fri Jan 11, 2013 2:11 pm

Pong ball
-----------------------------------
What is it you are trying to achieve?

Make the ball hit the paddles.

What is the problem you are having?


Describe what you have tried to solve this problem



Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)




import GUI
var font : int
var option : string
var movement1 : int := 0
var movement2 : int := 0
var keys : array char of boolean
var x2 := maxx div 2
var y2 := maxy div 2
var x := 175
var y := 0
colorback(black)
for i : 1 .. 25 %Counted loop makes the background stay black
    for j : 1 .. 80
        put " " ..
    end for
end for
setscreen ("nocursor,offscreenonly")
font := Font.New ("Arial Black:18")
Font.Draw ("Pong", 280, 320, font, red)
put whatdotcolor (x, y)
procedure startGame
    cls
    Draw.FillBox (0, 600, maxx, 360, brightred)
    Draw.FillBox (0, 00, maxx, 45, brightred)
    Draw.FillBox (0, 175 + movement1, 10, 225 + movement1, red)
    Draw.FillBox (630, x + movement2, maxx, 225 + movement2, red)
    Input.KeyDown (keys)
    if keys ('w') then
        Draw.FillBox (0, 175 + movement1, 10, 225 + movement1, red)
        if movement1 = 45 - 175 then
            movement1 := movement1 - 2
        end if
    end if

    if keys (KEY_UP_ARROW) then
        Draw.FillBox (630, 175 + movement2, maxx, 225 + movement2, red)
        if movement2 = 45 - 175 then
            movement2 := movement2 - 2
        end if
    end if
    for i:1..5 %makes sure the ball hits the paddle
    drawfilloval(x2,y2,10+i,10+i,blue)
        exit when x2=10
        x2:=x2-1
    end for
    
end startGame



var startButton : int := GUI.CreateButton (maxx div 2 - 30, 60, 60,"Start Game", startGame)
procedure helpGame
    cls
    Font.Draw ("Refer to the instruction manual", 100, 320, font, red)
end helpGame
var helpButtom : int := GUI.CreateButton (maxx div 2 - 200, 60, 60, "Help", helpGame)

loop
    startGame
    View.Update
    exit when GUI.ProcessEvent
end loop



Please specify what version of Turing you are using

4.1

-----------------------------------
Insectoid
Fri Jan 11, 2013 2:18 pm

RE:Pong ball
-----------------------------------
Look at any one of the dozens of collision detection tutorials available in the Turing Tutorials section. The Turing Walkthrough is a great place to start.

-----------------------------------
Salman
Mon Jan 14, 2013 12:52 pm

RE:Pong ball
-----------------------------------
I tried this, still doesn't work

import GUI
var x2, y2, xChange, yChange : int := 1
const radius : int := 10
var font : int
var option : string
var movement1 : int := 0
var movement2 : int := 0
var keys : array char of boolean
var x := 175
var y := 0
colorback (black)
for i : 1 .. 25 %Counted loop makes the background stay black
    for j : 1 .. 80
        put " " ..
    end for
end for
setscreen ("nocursor,offscreenonly")
font := Font.New ("Arial Black:18")
Font.Draw ("Pong", 280, 320, font, red)
put whatdotcolor (x, y)
procedure startGame
    cls
    Draw.FillBox (0, 600, maxx, 360, brightred)
    Draw.FillBox (0, 00, maxx, 45, brightred)
    Draw.FillBox (0, 175 + movement1, 10, 225 + movement1, brightred)
    Draw.FillBox (630, x + movement2, maxx, 225 + movement2, brightred)
    Input.KeyDown (keys)
    if keys ('w') then
        Draw.FillBox (0, 175 + movement1, 10, 225 + movement1, brightred)
        if movement1 = 45 - 175 then
            movement1 := movement1 - 2
        end if
    end if

    if keys (KEY_UP_ARROW) then
        Draw.FillBox (630, 175 + movement2, maxx, 225 + movement2, brightred)
        if movement2 = 45 - 175 then
            movement2 := movement2 - 2
        end if
    end if
    x := maxx div 2
    y := maxy div 2
    loop
        Draw.FillOval (x2, y2, radius, radius, brightblue)
        x +=xChange
        y +=yChange
      if View.WhatDotColour (x,y+radius)=brightred then
        yChange*=-1
        elsif View.WhatDotColour (x,y-radius)=brightred then
         yChange *=-1
       end if
       if View.WhatDotColour (x+radius,y)=brightred then
       xChange*=-1
       elsif  View.WhatDotColour (x-radius,y)=brightred then
       xChange*=-1
       end if
end loop

    end startGame



    var startButton : int := GUI.CreateButton (maxx div 2 - 30, 60, 60, "Start Game", startGame)
    procedure helpGame
        cls
        Font.Draw ("Refer to the instruction manual", 100, 320, font, red)
    end helpGame
    var helpButtom : int := GUI.CreateButton (maxx div 2 - 200, 60, 60, "Help", helpGame)

    loop
        startGame
        View.Update
        exit when GUI.ProcessEvent
    end loop

-----------------------------------
Insectoid
Mon Jan 14, 2013 1:06 pm

RE:Pong ball
-----------------------------------
loop 
Draw.FillOval (x2, y2, radius, radius, brightblue) 
x +=xChange 
y +=yChange 
if View.WhatDotColour (x,y+radius)=brightred then 
yChange*=-1 
elsif View.WhatDotColour (x,y-radius)=brightred then 
yChange *=-1 
end if 
if View.WhatDotColour (x+radius,y)=brightred then 
xChange*=-1 
elsif View.WhatDotColour (x-radius,y)=brightred then 
xChange*=-1 
end if 
end loop 

Will this loop ever exit?

-----------------------------------
Salman
Tue Jan 15, 2013 1:29 pm

RE:Pong ball
-----------------------------------
it will exit when the ball goes off the screen

-----------------------------------
Tony
Tue Jan 15, 2013 2:23 pm

RE:Pong ball
-----------------------------------
ok, followup question: from what line of the code will that loop "exit when the ball goes off the screen"?
