Tutorial Pong #4 - Animação do Jogo


Bom dia!!
Hoje vou mostrar como o jogo vai funcionar. A lógica dele é bem simples, vocês vão ver.
Primeiro vamos adicionar mais algumas variáveis e declarar mais algumas funções:
-- Variáveis
local escala    = 0.9
local variation = 0.05
local ballX     = 0
local ballY     = 0
local ballVar   = 0.5

-- Fim do Jogo
local fimJogo
local gameListeners   = {}
local comecarJogo     = {}
local update          = {}
local moverRquete     = {}
local fim             = {}

Agora vamos deixar a função mostrarTelaJogo da seguinte forma:
function gameListeners( action )
    if ( action == 'add' ) then
        bg:addEventListener( 'touch', moverRaquete )
    else
        bg:removeEventListener( 'touch', moverRaquete )
        Runtime:removeEventListener( 'enterFrame', update )
    end
end

function comecarJogo()
    bg:removeEventListener( 'tap', comecarJogo )
    Runtime:addEventListener( 'enterFrame', update )
end

function hitTestObject( objeto1, objeto2 )
    local esquerda = objeto1.contentBounds.xMin <= objeto2.contentBounds.xMin and objeto1.contentBounds.xMax >= objeto2.contentBounds.xMax
    local direita  = objeto1.contentBounds.xMin >= objeto2.contentBounds.xMin and objeto1.contentBounds.xMax <= objeto2.contentBounds.xMax
    local cima     = objeto1.contentBounds.yMin <= objeto2.contentBounds.yMin and objeto1.contentBounds.yMax >= objeto2.contentBounds.yMax
    local baixo    = objeto1.contentBounds.yMin >= objeto2.contentBounds.yMin and objeto1.contentBounds.yMax <= objeto2.contentBounds.yMax

    return ( esquerda or direita ) and ( cima or baixo )
end

function update()
    escala = escala - variation
    ball.xScale = escala
    ball.yScale = escala

    -- Raising
    if ( math.floor( ball.xScale * 10 ) >= 15 ) then
        variation = 0.05
    end
    -- Missed
    if ( math.floor( ball.xScale * 10 ) < 3 ) then
        fim()
    end

    -- Movendo a bolinha
    ball.x = ball.x - ballX
    ball.y = ball.y - ballY

    -- Falling and Hit with paddle
    if ( math.floor( ball.xScale * 10 ) == 3 and hitTestObject( paddleTop, ball ) ) then
        variation = -0.05
        if ( ball.x < paddle.x + 50 ) then
            ballX = ( math.random() * 0.5 ) + ballVar
        end
        if ( ball.x > paddle.x ) then
            ballX = ( math.random() * -0.5 ) - ballVar
        end
        if ( ball.y < paddle.y + 75 ) then
            ballY = ( math.random() * 0.5 ) + ballVar
        end
        if ( ball.y > paddle.y - 70 ) then
            ballY = ( math.random() * -0.5 ) - ballVar
        end

        ballVar = ballVar + 0.025
    end
end

function moverRaquete( e )
    if ( e.phase == 'moved' ) then
        paddle.x = e.x
        paddle.y = e.y
    end
end

function fim()
    fim = display.newImage( 'alert.png', _W / 2, _H / 2 )
    transition.to( fimJogo, { time = 300, xScale = 0.5, yScale = 0.5 } )
end

Bom, hoje foi um post relâmpago, só para tentar não perder este ritmo de postagens antes de acabar o tutorial.
Para quem tiver interesse, pode fazer o download do andamento do projeto aqui.

Sobre o autor

André Luís Batista Kunde
Técnico em Informática (2004), Bacharel em Administração com ênfase em Marketing (2009), buscando ser Engenheiro de Software, Trabalhando com software educacional e me aventurando pelo mundo da programação mobile.

0 comentários:

Copyright © 2013 Aprendendo Android and Blogger Themes.