How to DrawText then erase it?

BlitzMax Forums/BlitzMax Beginners Area/How to DrawText then erase it?

Bill246(Posted 2009) [#1]
In the game I'm making, I want to draw a simple "You Win" message on the screen when the player gets 10 points. The text will stay on the screen until the player hits the space bar. Right now it locks up when the score hits 10 points, and the message doesn't appear until on screen until after hitting the space bar. Here it my code:

If PlayerScore=10 Then
Repeat
DrawText "YOU WIN,200,200
Until KeyDown(KEY_SPACE)
PlayerScore=0
End If

I tried moving the DrawText to before the Repeat loop, but it still doesn't put the text on the screen until the space bar is hit, and then it's only visible for a second.

Does anyone have an ideas what I'm doing wrong?

Also, I'm not sure how to erase the text after the space bar is hit. I looked through documentation and didn't see an Erase command or anything.


Thank you!


GfK(Posted 2009) [#2]
The text is being drawn, but onto the back buffer. You need a Cls before it, and a Flip after it.


Czar Flavius(Posted 2009) [#3]
You can put your code in [ code ] tags :) (but remove the spaces)
If PlayerScore=10 Then
	Repeat
	*	DrawText "YOU WIN,200,200
	Until KeyDown(KEY_SPACE)
	PlayerScore=0
End If


I'm guessing that you have a main game loop, which updates the game, and then draws it before repeating. When the program gets to the part of the code marked with *, it gets stuck on that line until the spacebar is pressed. This means it isn't able to continue running the main game loop, which is why the program locks up.

As GfK says, to get it to appear before the spacebar you need a Flip after it (otherwise it can't get drawn until spacebar is pressed, and the program can get back to the main game loop where it will eventually get to the Flip)

The way the drawing routine works is you splash everything you need onto the screen/buffer, and then flip it and use Cls to clear the buffer and draw the next scene. Once drawn, you can't remove something from the buffer. If you don't mind the game pausing when this message appears, the above suggestion should work. If you want the game to continue playing and the message only disappear when spacebar is pressed, try this

'somewhere at the start of your code
Global HasWon = False

'in your game loop
If PlayerScore >= 10 Then
	HasWon = True
	PlayerScore = 0
End If

If HasWon
	DrawText "YOU WIN", 200, 200
	If KeyDown(KEY_SPACE)
		HasWon = False
	End If
End If


You could also put this on a timer, so after x seconds the message disappears on its own. I hope this helps.


Bill246(Posted 2009) [#4]
Thank you for the help!