
게임의 실패 시나리오는 Player가 Paddle에서 공을 놓치고 공이 보드 뒤쪽이나 Paddle 뒤에서 튀는 경우이다. 또한 Player에게 3번의 기회를 준다.
먼저 lives 속성을 설정한다.
var lives = 3
bounceBall에서는 공이 테두리의 상단과 하단에 대해 튕기는지 확인한다.
func bounceBall() {
...
if abs(ball.position.z) > gameArea.height / 2 {
ballVelocity.z = -ballVelocity.z
lives -= 1
if lives < 0 {
print("GAME OVER - YOU LOST")
} else {
print("Lives: ", lives)
}
}
...
}
볼 위치 Z를 확인한다. 볼의 위치가 실패 위치에 있으면 남은 생명 수를 줄인다.
생명이 0보다 적으면 게임에서 진 것이다.
// Ray Break에서 왼쪽 및 오른쪽 화살표 키에 대해
// 두 개의 속성을 생성하여 나중에 눌렸는지 여부를
// 확인할 수 있다.
var keyLeftDown = false
var keyRightDown = false
// Override Key Down and set these properties to true.
override func keyDown(key: Int, isARepeat: Bool) -> Bool {
switch key {
//the value for the left arrow
case 123:
keyLeftDown = true
// the value for the right arrow
case 124:
keyRightDown = true
default:
return false
}
return true
}
// Do the same for Key Up.
override func keyUp(key: Int) -> Bool {
switch key {
case 123:
keyLeftDown = false
case 124:
keyRightDown = false
default:
break
}
return true
}
// 매 프레임마다 화살표 키가 눌렸는지 확인하고
// 눌리면 Paddle 위치를 업데이트한다.
...
// In Update Scene, move the paddle left or right based on the pressed keyboard.
override func updateScene(deltaTime: Float) {
ball.position += ballVelocity * deltaTime
checkBricks()
bounceBall()
// We just have to constrain the paddle to stay within the boarder.
// Before updating the paddle position,
// store the oldPosition just in case the new position is over the edge.
let oldPaddlePosition = paddle.position.x
if keyLeftDown {
paddle.position.x -= Constants.paddleSpeed
}
if keyRightDown {
paddle.position.x += Constants.paddleSpeed
}
// After the paddle position update,
// we check that the bounding box of the paddle isn't over the boarder.
let paddleWidth = paddle.worldBoundingBox().width
let halfBorderWidth = border.worldBoundingBox().width / 2
// If the paddle is outside the boarder,
// then we restore the paddles previous position.
if abs(paddle.position.x) + (paddleWidth / 2) > halfBorderWidth {
paddle.position.x = oldPaddlePosition
}
// Here we update each of the instances transform, so that the brick rotates.
let transforms: [Transform] = bricks.transforms.map {
var transform = $0
transform.rotation.y += Float.pi / 4 * deltaTime
return transform
}
bricks.transforms = transforms
}

게임 이벤트에 대한 피드백을 플레이어에게 제공하는 음향 효과가 있는 게임은 조용한 게임보다 더 재미있다.
또한 배경 음악은 경험과 즐거움 더한다.
App에 SoundController를 추가해본다. RayBreak뿐만 아니라 개발자가 작성하는 다른 게임에서도 이 SoundController를 사용할 수 있다.