Android - Project Wall

After creating Project Wall in Flash, I wanted to challenge myself in building an Android Game.

Main Menu

The main menu contains buttons; Play Now, Leaderboards, Settings, Statistics and Help.

package comp208.sbartsch.asst4;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/*
 * About this activity:
    This activity displays the main menu where the player/user will make their selection.
    A player can also edit their name which is passed along the screens
 */

public class MenuActivity extends Activity{

    static public String playerName;
    private EditText playername;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        setContentView(R.layout.activity_main_menu);

        playername = (EditText) findViewById(R.id.txtPlayerName);

        if(playerName == null){
            playername.setText("Player");
        }
        else{
            playername.setText(playerName);
        }


        Button btnPlayNow = (Button) findViewById(R.id.btnPlayNow);
        Button btnLeaderboards = (Button) findViewById(R.id.btnLeaderboards);
        Button btnSettings = (Button) findViewById(R.id.btnSettings);
        Button btnStats = (Button) findViewById(R.id.btnStats);
        Button btnHelp = (Button) findViewById(R.id.btnHelp);

        btnPlayNow.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v){
                final Intent intent = new Intent(getApplicationContext(), GameActivity.class);
                playername = (EditText) findViewById(R.id.txtPlayerName);
                Walls wall = new Walls(getApplicationContext());
                wall.elapseTime = 0;
                wall.playerName = playername.getText().toString();
                playerName = playername.getText().toString();
                intent.putExtra("PLAYER_NAME",playerName);
                startActivity(intent);
            }
        });

        btnLeaderboards.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v){
                final Intent intent = new Intent(getApplicationContext(), LeaderboardActivity.class);
                startActivity(intent);
            }
        });

        btnStats.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v){
                final Intent intent = new Intent(getApplicationContext(), StatsActivity.class);
                intent.putExtra("PLAYER_NAME",playerName);
                startActivity(intent);
            }
        });

        btnSettings.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v){
                final Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
                startActivity(intent);
            }
        });

        btnHelp.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v){
                final Intent intent = new Intent(getApplicationContext(), HelpActivity.class);
                startActivity(intent);
            }
        });

    }
}

Game

The Wall class (which is huge) is the game. I won't display all of the code below but it's a good chunk. The following below displays collions detections between the player and the walls. It draws the walls, player and the stats on the screen.

for(int ctr=0;ctr<walls.length;ctr++){
    //Left
    if(y < -2){
        gox=-2;
        playerX -=1;
    }
    //Right
    if(y > 2){
        gox=+2;
        playerX +=1;
    }
    //Up
    if(d > 4){
        god=-2;
        playerY -=1;
    }
    //Down
    if(d < 0){
        god=+2;
        playerY +=1;
    }


    RectF rect = new RectF();
    rect.left = (walls[ctr][0] -=gox)*wallsize; //-=gox
    rect.top = (walls[ctr][1] -=god)*wallsize; //-=god
    rect.right = (walls[ctr][0] + walls[ctr][2])*wallsize;
    rect.bottom = (walls[ctr][1] + walls[ctr][3])*wallsize;
    canvas.drawRect(rect,paint);


    //Dummy rectangle for player (circle)
    //Allows collision detection a whole lot easier than hardcoded functions
    plyr.left = (screenWidth / 2);
    plyr.top = (screenHeight / 2);
    plyr.right = (screenWidth / 2);
    plyr.bottom = (screenHeight / 2);
    canvas.drawRect(plyr, paint);

    if(rect.intersect(plyr)){
        wallTouched = true;
        Log.i("WALL TOUCHED","WALL TOUCHED");
    }
}


RectF end = new RectF();
end.left = (ends[0][0] -=gox)*wallsize; //-=gox
end.top = (ends[0][1] -=god)*wallsize; //-=god
end.right = (ends[0][0] + ends[0][2])*wallsize;
end.bottom = (ends[0][1] + ends[0][3])*wallsize;
canvas.drawRect(end,paint);

if(end.intersect(plyr)){
    endTouched = true;
    Log.i("PLAYER COLLISION", "PLAYER WINS");
}

//and finally, text stats on top of the screen
paint.setColor(Color.rgb(255,255,255));
paint.setStrokeWidth(1);
paint.setTextSize(18);
paint.setStyle(Paint.Style.FILL_AND_STROKE);

canvas.drawText("PLAYER NAME", 20, 20, paint);
canvas.drawText(playerName, 20, 40, paint);
canvas.drawText("TIME", 800, 20, paint);
canvas.drawText(String.valueOf(elapseTime), 800, 40, paint);

Setting Up Walls, Coins & The Finish Line

This was the most time consuming part of the project. Flash allowed me to create objects on the screen with the same object name/collection but Android wasn't that fortunate. I opened an Adobe Software that allowed me to see the locations of the walls (rectangles) by copying and pasting the Flash map. The code gives you an idea on how I ported that.

    static public int[][] walls = new int[][]{
        //Walls
        {132,52,60,8},
        {132,52,8,60},
        //{184,52,8,60},
        {80,112,60,8},
        {184,112,60,8},
        {72,112,8,60},
        {244,112,8,60},
        {132,138,60,8},
        {80,164,60,8},
        {184,164,60,8},
        {132,172,8,73},
        {184,172,8,73},
        {140,245,8,60},
        {177,245,8,60},
        {148,305,8,60},
        {169,305,8,91},
        {-31,357,179,8},
        {-56,388,225,8},
        {-56,279,8,109},
        {-31,297,8,60},
        {-56,271,53,8},
        {-3,271,8,73},
        {5,336,73,8},
        {78,271,8,73},
        {102,239,8,118},
        {-83,239,185,8},
        {49,247,8,73},
        {25,271,8,65},
        {-83,247,8,211},
        {-75,450,252,8},
        {140,409,8,30},
        {177,396,73,8},
        {177,442,73,8},
        {205,418,30,8},
        {250,403,60,8},
        {250,434,60,8},
        {310,411,60,8},
        {310,426,60,8},
        //Squares
        {-56,431,8,8},
        {-48,411,8,8},
        {-27,431,8,8},
        {-8,404,8,8},
        {4,426,8,8},
        {35,410,8,8},
        {42,435,8,8},
        {64,401,8,8},
        {70,426,8,8},
        {96,406,8,8},
        {106,431,8,8},
        {119,416,8,8},
        {166,402,8,8},
        {166,428,8,8}
    };

    static public int[][] ends = new int[][]{
        {370,411,24,23}
    };

    static public int[][] coins = new int[][]{
        {301,425,8,8},
        {301,412,8,8},
        {239,418,8,8},
        {152,416,8,8},
        {-70,389,8,8},
        {-31,254,8,8},
        {-31,288,8,8},
        {-48,420,8,8},
        {114,401,8,8},
        {86,434,8,8},
        {47,410,8,8},
        {20,423,8,8},
        {6,399,8,8},
        {144,368,8,8},
        {168,296,8,8},
        {89,345,8,8},
        {65,324,8,8},
        {62,251,8,8},
        {10,324,8,8},
        {149,296,8,8},
        {171,232,8,8},
        {144,232,8,8},
        {228,138,8,8},
        {86,138,8,8},
        {157,72,8,8}

Player

Nothing too fancy but it draws the player in the centre of the player's screen. The player would physically move their tablet to navigate/play the game.

package comp208.sbartsch.asst4;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;


/*
 * About this class:
    This will display a square in the middle of the tablet's screen. 
    Nothing fancy, just for design. The real player control is made in the GameActivity
 */

public class Player{    
    static int x;
    static int y;

    public Player(Canvas canvas){       
        Paint paint = new Paint();
        paint.setStrokeWidth(1);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setColor(Color.rgb(225,0,0));
        canvas.drawCircle(x, y, 5, paint);
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }


}

Music

Android does not fart around when it comes to playing music in an app or game. The following class would be;

package comp208.sbartsch.asst4;

import android.content.Context;
import android.media.MediaPlayer;

/*
 * About this class:
    Simply controls the media player and plays the music created for this game.
 */

public class Music{
    private static MediaPlayer song;


    public Music(Context context){
        this.song = MediaPlayer.create(context, R.raw.wallmusic);
    }

    public void start(){
        this.song.start();
    }

    public void stop(){
        this.song.stop();
    }

    public void release(){
        this.song.release();
    }

    public boolean isPlaying(){
        return song.isPlaying();
    }
}

It's very simple to call the class and pass it the audio file to play.

Others

Other classes and activities include Leaderboards, Local Statistics, Help Menu, Game Over, and the actual game. Other files in the source would be HTTPManager which helps connect the leaderboards and any adapters that help with collections.