Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / Making an image disappear with a left mouseclick

Author
Message
Donald Smith
22
Years of Service
User Offline
Joined: 15th Nov 2002
Location:
Posted: 25th Aug 2019 05:44
Hello,
I'm making a game with a 5 x 5 panel on the left and right side of the screen. Each box in the in the 5x5 panel has the same image. The image is 40 pixels wide by 46 pixels high. My issue using mouseclick() =1, I want to click anywhere within the image and have it disappear.
In my code, I'm saving the X and Y coordinates of the pasted images and when the left mouse button gets clicked, it first reads the mousex() position to see if it's the left or the right panel. Then the comparison is done on the mousey() to seewhere the mouseclick is within the image (or what's want I want it to do).

Any ideas?

Thanks, Donald


REM *** These lines holds the coordinates of the 25 images by panels ***

DIM LeftPanel(25,2)
DIM RightPanel(25,2)

REM *** These lines holds the coordinates of the 25 images by panels ***


REM *** Draws the left frame for the 25 cards ***

ink rgb(0,255,0),1
line 0,77,225,77 : ` top left frame line
line 0,77,0,347 : ` left side left frame line
line 225,77,225,347 : ` right side left frame line
line 0,347,225,347 : ` bottom left frame line

REM *** Draws the left frame for the 25 cards ***

REM *** Draws the lines in the left frame separating the cards ***

Y = 54
ink rgb(255,255,255),1
Change = Y
For LineDraw = 1 to 4
line 0, 77 + Change, 225, 77 + Change
Change = Change + Y
Next LineDraw

REM *** Draws the lines in the left frame separating the cards ***

REM *** Places the Money vault image in the left frame ***
REM *** Stores the position of the 25 left images ***

Count = 1
X = 5 : Y = 82
For Down = 1 to 5
For Across = 1 to 5
paste image 1, X, Y
LeftPanel(Count,1) = X : LeftPanel(Count,2) = Y
X = X + 44 : Count = Count + 1
Next Across
X = 5 : Y = Y + 54
Next Down

REM *** Places the Money vault image in the left frame ***
REM *** Stores the position of the 25 left images ***

REM *** Draws the 10 slots ***
X = 235 : Y = 39
For Draw = 1 to 10
ink rgb(0,255,0),1

REM *** Draws the Outside Line then Inside Line ***

line x, y, x + 169, y
line x + 1 , y + 1, x + 168, y + 1

line x + 169, y ,x + 169, y + 30
line x + 168, y + 1 ,x + 168, y + 29

line x + 169, y + 30, x, y + 30
line x + 168, y + 29, x + 1, y + 29

line x, y + 30, x, y
line x + 1, y + 29, x + 1, y + 1

REM *** Draws the Outside Line then Inside Line ***

REM *** Draws the slot numbers, descending order ***

ink rgb(128,128,128),1
Num$ = STR$(11 - Draw)
set text font "Gulim"
set text size 12
if Draw = 1
center text 311,y + 5,"."
center text 317,y + 10,Num$
center text 326,y + 5,"."
else
if Draw >= 2 and Draw <= 9
center text 313,y + 5,"."
center text 317,y + 10,Num$
center text 323,y + 5,"."
else
if Draw = 10
center text 314,y + 5,"."
center text 317,y + 10,Num$
center text 322,y + 5,"."
endif
endif
endif

REM *** Draws the slot numbers, descending order ***

Y = Y + 35
Next Draw
REM *** Draws the 10 slots ***

REM *** Draws the right frame for the 25 cards ***

ink rgb(0,255,0),1
line 414,77,639,77 : ` top right frame line
line 414,77,414,347 : ` left side right frame line
line 639,77,639,347 : ` right side right frame line
line 414,347,639,347 : ` bottom right frame line

REM *** Draws the right frame for the 25 cards ***

REM *** Draws the lines in the right frame separating the cards ***

Y = 54
ink rgb(255,255,255),1
Change = Y
For LineDraw = 1 to 4
line 414, 77 + Change, 639, 77 + Change
Change = Change + Y
Next LineDraw

REM *** Draws the lines in the right frame separating the cards ***

REM *** Places the Money vault image in the right frame ***
REM *** Stores the position of the 25 right images ***

Count = 1
X = 419 : Y = 82
For Down = 1 to 5
For Across = 1 to 5
paste image 1, X, Y
RightPanel(Count,1) = X : RightPanel(Count,2) = Y
X = X + 44 : Count = Count + 1
Next Across
X = 419 : Y = Y + 54
Next Down

REM *** Places the Money vault image in the right frame ***
REM *** Stores the position of the 25 right images ***

show mouse

do

ink rgb(0,0,0),1
Set cursor 593,445 : Print X1
Set cursor 594,460 : Print Y1

ink rgb(128,0,0),1
line 3, 80, 45, 80
line 45, 80, 45, 129
line 45, 129, 3, 129
line 3, 129, 3, 80

Set cursor 520,445 : Print "Mouse X = ";mousex()
Set cursor 520,460 : Print "Mouse Y = ";mousey()

REM *** Figure out if the mouse pointer is on the image ***

REM *** Figure out if the mouseclick is on the image ***
if mouseclick()=1
if mousex() => 5 and mousex() <= 225

Count = 1
Y = 82
For Down = 1 to 5
if mousey()>= LeftPanel(Count,2) and mousey()<= LeftPanel(Count,2) + 46
goto Done
else
endif
Count = Count + 1
Y = Y + 54
Next Down
endif
endif

loop



Done:
LeftPanel(Count,1) = X1 : LeftPanel(Count,2) = Y1
ink rgb(128,0,0),1
Set cursor 258,445 : Print "Mouse X position = ";mousex()
Set cursor 258,460 : Print "Mouse Y position = ";mousey()

ink rgb(0,0,0),1
box X1,Y1,X1 + 40,Y1 + 46

do:loop

Donald Smith
22
Years of Service
User Offline
Joined: 15th Nov 2002
Location:
Posted: 25th Aug 2019 05:47
Sorry, I couldn't remember how to use code snippet.
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 29th Aug 2019 20:15 Edited at: 29th Aug 2019 20:19
Hi Donald.

I just realized I'm starting to forget the syntax for DBC.

Anyway I'll present two methods. The first method should return the number of the panel. Since each panel is 40 X 46, all we need is the start position (the upper left corner) for the whole are of the screen where the panels may be located. I didn't run your code (i'm not at a computer with DBC) so I'm not 100% sure where the panels start. Let's say the upper left for the start of the Left sides 5x5 panels is at Xstart=5 and Ystart=82

The theory is this: if you know which panel you are over, you can use that number, and then on mouseclick() go back to the array where the coordinates for that panel are stored, and then paste a new image over that panel at those coordinates - effectively making that panel disappear. You can find which panel you are over with a simple formula:




The second method would be to loop through the arrays that store each panels X and Y position and then calculate the X2 and Y2 (the lower right corner coordinates of the panel) positions based on the size of the panel . You would plug those values into the formula in the code below, and then if the return value is 1, test for a mouseclick() and paste a new image at the X and Y coordinates you tested for in the array.

Being a little forgetful about DBC function syntax, I think the function in the code below is correct. It's a quick way to check if the mouse pointer is within a box on the screen. It will return a zero if false and a 1 if true:




For this method, Basically you feed the dimensions of your panels
Enjoy your day.
Donald Smith
22
Years of Service
User Offline
Joined: 15th Nov 2002
Location:
Posted: 30th Aug 2019 04:51
Latch,
First, thank you for the code snippets, especially the 2nd snippet. I figured out how to make the boxes "disappear" before you replied to me by:

1) not using a multi-dimensional array for the starting top left corner of the image, eg. DIM LeftPanelX(25), LeftPanelY(25)
2) eliminating the two nested FOR commands that checked the position of mouseclick()=1 to only using one variable that incremented by 1.

In my code, since I had a white line between the rows and black space between the images, I used the point() command to check the color under the cursor when mouseclick()=1.
With your 2nd snippet, I don't need the point() command anymore and I can go back and use the multi-dimensional arrays. Plus, your seems to run faster to me ( could it be that I'm imagining this... )

Thank you for your help, Latch

Donald
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 3rd Sep 2019 20:47 Edited at: 3rd Sep 2019 21:13
No Problem. In fact, I miss messing around with DBC.

You might want to check out example 1 and play around with it. That code is a basic grid "collision" routine. It's useful on anything with a grid, even terrains. It uses a calculation based on the count of pixels, or vertices if you're using 3d, and returns a number of the square or cell your pointer, character, vehicle, whatever is over. You control the limits and you can modify the formula to account for things like the border in between or where the grid of cells starts. The formula returns the number of the grid square you are over. From there you take that number and do whatever you want with it - hide the square, make a monster jump out, crash a car, change the sound effect, etc.

As far as faster, that mousewithin() function should be pretty quick. It only tests what it needs to before getting out of the loop. if the first condition isn't met, it leaves the function without testing any of the others. It only moves forward in the tests if a condition returns true. It's faster than ANDing all four possible dimensions of rectangular area - and since the dimensions are passed to the function, it doesn't call mousex() and mousey() inside the function which would just slow it down also.

Speed related to Point(). Point() is notoriously slow. However, it can perform reasonably well if you realize that it automatically calls SYNC each time it is used.

Here's some info from a Post I made in 2009.

Posted: 3rd Mar 2009 17:26

If you are using the point() command in a loop, SYNC (refresh) is automatically called each loop. This example shows what happens. At a Sync rate of 60, it should take 16 or 17 ms to run through a 1000 iteration loop. If you SYNC each loop (which point does) it basically takes 16 ms to get through 1 loop iteration - that means it's about 1000 times slower to refresh each loop:



So, alternatives: if you need to call POINT() each iteration in your main loop, use it as the last command in your main DO LOOP instead of SYNC:



I dug through the forums and found an old entry I made in the DBC Challenges back in Jul 2007 (yikes - so long ago!) It's a bit complex but it uses the forms of grid selection that we talked about above. The function you'll want to dissect is mouse_cell(horz,vert,cellsizex,cellsizey) . It's a a little fancier that the formula in method 1 proposed in the thread but it basically does the same thing and guess what, it also uses the Point() command. The difference here is that the rest of the function is specific to the program it's running in and not only returns the cell number that's being clicked on, but changes the color in the onscreen grid. A little modification and this could be used to make your cells that are clicked on disappear.

Please note, the Grid() function is by TDK_Man.


Enjoy your day.

Login to post a reply

Server time is: 2024-11-17 22:48:57
Your offset time is: 2024-11-17 22:48:57