Posted: Wed Dec 10, 2008 11:22 pm Post subject: Can't get mousewait to work :(
Hi, I have another assignment and I'm suppose to make a game using mousewait and this is my code so far:
Quote:
% Declares Integers
% x: Where the user has clicked - x coordinate
% y: Where the user has clicked - y coordinate
% button: determine whether the mouse has been clicked or
% not and which button has been clicked
% x1, y1, x2, and y2 are randomized integers for a box
% c1 is a randomized integer for colour
var x, y, button : int
var x1, y1, x2, y2, c1 : int
% Display a message
put "Click the box to win!"
loop
cls
randint (x1, 30, 75)
randint (y1, 30, 75)
randint (x2, 30, 75)
randint (y2, 30, 75)
randint (c1, 1, maxcolour)
drawfillbox (x1, y1, x2, y2, c1)
mousewhere (x, y, button)
exit when button = 1 and x >= x1 and x <= x2 and y >= y1 and y <= y2
delay (2000)
put "Sorry, try again."
delay (1000)
end loop
put "Congratulations, you win!"
The thing is, it doesn't work; it's suppose to exit when the user has clicked in the box, but it's not doing that. Can anyone tell me what I am doing wrong?
Thanks
- chroncile
Sponsor Sponsor
DemonWasp
Posted: Thu Dec 11, 2008 1:14 am Post subject: RE:Can\'t get mousewait to work :(
You seem to be re-randomising the location of the box (x1, y1, x2, y2), clearing the screen, and redrawing the box, on every iteration of the loop. You only want to do that once, before the loop begins.
chroncile
Posted: Thu Dec 11, 2008 10:07 am Post subject: RE:Can\'t get mousewait to work :(
No no no, I need to do that because the game puts the box randomly on the screen and if I were to do that, it wouldn't work
Tony
Posted: Thu Dec 11, 2008 10:39 am Post subject: RE:Can\'t get mousewait to work :(
The code is doing exactly what it is told (read it slowly, one line at a time). You are just not clicking fast enough
Posted: Thu Dec 11, 2008 12:13 pm Post subject: RE:Can\'t get mousewait to work :(
Okay, I made it like this:
mousewhere (x, y, button)
delay (1000)
exit when button = 1 and x >= x1 and x <= x2 and y >= y1 and y <= y2
But it's still not working.
Tony
Posted: Thu Dec 11, 2008 12:56 pm Post subject: RE:Can\'t get mousewait to work :(
clearly, you fail to grasp what mousewhere does. Go back and re-read class notes / documentation / tutorials available on this website.
In the meantime...
Turing:
mousewhere(x, y, button) locate(1,1) put"Your mouse is at: ", x, " : ", y
put"Your box is between: ", x1, ":", y1, " and ", x2, ":",y2
exitwhen button =1and x >= x1 and x <= x2 and y >= y1 and y <= y2
Posted: Thu Dec 11, 2008 1:03 pm Post subject: RE:Can\'t get mousewait to work :(
Thanks for that, but the problem is that it's still not working. I clicked inside the box and it didn't work.
You say it's working, but how come it's not doing that for me?
Tony
Posted: Thu Dec 11, 2008 1:23 pm Post subject: Re: RE:Can\'t get mousewait to work :(
chroncile @ Thu Dec 11, 2008 1:03 pm wrote:
I clicked inside the box and it didn't work.
Yes, but was your program recording the status of your mouse at that instance? You are using mousewhere just once (per each box location) -- if your mouse is not over the box at the same moment as it's created, then it's not going to work. Perhaps you should check the status of the mouse more than once, preferably over some range of time (instead of a point).