How to make a simple ball bounce off walls?
Author |
Message |
jpui
|
Posted: Sun Dec 03, 2006 7:10 pm Post subject: How to make a simple ball bounce off walls? |
|
|
Hello,
I'm new to Turing and I am starting to learn it in my Gr. 11 Comp. Engineering class.
I need to know the code to make a simple ball bounce off walls.
So far I got:
code: |
var intX:int
var intY:int
var intTop:int
var intSide:int
var intA:int
var intB:int
%var intNegative:1
intX := 20
intY := 20
intTop := 400
intSide := 640
%put "Enter X coordinate to start"
%get intX
%put "Enter Y coordinate to start"
%get intY
%cls
loop
intA := intX
intB := intSide
if intA < intSide then
drawfilloval(intX,intY,30,30,blue)
delay(10)
drawfilloval(intX,intY,30,30,white)
intX := intX + 5
intY := intY + 2
intA := intX
intB := intY
else
drawfilloval(intX,intY,30,30,blue)
delay(10)
drawfilloval(intX,intY,30,30,white)
intX := intX - 5
intY := intY - 2
intA := intX
intSide := 0
end if
%if intA <= 0 then
% drawfilloval(intX,intY,30,30,blue)
% delay(10)
% drawfilloval(intX,intY,30,30,white)
% intX := intX + 5
% intY := intY + 2
% intA := intX
%end if
end loop |
Please help me on this. Thanks.
-jpui[/code] |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
jpui
|
Posted: Sun Dec 03, 2006 7:14 pm Post subject: (No subject) |
|
|
I can make the ball bounce once and then it gets stuck. If anyone know a different way that makes the ball bounce everywhere instead of just one direction, please post it here (Just make the code simple so I can at least understand it ).
Thanks.
(And Don't worry about the codes with the %, I don't need them.) |
|
|
|
|
 |
Clayton

|
Posted: Sun Dec 03, 2006 7:32 pm Post subject: (No subject) |
|
|
Go check out the Turing Walkthrough for the tutorials on collision detection, there you should find what you need, if not, post back here with more questions.
Welcome to CompSci.ca btw |
|
|
|
|
 |
jpui
|
Posted: Sun Dec 03, 2006 7:42 pm Post subject: (No subject) |
|
|
Freakman wrote: Go check out the Turing Walkthrough for the tutorials on collision detection, there you should find what you need, if not, post back here with more questions.
Welcome to CompSci.ca btw
Hello,
Thanks.
Sorry, but I'm in a bit of a hurry (due tmw), and i need the codes fast. Can someone just fix up the codes? It's just this time lol.
Thx. |
|
|
|
|
 |
jpui
|
Posted: Sun Dec 03, 2006 7:45 pm Post subject: (No subject) |
|
|
(Where's the Edit button?)
Anyways, There's nothing there I need.
I need a ball that bounces off walls, not another ball. |
|
|
|
|
 |
uberwalla

|
Posted: Sun Dec 03, 2006 7:56 pm Post subject: (No subject) |
|
|
1) dont rush people it makes us not want to help you
2) we cant just give you the code (or we can but theres a challenge to it)
ok.. so here it is. a ball that bounces off walls.
code: |
View.Set ("graphics:800;500,offscreenonly, nocursor")
var x, y : int := 10
var changeX, changeY := 10
loop
x += changeX
y += changeY
if whatdotcolor (x + 11, y) = red then
changeX := -changeX
x += changeX
y += changeY
end if
if whatdotcolor (x - 11, y) = red then
changeX := -changeX
x += changeX
y += changeY
end if
if whatdotcolor (x, y + 11) = red then
changeY := -changeY
x += changeX
y += changeY
end if
if whatdotcolor (x, y - 11) = red then
changeY := -changeY
x += changeX
y += changeY
end if
if whatdotcolor (x + 13, y + 13) = red then
changeX := -changeX
changeY := -changeY
x += changeX
y += changeY
end if
if whatdotcolor (x + 13, y - 13) = red then
changeX := -changeX
changeY := -changeY
x += changeX
y += changeY
end if
if whatdotcolor (x - 13, y + 13) = red then
changeX := -changeX
changeY := -changeY
x += changeX
y += changeY
end if
if whatdotcolor (x - 13, y - 13) = red then
changeX := -changeX
changeY := -changeY
x += changeX
y += changeY
end if
if x <= 10 or x >= 790 then
changeX := -changeX
sound (500, 10)
end if
if y <= 10 or y >= 490 then
changeY := -changeY
sound (500, 10)
end if
cls
Draw.FillOval (x, y, 10, 10, black)
View.Update
Time.Delay (10)
end loop
|
like i was talking about in #2 there is a challenge to this//catch.
just so u cant completely take the code u have to explain how it works to ur teacher , for it is not my job to do so. (not trying to sound like a jerk but i no1 here at compsci really wants to do ur work for u. we'll help (which i did u just explain it ) |
|
|
|
|
 |
jpui
|
Posted: Sun Dec 03, 2006 8:14 pm Post subject: (No subject) |
|
|
I know. Its a bit of a rush and thanks for the help .
I usually read the stuff slowly and try to figure the stuff out myself and with a bit help from other people. but since I've been stuck on this question for awhile and its due tmw, I needed some codes to make the program work.
Thanks uberwalla for the help and Freakman for trying to help . |
|
|
|
|
 |
jpui
|
Posted: Sun Dec 03, 2006 8:24 pm Post subject: (No subject) |
|
|
.....
I don't really understand the codes you put in. Is it okay if you please change my code around using simple codes (without the sound and the View.Update thing).
Thanks. |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Clayton

|
Posted: Sun Dec 03, 2006 8:27 pm Post subject: (No subject) |
|
|
If you read some tutorials in the Turing Walkthrough, I'm sure you'll find your answers soon enough. |
|
|
|
|
 |
Piro24
|
Posted: Sun Dec 03, 2006 8:32 pm Post subject: (No subject) |
|
|
You are overthinking it. Make another variable for axis of each ball, maybe call it direction or something.
So for one ball, make directionX, directionY.
If you want the ball to travel up and right, make their values 1 and 1. If you want them to travel down and left, make them -1 and -1.
Than use the normal parameters for the ball, and add it to the direction. Lets say the x-cordinate is called 'x1' on your ball and the y is 'y1'.
Do x1 += directionX and y1+=directionY
That will move the balls.
Than make statements like 'if x1 = maxx then direction *= -1'
The direction *= -1 will make the ball go in the opposite direction, since it is now the opposite integer (ie. If you made it positive to go up, it will be negative therefor go down. Same with left and right). |
|
|
|
|
 |
TokenHerbz

|
Posted: Sun Dec 03, 2006 8:36 pm Post subject: (No subject) |
|
|
if both x1/y1 are active at a time, it creats a dierections:
x1 = 1 so its moving right at 1 px / loop
y2 = 2 so its moving up
this will make the ball move at an angle shooting top right of the screen.
Adjust and minipulate the velocities of the ball to creat other directional points, and use ifs etc: to keep the ball inside. |
|
|
|
|
 |
jpui
|
Posted: Sun Dec 03, 2006 10:52 pm Post subject: (No subject) |
|
|
TokenHerbz wrote: if both x1/y1 are active at a time, it creats a dierections:
x1 = 1 so its moving right at 1 px / loop
y2 = 2 so its moving up
this will make the ball move at an angle shooting top right of the screen.
Adjust and minipulate the velocities of the ball to creat other directional points, and use ifs etc: to keep the ball inside.
Thank you guys for the many help.  |
|
|
|
|
 |
|
|