From 783ea0518cca0a164d3fd48a62aa04ba54a064b6 Mon Sep 17 00:00:00 2001 From: gumartinm Date: Sun, 6 Nov 2011 22:26:23 +0100 Subject: [PATCH] First steps checking the collisions :/ --- AndroidTetris/project.properties | 11 +++++ .../src/de/android/androidtetris/DrawView.java | 54 ++++++++++++++++------ 2 files changed, 50 insertions(+), 15 deletions(-) create mode 100644 AndroidTetris/project.properties diff --git a/AndroidTetris/project.properties b/AndroidTetris/project.properties new file mode 100644 index 0000000..730e911 --- /dev/null +++ b/AndroidTetris/project.properties @@ -0,0 +1,11 @@ +# 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, +# "ant.properties", and override values to adapt the script to your +# project structure. + +# Project target. +target=android-14 diff --git a/AndroidTetris/src/de/android/androidtetris/DrawView.java b/AndroidTetris/src/de/android/androidtetris/DrawView.java index 513372c..2583f11 100644 --- a/AndroidTetris/src/de/android/androidtetris/DrawView.java +++ b/AndroidTetris/src/de/android/androidtetris/DrawView.java @@ -4,7 +4,6 @@ package de.android.androidtetris; import java.util.Random; - import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; @@ -20,7 +19,6 @@ public class DrawView extends SurfaceView { int position = 150; private SurfaceHolder holder; private AndroidTetrisThread gameLoopThread; - private int y = 0; private static final int TILESIZE=16; //now for the map... @@ -28,13 +26,8 @@ public class DrawView extends SurfaceView { private static final int MAPHEIGHT=30; private static final int GREY=8; - /** Indicate whether the surface has been created & is ready to draw */ - private boolean mRun = false; private AndroidTetrisThread thread; - - /** Handle to the surface manager object we interact with */ - private SurfaceHolder mSurfaceHolder; Bitmap[] tileArray; Tile[][] mapMatrix; @@ -62,8 +55,9 @@ public class DrawView extends SurfaceView { try { c = view.getHolder().lockCanvas(); synchronized (view.getHolder()) { - view.DrawMap(c); - //view.onDraw(c); + view.move(0, 1); + view.drawMap(c); + //view.onDraw(c); } } finally { if (c != null) { @@ -170,7 +164,6 @@ public class DrawView extends SurfaceView { 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) @@ -178,7 +171,7 @@ public class DrawView extends SurfaceView { canvas.drawBitmap(tileArray[color], x*TILESIZE, y*TILESIZE, null); } - protected void DrawMap(Canvas canvas) + protected void drawMap(Canvas canvas) { canvas.drawColor(Color.WHITE); @@ -203,9 +196,42 @@ public class DrawView extends SurfaceView { 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); - - + } + + protected void move (int x, int y) + { + if (this.collisionTest(x, y)) + { + this.newBlock(); + } + else + { + currentPiece.x += x; + currentPiece.y += y; + } + } + + protected boolean collisionTest(int cx, int cy) + { + int newx = currentPiece.x + cx; + int newy = currentPiece.y + cy; + + //Check boundaries + for(int x=0; x<4; x++) + for(int y=0; y<4; y++) + if(currentPiece.size[x][y] != Tile.NOCOLOR) + if (newy + y > MAPHEIGHT) + return true; + //Check collisions + for(int x=0; x< MAPWIDTH; x++) + for(int y=0; y< MAPHEIGHT; y++) + if(x >= newx && x < newx + 4) + if(y >= newy && y < newy +4) + if(mapMatrix[x][y] != Tile.BLACK) + if(currentPiece.size[x - newx][y - newy] != Tile.NOCOLOR) + return true; + return false; } @Override @@ -223,7 +249,5 @@ public class DrawView extends SurfaceView { piece.x+(xmy*TILESIZE), piece.y+aux+(ymx*TILESIZE), null); aux = aux + 64; } - - } } \ No newline at end of file -- 2.1.4