clearing the screen

BlitzPlus Forums/BlitzPlus Beginners Area/clearing the screen

morcar(Posted 2013) [#1]
I have written a game (Space Invaders) and i have a small problem.

If I die on a level with aliens still on it and i go back to level 1 after going through a small simple menu the aliens i didnt kill are still there along with a full set of aliens on top of them.

I tried using CLS but this dont work and i still get the same problem.

Is there anything else I can do to sort this problem out ?


Yasha(Posted 2013) [#2]
It depends. If you've gone through a different screen and gone back I assume the problem is not with the buffers, but with the fact that your drawing system is regenerating the scene with all of the aliens from the previous game still contributing to the drawing process.

So - however you're representing aliens in memory, are they getting properly deleted and cleaned up after the player dies? I assume that advancing to the next level only happens when you've killed all of them anyway so no cleanup is necessary for normal level progression. Just something like a simple loop in the segment that handles player death, going over each alien and doing whatever you do when they are destroyed normally, ought to handle it if you haven't got something like that.


morcar(Posted 2013) [#3]
ok here is my listing (i knew i should have posted it)

See what you think and thank you for the help.

;*************************
;*************************
;***    PC Invaders    ***
;***                   ***
;*** By Preston Thomas ***
;*************************
;*************************

;graphics mode and set fps
Graphics 600,480,16,2

timer = CreateTimer(60)



	
;load images
AutoMidHandle True
;load ship
img_ship = LoadImage("ship.png")
img_shipgreen = LoadImage ("shipgreen.png")
img_shipblue = LoadImage ("shipblue.png")
img_shipred = LoadImage("shipred.png")
;load laser
img_bullet = LoadImage("fire.png")
;load alien
img_alien = LoadImage("alien.png")
;load alienfire
img_afire = LoadImage("afire.png")
;load gameover
img_gameover = LoadImage("gameover.png")
img_title = LoadImage("title.png")
;load sounds
snd_laser1 = LoadSound("lazer.wav")
snd_aexplode = LoadSound("alien exp.wav")
snd_laugh = LoadSound("speccy laugh.wav")
snd_tune = LoadSound ("invaders tune.wav")
.menu
;load hiscore
Cls
If ReadFile("score.dat") = 0 Then
	highscore = 100
Else
	file_score = ReadFile("score.dat")
	highscore = ReadInt (file_score)
	CloseFile (file_score)
EndIf

chnBackground=PlayMusic("invaders tune.wav") 

;draw menu
While Not KeyDown (1)
If KeyDown(57) Then StopChannel chnBackground  : Goto game


DrawImage img_title,300,240
 
Flip

Wend

.game
;graphics mode and set fps

SetBuffer BackBuffer()

;type bullet
Type bullet
	Field x
	Field y
	End Type

;type alien
Type alien
	Field x
	Field y
End Type

;type bomb
Type bomb
	Field x
	Field y
End Type

;set up where ship has to be
x=300	
y=450

;sets up lives
lives = 3

;sets up score
score = 0

;sets alien speed
aspeed = 2
amx = aspeed
chdir = False

;number of aliens
numaliens=0

;level to start on
level = 20
timer = CreateTimer(level)

;start main loop
While Not KeyDown(1)
If numaliens = 0 Then 
;add level
	level = level + 1
	timer = CreateTimer(level)

;generate aliens
	For z = 1 To 10
		For w = 1 To 5
		a.alien = New alien
		a\x = 50 + 40*z
		a\y = 50 + 40*w
		Next
Next
EndIf

;clear screen
Cls

;draw player
DrawImage img_ship,x,y

	If KeyDown(50) Then x=x+3
	If KeyDown(49) Then x=x-3
	If KeyDown(1) Then End
	If x<20 Then x=20
	If x>580 Then x=580

;fire bullet
	If KeyHit(57) Then
		PlaySound(snd_laser1)
		b.bullet = New bullet
		b\x = x
		b\y = y - 5
EndIf

;update and draw
For b.bullet = Each bullet
	b\y = b\y - 5
	If b\x < 0 Then Delete b
	DrawImage img_bullet,b\x,b\y
Next

;movement for aliens
If chdir = True Then
	amx = -amx
EndIf
chdir = False

numaliens = 0

;update and draw aliens	
For a.alien = Each alien

;count aliens
	numaliens = numaliens +1
;move alien
	a\x = a\x + amx
	If a\x > 580 Then chdir = True
	If a\x < 20 Then chdir = True
	
	;generate bombs
	If Rand(250) = 25 Then
	bombs.bomb = New bomb
	bombs\x = a\x
	bombs\y = a\y
	EndIf
	
	DrawImage img_alien,a\x,a\y
	For b.bullet = Each bullet
	If ImagesCollide(img_afire,b\x,b\y,0,img_alien,a\x,a\y,0)
	score=score+25
	PlaySound (snd_aexplode)
	Delete b
	Delete a
	If score > highscore Then highscore = score
	Exit
	EndIf
	Next
Next


;update and draw bombs
	For bombs.bomb = Each bomb
	bombs\y = bombs\y+4
	DrawImage img_afire,bombs\x,bombs\y
	
	If ImagesCollide(img_afire,bombs\x,bombs\y,0,img_ship,x,y,0)
	Delete bombs 
	lives = lives - 1
	ElseIf bombs\y > 500 Then
		Delete bombs
	EndIf 
	Next
;endgame screen
If lives=0 Then
	
	;save hiscore
	file_score = WriteFile("score.dat")
	WriteInt(file_score,highscore)
	CloseFile file_score
	Cls
	PlaySound (snd_laugh)
	DrawImage img_gameover,295,250
	Flip
	Delay 5000
	Cls
	Flip
	
	Goto menu
EndIf
;display lives
	;draw level
	Text 40,10,"Shields "+lives,1,1
	Text 300,10,"Score "+score,1,1
	Text 530,10,"High Score "+highscore,1,1
WaitTimer(timer)
Flip
Wend



Yasha(Posted 2013) [#4]
Yeah it looks like since your aliens are using shared resources you can make the solution pretty simple: try adding the line

Delete Each alien


...to the beginning of your "endgame screen" block. Repeat as necessary for any other actor types that are hanging around when the level ends and you don't want them to stay.

Otherwise, that list of un-killed aliens is still there when you leave for the menu, because nothing deleted them. When you start the new game, a bunch of new ones are created but they're stuck on the same list - because there's only the one alien list - and it still has old aliens left on it. Thus they form part of the new scene.

("Delete Each" is a shortcut for deleting everything in a particular list. For simple objects like these, where the aliens don't have ownership of any other resources, you don't need to bother with a full For/Each loop that takes care of their properties.)

One other thing that jumps out: remember to free the old timer with FreeTimer before you overwrite it with a new one for the next level, or it will "leak" and waste memory.


morcar(Posted 2013) [#5]
that worked perfect

thanks


morcar(Posted 2013) [#6]
Is there a way i can limit the amount of bullets that the ship fires ?


misth(Posted 2013) [#7]
If bulletCount < MAX_BULLETS Then CreateBullet()


Also, you can create a function that counts all the members in a type, but global variable to hold that count is faster (if needed in many places).