
-----------------------------------
HammadTheOne
Thu Jan 10, 2013 11:31 am

Need Help With Simple Turing - Making Ball Move with Keyboard with Collision
-----------------------------------
What is it you are trying to achieve?
I need a ball to be able to move smoothly with arrow keys and to change colors depending on direction. The edges of the program must also have collision implemented, and the ball must bounce off the sides. So far, I have the basics, but the program is choppy.

What is the problem you are having?
The collision is not working properly, instead of bouncing off, the ball teleports 5 points away. Also, for some reason, the cls causes the whole program to flicker, which makes it look bad.


Describe what you have tried to solve this problem
I put in some sort of collision, and tried tutorial threads, but I can't find a way to make it bounce off the edge. Also, experimenting with cls has not given me any results.


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
var X, Y   : int := 5    
var R1, R2 : int := 5 
var DX, DY : int := 1  
var x,y : int 
x:=100 
y:=100 
var chars : array char of boolean 
View.Set ("graphics:800;500")
loop
Input.KeyDown (chars) 


if chars (KEY_UP_ARROW) then 
y:=y+5 
Draw.FillOval(x,y,15,15,blue) 
delay(15)

end if 

if chars (KEY_DOWN_ARROW) then 
y:=y-5 
Draw.FillOval(x,y,15,15,purple) 
delay(15)

end if 

if chars (KEY_RIGHT_ARROW) then 
x:=x+5
Draw.FillOval(x,y,15,15,green) 
delay(15)
 
end if
 
if chars (KEY_LEFT_ARROW) then 
x:=x-5
Draw.FillOval(x,y,15,15,red) 
delay(15)

end if 
delay (10)
cls
Draw.FillOval(x,y,15,15,black)

if x = 795 then
x := x - 20
end if
if x = 0 then
x := x + 20
end if
if y = 495 then
y:= y - 20
end if
if y = 0 then
y:= y + 20
end if

end loop








Please specify what version of Turing you are using
4.1.1

-----------------------------------
HammadTheOne
Thu Jan 10, 2013 12:00 pm

Re: Need Help With Simple Turing - Making Ball Move with Keyboard with Collision
-----------------------------------
I found this code which supposedly reduces flickering, but I can't put it in properly.

View.Set ("graphics,offscreenonly")
var x, y, xChange, yChange : int := 1
const RADIUS : int := 5
Draw.Box (0, 0, maxx, maxy, black)
x := maxx div 2
y := maxy div 2
loop
    Draw.Cls
    Draw.Box (0, 0, maxx, maxy, black)
    Draw.FillOval (x, y, RADIUS, RADIUS, green)
    View.Update
    Time.Delay (5)
    x += xChange
    y += yChange
    if View.WhatDotColor (x, y + RADIUS) = black then
        yChange *= -1
    elsif View.WhatDotColor (x, y - RADIUS) = black then
        yChange *= -1
    end if
    if View.WhatDotColor (x + RADIUS, y) = black then
        xChange *= -1
    elsif View.WhatDotColor (x - RADIUS, y) = black then
        xChange *= -1
    end if
end loop

-----------------------------------
HammadTheOne
Thu Jan 10, 2013 12:01 pm

Re: Need Help With Simple Turing - Making Ball Move with Keyboard with Collision
-----------------------------------
I found this code which supposedly reduces flickering, but I can't put it in properly.

View.Set ("graphics,offscreenonly")
var x, y, xChange, yChange : int := 1
const RADIUS : int := 5
Draw.Box (0, 0, maxx, maxy, black)
x := maxx div 2
y := maxy div 2
loop
    Draw.Cls
    Draw.Box (0, 0, maxx, maxy, black)
    Draw.FillOval (x, y, RADIUS, RADIUS, green)
    View.Update
    Time.Delay (5)
    x += xChange
    y += yChange
    if View.WhatDotColor (x, y + RADIUS) = black then
        yChange *= -1
    elsif View.WhatDotColor (x, y - RADIUS) = black then
        yChange *= -1
    end if
    if View.WhatDotColor (x + RADIUS, y) = black then
        xChange *= -1
    elsif View.WhatDotColor (x - RADIUS, y) = black then
        xChange *= -1
    end if
end loop

-----------------------------------
Tony
Thu Jan 10, 2013 4:28 pm

RE:Need Help With Simple Turing - Making Ball Move with Keyboard with Collision
-----------------------------------
you should read the tutorials on the use of [tdoc]View.Update[/tdoc]

-----------------------------------
HammadTheOne
Thu Jan 10, 2013 5:55 pm

Re: RE:Need Help With Simple Turing - Making Ball Move with Keyboard with Collision
-----------------------------------
you should read the tutorials on the use of 

Thanks for the link, managed to make it flicker much less and work much smoother. Now how would I implement a bounce effect upon collision? Also, my welcome message doesn't seem to show.

Fixed code:


var x, y : int := 550
x := 100
y := 100
var chars : array char of boolean
View.Set ("offscreenonly")
put "Welcome to the Joystick Program. Press Any Button to Begin"
delay (1500)

loop
    Input.KeyDown (chars)

    if chars (KEY_UP_ARROW) then
        y := y + 5
        Draw.FillOval (x, y, 15, 15, blue)
        delay (5)
        View.Update
        Draw.FillOval (x, y, 15, 15, white)
    end if

    if chars (KEY_DOWN_ARROW) then
        y := y - 5
        Draw.FillOval (x, y, 15, 15, purple)
        delay (5)
        View.Update
        Draw.FillOval (x, y, 15, 15, white)
    end if

    if chars (KEY_RIGHT_ARROW) then
        x := x + 5
        Draw.FillOval (x, y, 15, 15, green)
        delay (5)
        View.Update
        Draw.FillOval (x, y, 15, 15, white)
    end if

    if chars (KEY_LEFT_ARROW) then
        x := x - 5
        Draw.FillOval (x, y, 15, 15, red)
        delay (5)
        View.Update
        Draw.FillOval (x, y, 15, 15, white)
    end if

    Draw.Oval (x, y, 15, 15, black)
    cls
    if x = 650 then
        x := x - 20
    end if
    if x = 0 then
        x := x + 20
    end if
    if y = 400 then
        y := y - 20
    end if
    if y = 0 then
        y := y + 20
    end if

     X := X + DX
   Y := Y + DY

   if (X-R1) = 0 or (X+R1) = maxx then     % Check if left / right side of ball hits left left border
                                           % or right side of ball hits right border.
      DX := (-1)*DX                       
   elsif (Y-R1) = 0 or (Y+R1) = maxy then  % Check if bottom of ball hits bottom border
                                           % or top of ball hits top border.
      DY := (-1)*DY
   end if
    
end loop

-----------------------------------
HammadTheOne
Fri Jan 11, 2013 5:50 pm

Re: Need Help With Simple Turing - Making Ball Move with Keyboard with Collision
-----------------------------------
Small bump. I've been looking around for some collision tutorials, but some are really difficult (for me) to implement. I took a look at these ones (http://compsci.ca/v3/viewtopic.php?t=5246&highlight=circular+collision), along with your Start to Pool one, but I can't seem to put the bounce effect into the 4 walls along with the player controls. Any help would be appreciated.

-----------------------------------
Tony
Fri Jan 11, 2013 6:26 pm

RE:Need Help With Simple Turing - Making Ball Move with Keyboard with Collision
-----------------------------------
- ideally you should have only one View.Update and only one delay, otherwise you are drawing and pausing multiple times in a single game turn. E.g. if you hold down both RIGHT and UP buttons, then your program will briefly pause in between and draw to screen twice.

As for collisions
[code]
if (X-R1) = 0
[/code]
seems like a problem. Unless you are really precise with all of the operations, it's very likely that there will be a state where you don't have an exact match. e.g., all of the following values are not equal to 0: -1, 0.0, 0.1, 1
