Original article, reprinted Please note:Reprinted from All-iPad.netLink:Create a brick breaker game with the corona SDK: Collision Detection
This is the second part of the series of tutorials. Click the link on the next page for the complete content:
- Create a brick breaker game with the corona SDK: Application setup
- Create a brick breaker game with the corona SDK: game controls
- Create a brick breaker game with the corona SDK: Collision Detection
Step 35: Detect brick collisions
When the ball hits the brick, we will use the same method as the ball board collision to check which side is running. The Code is as follows:
302303304305306307308309 |
function removeBrick(e) -- Check the which side of the brick the ball hits, left, right if (e.other.name == 'brick' and (ball.x + ball.width * 0.5) < (e.other.x + e.other.width * 0.5)) then xSpeed = -5 elseif(e.other.name == 'brick' and (ball.x + ball.width * 0.5) >= (e.other.x + e.other.width * 0.5)) then xSpeed = 5 end end |
Step 37: Remove brick
When the ball hits the brick, the brick disappears.
312313314315316317 |
-- Bounce, Remove if (e.other.name == 'brick' ) then ySpeed = ySpeed * -1 e.other:removeSelf() e.other = nil bricks.numChildren = bricks.numChildren - 1 |
Step 38: Add score
Each time you encounter a brick, you get 100 points.
318 |
score = score + 1 scoreNum.text = score * SCORE_CONST scoreNum:setReferencePoint(display.CenterLeftReferencePoint) scoreNum.x = 54 end |
Step 39: Check bricks
When all bricks are eliminated, the player wins
326327328329330 |
if (bricks.numChildren < 0) then alert( ' You Win!' , ' Next Level ›' ) gameEvent = 'win' end end |
Step 40: ball movement
The movement of the ball is controlled by the xspeed and yspeed variables. When the UPDATE function is executed, the ball starts to move.
332333334335336 |
function update(e) -- Ball Movement ball.x = ball.x + xSpeed ball.y = ball.y + ySpeed |
Step 41: Wall collision
The following code checks the collision between the ball and the wall, and returns a reverse direction to the ball when a collision occurs.
341342343 |
if (ball.x < 0) then ball.x = ball.x + 3 xSpeed = -xSpeed end--Left if ((ball.x + ball.width) > display.contentWidth) then ball.x = ball.x - 3 xSpeed = -xSpeed end--Right if (ball.y < 0) then ySpeed = -ySpeed end--Up |
Step 42: loss condition
344 |
if (ball.y + ball.height > paddle.y + paddle.height) then alert( ' You Lose' , ' Play Again ›' ) gameEvent = 'lose' end--down/lose |
Step 43: Game status message
347348349350351352353354355356357358359360361362363364365366367368 |
function alert(t, m) gameListeners( 'remove' ) alertBg = display.newImage( 'alertBg.png' ) box = display.newImage( 'alertBox.png' , 90, 202) transition.from(box, {time = 300, xScale = 0.5, yScale = 0.5, transition = easing.outExpo}) titleTF = display.newText(t, 0, 0, 'akashi' , 19) titleTF:setTextColor(254,203,50) titleTF:setReferencePoint(display.CenterReferencePoint) titleTF.x = display.contentCenterX titleTF.y = display.contentCenterY - 15 msgTF = display.newText(m, 0, 0, 'akashi' , 12) msgTF:setTextColor(254,203,50) msgTF:setReferencePoint(display.CenterReferencePoint) msgTF.x = display.contentCenterX msgTF.y = display.contentCenterY + 15 box:addEventListener( 'tap' , restart) alertScreen = display.newGroup() alertScreen:insert(alertBg) alertScreen:insert(box) alertScreen:insert(titleTF) alertScreen:insert(msgTF) end |
Step 44: restart
376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410 |
function restart(e) if (gameEvent == 'win' and table.maxn(levels) > currentLevel) then currentLevel = currentLevel + 1 changeLevel(levels[currentLevel])--next level levelNum.text = tostring(currentLevel) elseif(gameEvent == 'win' and table.maxn(levels) <= currentLevel) then box:removeEventListener( 'tap' , restart) alertScreen:removeSelf() alertScreen = nil alert( ' Game Over' , ' Congratulations!' ) gameEvent = 'finished' elseif(gameEvent == 'lose' ) then changeLevel(levels[currentLevel])--same level elseif(gameEvent == 'finished' ) then addMenuScreen() transition.from(menuScreen, {time = 300, y = -menuScreen.height, transition = easing.outExpo}) box:removeEventListener( 'tap' , restart) alertScreen:removeSelf() alertScreen = nil currentLevel = scoreText:removeSelf() scoreText = nil scoreNum:removeSelf() scoreNum = nil levelText:removeSelf() levelText = nil levelNum:removeSelf() levelNum = nil ball:removeSelf() ball = nil paddle:removeSelf() paddle = nil score = 0 end end |
Step 45: Change level
417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446 |
function changeLevel(level) -- Clear Level Bricks bricks:removeSelf() bricks.numChildren = 0 bricks = display.newGroup() -- Remove Alert box:removeEventListener( 'tap' , restart) alertScreen:removeSelf() alertScreen = nil -- Reset Ball and Paddle position ball.x = (display.contentWidth * 0.5) - (ball.width * 0.5) ball.y = (paddle.y - paddle.height) - (ball.height * 0.5) -2 paddle.x = display.contentWidth * 0.5 -- Redraw Bricks buildLevel(level) -- Start background:addEventListener( 'tap' , startGame) end |
Step 46: Call Main Function
Call the main () function to start the program.
Step 47: Create the loading screen
Default.png is added to the project directory, and corona compiler automatically adds it as loading screen
Step 48: icon
Note that different platforms require different icon sizes
Step 49: Testing in the simulator
Use the corona simulator to open the project directory for testing.
Step 50: Build
Select build from the File menu of Corona simulator to start compilation.
Address: http://mobile.tutsplus.com/tutorials/corona/create-a-brick-breaker-game-with-the-corona-sdk-collision-detection/
Original article, reprinted Please note:Reprinted from All-iPad.net
Link:Create a brick breaker game with the corona SDK: Collision Detection