ブロック崩しコード全体
width = display.contentWidth
height = display.contentHeight
displayGroup = display.newGroup()
physics = require("physics")
physics.start(true)
physics.setGravity(0, 0)
background = display.newImageRect(displayGroup, "bg_space.png", width, height)
background.x = width/2
background.y = height/2
walls = {}
walls[1] = display.newLine(displayGroup, 0, 0, width, 0)
walls[1].tag = "topWall"
walls[2] = display.newLine(displayGroup, 0, 0, 0, height)
walls[2].tag = "leftWall"
walls[3] = display.newLine(displayGroup, width, 0, width, height)
walls[3].tag = "rightWall"
walls[4] = display.newLine(displayGroup, 0, height, width, height)
walls[4].tag = "bottomWall"
for i = 1, #walls, 1 do
walls[i].strokeWidth = 50
physics.addBody(walls[i], "static", {density = 0.0, friction = 0.0, bounce = 1.0})
end
ball = display.newImageRect(displayGroup, "star.png", 50, 50)
ball.tag = "ball"
physics.addBody(ball, "dynamic", {density = 0.0, friction = 0.0, bounce = 1.0})
function resetBallPos()
ball.x = width/2
ball.y = 1200
end
function gameStart()
resetBallPos()
ball:setLinearVelocity(0, 500)
end
gameStart()
maxNumBlocks = 0
numBlocks = 0
blocks = {}
function deleteBlock(index)
if (blocks[index] == nil) then
return
end
blocks[index]:removeSelf()
blocks[index] = nil
numBlocks = numBlocks - 1
end
function deleteAllBlocks()
for i = 0, maxNumBlocks, 1 do
deleteBlock(i)
end
maxNumBlocks = 0
numBlocks = 0
blocks = {}
end
function deployBlocks()
deleteAllBlocks()
for y = 0, 1, 1 do
for x = 0, 4, 1 do
local index = x + (y * 5)
blocks[index] = display.newImageRect(displayGroup,
"block.png", width * 1/8, 100)
blocks[index].x = (x + 1) * (width * 1/6)
blocks[index].y = 400 + (200 * y)
blocks[index].tag = "block"
blocks[index].index = index
physics.addBody(blocks[index], "static",
{density = 0.0, friction = 0.0, bounce = 1.0})
numBlocks = numBlocks + 1
end
end
maxNumBlocks = numBlocks
end
deployBlocks()
racket = display.newRect(displayGroup, width/2, 1700, 200, 20)
racket.tag = "racket"
racket:setFillColor(1.0, 1.0, 0.0)
physics.addBody(racket, "static", {density = 0.0, friction = 0.0, bounce = 1.0})
function moveRacket(xPosition)
racket.x = xPosition
end
function displayTouchListener(event)
moveRacket(event.x)
end
Runtime:addEventListener("touch", displayTouchListener)
completeText = nil
function completeGame()
physics.pause()
completeText = display.newText(displayGroup, "Complete", width/2, height/2, native.systemFont, 100)
completeText:setTextColor(1.0, 1.0, 1.0)
Runtime:addEventListener("tap", resetGame)
end
function failGame()
physics.pause()
completeText = display.newText(displayGroup, "Fail", width/2, height/2, native.systemFont, 100)
completeText:setTextColor(1.0, 1.0, 1.0)
Runtime:addEventListener("tap", resetGame)
end
function ballStabilization()
local vx, vy = ball:getLinearVelocity()
if (0 < vx) then
vx = 500
else
vx = -500
end
if (0 < vy) then
vy = 500
else
vy = -500
end
ball:setLinearVelocity(vx, vy)
ball:applyTorque(90)
end
function ballCollision(event)
if (event.phase == "began") then
print("collision: "..event.other.tag)
elseif (event.phase == "ended") then
ballStabilization()
if (event.other.tag == "block") then
local hitBlock = event.other
deleteBlock(hitBlock.index)
if (numBlocks == 0) then
completeGame()
end
elseif (event.other.tag == "bottomWall") then
failGame()
end
end
end
ball:addEventListener("collision", ballCollision)
function resetGame()
Runtime:removeEventListener("tap", resetGame)
completeText:removeSelf()
completeText = nil
physics.start(true)
deployBlocks()
resetBallPos()
gameStart()
end