Ending

Blitz3D Forums/Blitz3D Beginners Area/Ending

Happy Llama(Posted 2012) [#1]
I have a game that when your health goes to 0, a ending menu comes up. But it just flickers. Why?

If playerhealth < 0 Then  
Cls 
Text GraphicsWidth()/2,GraphicsHeight()/2, "You Loose. Press Enter to Restart or Space to Exit"
Flip
If KeyDown(57) Then Exit
If KeyDown(28) Then Goto start
EndIf


Last edited 2012


Guy Fawkes(Posted 2012) [#2]
Shouldn't this belong in the General Discussion topic?

Last edited 2012


Happy Llama(Posted 2012) [#3]
Yah sorry about that...


Guy Fawkes(Posted 2012) [#4]
Np :)


Gabriel(Posted 2012) [#5]
Without looking at your code in full, it's hard to say for sure, but I suspect you're using multiple calls to cls and/or flip for various game states. That's not a good way to go. Have one call to cls and one to flip for the entire game. If you only want certain things draw in certain circumstances, use variables to control that. Don't try to have multiple game loops like that or you'll tie yourself up in knots. (Voice of experience)

If you're not doing that, my apologies, but that's how it appears.


Guy Fawkes(Posted 2012) [#6]
Put the Loop code in a function like so:





Then call it as needed! :)


Rob the Great(Posted 2012) [#7]
Like Gabriel says, you're calling probably Cls and Flip twice. For example:
While Not KeyDown(1)
   Cls ;Black Screen on the backbuffer now...
   If playerhealth <= 0
      GameOver()
   EndIf
   Flip ;but when you flip the buffers again, you will show a screen with no drawing done, or a "blackscreen".
Wend

Function GameOver()
   Cls ;Blackscreen on the backbuffer now
   Text GraphicsWidth()/2,GraphicsHeight()/2, "You Loose. Press Enter to Restart or Space to Exit"
   Flip ;When you flip buffers, it will quickly show the text above
End Function

The easiest remedy? Either make sub-loop in your GameOver() function, or get rid of the double Flip and Cls commands in the GameOver() function. Using the first method:
While Not KeyDown(1)
   Cls
   If playerhealth <= 0
      GameOver()
   EndIf
   Flip
Wend

Function GameOver()
   Repeat ;Add a subloop
      Cls
      Text GraphicsWidth()/2,GraphicsHeight()/2, "You Loose. Press Enter to Restart or Space to Exit"
      Flip
   Until KeyHit(57)
End Function

Using the second method:
While Not KeyDown(1)
   Cls
   If playerhealth <= 0
      GameOver()
   EndIf
   Flip
Wend

Function GameOver()
   Cls ;This is fine, it just clears the screen
   Text GraphicsWidth()/2,GraphicsHeight()/2, "You Loose. Press Enter to Restart or Space to Exit"
   ;REMOVE FLIP HERE!
End Function


Last edited 2012


Gabriel(Posted 2012) [#8]
Thundros, that's just as bad if not worse. I appreciate you're trying to help, but you're not ready to help people. You could probably get a lot further with your own problems if you stopped getting distracted posting about conspiracy theories and posting worse changes to other people's code.


Happy Llama(Posted 2012) [#9]
But I want it so when you press space, the program ends. But I can't put the end command in a function. Is there an different command?


Rob the Great(Posted 2012) [#10]
Sure you can. Reading over the code a little more, you have two options (based off of methods 1 and 2 from above).

Method 1:

While Not KeyDown(1)
   Cls
   If playerhealth <= 0
      GameOver()
   EndIf
   Flip
Wend

Function GameOver()
   FlushKeys()
   Repeat
      If KeyHit(57) Then End ;If they hit the space bar, then end
      Cls
      Text GraphicsWidth()/2,GraphicsHeight()/2, "You Loose. Press Enter to Restart or Space to Exit"
      Flip
   Until KeyHit(28) ;If they hit the enter, restart
   Goto start
End Function

Method 2:
While Not KeyDown(1)
   Cls
   If playerhealth <= 0
      GameOver()
      If KeyHit(57) Then End
      If KeyHit(28) Then Goto start
   EndIf
   Flip
Wend

Function GameOver()
   Cls
   Text GraphicsWidth()/2,GraphicsHeight()/2, "You Loose. Press Enter to Restart or Space to Exit"
End Function



Guy Fawkes(Posted 2012) [#11]
Yea.




How's THAT for BAD, Gabriel? ;)

Last edited 2012


Rob the Great(Posted 2012) [#12]
Hmm...
While QUIT=1 ;QUIT is not yet initialized, therefore QUIT = 0 and skips this part
   If KeyHit(1) then QUIT=1 ;Doesn't matter, because it's all been skipped
   Flip ;Doesn't matter, because it's all been skipped
Wend ;Since QUIT is not yet initialized, the program skips to here
End ;And ends.

I'm not sure that does quite what you want it to do.


H&K(Posted 2012) [#13]
@Rez Thats as bad as all your code

Last edited 2012


Adam Novagen(Posted 2012) [#14]
Really simple question.

You're using Flip(), but are you sure you used SetBuffer BackBuffer() at the start of the program?


Happy Llama(Posted 2012) [#15]
When I use the command Goto, I get the error message "Undefined Label".


_PJ_(Posted 2012) [#16]
Have you got the .Start label somewhere in your code? (Assuming you're still referring to :
Goto Start


Personally, I would advocate the use of functions rather than Goto - similar to how RobTheGreat suggested above it's much 'cleaner' in my opinion, but it's up to you at the end of the day :)


Guy Fawkes(Posted 2012) [#17]
H&K, stop bloody criticizing me.


H&K(Posted 2012) [#18]
@Thundros. I wasnt criticizing you, I was pointing out to another Poster here (REZ) that the joke bad code you posted was as bad as his code always is.

I just thought it was funny that you managed as a joke to make nearly every line wrong, something he does without trying.

(PS Saying something is as bad or as good as other work by the same person isnt criticism, just a statement of consistency)

@Rez Thats as good as all your code


stayne(Posted 2012) [#19]
H&K: Thundros = Rez


Ginger Tea(Posted 2012) [#20]
I'm still unsure if the joke code was joke code, we've had threads go on for a few dozzen posts over simple issues before. I quoted it and rob's responce in aother thundros post as it was rather topical to that one.


Happy Llama(Posted 2012) [#21]
Yah I have the .start label. Hmmmm...

Last edited 2012


_PJ_(Posted 2012) [#22]
Ah I got it
The problem is that Functions serem to be compiled before labels are declared.

This means, that regardless of where the Function declaration is in your code, the compiler still wont have defined the label ".start"

---
You can see this by the following:
Function Eg()
	Goto start
End Function


.start
Print "success"


.start
Print "success"

Function Eg()
	Goto start
End Function


Both of these will fail.

It would seem the two best candidate soilutions would be either:

A) Replace the use of GOTO or GOSUB and their assoociated labels with separate function calls

or

B) Put the subroutines (the code under each label, including the label) in separate .bb files and 'include' them with the Include command - By using Include, the labels are then defined and compiled when the included file is imported to the compiler by the Include "" command.

PErsonally, I woul;d rather go with option A - I very much favour using Functions over subroutines. However, depending on how much code you have and what that might entail, perhaps option B may be easier for you?


Yasha(Posted 2012) [#23]
I haven't been following this thread really, but it's worth pointing out that:

1) Goto cannot jump "into" or "out of" a function: the label and the goto statement must both be in the same scope (either within one function, or within the main scope). This is because, if you think about it, the resulting control flow is not well-defined: how would return statements work? Which set of locals become visible? The code wouldn't make sense. This is enforced/helped by the fact that Goto labels are locally-named (so you can have the same label in more than one function, and they won't conflict).

2) You cannot use Gosub from within a function at all. Functions and subroutines are mutually exclusive (again, it would confuse control flow). They compete to provide similar functionality anyway, so hopefully this should never be a real problem.


Happy Llama(Posted 2012) [#24]
I've taken out the Goto (I'll figure that out later) and just made a simple end function. But when I call it, i get a flickering effect.



Why is this?

Last edited 2012


Midimaster(Posted 2012) [#25]
try to add a FLIP 1 before WaitKey()


Happy Llama(Posted 2012) [#26]
It doesn't display the text.
Hmmm...


Happy Llama(Posted 2012) [#27]
When I take out the Cls command the text is displayed.


Midimaster(Posted 2012) [#28]
try this:
Function win()
     FlushKeys
     Repeat
          Cls
          Text GraphicsWidth()/2,GraphicsHeight()/2, "You Win!"
          Flip 1
     Until GetKey()<>0
     End
End Function



_PJ_(Posted 2012) [#29]
You have previously used SetBuffer to BackBuffer() ?


Happy Llama(Posted 2012) [#30]
OK I've changed my code:


If playerhealth <= 0 Or score > 20 Then
Cls
gameover()
End If
----------------------------------------------

Function gameover()

If score >= 20 Then
FlushKeys
win=LoadImage("win.png")
MaskImage win,255,255,255  
DrawImage win,0,0
WaitKey
End
End If

If playerhealth <= 0 Then
FlushKeys
loose=LoadImage("loose.png")
MaskImage loose,255,255,255
DrawImage loose,0,0
WaitKey
End
End If

End Function 



When you win or loose, the program stops. When you hit a key, the win or loose image flashes then the program ends. Why is this?


H&K(Posted 2012) [#31]
No flip.

Put before waitkey


Happy Llama(Posted 2012) [#32]
Yay thanks!