From: gumartinm Date: Sun, 6 Nov 2011 20:10:01 +0000 (+0100) Subject: Current and next piece first steps. X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=e8cc1fb82709ec69d49243e3391613ad4f8080b6;p=JavaForFun Current and next piece first steps. --- diff --git a/AndroidTetris/default.properties b/AndroidTetris/default.properties deleted file mode 100644 index ac9e1a0..0000000 --- a/AndroidTetris/default.properties +++ /dev/null @@ -1,11 +0,0 @@ -# This file is automatically generated by Android Tools. -# Do not modify this file -- YOUR CHANGES WILL BE ERASED! -# -# This file must be checked in Version Control Systems. -# -# To customize properties used by the Ant build system use, -# "build.properties", and override values to adapt the script to your -# project structure. - -# Project target. -target=android-13 diff --git a/AndroidTetris/res/layout/main.xml b/AndroidTetris/res/layout/main.xml index 3a5f117..4361cfe 100644 --- a/AndroidTetris/res/layout/main.xml +++ b/AndroidTetris/res/layout/main.xml @@ -4,9 +4,4 @@ android:layout_width="fill_parent" android:layout_height="fill_parent" > - diff --git a/AndroidTetris/src/de/android/androidtetris/DrawView.java b/AndroidTetris/src/de/android/androidtetris/DrawView.java index b4b4e79..513372c 100644 --- a/AndroidTetris/src/de/android/androidtetris/DrawView.java +++ b/AndroidTetris/src/de/android/androidtetris/DrawView.java @@ -17,7 +17,6 @@ import android.view.SurfaceView; * */ public class DrawView extends SurfaceView { - private Random random = new Random();; int position = 150; private SurfaceHolder holder; private AndroidTetrisThread gameLoopThread; @@ -38,6 +37,9 @@ public class DrawView extends SurfaceView { private SurfaceHolder mSurfaceHolder; Bitmap[] tileArray; + Tile[][] mapMatrix; + Piece prePiece; + Piece currentPiece; class AndroidTetrisThread extends Thread { private DrawView view; @@ -60,7 +62,8 @@ public class DrawView extends SurfaceView { try { c = view.getHolder().lockCanvas(); synchronized (view.getHolder()) { - view.onDraw(c); + view.DrawMap(c); + //view.onDraw(c); } } finally { if (c != null) { @@ -84,11 +87,14 @@ public class DrawView extends SurfaceView { public DrawView(Context context) { super(context); - this.resetTiles(9); + this.resetTiles(10); for (Tile color : Tile.values() ) { this.loadTile(color.getColor(), color.getColorRGBA()); } + mapMatrix = new Tile[MAPWIDTH][MAPHEIGHT+1]; + this.NewGame(); + this.newBlock(); // register our interest in hearing about changes to our surface //SurfaceHolder holder = getHolder(); @@ -141,22 +147,71 @@ public class DrawView extends SurfaceView { tileArray[key] = bitmap; } + protected void NewGame() + { + //start out the map + for(int x=0;x< MAPWIDTH;x++) + { + for(int y=0;y< MAPHEIGHT+1;y++) + { + mapMatrix[x][y]=Tile.BLACK; + } + } + } + + protected void newBlock() + { + Random random = new Random(); + + currentPiece = Piece.getPiece(random.nextInt(7)%7); + currentPiece.x = MAPWIDTH/2-2; + currentPiece.y = -1; + + prePiece = Piece.getPiece(random.nextInt(7)%7); + prePiece.x=MAPWIDTH+2; + prePiece.y=GREY/4; + + } + + protected void drawTile(Canvas canvas, int color, int x, int y) + { + canvas.drawBitmap(tileArray[color], x*TILESIZE, y*TILESIZE, null); + } + + protected void DrawMap(Canvas canvas) + { + canvas.drawColor(Color.WHITE); + + //draw the left bar (with scores, and next pieces + for(int x=MAPWIDTH; x< MAPWIDTH+GREY; x++) + for(int y=0; y< MAPHEIGHT; y++) + drawTile(canvas, Tile.GRAY.getColor(), x, y); + + //draw the pre-piece + for(int x=0; x<4; x++) + for(int y=0; y<4; y++) + if(prePiece.size[x][y] != Tile.NOCOLOR) + drawTile(canvas, prePiece.size[x][y].getColor(), prePiece.x+x, prePiece.y +y); + + //draw grid + for(int x=0; x< MAPWIDTH; x++) + for(int y=0; y< MAPHEIGHT; y++) + drawTile(canvas, mapMatrix[x][y].getColor(), x, y); + + //draw the current block + for(int x=0; x<4; x++) + for(int y=0; y<4; y++) + if(currentPiece.size[x][y] != Tile.NOCOLOR) + drawTile(canvas, currentPiece.size[x][y].getColor(), currentPiece.x+x, currentPiece.y +y); + + + + } + @Override protected void onDraw(Canvas canvas) { canvas.drawColor(Color.BLACK); int aux = 0; - - //for (Bitmap bitmap : tileArray) - //{ - // if (y < this.getHeight() - bitmap.getHeight()) - // { - // y++; - // } - // else - // y = 0; - // canvas.drawBitmap(bitmap, aux, y, null); - // aux = aux + 20; - //} //draw moving block for (Piece piece : Piece.values()) diff --git a/AndroidTetris/src/de/android/androidtetris/Piece.java b/AndroidTetris/src/de/android/androidtetris/Piece.java index c5b28b9..af0b343 100644 --- a/AndroidTetris/src/de/android/androidtetris/Piece.java +++ b/AndroidTetris/src/de/android/androidtetris/Piece.java @@ -105,7 +105,7 @@ public enum Piece { this.y = 10; this.x = 10; - //Pre-Initialization of size matrix + //Pre-Initialization of matrix size for (int i=0; i< WIDTH; i++) for (int j=0; j< WIDTH; j++) size[i][j]= Tile.NOCOLOR; @@ -113,7 +113,7 @@ public enum Piece { this.refill(); } - public Piece getPiece (int pieceNumber) + public static final Piece getPiece (int pieceNumber) { return pieceMap.get(pieceNumber); } diff --git a/AndroidTetris/src/de/android/androidtetris/Tile.java b/AndroidTetris/src/de/android/androidtetris/Tile.java index 4cf309d..b8d07c1 100644 --- a/AndroidTetris/src/de/android/androidtetris/Tile.java +++ b/AndroidTetris/src/de/android/androidtetris/Tile.java @@ -55,7 +55,13 @@ public enum Tile { return Color.GRAY; } }, - NOCOLOR(8) { + BLACK(8) { + @Override + int getColorRGBA() { + return Color.BLACK; + } + }, + NOCOLOR(9) { @Override int getColorRGBA() { return Color.TRANSPARENT;