How to make objects bounce of walls need help plz
Author |
Message |
Confused Child
|
Posted: Tue Nov 11, 2008 4:50 pm Post subject: How to make objects bounce of walls need help plz |
|
|
Hi guys so I was just bored and wanted to make a game on Turing. My game will basically feature a ball which is on your mouse and squares bounce around the screen, you have to make sure you do not go inside the square. Just one problem, how do i make the squares bounce around and how can i make the program exit when the ball hits a square.
Heres my program, please tell me if changes can be made to make it better,
P.S. Its my ISU. So i really need a good mark
Any help will be appriciated!
setscreen ("graphics")
var font1, font2, font3, font4 : int
drawfillbox (maxx, maxy, 0, 0, 7)
for xc : 1 .. 310 by 2
drawfilloval (xc, 10, 10, 10, 13)
drawfilloval (maxx - xc, 10, 10, 10, 13)
delay (10)
drawfilloval (xc, 10, 10, 10, 7)
drawfilloval (maxx - xc, 10, 10, 10, 7)
end for
font1 := Font.New ("Arial:50")
font2 := Font.New ("Times New Roman:20")
font3 := Font.New ("Times New Roman:15")
font4 := Font.New ("Roaringfire:30")
Font.Draw ("Welcome to my ISU!", 200, 200, font2, 12)
delay (5000)
cls
drawfillbox (maxx, maxy, 0, 0, 3)
Font.Draw ("Welcome to dodge the balls!", 200, 200, font2, blue)
Font.Draw ("Try too keep the ball away from the sqaures!", 160, 170, font3, black)
cls
var x, y, button : int
drawbox (50, 50, 0, 0, 10)
loop
mousewhere (x, y, button)
locate (1, 1)
if button = 0 then
put x : 4, " ", y : 4, " button up"
else
put x : 4, " ", y : 4, " button down"
end if
drawfilloval (x, y, 5, 5, 13)
delay (10)
drawfilloval (x, y, 5, 5, 31)
end loop
var b11x, b11y: int
plz post TY! |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Insectoid
|
Posted: Tue Nov 11, 2008 4:58 pm Post subject: RE:How to make objects bounce of walls need help plz |
|
|
So you decided to start your ISU because you were bored? What would happen if you weren't bored?
If adding a positive number to an X value makes it go right, what value will make it go left? Hint: Hold the 'movement' in a variable.
Ex.
code: |
var moveX := 1
x += moveX
|
Now what can you do to 'moveX' to make it the value to go to the left? When will you do this? |
|
|
|
|
|
Confused Child
|
Posted: Tue Nov 11, 2008 5:06 pm Post subject: RE:How to make objects bounce of walls need help plz |
|
|
And this is going to make it bounce of walls? |
|
|
|
|
|
Insectoid
|
Posted: Tue Nov 11, 2008 5:12 pm Post subject: RE:How to make objects bounce of walls need help plz |
|
|
If you think really hard about it.
Where are the ball's X and Y values in relation to a wall's coordinates when they collide? How could you make it change direction? when what conditions are met will you change direction? |
|
|
|
|
|
Confused Child
|
Posted: Tue Nov 11, 2008 5:27 pm Post subject: RE:How to make objects bounce of walls need help plz |
|
|
It's squares i want to make bounce of walls, the ball in controlled by mouse. Would it have to do something with x1,y1,x2,y2 of the squares?
sorry programming not my thing trying to learn! |
|
|
|
|
|
Insectoid
|
Posted: Tue Nov 11, 2008 5:46 pm Post subject: RE:How to make objects bounce of walls need help plz |
|
|
Same idea. If What is the relation of the box's coordinates and the wall's when they collide? |
|
|
|
|
|
Confused Child
|
Posted: Tue Nov 11, 2008 6:23 pm Post subject: RE:How to make objects bounce of walls need help plz |
|
|
Im still confused |
|
|
|
|
|
TheGuardian001
|
Posted: Tue Nov 11, 2008 6:30 pm Post subject: Re: How to make objects bounce of walls need help plz |
|
|
for information on collision detection, you should read this: http://compsci.ca/v3/viewtopic.php?t=13661. that should help you understand the basics of collisions.[/url] |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Confused Child
|
Posted: Tue Nov 11, 2008 6:58 pm Post subject: RE:How to make objects bounce of walls need help plz |
|
|
I read that guide. There one prob, since im using mousewhere I want to control that ball i have on the mouse but i want to make other balls bounce around I got a code plz tell me why it doesnt work. The balls dont even come!
setscreen ("graphics")
var font1, font2, font3, font4 : int
drawfillbox (maxx, maxy, 0, 0, 7)
for xc : 1 .. 310 by 2
drawfilloval (xc, 10, 10, 10, 13)
drawfilloval (maxx - xc, 10, 10, 10, 13)
delay (10)
drawfilloval (xc, 10, 10, 10, 7)
drawfilloval (maxx - xc, 10, 10, 10, 7)
end for
font1 := Font.New ("Arial:50")
font2 := Font.New ("Times New Roman:20")
font3 := Font.New ("Times New Roman:15")
font4 := Font.New ("Roaringfire:30")
Font.Draw ("Welcome to my ISU!", 200, 170, font2, 12)
delay (5000)
cls
Font.Draw ("The Dodge the squares game!", 200, 200, font3, 12)
Font.Draw ("Keep the ball(the mouse) away from the squares", 100, 170, font3, 12)
delay (3000)
cls
var x, y, button : int
loop
mousewhere (x, y, button)
locate (1, 1)
if button = 0 then
put x : 4, " ", y : 4, " button up"
else
put x : 4, " ", y : 4, " button down"
end if
drawfilloval (x, y, 5, 5, 13)
drawfilloval (x, y, 5, 5, 31)
end loop
var circle_1_x, circle_1_y, circle_1_radius : int
var circle_2_x, circle_2_y, circle_2_radius : int
var distance_between_centres : real
%give our two circles values. These can obviously be changed.
circle_1_x := 4
circle_1_y := 4
circle_1_radius := 5
circle_2_x := 210
circle_2_y := 190
circle_2_radius := 20
%main program
loop
%draw our circles. Remember, the x-radius and y-radius are the same!
cls
drawoval (circle_1_x, circle_1_y, circle_1_radius, circle_1_radius, 12)
drawoval (circle_2_x, circle_2_y, circle_2_radius, circle_2_radius, 9)
delay (20)
%Move the first oval diagonally
circle_2_x += 1
circle_2_y += 1
%calculate the distance between the centres of the circles
distance_between_centres := sqrt ((circle_2_x - circle_1_x) ** 2 + (circle_2_y - circle_1_y) ** 2)
% collision detection method
if distance_between_centres <= circle_1_radius + circle_2_radius then
put "The circles have collided!"
exit
end if
end loop |
|
|
|
|
|
S_Grimm
|
Posted: Tue Nov 11, 2008 7:07 pm Post subject: RE:How to make objects bounce of walls need help plz |
|
|
Heres a hint: Use code tags. And take a look into the and commands |
|
|
|
|
|
TheGuardian001
|
Posted: Tue Nov 11, 2008 7:35 pm Post subject: Re: How to make objects bounce of walls need help plz |
|
|
you may wish to look at the way your code is structured.
code: |
1)loop
2) mousewhere (x, y, button)
3) locate (1, 1)
4) if button = 0 then
5) put x : 4, " ", y : 4, " button up"
6) else
7) put x : 4, " ", y : 4, " button down"
8) end if
9) drawfilloval (x, y, 5, 5, 13)
10) drawfilloval (x, y, 5, 5, 31)
11)end loop
%----------------------------------------
%and heres the rest of the code...
%----------------------------------------
loop
%draw our circles. Remember, the x-radius and y-radius are the same!
cls
drawoval (circle_1_x, circle_1_y, circle_1_radius, circle_1_radius, 12)
drawoval (circle_2_x, circle_2_y, circle_2_radius, circle_2_radius, 9)
View.Update
delay (20)
%Move the first oval diagonally
circle_2_x += 1
circle_2_y += 1
%calculate the distance between the centres of the circles
distance_between_centres := sqrt ((circle_2_x - circle_1_x) ** 2 + (circle_2_y - circle_1_y) ** 2)
% collision detection method
if distance_between_centres <= circle_1_radius + circle_2_radius then
put "The circles have collided!"
exit
end if
end loop
|
looking at the lines i've numbered here, we can see that you draw the circle at the mouse. then you hit the end loop, so you go back up to 1, and keep doing this forever.
later on in your program, we get to drawing the obstacles. but wait... we never left the first loop of drawing the ball at the mouse. so our program is still back up there repeating the lines I've numbered 1 to 11.
you need to keep all of your games main code in ONE main loop. that way the program runs all of its code before restarting the loop, instead of doing what yours does and do only one chunk of the code over and over. |
|
|
|
|
|
|
|