Thursday 10 March 2011

miTanks, Part 11

FINALLY!!! After 5 days searching the net and testing heaps of different custom views, I've drawn a circle. That's right. 5 days of work and all I've done is draw a circle. This must sound rediculous but this is the biggest break though. Look! Here it is:



It's amazing. Now I simply had to copy over the view handling bits of code to miTanks and BAM!



We get an ugly, badly drawn game of tanks. It's nice. So now I'm working on tweeking the look so that it, well, works. For those of you using the code snippets I'm putting on here, the main bits are:

In the main class:

public class miTanks extends Activity {

private GameView gv;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GameView gv = new GameView(this);
setContentView(gv);
}

In the GameView:

public class GameView extends View{

private final Paint PAINT;
private static final Bitmap[] TANK_IMAGES = new Bitmap[8];
private static final int[] TANK_COLOURS = new int[8];
private final GradientDrawable BACKGROUND_GRADIENT;
private static GameView INSTANCE;

/**
 * Initialises the GameView.
 * @param context
 */
public GameView(Context context) {
super(context);
setFocusable(true);
setFocusableInTouchMode(true);
...
/**
 * Draws the game onto the canvas.
 */
protected void onDraw(Canvas canvas){
drawBackground(canvas);
drawGround(canvas);
drawTanks(canvas);
}

In the main.xml:

<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <com.miTanks.game.GameView
  android:id="@+id/View01"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:maxHeight="40dip" />
 </LinearLayout>

3 comments:

  1. Hey that looks like Combat on the atari 2600

    ReplyDelete
  2. Interesting stuff, abit like minesweeper too

    ReplyDelete
  3. This is neat, I wish I could do it.

    ReplyDelete