Create a brick breaker game with the corona SDK: Collision Detection

Source: Internet
Author: User
Tags corona sdk

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 functionremoveBrick(e)-- Check the which side of the brick the ball hits, left, rightif(e.other.name == 'brick'and (ball.x + ball.width * 0.5) < (e.other.x + e.other.width * 0.5)) thenxSpeed = -5elseif(e.other.name == 'brick'and (ball.x + ball.width * 0.5) >= (e.other.x + e.other.width * 0.5)) thenxSpeed = 5endend

 

 

Step 37: Remove brick

When the ball hits the brick, the brick disappears.

312313314315316317 -- Bounce, Removeif(e.other.name == 'brick') thenySpeed = ySpeed * -1e.other:removeSelf()e.other = nilbricks.numChildren = bricks.numChildren - 1

 

 

Step 38: Add score

Each time you encounter a brick, you get 100 points.

318 score = score + 1scoreNum.text = score * SCORE_CONSTscoreNum:setReferencePoint(display.CenterLeftReferencePoint)scoreNum.x = 54end

 

Step 39: Check bricks

When all bricks are eliminated, the player wins

326327328329330 if(bricks.numChildren < 0) thenalert('  You Win!''  Next Level ›')gameEvent = 'win'endend

 

 

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 functionupdate(e)-- Ball Movementball.x = ball.x + xSpeedball.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--Leftif((ball.x + ball.width) > display.contentWidth) then ball.x = ball.x - 3 xSpeed = -xSpeed end--Rightif(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 functionalert(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.contentCenterXtitleTF.y = display.contentCenterY - 15msgTF = display.newText(m, 0, 0, 'akashi', 12)msgTF:setTextColor(254,203,50)msgTF:setReferencePoint(display.CenterReferencePoint)msgTF.x = display.contentCenterXmsgTF.y = display.contentCenterY + 15box:addEventListener('tap', restart)alertScreen = display.newGroup()alertScreen:insert(alertBg)alertScreen:insert(box)alertScreen:insert(titleTF)alertScreen:insert(msgTF)end

 

 

Step 44: restart
376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410 functionrestart(e)if(gameEvent == 'win'and table.maxn(levels) > currentLevel) thencurrentLevel = currentLevel + 1changeLevel(levels[currentLevel])--next levellevelNum.text = tostring(currentLevel)elseif(gameEvent == 'win'and table.maxn(levels) &lt;= currentLevel) thenbox:removeEventListener('tap', restart)alertScreen:removeSelf()alertScreen = nilalert('  Game Over''  Congratulations!')gameEvent = 'finished'elseif(gameEvent == 'lose') thenchangeLevel(levels[currentLevel])--same levelelseif(gameEvent == 'finished') thenaddMenuScreen()transition.from(menuScreen, {time = 300, y = -menuScreen.height, transition = easing.outExpo})box:removeEventListener('tap', restart)alertScreen:removeSelf()alertScreen = nilcurrentLevel =scoreText:removeSelf()scoreText = nilscoreNum:removeSelf()scoreNum = nillevelText:removeSelf()levelText = nillevelNum:removeSelf()levelNum = nilball:removeSelf()ball = nilpaddle:removeSelf()paddle = nilscore = 0endend

 

 

Step 45: Change level
417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446 functionchangeLevel(level)-- Clear Level Bricksbricks:removeSelf()bricks.numChildren = 0bricks = display.newGroup()-- Remove Alertbox:removeEventListener('tap', restart)alertScreen:removeSelf()alertScreen = nil-- Reset Ball and Paddle positionball.x = (display.contentWidth * 0.5) - (ball.width * 0.5)ball.y = (paddle.y - paddle.height) - (ball.height * 0.5) -2paddle.x = display.contentWidth * 0.5-- Redraw BricksbuildLevel(level)-- Startbackground:addEventListener('tap', startGame)end

 

 

Step 46: Call Main Function

Call the main () function to start the program.

448 Main()

 

 

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.