How to make it so that game exits when circles hit mouse
Author |
Message |
Confused Child
|
Posted: Wed Nov 26, 2008 12:11 pm Post subject: How to make it so that game exits when circles hit mouse |
|
|
Hey guys I made a game that has circles bouncing around the screen and the point is to dodge them. However I want to make it so that when one circle hits the mouse (which has a small circle on it) the game exits and you lose. That is what I am having trouble on.
Please do help me because it is my ISU
Here's the code:
Turing: |
var counter, circle_ 1, circle_ 2, 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
var c_1x_inc, c_2x_inc, c_1y_inc, c_2y_inc, mouseball_radius : int
%give our two circles values. These can obviously be changed.
counter := 0
c_1x_inc := 15
c_2x_inc := 25
c_1y_inc := 15
c_2y_inc := 25
circle_ 1_x := 5
circle_ 1_y := 5
circle_ 1_radius := 55
circle_ 2_x := 210
circle_ 2_y := 190
circle_ 2_radius := 20
%main program
loop
var x, y, button : int
mousewhere (x, y, button )
locate (1, 1)
drawfilloval (x, y, 5, 5, 13)
delay (10)
drawfilloval (x, y, 5, 5, 31)
%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)
delay (20)
drawoval (circle_ 2_x, circle_ 2_y, circle_ 2_radius, circle_ 2_radius, 9)
%Move the first oval diagonally
circle_ 1_x + = c_1x_inc
circle_ 1_y + = c_1y_inc
circle_ 2_x + = c_2x_inc
circle_ 2_y + = c_2y_inc
if circle_ 1_x >= maxx or circle_ 1_x <= 0 then
c_1x_inc := -c_1x_inc
end if
if circle_ 1_y >= maxy or circle_ 1_y <= 0 then
c_1y_inc := -c_1y_inc
end if
if circle_ 2_x >= maxx or circle_ 2_x <= 0 then
c_2x_inc := -c_2x_inc
end if
if circle_ 2_y >= maxy or circle_ 2_y <= 0 then
c_2y_inc := -c_2y_inc
end if
%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
put " Score ", counter
counter := counter + 1
end loop
put "The circles have collided!"
|
I have an exit statement too but it only exits when the two bouncing circles hit. Here it is:
distance_between_centres := sqrt ((circle_2_x - circle_1_x) ** 2 + (circle_2_y - circle_1_y) ** 2)
exit when distance_between_centres <= circle_1_radius + circle_2_radius
Please help me change this so that it exits when the circles hit the mouse.
Thank you in advance!
MOD EDIT: Remember the code tags! |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
S_Grimm

|
Posted: Wed Nov 26, 2008 12:47 pm Post subject: Re: How to make it so that game exits when circles hit mouse |
|
|
Confused Child @ Wed Nov 26, 2008 12:11 pm wrote:
Please do help me because it is my ISU
Ummmmm........................
Just because it's your ISU does not mean we should help you. If anything, I think it means we should post advice and let you go from there.
Another thing: CODE TAGS.
Third. Mouse.Where (x,y,z) use it to determine if the location is inside or touching one of your circles. |
|
|
|
|
 |
MihaiG

|
Posted: Wed Nov 26, 2008 3:05 pm Post subject: Re: How to make it so that game exits when circles hit mouse |
|
|
essentially to detect when a circle hits a mouse, you need two know 5things
the x coordinate of the circle, the y coordinate of the circle, the x coordinate of the mouse and the y coordinate of the mouse and the radius
using this we can use the Pythagorean theorem to find the distance between the two points and compare it to the radius such as...
code: |
var px,py,b : int
var cx,cy : int
var radius : int := 25
loop
mousewhere(px,py,b)
if sqrt((px-cx)**2 + (py-cy)**2) -25 < 0.01 then
put "mouse inside circle"
end if
end loop
|
so i take the distance between the two points and subtract 25 from it, and compare it to 0.01 , if i do sqrt((px-cx)**2 + (py-cy)**2) -25 =0, there can be errors in the code which can screw it up, and this guarantees for any error in onscreen drawing of the circle
you basically had the samething in your code, i |
|
|
|
|
 |
Parker
|
Posted: Wed Nov 26, 2008 3:20 pm Post subject: RE:How to make it so that game exits when circles hit mouse |
|
|
Mihai, in my opinion that sounds a bit confusing. Probably more accurate the what I just did but I will give him another option. I am going to post the code for him to use because I believe he was on the right track and this was just a minor logic error. For your own good find out what you did wrong and what I fixed. Here is the new code :
Turing: | put "Final Project."
|
|
|
|
|
|
 |
Jurica
|
Posted: Wed Nov 26, 2008 5:59 pm Post subject: RE:How to make it so that game exits when circles hit mouse |
|
|
Made it a little better:
Turing: |
put "Better Final Project."
|
Mod Edit: Put in a missing ] in the tags |
|
|
|
|
 |
Clayton

|
Posted: Wed Nov 26, 2008 6:51 pm Post subject: RE:How to make it so that game exits when circles hit mouse |
|
|
Careful on giving away full solutions guys. |
|
|
|
|
 |
Parker
|
Posted: Thu Nov 27, 2008 8:11 am Post subject: RE:How to make it so that game exits when circles hit mouse |
|
|
Sorry Clayton, forgot that it was for Final Project. Confused, the best I can do is try to tell you what you are missing. You have this line:
Turing: | distance_between_centres := sqrt ((circle_ 2_x - circle_ 1_x ) ** 2 + (circle_ 2_y - circle_ 1_y ) ** 2) |
That is an important part of your collision. So you are giving distance_between_centres a value but then are doing nothing with it. Use that variable with an if statement to check if the circles are touching.
Hope I helped, Parker. |
|
|
|
|
 |
|
|