How do you delete images ?

BlitzMax Forums/BlitzMax Programming/How do you delete images ?

Twinprogrammer(Posted 2011) [#1]
Hey Guys,

I'm trying to make a simple breakout game (which is pretty good) , but I can't figure out how to delete the one brick I placed when the ball collides. How can I code this ?


Twinprogrammer


Jesse(Posted 2011) [#2]
image = null


WERDNA(Posted 2011) [#3]
Depends on how you're keeping track of the image twinprogrammer.

Sorry to go against the normally awesome Jesse on this one(Who has certainly helped me out
more than once ;)
but I think image = null is a bad idea. It will probably crash your program if you're
still using DrawImage brick, x,y to draw the brick.

What you could do(And maybe this is what Jesse meant, it's late and I'm sleepy right now), is this.
Put an End if statement around the section where you draw the block. I.e,

If BlockImage <> Null
Drawimage Block, Blockx, Blocky
End If

Cheers,


Jesse(Posted 2011) [#4]
no, you are not going against me. You are going on a different direction. the bottom line is that the only way to delete the image is to allow the garbage collector to restore it to free ram. in other words if there is no variable pointing to it, the memory used to store the image will be collected by the garbage collector and restored to ram.
That was In lain-mans terms, sort of. ;)

what I think he needs, is a way to to remove the block and not access it any more.
I don't know how he is going about it but the source code might be better in helping us figure out how his logic works and will stop us from guessing like...

Last edited 2011


therevills(Posted 2011) [#5]
Jesse is right - we need to see an example of the source code.

twinprogrammer could have coded their blocks using different ways from an array to a list.

If using an array eg blockarray[10,10] and the ball has hit 10,10 , I would just assign blockarray[10,10] to zero and have my drawing/collision code to check more than zero.

If its a list, I would just remove the block out of the list...

Twinporgrammer's post heading asked a question and the content asked another... :P

Last edited 2011


H&K(Posted 2011) [#6]
Draw over it with something else


WERDNA(Posted 2011) [#7]
Agreed. We demand source code :)


Czar Flavius(Posted 2011) [#8]
I don't think he means actually deleting an image, but removing blocks from the game when they are hit?


Kryzon(Posted 2011) [#9]
If using an array eg blockarray[10,10] and the ball has hit 10,10 , I would just assign blockarray[10,10] to zero and have my drawing/collision code to check more than zero.

If its a list, I would just remove the block out of the list.

Behold the answer.

@twin: You just need to find out which brick the ball has collided with; that can be done with ImagesCollide(). Once you have the brick, delete it from the list or blank its value in the array and there you have it.

Last edited 2011


H&K(Posted 2011) [#10]
@Kryzon

You are assuming that twin is using a bank or list, he MIGHT simply be drawing them on the screen then using plot/point to see collisions.

(That is he may be reading the position data via read, and not storing the info any other way)

Without code, or at least pseudo code, we can only guess.

(ATM, my answer if draw over it with something else is the best lol)


Twinprogrammer(Posted 2011) [#11]
H&K is right , I am just using ONE image to test a collision


Twinprogrammer(Posted 2011) [#12]
I'm not entirely sure how to send a source code , though.


Czar Flavius(Posted 2011) [#13]
[ code ] Paste it here [ / code ] but without the spaces.


Twinprogrammer(Posted 2011) [#14]
alright , here is the code:





paddleimage = LoadImage ("C:\Users\TronKyle\Pictures\images\paddle.png")
paddlerectx = ImageWidth (paddleimage)
paddlerecty = ImageHeight (paddleimage)


ballimage = LoadImage ("C:\Users\TronKyle\Pictures\images\ball.png")
ballrectx = ImageWidth (ballimage)
ballrecty = ImageHeight (ballimage)


brickimage = LoadImage ("C:\Users\TronKyle\Pictures\images\brick.png")
brickx = 0
bricky = 30


paddlex = 0
paddley = 550
epaddley = 350

ballx = 380
ballx2 = 0
bally2 = 0
bally = 220
ballspeed:Float = 1.5


'INTRO-----------------------------------------------------------------

Graphics 400,50

DrawText "--Coat of Arms Games--",120,0
DrawText "--Presents--",150,20
Flip
Delay 3000
'MAIN MENU-------------------------------------------------------------
#menu

speedy = 5
speedx = 5


Graphics 640,400

While Not KeyDown (key_p)
Cls

DrawText "Blitz Breakout",300,0
DrawText "(P) to play",30,50
DrawText "(h) for help",450,50
DrawText "(O) for options",30,100
DrawText "(C) for credits",450,100
DrawText "(escape) to quit",250,370
DrawImage paddleimage,paddlex,epaddley
DrawImage ballimage,ballx2,bally2
ballx2:+ speedx
bally2:+ speedy
paddlex:+ 3
If paddlex > 640 paddlex = 0

If ballx2 > 640 speedx = -5
If ballx2 < 0 speedx = 5
If bally2 > 400 speedy = -5
If bally2 < 0 speedy = 5


If KeyDown (key_p) Goto game
If KeyDown (key_escape) Goto exitgame
If KeyDown (key_c) Goto credits
If KeyDown (key_h) Goto help

Flip

Wend


'MAIN GAME-----------------------------------------------------------



#game
lives = 3
score = 0


Graphics 800,600

While Not KeyDown (key_escape)
Cls

DrawImage paddleimage,MouseX(),paddley
DrawImage ballimage,ballx,bally
DrawImage brickimage,brickx,bricky


ballx:+ 3
bally:+ 3
HideMouse()
DrawText "^",MouseX(),MouseY()
DrawText " lives: " + lives + " score: " + score,0,0

ballx:+ speedx
bally:+ speedy



If ballx > 800 speedx = -10
If ballx < 0 speedx = 5
If bally < 0 speedy = 5
If bally > 600
Delay 400
ballx = 380
bally = 220
lives:- 1
score:- 50
EndIf

If (ImagesCollide(paddleimage,MouseX(),paddley,0,ballimage,ballx,bally,0))
speedy = -10
EndIf

If (ImagesCollide(brickimage,brickx,bricky,0,ballimage,ballx,bally,0))

score:+ 100
speedy = 5
EndIf

If lives <= 0
Delay 1000
Goto menu
EndIf

If KeyDown (key_p)
While Not KeyDown (key_p)
Wend
EndIf

Flip
Wend
Goto menu

#help
'HELP SCREEN-----------------------------------------------------------

Graphics 400,200

While Not KeyDown (KEY_b)
Cls

DrawText "---HELP SCREEN---",150,0
DrawText "use the mouse to move the paddle (escape) to quit",0,20
DrawText "reflect the ball to hit the bricks for points",0,40
DrawText "misses will result as a subtraction to your lives.",0,60
DrawText "press (b) to go back",170,170

Flip
Wend
Goto menu

'EXIT GAME------------------------------------------------------------
#EXITGAME
Graphics 400,200


While Not KeyDown (key_y)

Cls
DrawText "Are you sure you want to exit?",140,0
DrawText "(y) yes :( (n) no :)",160,100
If KeyDown (key_n) Goto menu

Flip
Wend

End

#credits

cretext = 400
cretext2 = 500
cretext3 = 600
cretext4 = 700
Graphics 640,400

While Not KeyDown (key_b)

DrawText "--CREDITS--",310,50
DrawText "art by Kyle Costigan",300,cretext
DrawText "main programming by kyle Costigan",200,cretext2
DrawText "additional programming by Connor Costigan",200,cretext3
DrawText "...Made by Coat of Arms Games...",200,375
DrawText "(B) to exit credits...",280,cretext4
cretext:- 1
cretext2:- 1
cretext3:- 1
cretext4:- 1
Flip;Cls
Wend

Goto menu


Twinprogrammer(Posted 2011) [#15]
sorry about the code try copying it and running it


therevills(Posted 2011) [#16]
First: wrap the code in [ code ] [/ code] like Czar said
Second: I guess your pretty new to coding... sorry but that code is pretty bad
Third: Repeat after me: Never use GOTO! :P
Forth: Use at least Strict.. if not SuperStrict!

Looking at the code... you have only got one brick?

BTW we cant run your code straight away because we havent got your images...

Last edited 2011


therevills(Posted 2011) [#17]
I quickly coded this for you, this uses objects and a list for the bricks. Hope it helps. (No images needed - it creates them)

SuperStrict

Const APP_NAME$ = "BAT AND BALL GAME!"
SeedRnd MilliSecs()

Const SCREEN_WIDTH% = 800
Const SCREEN_HEIGHT% = 600

HideMouse()

AppTitle = APP_NAME

Global paddleImage:TImage, ballImage:TImage, brickImage:TImage

Global game:TGame = New TGame

game.Setup()
game.Run()

Type TGame
	Field paddle:TPaddle ' the player
	Field ball:TBall

	Method Setup()
		Graphics SCREEN_WIDTH, SCREEN_HEIGHT
		SetBlend ALPHABLEND
		CreateImages()
		
		' create paddle and ball
		paddle = TPaddle.Create(paddleImage, SCREEN_WIDTH / 2, SCREEN_HEIGHT - paddleImage.height / 2)
		ball = TBall.Create(ballImage, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2)
		
		paddle.reset()
		
		' create bricks
		For Local i% = 0 To 12
			For Local j% = 0 To 5
				TBrick.Create(brickImage, i * (brickImage.width + 5) + 45 , j * (brickImage.height + 5) + 100)
			Next
		Next
		
		MoveMouse(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2)
	End Method

	Method Run()
		Repeat
			Logic()
			Draw()
		Until AppTerminate() Or KeyHit(KEY_ESCAPE)
	End Method
	
	Method Logic()
		paddle.Logic()
		ball.Logic()
		
		Collisions()
	End Method
	
	Method Collisions()
		If (ImagesCollide(paddle.image, MouseX(), paddle.y, 0, ball.image, ball.x, ball.y,0))
			ball.speedy = -ball.speedy
		EndIf
		
		If TBrick.list
			For Local b:TBrick = EachIn TBrick.list
				If (ImagesCollide(b.image, b.x, b.y, 0, ball.image, ball.x, ball.y, 0))
					paddle.score:+100
					ball.speedy = -ball.speedy
					b.Kill()
				EndIf
			Next
		EndIf
	End Method
	
	Method Draw()
		Cls
			paddle.Draw()
			ball.Draw()
			TBrick.DrawAll()
			DrawHUD()
		Flip
	End Method
	
	Method DrawHUD()
		DrawText "SCORE: " + paddle.score, 10, 10
		DrawText "LIVES: " + paddle.lives, 10, 25		
	End Method

	Method CreateImages()
		Cls
		' create the paddle
		paddleImage = CreateImage(100,20)	
		SetColor 255, 0, 0
		DrawRect 0, 0, 100, 20
		GrabImage paddleImage, 0, 0
		SetImageHandle paddleImage, 50, 20
		
		Cls
		' create the ball
		ballImage = CreateImage(16,16)	
		SetColor 0, 255, 0
		DrawOval 0, 0, 16, 16
		GrabImage ballImage, 0, 0
		SetImageHandle ballImage, 8, 8
			
		Cls	
		' create the brick
		brickImage = CreateImage(50,20)	
		SetColor 255, 255, 255
		DrawRect 0, 0, 50, 20
		GrabImage brickImage, 0, 0
				
	End Method 	
End Type

Type TSprite
	Field x#, y#, speedX#, speedY#
	Field image:TImage
	Field red% = 255, green% = 255, blue% = 255
	
	Method Draw()
		SetColor red, green, blue
		DrawImage image, x, y
		SetColor 255, 255, 255	
	End Method
	
	Method Logic()
	End Method
End Type

Type TPaddle Extends TSprite
	Field lives%, score%
	
	Function Create:TPaddle(image:TImage, x#, y#)
		Local p:TPaddle = New TPaddle
		p.x = x
		p.y = y
		p.image = image
		Return p
	End Function
	
	Method Reset()
		lives = 3
		score = 0
	End Method
	
	Method Draw()
		Super.Draw()
		DrawText "^",MouseX(),MouseY()
	End Method
	
	Method Logic()
		x = MouseX()
	End Method
End Type

Type TBall Extends TSprite
	Function Create:TBall(image:TImage, x#, y#)
		Local b:TBall = New TBall
		b.x = x
		b.y = y
		b.speedX = 5
		b.speedY = 5
		b.image = image
		Return b
	End Function

	Method Reset()
		x = SCREEN_WIDTH / 2
		y = SCREEN_HEIGHT / 2
	End Method
	
	Method Logic()
		X:+speedX
		y:+speedY
		
		If x > SCREEN_WIDTH Or x < 0
			speedX = -speedX
		End If
		If y > SCREEN_HEIGHT Or y <0
			speedY = -speedY
		End If		
	End Method
End Type

Type TBrick Extends TSprite
	Global list:TList

	Function Create:TBrick (image:TImage, x#, y#)
		Local b:TBrick = New TBrick 
		If list = Null list = CreateList()
		b.x = x
		b.y = y
		b.red = Rand(50,255)
		b.green = Rand(50,255)
		b.blue = Rand(50,255)
		b.image = image
		
		list.AddLast b
		
		Return b
	End Function

	Function DrawAll()
		If Not list Return
		For Local b:TBrick = EachIn TBrick.list
			b.Draw()
		Next
	End Function
	
	Method Kill()
		If Not list Return
		list.Remove(Self)
	End Method

	Function KillAll()
		If Not list Return
		For Local b:TBrick = EachIn list
			b.Kill()
		Next
	End Function
End Type



Czar Flavius(Posted 2011) [#18]
That's pretty cool!

Why not do the list like this:

Type TBrick Extends TSprite
	Global list:TList = New TList
	Field _link:TLink

	Method New()
		_link = list.AddLast Self
	End Method

	Function Create:TBrick (image:TImage, x#, y#)
		Local b:TBrick = New TBrick 
		b.x = x
		b.y = y
		b.red = Rand(50,255)
		b.green = Rand(50,255)
		b.blue = Rand(50,255)
		b.image = image

		Return b
	End Function

	Function DrawAll()
		For Local b:TBrick = EachIn TBrick.list
			b.Draw()
		Next
	End Function
	
	Method Kill()
		_link.Remove()
		_link = Null
	End Method

	Function KillAll()
		For Local b:TBrick = EachIn list
			b.Kill()
		Next
	End Function
End Type


Last edited 2011

Last edited 2011