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

Username:   Password: 
 RegisterRegister   
 Need Help With Simple Turing - Making Ball Move with Keyboard with Collision
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
HammadTheOne




PostPosted: Thu Jan 10, 2013 11:31 am   Post subject: 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


Turing:


<Add your code here>



Please specify what version of Turing you are using
4.1.1
Sponsor
Sponsor
Sponsor
sponsor
HammadTheOne




PostPosted: Thu Jan 10, 2013 12:00 pm   Post subject: 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




PostPosted: Thu Jan 10, 2013 12:01 pm   Post subject: 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




PostPosted: Thu Jan 10, 2013 4:28 pm   Post subject: RE:Need Help With Simple Turing - Making Ball Move with Keyboard with Collision

you should read the tutorials on the use of View.Update
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
HammadTheOne




PostPosted: Thu Jan 10, 2013 5:55 pm   Post subject: Re: RE:Need Help With Simple Turing - Making Ball Move with Keyboard with Collision

Tony @ Thu Jan 10, 2013 4:28 pm wrote:
you should read the tutorials on the use of View.Update


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:

Quote:

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




PostPosted: Fri Jan 11, 2013 5:50 pm   Post subject: 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




PostPosted: Fri Jan 11, 2013 6:26 pm   Post subject: 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

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
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
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  [ 7 Posts ]
Jump to:   


Style:  
Search: