Is there a better way to do this???

Previous topic - Next topic

fuzzy70

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]
"Why don't you just make ten louder and make ten be the top number and make that a little louder?"
- "These go to eleven."

This Is Spinal Tap (1984)

Ian Price

#1
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...
I came. I saw. I played.

kanonet

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
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

fuzzy70

#3
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
"Why don't you just make ten louder and make ten be the top number and make that a little louder?"
- "These go to eleven."

This Is Spinal Tap (1984)

Kitty Hello

I use boxcoll for that:
BOXCOLL(left, top, width, height, mousex, mousey, 1,1)

fuzzy70

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  :)
"Why don't you just make ten louder and make ten be the top number and make that a little louder?"
- "These go to eleven."

This Is Spinal Tap (1984)

Ian Price

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. :)
I came. I saw. I played.

fuzzy70

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
"Why don't you just make ten louder and make ten be the top number and make that a little louder?"
- "These go to eleven."

This Is Spinal Tap (1984)

Ian Price

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 :)
I came. I saw. I played.

fuzzy70

This was my Facebook avatar for ages lol

[attachment deleted by admin]
"Why don't you just make ten louder and make ten be the top number and make that a little louder?"
- "These go to eleven."

This Is Spinal Tap (1984)

Ian Price

I came. I saw. I played.

Hatonastick

I like too, another big fan of The Trapdoor here!
Mat. 5: 14 - 16

Android: Toshiba Thrive Tablet (3.2), Samsung Galaxy Tab 2 (4.1.2).
Netbook: Samsung N150+ Netbook (Win 7 32-bit + Ubuntu 11.10).
Desktop: Intel i5 Desktop with NVIDIA GeForce GTX 460 (Win 8.1 64-bit).