Breakout Game Help

BlitzPlus Forums/BlitzPlus Beginners Area/Breakout Game Help

Paul Murray(Posted 2006) [#1]
I've got the basics of a Breakout style game pretty much done. It's my second real attempt at a BB game, so this might seem kinda nooby.

Anyway, all the images are loaded and colliding,and I've got the blocks displaying.

But I'm stumped on how to make the blocks dissapear when they are collided with. Any ideas, or exmaples I can look at?

Code...



*EDIT*

Also, the ball has a habit of going behind the blocks at times, but I'm curious to see if removing the block when it's collided with will alter this. It only seems to go behind when it hits the sides of the blocks or a corner, not the top or bottom.


Amiga(Posted 2006) [#2]
Hi Muzzer, you need to "delete b" each block.type
One another thing, include "Cls" in the beginning of your loop, just under
While Not KeyHit(1)
Cls.
.......... do your maskimage code there you load your images, it's unnecessary to do it every time, do it like this
imgPlayer = LoadImage("media/images/player.PNG")
maskimage imgPlayer xx,xx,xx ; what ever color you want.

 
Function DrawBlocks()

	For b.block = Each block
		DrawImage imgBlocks,b\bx,b\by,b\image
		MaskImage imgBlocks,20,20,60	;random number to allow black outlines to show
		
		;------------------
		;Ball to Block Collision
		;------------------		

		If ImagesCollide(imgBall, Ball_x, Ball_y,0, imgBlocks, b\bx,b\by,0) Then 
			SpeedY = SpeedY - (2*SpeedY)	;ball to block collision	
			delete b ; <--- add this, this delete each block thats got an hit.
		end if
	Next
	
End Function



Paul Murray(Posted 2006) [#3]
Just what I needed, thank you. I knew it would be something simple.

Shouldn't code so late at night.

Need to work on the collisions now. Just tried it and the ball goes behind the blocks and clears most of them in about 3 seconds. :p

Thanks again


WolRon(Posted 2006) [#4]
You can simplify reversing your balls direction by simply writing your code like this:
SpeedY = -SpeedY

Also, theres no need to mask your images every loop of your program, just do that once at the beginning of your program.

Also, the ball has a habit of going behind the blocks at times, but I'm curious to see if removing the block when it's collided with will alter this. It only seems to go behind when it hits the sides of the blocks or a corner, not the top or bottom.

Most likely this is because you are only changing the Y value when you collide with a block. To correct this, you will have to change the X value (not the Y) if you collide with the side of a block or both if you collide with the centerline of the block.

Here's some code that should work:
Function DrawBlocks()

	For b.block = Each block
		DrawImage imgBlocks,b\bx,b\by,b\image
		MaskImage imgBlocks,20,20,60	;random number to allow black outlines to show
		
		;------------------
		;Ball to Block Collision
		;------------------		

		If ImagesCollide(imgBall, Ball_x, Ball_y,0, imgBlocks, b\bx,b\by,0)
			;determine which side was hit
			;reduce x and y values to balls center position on the block
			tx = Ball_x - b\bx + widthOfBallInPixels/2
			ty = Ball_y - b\by + heightOfBallInPixels/2
			If tx < ty ;lowerleft
				If tx > heightOfBlockInPixels-ty ;lowerright
					;ball hit bottom
					SpeedY = -SpeedY
				Else
					;ball hit left
					SpeedX = -SpeedX
				EndIf
			ElseIf tx > ty ;upperright
				If tx > heightOfBlockInPixels-ty ;lowerright
					;ball hit right
					SpeedX = -SpeedX
				Else
					;ball hit top
					SpeedY = -SpeedY
				EndIf
			Else
				;ball hit centerline of block, reverse both directions
				SpeedX = -SpeedX
				SpeedY = -SpeedY
			EndIf
			Delete b ; <--- add this, this delete each block thats got an hit.
		End If
	Next
End Function
Note that heightOfBlockInPixels, heightOfBallInPixels, and widthOfBallInPixels need to equal those values of your images (in pixels) :)


Paul Murray(Posted 2006) [#5]
Thanks alot, much appreciated.

Works perfectly as far as I can see.

Here's a quick screeny...



Still some fancy stuff I want to add, but (fingers crossed) I'll be able to work that out on my own.

Just noticed, the health bulbs in the bottom left look a little off too...


Buggy(Posted 2006) [#6]
Just a note: If you add fancy stuff after you check to see if you can delete the block, you have to put

Return


after you delete them. Otherwise it will check on blocks that don't exist anymore.