GLBasic forum

Main forum => GLBasic - en => Topic started by: fuzzy70 on 2011-Nov-03

Title: Is there a better way to do this???
Post by: fuzzy70 on 2011-Nov-03
The logo game continues with earnest lol. Just a quick question asking if there is a better way to do the following.

Code (glbasic) Select
// Check Mouse zone
FUNCTION MouseArea:
LOCAL gamegrid
IF mx>=10 AND mx<=228 AND my>=10 AND my<=228 THEN gamegrid=TRUE // Mouse clicked in Playarea
IF mx>=200 AND mx<=206 AND my>=270 AND my<=276 THEN colour=0 //  ""     ""    on Blue Option
IF mx>=207 AND mx<=213 AND my>=270 AND my<=276 THEN colour=2 //  ""     ""    on Red Option
IF mx>=214 AND mx<=220 AND my>=270 AND my<=276 THEN colour=4 //  ""     ""    on Green Option
IF gamegrid THEN PieceClicked((mx-10)/22,(my-10)/22)
ENDFUNCTION


Ignoring the gamegrid variable for the moment (it's a left over from a test earlier) as that can be moved back on the end of the IF statement. The colour variable just basically allows the changing of the game colours while playing. The above code works fine but just somehow looks odd to me with the multiple AND's & was just wondering if there is a better or alternative method as i may be adding over zones to be checked.

Screenshot attached to help, although they are just working gfx & may (prob will) change.

Cheers

Lee



[attachment deleted by admin]
Title: Re: Is there a better way to do this???
Post by: Ian Price on 2011-Nov-03
You could use -
Code (glbasic) Select

IF my>=270 AND my<=276 AND b1
IF mx>=200 AND mx<=206 THEN colour=0 //  ""     ""    on Blue Option
IF mx>=207 AND mx<=213 THEN colour=2 //  ""     ""    on Red Option
IF mx>=214 AND mx<=220 THEN colour=4 //  ""     ""    on Green Option
ENDIF


(b1=mousebutton 1)

If the "colour" numbers were consecutive you could even just use do something like -
Code (glbasic) Select

IF mx>=207 AND mx<=220 THEN colour%= (mx-207)/7

instead of the IF... ANDs.

Or something like that...
Title: Re: Is there a better way to do this???
Post by: kanonet on 2011-Nov-03
Darn Ian was faster. :D

I would use something like that, not tested, small errors are possible, but you get the idea (reducing variable calls and comparisons -> bit more speed):
Code (glbasic) Select
// Check Mouse zone
FUNCTION MouseArea:
IF mx>=10 AND mx<=228 AND my>=10 AND my<=228
PieceClicked((mx-10)/22,(my-10)/22) // Mouse clicked in Playarea
ELSEIF mx<=220 AND my>=270 AND my<=276 THEN
IF mx>=214
colour=4 //  ""     ""    on Green Option
ELSEIF mx>=207
colour=2 //  ""     ""    on Red Option
ELSEIF mx>=200
colour=0 //  ""     ""    on Blue Option
ENDIF
ENDIF
ENDFUNCTION
Title: Re: Is there a better way to do this???
Post by: fuzzy70 on 2011-Nov-03
Good call, I can make the colours consecutive as all they do at the moment is reference the frames, 0=dark blue, 1 = bright blue, 2 = dark red & so on. Then just multiply by 2 in the draw routine or similar 

I went with the following

Code (glbasic) Select
// Check Mouse zone
FUNCTION MouseArea:
IF my>=10 AND my<=228 AND mx>=10 AND mx<=228 THEN PieceClicked((mx-10)/22,(my-10)/22) // Mouse clicked in Playarea
IF my>=270 AND my<=276 // Select Colours
IF mx>=207 AND mx<=220 // 200-206=Blue, 207-213=Red, 214-220=Green
    colour=(mx-200)/7
    ELSE
    colour=0
    ENDIF
ENDIF
ENDFUNCTION


I had to subtract 200 Ian instead of 207 as it was giving me a result of 0 or 1 when I needed 1 or 2 but that's not a problem, Thanks yet again :)

To me that looks a lot cleaner than my original code & if/when I move or change the graphic for that it will be a nice & easy edit  =D
Title: Re: Is there a better way to do this???
Post by: Kitty Hello on 2011-Nov-04
I use boxcoll for that:
BOXCOLL(left, top, width, height, mousex, mousey, 1,1)
Title: Re: Is there a better way to do this???
Post by: fuzzy70 on 2011-Nov-04
Quote from: Kitty Hello on 2011-Nov-04
I use boxcoll for that:
BOXCOLL(left, top, width, height, mousex, mousey, 1,1)

Not a command I have got around to using yet lol. Doesn't that just return a true/false on a collision?. If so then still need to check what has been hit & what area. Will have a look at that when back home later  :)
Title: Re: Is there a better way to do this???
Post by: Ian Price on 2011-Nov-04
Indeed,the BOXCOLL still needs the X & Y info for each box and the mouse position. I don't tend to use it myself either, so can't say if or how much faster this is than my general FOR/NEXT loop.

I forgot to say, I love your avatar. Big fan of Chorlton and the Wheelies. :)
Title: Re: Is there a better way to do this???
Post by: fuzzy70 on 2011-Nov-04
Quote from: Ian Price on 2011-Nov-04
I forgot to say, I love your avatar. Big fan of Chorlton and the Wheelies. :)

Thanks Ian   :booze:

I wouldn't say I am obsessed with 70/80's kids tv, but Chorlton and the Wheelies, The Flumps, The Clangers, Jamie & his Magic Torch, Trapdoor etc do bring back good childhood memories. Talking of Chorlton and the Wheelies, I do have the full DVD set  :D

Lee
Title: Re: Is there a better way to do this???
Post by: Ian Price on 2011-Nov-04
QuoteTalking of Chorlton and the Wheelies, I do have the full DVD set
So do I!

And The TrapDoor (my personal fave) and Jamie & The Magic Torch, The Flumps, Paddington, BagPuss, The Wombles and several others!

We're all just big kids at heart :)
Title: Re: Is there a better way to do this???
Post by: fuzzy70 on 2011-Nov-04
This was my Facebook avatar for ages lol

[attachment deleted by admin]
Title: Re: Is there a better way to do this???
Post by: Ian Price on 2011-Nov-04
Like!
Title: Re: Is there a better way to do this???
Post by: Hatonastick on 2011-Nov-05
I like too, another big fan of The Trapdoor here!