Crazy Harvest – Project – Eclipse + Android SDK

Crazy Harvest – Project – Eclipse + Android SDK

logo

Neste post demonstro o desenvolvimento de um game para Android, criado para apresentar na matéria de Programação de Jogos para Dispositivos Móveis na Pontifícia Universidade Católica do Paraná no segundo semestre de 2014.

help3

O jogo possui algumas classes básicas para um game Android e foi criado a partir do clone do game SNAKE, conhecido entre os desenvolvedores de jogos mobile com o nome de “MrNomGame”.

help1

O Jogo consiste em realizar toda a colheita procurando não passar duas vezes pelo mesmo local e sem estragar a colheitadeira, isso ocorre quando batemos em pedras que surgem no meio da plantação.

help2

Para o desenvolvimento foi utilizada a IDE ECLIPSE com as devidas atualizações para java em união com a Android SDK.

Eclipse

Eclipse é um IDE para desenvolvimento Java, porém suporta várias outras linguagens a partir de plugins como C/C++,PHP,ColdFusion, Python, Scala e plataforma Android. Ele foi feito em Java e segue o modelo open source de desenvolvimento de software. Atualmente faz parte do kit de desenvolvimento de software recomendado para desenvolvedores Android.

Para visualizar o projeto siga os passos a seguir :

1) INSTALE O AMBIENTE DE DESENVOLVIMENTO

  • ECLIPSE IDE / JDK 6 OU JDL 7 (APENAS A JRE NÃO É SUFICIENTE) / ANDROID SDK

Para a instalação dos ambientes de desenvolvimento siga os passos no link abaixo

Eclipse Android

Ambientes de Desenvolvimento

2) DESCOMPACTE O ARQUIVO ZIP ABAIXO NA PASTA DE PROJETOS DO ECLIPSE

Crazy Harvest – Project – Eclipse + Android SDK.zip

3) ABRA A PASTA PELO ECLIPSE COMO PROJETO ANDROID

4) A SEGUINTE HIERARQUIA DE ARQUIVOS SERÁ DISPONIBILIZADA

\.classpath
\.project
\.settings
\androidmanifest.xml
\arquivos.txt
\assets
\bin
\files.bat
\gen
\project.properties
\res
\src
\.settings\org.eclipse.jdt.core.prefs
\assets\background - clean.png
\assets\background.png
\assets\bitten.ogg
\assets\bobargb8888.png
\assets\bobrgb888.png
\assets\buttons.png
\assets\click.ogg
\assets\eat.ogg
\assets\engine.ogg
\assets\gameover.png
\assets\headdown.png
\assets\headleft.png
\assets\headright.png
\assets\headup.png
\assets\help1.png
\assets\help2.png
\assets\help3.png
\assets\logo.png
\assets\mainmenu.png
\assets\music.ogg
\assets\numbers.png
\assets\obstaculo1.png
\assets\obstaculo2.png
\assets\obstaculo3.png
\assets\pausemenu.png
\assets\ready.png
\assets\tail.png
\assets\test.txt
\assets\thumbs.db
\bin\androidmanifest.xml
\bin\classes
\bin\classes.dex
\bin\crazyharvest.apk
\bin\dexedlibs
\bin\res
\bin\resources.ap_
\bin\classes\com
\bin\classes\com\badlogic
\bin\classes\com\badlogic\androidgames
\bin\classes\com\badlogic\androidgames\framework
\bin\classes\com\badlogic\androidgames\mrnom
\bin\classes\com\badlogic\androidgames\framework\audio.class
\bin\classes\com\badlogic\androidgames\framework\color.class
\bin\classes\com\badlogic\androidgames\framework\fileio.class
\bin\classes\com\badlogic\androidgames\framework\game.class
\bin\classes\com\badlogic\androidgames\framework\graphics$pixmapformat.class
\bin\classes\com\badlogic\androidgames\framework\graphics.class
\bin\classes\com\badlogic\androidgames\framework\impl
\bin\classes\com\badlogic\androidgames\framework\input$keyevent.class
\bin\classes\com\badlogic\androidgames\framework\input$touchevent.class
\bin\classes\com\badlogic\androidgames\framework\input.class
\bin\classes\com\badlogic\androidgames\framework\music.class
\bin\classes\com\badlogic\androidgames\framework\pixmap.class
\bin\classes\com\badlogic\androidgames\framework\pool$poolobjectfactory.class
\bin\classes\com\badlogic\androidgames\framework\pool.class
\bin\classes\com\badlogic\androidgames\framework\screen.class
\bin\classes\com\badlogic\androidgames\framework\sound.class
\bin\classes\com\badlogic\androidgames\framework\testscreen.class
\bin\classes\com\badlogic\androidgames\framework\impl\accelerometerhandler.class
\bin\classes\com\badlogic\androidgames\framework\impl\androidaudio.class
\bin\classes\com\badlogic\androidgames\framework\impl\androidfastrenderview.class
\bin\classes\com\badlogic\androidgames\framework\impl\androidfileio.class
\bin\classes\com\badlogic\androidgames\framework\impl\androidgame.class
\bin\classes\com\badlogic\androidgames\framework\impl\androidgraphics.class
\bin\classes\com\badlogic\androidgames\framework\impl\androidinput.class
\bin\classes\com\badlogic\androidgames\framework\impl\androidmusic.class
\bin\classes\com\badlogic\androidgames\framework\impl\androidpixmap.class
\bin\classes\com\badlogic\androidgames\framework\impl\androidsound.class
\bin\classes\com\badlogic\androidgames\framework\impl\keyboardhandler$1.class
\bin\classes\com\badlogic\androidgames\framework\impl\keyboardhandler.class
\bin\classes\com\badlogic\androidgames\framework\impl\multitouchhandler$1.class
\bin\classes\com\badlogic\androidgames\framework\impl\multitouchhandler.class
\bin\classes\com\badlogic\androidgames\framework\impl\singletouchhandler$1.class
\bin\classes\com\badlogic\androidgames\framework\impl\singletouchhandler.class
\bin\classes\com\badlogic\androidgames\framework\impl\touchhandler.class
\bin\classes\com\badlogic\androidgames\mrnom\assets.class
\bin\classes\com\badlogic\androidgames\mrnom\buildconfig.class
\bin\classes\com\badlogic\androidgames\mrnom\gamescreen$gamestate.class
\bin\classes\com\badlogic\androidgames\mrnom\gamescreen.class
\bin\classes\com\badlogic\androidgames\mrnom\helpscreen.class
\bin\classes\com\badlogic\androidgames\mrnom\helpscreen2.class
\bin\classes\com\badlogic\androidgames\mrnom\helpscreen3.class
\bin\classes\com\badlogic\androidgames\mrnom\highscorescreen.class
\bin\classes\com\badlogic\androidgames\mrnom\loadingscreen.class
\bin\classes\com\badlogic\androidgames\mrnom\mainmenuscreen.class
\bin\classes\com\badlogic\androidgames\mrnom\mrnomgame.class
\bin\classes\com\badlogic\androidgames\mrnom\obstaculo.class
\bin\classes\com\badlogic\androidgames\mrnom\r$attr.class
\bin\classes\com\badlogic\androidgames\mrnom\r$drawable.class
\bin\classes\com\badlogic\androidgames\mrnom\r$layout.class
\bin\classes\com\badlogic\androidgames\mrnom\r$string.class
\bin\classes\com\badlogic\androidgames\mrnom\r.class
\bin\classes\com\badlogic\androidgames\mrnom\settings.class
\bin\classes\com\badlogic\androidgames\mrnom\tractor.class
\bin\classes\com\badlogic\androidgames\mrnom\tractorpart.class
\bin\classes\com\badlogic\androidgames\mrnom\world.class
\bin\res\crunch
\bin\res\crunch\drawable
\bin\res\crunch\drawable-hdpi
\bin\res\crunch\drawable-ldpi
\bin\res\crunch\drawable-mdpi
\bin\res\crunch\drawable\icon.png
\bin\res\crunch\drawable-hdpi\icon.png
\bin\res\crunch\drawable-ldpi\icon.png
\bin\res\crunch\drawable-mdpi\icon.png
\gen\com
\gen\com\badlogic
\gen\com\badlogic\androidgames
\gen\com\badlogic\androidgames\mrnom
\gen\com\badlogic\androidgames\mrnom\buildconfig.java
\gen\com\badlogic\androidgames\mrnom\r.java
\res\drawable
\res\drawable-hdpi
\res\drawable-ldpi
\res\drawable-mdpi
\res\drawable-xhdpi
\res\layout
\res\values
\res\drawable\icon.png
\res\drawable-hdpi\icon.png
\res\drawable-ldpi\icon.png
\res\drawable-mdpi\icon.png
\res\layout\main.xml
\res\values\strings.xml
\src\com
\src\com\badlogic
\src\com\badlogic\androidgames
\src\com\badlogic\androidgames\framework
\src\com\badlogic\androidgames\mrnom
\src\com\badlogic\androidgames\framework\audio.java
\src\com\badlogic\androidgames\framework\color.java
\src\com\badlogic\androidgames\framework\fileio.java
\src\com\badlogic\androidgames\framework\game.java
\src\com\badlogic\androidgames\framework\graphics.java
\src\com\badlogic\androidgames\framework\impl
\src\com\badlogic\androidgames\framework\input.java
\src\com\badlogic\androidgames\framework\music.java
\src\com\badlogic\androidgames\framework\pixmap.java
\src\com\badlogic\androidgames\framework\pool.java
\src\com\badlogic\androidgames\framework\screen.java
\src\com\badlogic\androidgames\framework\sound.java
\src\com\badlogic\androidgames\framework\testscreen.java
\src\com\badlogic\androidgames\framework\impl\accelerometerhandler.java
\src\com\badlogic\androidgames\framework\impl\androidaudio.java
\src\com\badlogic\androidgames\framework\impl\androidfastrenderview.java
\src\com\badlogic\androidgames\framework\impl\androidfileio.java
\src\com\badlogic\androidgames\framework\impl\androidgame.java
\src\com\badlogic\androidgames\framework\impl\androidgraphics.java
\src\com\badlogic\androidgames\framework\impl\androidinput.java
\src\com\badlogic\androidgames\framework\impl\androidmusic.java
\src\com\badlogic\androidgames\framework\impl\androidpixmap.java
\src\com\badlogic\androidgames\framework\impl\androidsound.java
\src\com\badlogic\androidgames\framework\impl\keyboardhandler.java
\src\com\badlogic\androidgames\framework\impl\multitouchhandler.java
\src\com\badlogic\androidgames\framework\impl\singletouchhandler.java
\src\com\badlogic\androidgames\framework\impl\touchhandler.java
\src\com\badlogic\androidgames\mrnom\assets.java
\src\com\badlogic\androidgames\mrnom\gamescreen.java
\src\com\badlogic\androidgames\mrnom\helpscreen.java
\src\com\badlogic\androidgames\mrnom\helpscreen2.java
\src\com\badlogic\androidgames\mrnom\helpscreen3.java
\src\com\badlogic\androidgames\mrnom\highscorescreen.java
\src\com\badlogic\androidgames\mrnom\loadingscreen.java
\src\com\badlogic\androidgames\mrnom\mainmenuscreen.java
\src\com\badlogic\androidgames\mrnom\mrnomgame.java
\src\com\badlogic\androidgames\mrnom\obstaculo.java
\src\com\badlogic\androidgames\mrnom\settings.java
\src\com\badlogic\androidgames\mrnom\tractor.java
\src\com\badlogic\androidgames\mrnom\tractorpart.java
\src\com\badlogic\androidgames\mrnom\world.java

4.1) CODIGO FONTE

Assets.java

package com.badlogic.androidgames.mrnom;

import com.badlogic.androidgames.framework.Pixmap;
import com.badlogic.androidgames.framework.Sound;

public class Assets {
    public static Pixmap background;
    public static Pixmap logo;
    public static Pixmap mainMenu;
    public static Pixmap buttons;
    public static Pixmap help1;
    public static Pixmap help2;
    public static Pixmap help3;
    public static Pixmap numbers;
    public static Pixmap ready;
    public static Pixmap pause;
    public static Pixmap gameOver;
    public static Pixmap headUp;
    public static Pixmap headLeft;
    public static Pixmap headDown;
    public static Pixmap headRight;
    public static Pixmap tail;
    public static Pixmap obstaculo1;
    public static Pixmap obstaculo2;
    public static Pixmap obstaculo3;   
    
    public static Sound click;
    public static Sound eat;
    public static Sound bitten;
    public static Sound engine;
    public static Sound music;
}

GameScreen.java

package com.badlogic.androidgames.mrnom;

import java.util.List;

import android.graphics.Color;

import com.badlogic.androidgames.framework.Game;
import com.badlogic.androidgames.framework.Graphics;
import com.badlogic.androidgames.framework.Input.TouchEvent;
import com.badlogic.androidgames.framework.Pixmap;
import com.badlogic.androidgames.framework.Screen;

public class GameScreen extends Screen {
    enum GameState {

        Ready,
        Running,
        Paused,
        GameOver
    }
    
    GameState state = GameState.Ready;
    World world;
    int oldScore = 0;
    String score = "0";
    
    public GameScreen(Game game) {
        super(game);
        world = new World();
    }

    @Override
    public void update(float deltaTime) {
        List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
        game.getInput().getKeyEvents();
        
        if(state == GameState.Ready)
            updateReady(touchEvents);
        if(state == GameState.Running)
            updateRunning(touchEvents, deltaTime);
        if(state == GameState.Paused)
            updatePaused(touchEvents);
        if(state == GameState.GameOver)
            updateGameOver(touchEvents);        
    }
    
    private void updateReady(List<TouchEvent> touchEvents) {
        if(touchEvents.size() > 0)
            state = GameState.Running;
    }
    
    private void updateRunning(List<TouchEvent> touchEvents, float deltaTime) {        
        int len = touchEvents.size();
        for(int i = 0; i < len; i++) {
            TouchEvent event = touchEvents.get(i);
            if(event.type == TouchEvent.TOUCH_UP) {
                if(event.x < 64 && event.y < 64) {
                    if(Settings.soundEnabled)
                        Assets.click.play(1);
                    state = GameState.Paused;
                    return;
                }
            }
            if(event.type == TouchEvent.TOUCH_DOWN) {
                if(event.x < 64 && event.y > 416) {
                    world.tractor.turnLeft();
                }
                if(event.x > 256 && event.y > 416) {
                    world.tractor.turnRight();
                }
            }
        }
        
        world.update(deltaTime);
        if(world.gameOver) {
            if(Settings.soundEnabled)
                Assets.bitten.play(1);
            state = GameState.GameOver;
        }
        if(oldScore != world.score) {
            oldScore = world.score;
            score = "" + oldScore;
            if(Settings.soundEnabled)
                Assets.eat.play(1);
        }
    }
    
    private void updatePaused(List<TouchEvent> touchEvents) {
        int len = touchEvents.size();
        for(int i = 0; i < len; i++) {
            TouchEvent event = touchEvents.get(i);
            if(event.type == TouchEvent.TOUCH_UP) {
                if(event.x > 80 && event.x <= 240) {
                    if(event.y > 100 && event.y <= 148) {
                        if(Settings.soundEnabled)
                            Assets.click.play(1);
                        state = GameState.Running;
                        return;
                    }                    
                    if(event.y > 148 && event.y < 196) {
                        if(Settings.soundEnabled)
                            Assets.click.play(1);
                        game.setScreen(new MainMenuScreen(game));                        
                        return;
                    }
                }
            }
        }
    }
    
    private void updateGameOver(List<TouchEvent> touchEvents) {
        int len = touchEvents.size();
        for(int i = 0; i < len; i++) {
            TouchEvent event = touchEvents.get(i);
            if(event.type == TouchEvent.TOUCH_UP) {
                if(event.x >= 128 && event.x <= 192 &&
                   event.y >= 200 && event.y <= 264) {
                    if(Settings.soundEnabled)
                        Assets.click.play(1);
                    game.setScreen(new MainMenuScreen(game));
                    return;
                }
            }
        }
    }
    

    @Override
    public void present(float deltaTime) {
        Graphics g = game.getGraphics();
        
        g.drawPixmap(Assets.background, 0, 0);
        drawWorld(world);
        if(state == GameState.Ready) 
            drawReadyUI();
        if(state == GameState.Running)
            drawRunningUI();
        if(state == GameState.Paused)
            drawPausedUI();
        if(state == GameState.GameOver)
            drawGameOverUI();
        
        drawText(g, score, g.getWidth() / 2 - score.length()*20 / 2, g.getHeight() - 42);                
    }
    
    private void drawWorld(World world) {
        Graphics g = game.getGraphics();
        tractor tractor = world.tractor;
        tractorPart head = tractor.parts.get(0);
        Obstaculo obstaculo = world.obstaculo;
        Assets.engine.play(1);
        
        
        Pixmap obstaculoPixmap = null;
        if(obstaculo.type == Obstaculo.TYPE_1)
            obstaculoPixmap = Assets.obstaculo1;
        if(obstaculo.type == Obstaculo.TYPE_2)
            obstaculoPixmap = Assets.obstaculo2;
        if(obstaculo.type == Obstaculo.TYPE_3)
            obstaculoPixmap = Assets.obstaculo3;
        int x = obstaculo.x * 32;
        int y = obstaculo.y * 32;      
        g.drawPixmap(obstaculoPixmap, x, y);             
        
        int len = tractor.parts.size();
        for(int i = 1; i < len; i++) {
            tractorPart part = tractor.parts.get(i);
            x = part.x * 32;
            y = part.y * 32;
            g.drawPixmap(Assets.tail, x, y);
        }
        
        Pixmap headPixmap = null;
        if(tractor.direction == tractor.UP) 
            headPixmap = Assets.headUp;
        if(tractor.direction == tractor.LEFT) 
            headPixmap = Assets.headLeft;
        if(tractor.direction == tractor.DOWN) 
            headPixmap = Assets.headDown;
        if(tractor.direction == tractor.RIGHT) 
            headPixmap = Assets.headRight;        
        x = head.x * 32 + 16;
        y = head.y * 32 + 16;
        g.drawPixmap(headPixmap, x - headPixmap.getWidth() / 2, y - headPixmap.getHeight() / 2);
    }
    
    private void drawReadyUI() {
        Graphics g = game.getGraphics();
        
        g.drawPixmap(Assets.ready, 47, 100);
        g.drawLine(0, 416, 480, 416, Color.BLACK);
    }
    
    private void drawRunningUI() {
        Graphics g = game.getGraphics();

        g.drawPixmap(Assets.buttons, 0, 0, 64, 128, 64, 64);
        g.drawLine(0, 416, 480, 416, Color.BLACK);
        g.drawPixmap(Assets.buttons, 0, 416, 64, 64, 64, 64);
        g.drawPixmap(Assets.buttons, 256, 416, 0, 64, 64, 64);
    }
    
    private void drawPausedUI() {
        Graphics g = game.getGraphics();
        
        g.drawPixmap(Assets.pause, 80, 100);
        g.drawLine(0, 416, 480, 416, Color.BLACK);
    }

    private void drawGameOverUI() {
        Graphics g = game.getGraphics();
        
        g.drawPixmap(Assets.gameOver, 62, 100);
        g.drawPixmap(Assets.buttons, 128, 200, 0, 128, 64, 64);
        g.drawLine(0, 416, 480, 416, Color.BLACK);
    }
    
    public void drawText(Graphics g, String line, int x, int y) {
        int len = line.length();
        for (int i = 0; i < len; i++) {
            char character = line.charAt(i);

            if (character == ' ') {
                x += 20;
                continue;
            }

            int srcX = 0;
            int srcWidth = 0;
            if (character == '.') {
                srcX = 200;
                srcWidth = 10;
            } else {
                srcX = (character - '0') * 20;
                srcWidth = 20;
            }

            g.drawPixmap(Assets.numbers, x, y, srcX, 0, srcWidth, 32);
            x += srcWidth;
        }
    }
    
    @Override
    public void pause() {
        if(state == GameState.Running)
            state = GameState.Paused;
        
        if(world.gameOver) {
            Settings.addScore(world.score);
            Settings.save(game.getFileIO());
        }
    }

    @Override
    public void resume() {
        
    }

    @Override
    public void dispose() {
        
    }
}

HelpScreen.java

package com.badlogic.androidgames.mrnom;

import java.util.List;

import com.badlogic.androidgames.framework.Game;
import com.badlogic.androidgames.framework.Graphics;
import com.badlogic.androidgames.framework.Input.TouchEvent;
import com.badlogic.androidgames.framework.Screen;

public class HelpScreen extends Screen {      
    public HelpScreen(Game game) {
        super(game);
    }

    @Override
    public void update(float deltaTime) {
        List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
        game.getInput().getKeyEvents();
        
        int len = touchEvents.size();
        for(int i = 0; i < len; i++) {
            TouchEvent event = touchEvents.get(i);
            if(event.type == TouchEvent.TOUCH_UP) {
                if(event.x > 256 && event.y > 416 ) {
                    game.setScreen(new HelpScreen2(game));
                    if(Settings.soundEnabled)
                        Assets.click.play(1);
                    return;
                }
            }
        }
    }

    @Override
    public void present(float deltaTime) {
        Graphics g = game.getGraphics();      
        g.drawPixmap(Assets.background, 0, 0);
        g.drawPixmap(Assets.help1, 64, 100);
        g.drawPixmap(Assets.buttons, 256, 416, 0, 64, 64, 64);
    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void dispose() {

    }
}

HelpScreen2.java

package com.badlogic.androidgames.mrnom;

import java.util.List;

import com.badlogic.androidgames.framework.Game;
import com.badlogic.androidgames.framework.Graphics;
import com.badlogic.androidgames.framework.Screen;
import com.badlogic.androidgames.framework.Input.TouchEvent;

public class HelpScreen2 extends Screen {

    public HelpScreen2(Game game) {
        super(game);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void update(float deltaTime) {
        List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
        game.getInput().getKeyEvents();
        
        Graphics g = game.getGraphics();
        int len = touchEvents.size();
        for(int i = 0; i < len; i++) {
            TouchEvent event = touchEvents.get(i);
            if(event.type == TouchEvent.TOUCH_UP) {
                if(event.x > g.getWidth() - 64 && event.y > g.getHeight() - 64 ) {
                    game.setScreen(new HelpScreen3(game));
                    if(Settings.soundEnabled)
                        Assets.click.play(1);
                    return;
                }
            }
        }
    }

    @Override
    public void present(float deltaTime) {
        Graphics g = game.getGraphics();      
        g.drawPixmap(Assets.background, 0, 0);
        g.drawPixmap(Assets.help2, 64, 100);
        g.drawPixmap(Assets.buttons, 256, 416, 0, 64, 64, 64);
    }

    @Override
    public void pause() {
        // TODO Auto-generated method stub

    }

    @Override
    public void resume() {
        // TODO Auto-generated method stub

    }

    @Override
    public void dispose() {
        // TODO Auto-generated method stub

    }

}

HelpScreen3.java

package com.badlogic.androidgames.mrnom;

import java.util.List;

import com.badlogic.androidgames.framework.Game;
import com.badlogic.androidgames.framework.Graphics;
import com.badlogic.androidgames.framework.Screen;
import com.badlogic.androidgames.framework.Input.TouchEvent;

public class HelpScreen3 extends Screen {
    
    public HelpScreen3(Game game) {
        super(game);
    }
    
    @Override
    public void update(float deltaTime) {
        List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
        game.getInput().getKeyEvents();
        
        int len = touchEvents.size();
        for(int i = 0; i < len; i++) {
            TouchEvent event = touchEvents.get(i);
            if(event.type == TouchEvent.TOUCH_UP) {
                if(event.x > 256 && event.y > 416 ) {
                    game.setScreen(new MainMenuScreen(game));
                    if(Settings.soundEnabled)
                        Assets.click.play(1);
                    return;
                }
            }
        }
    }

    @Override
    public void present(float deltaTime) {
        Graphics g = game.getGraphics();      
        g.drawPixmap(Assets.background, 0, 0);
        g.drawPixmap(Assets.help3, 64, 100);
        g.drawPixmap(Assets.buttons, 256, 416, 0, 128, 64, 64);
    }

    @Override
    public void pause() {
        // TODO Auto-generated method stub

    }

    @Override
    public void resume() {
        // TODO Auto-generated method stub

    }

    @Override
    public void dispose() {
        // TODO Auto-generated method stub

    }

}

HighscoreScreen.java

package com.badlogic.androidgames.mrnom;

import java.util.List;

import com.badlogic.androidgames.framework.Game;
import com.badlogic.androidgames.framework.Graphics;
import com.badlogic.androidgames.framework.Screen;
import com.badlogic.androidgames.framework.Input.TouchEvent;

public class HighscoreScreen extends Screen {
    String lines[] = new String[5];

    public HighscoreScreen(Game game) {
        super(game);

        for (int i = 0; i < 5; i++) {
            lines[i] = "" + (i + 1) + ". " + Settings.highscores[i];
        }
    }

    @Override
    public void update(float deltaTime) {
        List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
        game.getInput().getKeyEvents();

        int len = touchEvents.size();
        for (int i = 0; i < len; i++) {
            TouchEvent event = touchEvents.get(i);
            if (event.type == TouchEvent.TOUCH_UP) {
                if (event.x < 64 && event.y > 416) {
                    if(Settings.soundEnabled)
                        Assets.click.play(1);
                    game.setScreen(new MainMenuScreen(game));
                    return;
                }
            }
        }
    }

    @Override
    public void present(float deltaTime) {
        Graphics g = game.getGraphics();

        g.drawPixmap(Assets.background, 0, 0);
        g.drawPixmap(Assets.mainMenu, 64, 20, 0, 42, 196, 42);

        int y = 100;
        for (int i = 0; i < 5; i++) {
            drawText(g, lines[i], 20, y);
            y += 50;
        }

        g.drawPixmap(Assets.buttons, 0, 416, 64, 64, 64, 64);
    }

    public void drawText(Graphics g, String line, int x, int y) {
        int len = line.length();
        for (int i = 0; i < len; i++) {
            char character = line.charAt(i);
    
            if (character == ' ') {
                x += 20;
                continue;
            }
    
            int srcX = 0;
            int srcWidth = 0;
            if (character == '.') {
                srcX = 200;
                srcWidth = 10;
            } else {
                srcX = (character - '0') * 20;
                srcWidth = 20;
            }
    
            g.drawPixmap(Assets.numbers, x, y, srcX, 0, srcWidth, 32);
            x += srcWidth;
        }
    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void dispose() {

    }
}

LoadingScreen.java

package com.badlogic.androidgames.mrnom;

import com.badlogic.androidgames.framework.Game;
import com.badlogic.androidgames.framework.Graphics;
import com.badlogic.androidgames.framework.Screen;
import com.badlogic.androidgames.framework.Graphics.PixmapFormat;

public class LoadingScreen extends Screen {
    public LoadingScreen(Game game) {
        super(game);
    }

    @Override
    public void update(float deltaTime) {
        Graphics g = game.getGraphics();
        Assets.background = g.newPixmap("background.png", PixmapFormat.RGB565);
        Assets.logo = g.newPixmap("logo.png", PixmapFormat.ARGB4444);
        Assets.mainMenu = g.newPixmap("mainmenu.png", PixmapFormat.ARGB4444);
        Assets.buttons = g.newPixmap("buttons.png", PixmapFormat.ARGB4444);
        Assets.help1 = g.newPixmap("help1.png", PixmapFormat.ARGB4444);
        Assets.help2 = g.newPixmap("help2.png", PixmapFormat.ARGB4444);
        Assets.help3 = g.newPixmap("help3.png", PixmapFormat.ARGB4444);
        Assets.numbers = g.newPixmap("numbers.png", PixmapFormat.ARGB4444);
        Assets.ready = g.newPixmap("ready.png", PixmapFormat.ARGB4444);
        Assets.pause = g.newPixmap("pausemenu.png", PixmapFormat.ARGB4444);
        Assets.gameOver = g.newPixmap("gameover.png", PixmapFormat.ARGB4444);
        Assets.headUp = g.newPixmap("headup.png", PixmapFormat.ARGB4444);
        Assets.headLeft = g.newPixmap("headleft.png", PixmapFormat.ARGB4444);
        Assets.headDown = g.newPixmap("headdown.png", PixmapFormat.ARGB4444);
        Assets.headRight = g.newPixmap("headright.png", PixmapFormat.ARGB4444);
        Assets.tail = g.newPixmap("tail.png", PixmapFormat.ARGB4444);
        Assets.obstaculo1 = g.newPixmap("obstaculo1.png", PixmapFormat.ARGB4444);
        Assets.obstaculo2 = g.newPixmap("obstaculo2.png", PixmapFormat.ARGB4444);
        Assets.obstaculo3 = g.newPixmap("obstaculo3.png", PixmapFormat.ARGB4444);
        Assets.click = game.getAudio().newSound("click.ogg");
        Assets.eat = game.getAudio().newSound("eat.ogg");
        Assets.bitten = game.getAudio().newSound("bitten.ogg");
        Assets.engine = game.getAudio().newSound("engine.ogg");
        Settings.load(game.getFileIO());
        game.setScreen(new MainMenuScreen(game));
    }

    @Override
    public void present(float deltaTime) {

    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void dispose() {

    }
}

MainMenuScreen.java

package com.badlogic.androidgames.mrnom;

import java.util.List;

import com.badlogic.androidgames.framework.Game;
import com.badlogic.androidgames.framework.Graphics;
import com.badlogic.androidgames.framework.Input.TouchEvent;
import com.badlogic.androidgames.framework.Screen;

public class MainMenuScreen extends Screen {
    public MainMenuScreen(Game game) {
        super(game);               
    }   

    @Override
    public void update(float deltaTime) {
        Graphics g = game.getGraphics();
        List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
        game.getInput().getKeyEvents();       
        
        int len = touchEvents.size();
        for(int i = 0; i < len; i++) {
            TouchEvent event = touchEvents.get(i);
            if(event.type == TouchEvent.TOUCH_UP) {
                if(inBounds(event, 0, g.getHeight() - 64, 64, 64)) {
                    Settings.soundEnabled = !Settings.soundEnabled;
                    if(Settings.soundEnabled)
                        Assets.click.play(1);
                }
                if(inBounds(event, 64, 220, 192, 42) ) {
                    game.setScreen(new GameScreen(game));
                    if(Settings.soundEnabled)
                        Assets.click.play(1);
                    return;
                }
                if(inBounds(event, 64, 220 + 42, 192, 42) ) {
                    game.setScreen(new HighscoreScreen(game));
                    if(Settings.soundEnabled)
                        Assets.click.play(1);
                    return;
                }
                if(inBounds(event, 64, 220 + 84, 192, 42) ) {
                    game.setScreen(new HelpScreen(game));
                    if(Settings.soundEnabled)
                        Assets.click.play(1);
                    return;
                }
            }
        }
    }
    
    private boolean inBounds(TouchEvent event, int x, int y, int width, int height) {
        if(event.x > x && event.x < x + width - 1 && 
           event.y > y && event.y < y + height - 1) 
            return true;
        else
            return false;
    }

    @Override
    public void present(float deltaTime) {
        Graphics g = game.getGraphics();
        
        g.drawPixmap(Assets.background, 0, 0);
        g.drawPixmap(Assets.logo, 32, 20);
        g.drawPixmap(Assets.mainMenu, 64, 220);
        if(Settings.soundEnabled)
            g.drawPixmap(Assets.buttons, 0, 416, 0, 0, 64, 64);
        else
            g.drawPixmap(Assets.buttons, 0, 416, 64, 0, 64, 64);
    }

    @Override
    public void pause() {        
        Settings.save(game.getFileIO());
    }

    @Override
    public void resume() {

    }

    @Override
    public void dispose() {

    }
}

MrNomGame.java

package com.badlogic.androidgames.mrnom;

import com.badlogic.androidgames.framework.Screen;
import com.badlogic.androidgames.framework.impl.AndroidGame;

public class MrNomGame extends AndroidGame {
    @Override
    public Screen getStartScreen() {
        return new LoadingScreen(this); 
    }
}

Obstaculo.java

package com.badlogic.androidgames.mrnom;

public class Obstaculo {
    public static final int TYPE_1 = 0;
    public static final int TYPE_2 = 1;
    public static final int TYPE_3 = 2;
    public int x, y;
    public int type;

    public Obstaculo(int x, int y, int type) {
        this.x = x;
        this.y = y;
        this.type = type;
    }
}

Settings.java

package com.badlogic.androidgames.mrnom;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import com.badlogic.androidgames.framework.FileIO;

public class Settings {
    public static boolean soundEnabled = true;
    public static int[] highscores = new int[] { 100, 80, 50, 30, 10 };

    public static void load(FileIO files) {
        BufferedReader in = null;
        try {
            in = new BufferedReader(new InputStreamReader(
                    files.readFile(".mrnom")));
            soundEnabled = Boolean.parseBoolean(in.readLine());
            for (int i = 0; i < 5; i++) {
                highscores[i] = Integer.parseInt(in.readLine());
            }
        } catch (IOException e) {
            // :( It's ok we have defaults
        } catch (NumberFormatException e) {
            // :/ It's ok, defaults save our day
        } finally {
            try {
                if (in != null)
                    in.close();
            } catch (IOException e) {
            }
        }
    }

    public static void save(FileIO files) {
        BufferedWriter out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(
                    files.writeFile(".mrnom")));
            out.write(Boolean.toString(soundEnabled));
            out.write("\n");
            for (int i = 0; i < 5; i++) {
                out.write(Integer.toString(highscores[i]));
                out.write("\n");
            }

        } catch (IOException e) {
        } finally {
            try {
                if (out != null)
                    out.close();
            } catch (IOException e) {
            }
        }
    }

    public static void addScore(int score) {
        for (int i = 0; i < 5; i++) {
            if (highscores[i] < score) {
                for (int j = 4; j > i; j--)
                    highscores[j] = highscores[j - 1];
                highscores[i] = score;
                break;
            }
        }
    }
}

tractor.java

package com.badlogic.androidgames.mrnom;

import java.util.ArrayList;
import java.util.List;

public class tractor {
    public static final int UP = 0;
    public static final int LEFT = 1;
    public static final int DOWN = 2;
    public static final int RIGHT = 3;
    
    public List<tractorPart> parts = new ArrayList<tractorPart>();
    public int direction;    
    
    public tractor() {        
        direction = UP;
        parts.add(new tractorPart(5, 6));
        parts.add(new tractorPart(5, 7));
        parts.add(new tractorPart(5, 8));
    }
    
    public void turnLeft() {
        direction += 1;
        if(direction > RIGHT)
            direction = UP;
    }
    
    public void turnRight() {
        direction -= 1;
        if(direction < UP)
            direction = RIGHT;
    }
    
    public void eat() {
        tractorPart end = parts.get(parts.size()-1); 
        parts.add(new tractorPart(end.x, end.y));
    }
    
    public void advance() {
        tractorPart head = parts.get(0);               
        
        int len = parts.size() - 1;
        for(int i = len; i > 0; i--) {
            tractorPart before = parts.get(i-1);
            tractorPart part = parts.get(i);
            part.x = before.x;
            part.y = before.y;
        }
        
        if(direction == UP)
            head.y -= 1;
        if(direction == LEFT)
            head.x -= 1;
        if(direction == DOWN)
            head.y += 1;
        if(direction == RIGHT)
            head.x += 1;
        
        if(head.x < 0)
            head.x = 9;
        if(head.x > 9)
            head.x = 0;
        if(head.y < 0)
            head.y = 12;
        if(head.y > 12)
            head.y = 0;
    }
    
    public boolean checkBitten() {
        int len = parts.size();
        tractorPart head = parts.get(0);
        for(int i = 1; i < len; i++) {
            tractorPart part = parts.get(i);
            if(part.x == head.x && part.y == head.y)
                return true;
        }        
        return false;
    }      
}

tractorPart.java

package com.badlogic.androidgames.mrnom;

public class tractorPart {
    public int x, y;
    
    public tractorPart(int x, int y) {
        this.x = x;
        this.y = y;
    }
}       

World.java

package com.badlogic.androidgames.mrnom;

import java.util.Random;

public class World {
    static final int WORLD_WIDTH = 10;
    static final int WORLD_HEIGHT = 13;
    static final int SCORE_INCREMENT = 1;
    static final int SCORE_DECREMENT = -2;
    static final float TICK_INITIAL = 0.3f;
    static final float TICK_DECREMENT = 1f;

    public tractor tractor;
    public Obstaculo obstaculo;

    public boolean gameOver = false;;
    public int score = 1;
    public int changerockstate = 0;

    boolean fields[][] = new boolean[WORLD_WIDTH][WORLD_HEIGHT];
    Random random = new Random();
    float tickTime = 0;
    static float tick = TICK_INITIAL;

    public World() {
        tractor = new tractor();
        placeObstaculo();
    }

    private void placeObstaculo() {
        for (int x = 0; x < WORLD_WIDTH; x++) {
            for (int y = 0; y < WORLD_HEIGHT; y++) {
                fields[x][y] = false;
            }
        }

        int len = tractor.parts.size();
        for (int i = 0; i < len; i++) {
            tractorPart part = tractor.parts.get(i);
            fields[part.x][part.y] = true;
        }

        int obstaculoX = random.nextInt(WORLD_WIDTH);
        int obstaculoY = random.nextInt(WORLD_HEIGHT);
        while (true) {
            if (fields[obstaculoX][obstaculoY] == false)
                break;
            obstaculoX += 1;
            if (obstaculoX >= WORLD_WIDTH) {
                obstaculoX = 0;
                obstaculoY += 1;
                if (obstaculoY >= WORLD_HEIGHT) {
                    obstaculoY = 0;
                }
            }
        }
        if ((obstaculoX >= 6 && obstaculoY >= 1) && (obstaculoX <= 8 && obstaculoY <= 3)) {   
        }else{
            obstaculo = new Obstaculo(obstaculoX, obstaculoY, random.nextInt(3));
        }
    }

    public void update(float deltaTime) {
        
        if (score <= 0)
            gameOver = true;
        
        if (gameOver)
            return;

        tickTime += deltaTime;

        while (tickTime > tick) {
            tickTime -= tick;
            tractor.advance();
            if (tractor.checkBitten()) {
                score += SCORE_DECREMENT;
            }else
            {
                score += SCORE_INCREMENT;
            }

            tractorPart head = tractor.parts.get(0);
            tractor.eat();
            
            changerockstate = changerockstate + 1;
            
            if (changerockstate > 10) {
                placeObstaculo();
                changerockstate = 0;
             }
           
            if (head.x == obstaculo.x && head.y == obstaculo.y) {
                gameOver = true;

                if (score % 100 == 0 && tick - TICK_DECREMENT > 0) {
                    tick -= TICK_DECREMENT;
                }
            }
            if ((head.x >= 6 && head.y >= 1) && (head.x <= 8 && head.y <= 3)) {
                gameOver = true;

                if (score % 100 == 0 && tick - TICK_DECREMENT > 0) {
                    tick -= TICK_DECREMENT;
                }
            }
        }
    }
}

4.2) ASSETS

background - clean background buttons comission_farmland__s_tileset_by_wesleyfg-d4g4hcj gameover headdown headleft headright headup mainmenu numbers obstaculo1 obstaculo2 obstaculo3 pausemenu ready tail

5) O JOGO PODE COMPILADO E TESTADO NO PROPRIO PC

Para testar o jogo utilize o emulador da própria SDK ANDROID ou um emulador Android de terceiros.

Recomendamos o uso do software Genymotion, que pode instalar uma série de máquinas virtuais com diferentes dispositivos Android, podendo ser vinculada a IDE do ECLIPSE para testes dos aplicativos e jogos desenvolvidos.

ANDROID GENYMOTION

Clique no link abaixo para informações detalhadas

Rodar o emulador do Android

BOA SORTE, APRENDA E DIVIRTA-SE

Anúncios

Deixe um comentário

Preencha os seus dados abaixo ou clique em um ícone para log in:

Logotipo do WordPress.com

Você está comentando utilizando sua conta WordPress.com. Sair / Alterar )

Imagem do Twitter

Você está comentando utilizando sua conta Twitter. Sair / Alterar )

Foto do Facebook

Você está comentando utilizando sua conta Facebook. Sair / Alterar )

Foto do Google+

Você está comentando utilizando sua conta Google+. Sair / Alterar )

Conectando a %s