9. HUD / MousePick
It's now time to add a few juicy features that add polish and style to your game! A 2D overlay "HUD" screen that includes credits, start and quit options, mouse selection of characters that can now "talk" to you, just to name a few.

If you haven't already downloaded the tutorial files do so now, unzip to your desktop and OPEN up lesson9.bb, functions9.bb, and loadworld9.bb (below) and RUN / LAUNCH the lesson9.bb program by pressing the ROCKET ICON

and you should see this...


Here's the CODE (NEW CODE is BOLD) with short explanations.
lesson9.bb

...
Include "functions9.bb"

Global escape_key = 1, QUIT = False
;add a "QUIT" flag since Escape will now bring up MENU!
Global screen_width = 640, screen_height = 480

Graphics3D screen_width,screen_height, 16, 2
...

AmbientLight 200, 200, 200
light1 = CreateLight(2)

Global cam1 = CreateCamera()
;Globalize Camera for use in PICKing function
CameraViewport cam1, 0, 0, screen_width, screen_height
PositionEntity cam1, 0, 5, 0

Include "loadworld9.bb"

...

MoveMouse screen_width/2, screen_height/2

fntArial=LoadFont("Arial", 36, True )
SetFont fntArial

; Change Default Font to something Bigger

MENU()
; Run Menu at Start of Game!

While QUIT = False ; Use QUIT flag instead

   If KeyHit( escape_key ) Then MENU()
   ; Escape key now brings up menu!
   MousePick()
   ; New function allows us to SELECT sprites with our mouse!

   ...

      UpdateWorld
      RenderWorld

   Color 255,255,0
   Text 50,410, AMMO
   Text screen_width/2, 10, speech$, True

   ; Now we have 2D Text!

      Flip
      Delay 10
Wend

End


functions9.bb

; ADD NEW MENU, AMMO & MOUSE PICK variables HERE
Global spr_menu, spr_start, spr_quit, spr_credits
Global currentpicked, lastpicked, LeaveMenu=False
Global ShowCredits=False, speech$, AMMO=10

Global up_key = 200, down_key=208, left_key=203, right_key=205
...

Function MENU() ; MENU
   ShowEntity spr_menu
   ShowEntity spr_start
   ShowEntity spr_quit
   ShowEntity spr_credits
   LeaveMenu=False

   While Not KeyHit( escape_key )
      MousePick()
      If QUIT=True Then Return
      If LeaveMenu = True Then Exit

      UpdateWorld
      RenderWorld

      If ShowCredits = True Then
         Color 0,0,0
         Text 10,10, "Made by"
         Text 10,40, "J"
      EndIf

      Flip
      Delay 10
   Wend

   HideEntity spr_menu
   HideEntity spr_start
   HideEntity spr_quit
   HideEntity spr_credits
   LeaveMenu=False
   ShowCredits=False
End Function

Function MousePick() ; MOUSEPICK
   currentpicked = CameraPick( cam1, MouseX(), MouseY() )
   If currentpicked>0 And MouseHit(1)
      If currentpicked = spr_start Then LeaveMenu = True
      If currentpicked = spr_quit Then QUIT=True
      If currentpicked = spr_credits Then ShowCredits = True
      For Character.Sprite = Each Sprite
         If currentpicked = Character\ID
            speech$ = Character\speech$
            Exit
         EndIf
      Next
   EndIf
   If MouseHit(2) Then speech$=""

    ;Right Mouse Click Closes Speech
   If currentpicked<>lastpicked
   If lastpicked Then EntityAlpha lastpicked, 1

    ;UNDO fade when mouse leaves
   lastpicked=currentpicked
   EndIf
   If currentpicked
      EntityAlpha currentpicked, Sin( MilliSecs() )*.3+.7
   EndIf
End Function

...

Function object_key_control( obj )
   If KeyHit(57) And AMMO>0 Then CreateProjectile( player ) :
AMMO=AMMO-1
   ; Keep Track of AMMO

   ...
End Function


loadworld9.bb

Type Sprite
Field ID, char_tex
Field pt1, pt2
Field targetpt
Field temper

Field speech$ ; Let's add some conversation!
End Type

; Load MENU Sprites
spr_menu = LoadSprite( "menu.bmp", 7, cam1 ) ; Stick to camera
ScaleSprite spr_menu, 1.5, 1.5 ; Enlarge a bit
MoveEntity spr_menu, 0, 0, 2 ; Move in front of camera
HideEntity spr_menu ; Hide until needed

spr_start = LoadSprite( "btn_start.bmp", 7, cam1 ) ; Stick to camera
ScaleSprite spr_start, .25, .25 ; Shrink a bit
EntityRadius spr_start, .25 ; Adjust PICK radius
MoveEntity spr_start, -.5, 0, 2 ; Move in front of camera
HideEntity spr_start ; Hide until needed
EntityPickMode spr_start,1 ; Make "PICKable"

spr_quit = LoadSprite( "btn_quit.bmp", 7, cam1 )
ScaleSprite spr_quit, .25, .25
EntityRadius spr_quit, .25
MoveEntity spr_quit, 0, 0, 2
HideEntity spr_quit
EntityPickMode spr_quit,1

spr_credits = LoadSprite( "btn_credits.bmp", 7, cam1 )
ScaleSprite spr_credits, .25, .25
EntityRadius spr_credits, .25
MoveEntity spr_credits, .5, 0, 2
HideEntity spr_credits
EntityPickMode spr_credits,1

; Load HUD Sprite
spr_ammo = LoadSprite( "heart.bmp", 7, cam1 )
ScaleSprite spr_ammo, .25, .25
MoveEntity spr_ammo, -1.7, -1.3, 2.1
EntityAlpha spr_ammo, .5

...
For C=1 To 3
Character.Sprite = New Sprite
Character\ID = CreateSprite()

Read temper, img$, X, Y, Z, X2, Y2, Z2, speech$ ;adding speech variable
Character\speech$ = speech$
speech$="" ; Clear the speech variable

...
.Characters
Data 0, "char2.bmp", 30, 0, 30, 30, 0, 0, "zzzzzzzzzzz..." ;adding speech lines
Data 1, "char3.bmp",-30, 0, 30, 20, 0, 30, "Who goes there?!"
Data 2, "char4.bmp", 0, 0, 30, -30, 0, 0, "Meow..."



THE CHALLENGE!!!
Change the speeches, font, menu buttons, credits, and ammo count!

NEXT : Lesson 10 - Terrain