Exiting Parts Of A Game

Blitz3D Forums/Blitz3D Beginners Area/Exiting Parts Of A Game

Clyde(Posted 2003) [#1]
Hi,

I was wondering if you could put me on the right track with this problem?

What I would like to happen is to be able to exit back to the title screen from the main game, and when on the title screen exit the game proper, by using escape.

I am using the following code, but it exits completly no matter which area your at


;Example

setup_variables() ; sets up any variables

While not keyhit(_esc)

title_screen()
main_game()


Wend
savescores()

End


I know Im doing something drastically wrong.

Please could you show me a better method, if im going about it the wrong way?

Many Thanks, Mikey F


Tiger(Posted 2003) [#2]
Try to change _esc to 1.

Bye.


jhocking(Posted 2003) [#3]
First make the main game loop an infinite loop:
While 1

Now in the title_screen function check for the Escape key being hit. If it is, End:
Function title_screen()
if KeyHit(1) End
End Function


Clyde(Posted 2003) [#4]
what does " while 1" mean?


jhocking(Posted 2003) [#5]
It is shorthand for "while 1 is true." Since that is always true (in Blitz "1" is a synonym for "true") you get an infinite loop. That's a pretty common way to do infinite loops in just about all programming languages. As in:
While 1
title_screen()
Wend

Then just make sure the program executes End at some point inside the loop. "End" exits the program immediately.


QuietBloke(Posted 2003) [#6]
the while loop repeats until the expression you give it returns a false. Usually you would do something like while a < 10. By typing while 1 what you are doing is creating an infinate loop because 1 will always return true.


Clyde(Posted 2003) [#7]
Thanks guys.

Are there any other methods, for selecting which parts of the game to go to?


Binary_Moon(Posted 2003) [#8]
while 1 - wend

Or you could do it the 'proper' way and use

repeat - forever :)

Obviously it isn't really the proper way but it saves adding a check every loop. Won't give you any noticeable speed gains but I think it looks nicer. Certainly easier to read/ understand.


JazzieB(Posted 2003) [#9]
If you're exiting the both the game and the title screen with Esc, then you need to wait until Esc is released before exiting the game portion of code. It's quite easy to accomplish. Once you know that Esc has been pressed, wait until it's not before continuing to the next section of code.

Function game()
  Repeat
    ...game...
  Until KeyDown(1)

  ; now wait for Esc to be released
  Repeat
  Until Not KeyDown(1)
End Function


Hope that points you in the right direction.


QuietBloke(Posted 2003) [#10]
You could use a simple state engine to figure out what part of the game you are in. Create a variable which will store what stage the game is in. In the main loop you can then use this variable to decide what to do.

eg.

Global gameState = 0

while gameState != 2
select gameState
case 0:
; We are in the title screen
TitleScreen()
case 1:
; we are in the game
MainGame()
wend

The TitleScreen() function can then change the GameState to 2 if the Esc key is pressed.
The mainGame function can change the state back to 0 when the game ends.

You can further improve readablitiy by creating constants for the game states.

Const GAME_TITLE = 0
Const GAME_PLAY = 1
Const GAME_END = 2

Now the code will be

Global gameState = GAME_TITLE

while gameState != GAME_END
select gameState
case GAME_TITLE:
; We are in the title screen
TitleScreen()
case GAME_PLAY:
; we are in the game
MainGame()
wend

This is basically how I tend to write my code. If later on you decide you want another screen to perhaps allow the player to change the controls you just insert a new state.

Const GAME_TITLE = 0
Const GAME_SETCONTROLS = 1
Const GAME_PLAY = 2
Const GAME_END = 3

Global gameState = GAME_TITLE

while gameState != GAME_END
select gameState
case GAME_TITLE:
; We are in the title screen
TitleScreen()
case GAME_SETCONTROLS:
; we are in the game
SetControls()
case GAME_PLAY:
; we are in the game
MainGame()
wend

Now your TitleScreen can check if the users hits the C key or clicks on a button on the screen then it can set the gameState to GAME_SETCONTROLS.

The SetControls function can set the state back to GAME_TITLE when it is finished.

Hope that makes sense.


cyberseth(Posted 2003) [#11]
Don't listen to me.
It's already been said what I was going to say.


Clyde(Posted 2003) [#12]
So would I then have the functions like this?


Function Title_Screen()

While Not Keyhit(1) ;If I havent pressed escape key

 Cls
 Text 320,240,"T I T L E"
  
 If keyhit(57) then GAME_STATE=MAIN_GAME ;space bar goes to next GAME_STATE 

Flip

Wend
GAME_STATE=GAME_END
End Function

Function Main_Game()

While Not Keyhit(1)

Cls
Text 320,240,"M A I N    G A M E"

Flip
Wend
GAME_STATE=TITLE_SCREEN ; or the next section e.g game over/hiscore etc?

End Function



Thanks ever so much guys!!


DarkEagle(Posted 2003) [#13]
global ExitGame = 0

while not ExitGame

whattodo = MenuScreen()
if whattodo = 1
 DoGame()
end if
ShowHighScores()

wend

function MainMenu()

while not exitfunc
 if keyhit(1)
  exitfunc = 1
  exitgame = 1
  return 0
 end if
 if keyhit(57)
  exitfunc = 1
  return 1
 end if
end function



Foppy(Posted 2003) [#14]
I just want to add here that instead of saying
While 1
you can also say
While True
since Blitz knows the keywords True and False. Using 1 is not wrong, but using True makes the code a bit easier to read. The same is true for the example in the message just above this one; exitgame = 1 could be written as exitgame = True, return 0 as return False etc.


Clyde(Posted 2003) [#15]
Thanks ever so much guys for pointing me in the right way, much appreciated.


Drago(Posted 2003) [#16]
btw, you might want to flush the keys after you exit the game to the amin menu cause somethime blitz checks the input buffer and it still has the esc key it it, so basically iif the person presses esc in game, then it might also quit from the main menu.

this is done easily with the
FlushKeys
command.


JazzieB(Posted 2003) [#17]
I tend not to rely on the Flush commands, because for some reason they don't appear to completely clear the buffer. Don't know why, but I've found myself using the Repeat...Until Not KeyDown() route quite a few times - even after using FlushKeys/Mouse.


Foppy(Posted 2003) [#18]
Or, like in the examples, use KeyHit() instead of KeyDown(). If the key is hit once but not released, KeyHit() will register the key being hit only once, while using KeyDown would result in "hits" being registered continuously.