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.

Newcomers DBPro Corner / menu screen

Author
Message
Rich S
13
Years of Service
User Offline
Joined: 27th Jul 2011
Location:
Posted: 27th Jul 2011 13:09
Hi all,

I'm starting a new project and I could use some advice with creating a menu screen.

I've got 2 buttons, a 'new game' and 'quit' button. They are both images. Whats the best way to make them functional?
I'm a quite new to DBP so I dont know if I've been doing it a good way. I've been trying (I havnt succeeded yet) on checking where the mouse position is, comparing that to the area the images cover then saying if mouse click = true load game or exit depending on the button.

I'm not sure if this is the right way though as its been more difficult that I thought. Any advice would be much appreciated, thanks.

Dont get mad, get paid
29 games
18
Years of Service
User Offline
Joined: 23rd Nov 2005
Location: not entirely sure
Posted: 28th Jul 2011 00:37
You're pretty much there.

If you know the size and position of the buttons then you know where the edges of the buttons are, so:

If the left edge is at x position 10 and right edge is at x position 20. If the top edge of the button is at y position 30 and bottom edge is at y position 40 (don't forget zero y is at the top of the screen. The you check:

If the x position of the mouse is greater then 10 (i.e. to the right of the left hand edge)

If the x position of the mouse is less than 20 (i.e. to the left of the right edge)

If the y position of the mouse is greater than 30 (i.e under the top edge of the button)

If the y position of the mouse is less than 40 (i.e. above the bottom edge of the button)

Then the cursor in inside the button.

If mousclick() = 1 (i.e. if you've pressed the left hand mouse button) when the cursor is inside the button then the button has been pressed.

Hope that makes sense.

However, if you don't know the size of the button (which you might not do if you are using an image for the button) I'd recommend replacing the cursor with a sprite and then using the sprite collision command to detect if the cursor is over lapping a button.

I've assumed you're doing this in 2D but I hope this helps.
Rich S
13
Years of Service
User Offline
Joined: 27th Jul 2011
Location:
Posted: 28th Jul 2011 09:50
ok great thanks for the help.

When I do the IF statements, can I use AND as well?

So it would be like
IF mouseX <= 500 AND >= 645 AND mouseY ....

THEN IF mouseclick() = 1 (start new game)

ENDIF
ENDIF


or am I going to run into problems that way?

thanks

Dont get mad, get paid
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 28th Jul 2011 20:35 Edited at: 28th Jul 2011 20:51
Quote: "IF mouseX <= 500 AND >= 645 AND mouseY"

...is definitely appropriate and urged usage.

to further your best practice dbpro habits, you should only call mousex() / mousey() once during each iteration of your menu routine and store them as variabes for later checks:


the reason behind the practice is that the mouse can move/be repositioned during a single loop/iteration which could cause inaccuracies/ill-effects.

ie, in the basic menu routine above, imagine if the mouse y-position changes between button checks within the for/next loop, then, technically, 2 of the buttons could be active at once =

you won't necessarily notice this in such a short piece of code as processing it is a breeze for your system but imagine a seriously-complex, lengthy routine combined with everything else that could be going on in a fully-developed application/game... i've experienced it myself, as have many others here and, without accounting for it, it's a tough "bug" to track down.

edit: cleaned code up a little

Virtual Nomad @ California, USA . DBPro V7.5
AMD Phenomâ„¢ X4 9750 Quad-Core @ 2.4 GHz . 8 GB PC2-6400 RAM
ATI Radeon HD 3650 @ 512 MB . Vista Home Premium 64 Bit
29 games
18
Years of Service
User Offline
Joined: 23rd Nov 2005
Location: not entirely sure
Posted: 28th Jul 2011 22:07
To add to Virtual Normad's post:


IF mouseX <= 500 AND >= 645 AND mouseY ....


Is correct, except it should be written like this:


IF mouseX <= 500 AND mouseX >= 645 AND mouseY ....


Not notice that after the first "and" you need to restate the variable otherwise DBP will not know what variable you are checking and you'll get an error.

Also, you have your numbers back to front. The way you have it, the mouse would be outside the button. If you look at just the mouseX check:


IF mouseX <= 500 AND mouseX >= 645...


The left hand edge of the button is at 500 and the right hand edge is at 645. The way you have your code written is checking whether or not the mouse is outside the button (ie to the mouse to the left and the left edge and mouse to the right of the right edge) so you need to write:


IF mouseX >= 500 AND mouseX <= 645...


Have a look at line 31 of Virtual Normad's demo and you'll see this.
Rich S
13
Years of Service
User Offline
Joined: 27th Jul 2011
Location:
Posted: 29th Jul 2011 15:18
thats for all the advice and help guys.

I understand the concept a bit better now thanks.

I've had a few tries to code it but I've been running into some errors. If I cant get it sorted could I post my code on here for some help please?

Dont get mad, get paid
enderleit
17
Years of Service
User Offline
Joined: 30th May 2007
Location: Denmark
Posted: 29th Jul 2011 16:28
Post away... we'll look at it...

Daniel TGC
Retired Moderator
17
Years of Service
User Offline
Joined: 19th Feb 2007
Location: TGC
Posted: 29th Jul 2011 17:10
Here's a video tutorial showing you how to do all this

http://www.youtube.com/playlist?list=PL8482535BB381E4F8
Rich S
13
Years of Service
User Offline
Joined: 27th Jul 2011
Location:
Posted: 1st Aug 2011 21:21
hey guys
got the buttons working, thanks for all the help, really appreciate it.

I've got a quick question about my 'new game' button.

How should I go about creating the new game? Should the button open up a new exe file? like a 'game.exe' and close the main menu.exe? or should I code the game into the same file that I've done the main menu? or is there a way to write a new dbp file for the game and link them into one exe at the end?

I'm a total noob when it comes to what files should do what in game design, sorry lol.

Thanks again for all your help

Dont get mad, get paid
29 games
18
Years of Service
User Offline
Joined: 23rd Nov 2005
Location: not entirely sure
Posted: 3rd Aug 2011 23:57
This is all about constructing loops within loops and, no, you do not need to start a new exe file or anything like that.

How you actually structure your code will depend on how the program works.

Generally speaking the program may have one main loop. Anything that needs to be set up that won't be changed during the game will go before he main loop. Anything that the player needs to interact with will go inside the main loop. The actual game might have its own loop inside the main loop. You might also have an "outro" screen that will be put after the main loop.

The basic structure for a simple program might look like this:



Things like arrays, types and global variables will appear before the main loop.

For simple games, you might load all the media before the main program loop. For more complex games media might be loaded and deleted from memory as required (i.e. for games with different levels, you might delete the level 1 map before loading the level 2 map for instance).

Any code where the player needs to interact with the program (menu screens, the game itself) will go inside the main loop.


Have a look at some of the games in the 20 line challenge board:

http://forum.thegamecreators.com/?m=forum_view&t=178125&b=11

http://forum.thegamecreators.com/?m=forum_view&t=172757&b=11

Both of these games have a "play again" choice at the end of the game.

For a more complex game have a look at ermes' ESF as he put the source code in the zip file:

http://forum.thegamecreators.com/?m=forum_view&t=181249&b=5


Also, have a look a Daniel TGC tutorials (I've not looked at them but there might be something in there that might help).

As I said the actual structure you adopt will depend on what the program needs to do but hopefully this'll get you started.
Rich S
13
Years of Service
User Offline
Joined: 27th Jul 2011
Location:
Posted: 4th Aug 2011 11:33
thanks for that,

I'll spend some time looking through those links before I jump into the rest of my game then.

The example code has given me a good idea of whats needed too.

Thanks for all the help,
I really appreciate it, hopefully I'll have some screenshots or something to show you soon lol

Dont get mad, get paid

Login to post a reply

Server time is: 2024-11-22 18:24:57
Your offset time is: 2024-11-22 18:24:57