Do you like my tutorials?

Then consider supporting me on Ko-fi

Talking about Breakout game, Box2D and Game development.

This prototype comes directly from Dmitry Sovetov, the brain behind Dreemchest. It’s a breakout prototype ready to be published as a standalone Windows/Mac game or as an Android/iOS game.

All starts from the visual editor

Then it’s just a matter of writing some lines of code, for the main class:

class "App"( StageObjectContainer )

function App:main()
	local w = Stage.getWidth()
	local h = Stage.getHeight()
	
	Physics.setGravity( 0, 0 )
	
	-- Horizontal walls
	self:createWall( w * 0.5, 0, w, 5 )
	self:createWall( w * 0.5, h, w, 5 )
	
	-- Vertical walls
	self:createWall( 0, h * 0.5, 5, h )
	self:createWall( w, h * 0.5, 5, h )
end

function App:createWall( x, y, w, h )
	local wall 	 = Graphics.new()
	wall.x		 = x
	wall.y 		 = y
	wall.visible = false
	wall:setAsRectangle( w, h )
	wall:physicalize( { static = true } )
	
	self:attach( wall )
end

for the ball:

class "Ball"( Graphics )

function Ball:Ball()
	self.texture = 'bmpBall1'
	
	self:physicalize( { radius = self.width * 0.5 } )
	self.physicalBody:setLinearVelocity( math.random() * 10 - 10, math.random() * 10 - 10 )
	
	self:attachListener( Event.Update, self )
end

function Ball:onUpdate( e )
	local body  = self.physicalBody
	local vel	= body.linearVelocity
	local speed = math.abs( vel.x ) + math.abs( vel.y )

	if speed <= 0.5 then
		speed = 0.5
	end

	if speed < 5 then
		local rescale 	= 5 / speed
		body:setLinearVelocity( vel.x * rescale, vel.y * rescale )
	end
end

the block:

class "Block"( bmpBox )

function Block:Block()
	self:physicalize( { static = true } )
	self:attachListener( CollisionEvent.Begin, self )
end

function Block:onCollisionBegin( e )
	self:release()
end

and obviously the pad:

class "Pad"( bmpPad )

function Pad:Pad()
	self:physicalize( { shape = 'shPad', kinematic = true } )
	Stage.attachListener( TapEvent.Move, self )
end

function Pad:onTapMove( e )
	self.x = e.x
end

That’s 72 lines of code, and you’re ready to run your mobile breakout, thanks to Dreemchest power.

Download the project source.

Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.