Improving my Android Simple Tetris.
authorgumartinm <gustavo@gumartinm.name>
Tue, 1 May 2012 23:31:49 +0000 (01:31 +0200)
committergumartinm <gustavo@gumartinm.name>
Tue, 1 May 2012 23:31:49 +0000 (01:31 +0200)
Now, with score and level.

Android/AndroidTetris/gen/de/android/androidtetris/R.java
Android/AndroidTetris/res/layout/main.xml
Android/AndroidTetris/res/values/strings.xml
Android/AndroidTetris/src/de/android/androidtetris/AndroidTetrisActivity.java
Android/AndroidTetris/src/de/android/androidtetris/DrawView.java
Android/AndroidTetris/src/de/android/androidtetris/GestureListener.java

index 1a06597..0fa0822 100644 (file)
@@ -17,6 +17,12 @@ public final class R {
         public static final int yellowstar=0x7f020003;
     }
     public static final class id {
+        public static final int button1=0x7f050003;
+        public static final int button2=0x7f050004;
+        public static final int button3=0x7f050006;
+        public static final int button4=0x7f050008;
+        public static final int frameLayout1=0x7f050005;
+        public static final int frameLayout2=0x7f050007;
         public static final int game_display=0x7f050002;
         public static final int level_display=0x7f050001;
         public static final int score_display=0x7f050000;
@@ -25,7 +31,11 @@ public final class R {
         public static final int main=0x7f030000;
     }
     public static final class string {
+        public static final int down=0x7f040004;
+        public static final int left=0x7f040003;
         public static final int level=0x7f040001;
+        public static final int right=0x7f040002;
+        public static final int rotate=0x7f040005;
         public static final int score=0x7f040000;
     }
 }
index 2b6b050..69b89ec 100644 (file)
         android:layout_height="wrap_content"
         >
         <TextView
-            android:id="@+id/score_display"
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:text="@string/score"
-            android:textSize="20dip"
-            android:typeface="monospace" >
-         </TextView>
+        android:id="@+id/score_display"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:text="@string/score"
+        android:textSize="20dip"
+        android:gravity="left"
+        android:typeface="monospace" />
 
         <TextView
-            android:id="@+id/level_display"
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:text="@string/level"
-            android:textSize="20dip"
-            android:typeface="monospace" />
+        android:id="@+id/level_display"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:text="@string/level"
+        android:textSize="20dip"
+        android:gravity="right"
+        android:typeface="monospace" />
+        
     </LinearLayout>
-
+    
     <de.android.androidtetris.DrawView
         android:id="@+id/game_display"
-        android:layout_width="267dp"
+        android:layout_width="fill_parent"
         android:layout_height="0dip"
         android:layout_weight="0.20" />
+    <LinearLayout
+        android:gravity="center"
+        android:orientation="vertical" 
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        >
+         <Button
+            android:id="@+id/button1"
+            android:onClick="onClickRotate"
+            android:layout_width="70dp"
+            android:layout_height="40dp"
+            android:layout_gravity="center"
+            android:text="@string/rotate" />
+    </LinearLayout>
+    <LinearLayout
+        android:orientation="horizontal" 
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        >
+
+        <Button
+            android:id="@+id/button2"
+            android:onClick="onClickLeft"
+            android:layout_width="70dp"
+            android:layout_height="70dp"
+            android:layout_gravity="left"
+            android:text="@string/left" />
+        <FrameLayout 
+        android:id="@+id/frameLayout1" 
+        android:layout_width="70dp" 
+        android:layout_height="wrap_content">
 
+
+        </FrameLayout>
+        <Button
+            android:id="@+id/button3"
+            android:onClick="onClickDown"
+            android:layout_width="70dp"
+            android:layout_height="40dp"
+            android:layout_gravity="center"
+            android:text="@string/down" />
+        <FrameLayout 
+        android:id="@+id/frameLayout2" 
+        android:layout_width="70dp" 
+        android:layout_height="wrap_content">
+
+
+        </FrameLayout>
+
+        <Button
+            android:id="@+id/button4"
+            android:onClick="onClickRight"
+            android:layout_width="70dp"
+            android:layout_height="70dp"
+            android:layout_gravity="right"
+            android:text="@string/right" />
+    </LinearLayout>
 </LinearLayout>
index ab72029..4eeee61 100644 (file)
@@ -2,4 +2,8 @@
 <resources>
     <string name="score">Score: </string>
     <string name="level">Level: </string>
+    <string name="right">Right </string>
+    <string name="left">Left </string>
+    <string name="down">Down </string>
+    <string name="rotate">Rotate </string>
 </resources>
index f855210..9f626b2 100644 (file)
@@ -3,6 +3,8 @@ package de.android.androidtetris;
 import android.app.Activity;
 import android.os.Bundle;
 import android.util.DisplayMetrics;
+import android.view.View;
+import android.widget.TextView;
 
 public class AndroidTetrisActivity extends Activity {
        DrawView drawView;
@@ -18,5 +20,43 @@ public class AndroidTetrisActivity extends Activity {
         //drawView.setDimensions(displayMetrics.widthPixels, displayMetrics.heightPixels);
         //this.setContentView(drawView);
         //drawView.requestFocus();
+        
+        runOnUiThread(new Runnable() {
+                       public void run() {
+               ((TextView)findViewById(R.id.score_display)).
+                                                               setText(getResources().getString(R.string.score) + "0000");
+               ((TextView)findViewById(R.id.level_display)).
+                                                               setText(getResources().getString(R.string.level) + "0000");
+            }
+               });
+    }
+    
+    @Override
+    public void onPause() {
+       super.onPause();
+    }
+    
+    public void onClickRight(View v) {
+       DrawView view = (DrawView)this.getCurrentFocus();
+       
+       view.onRightSwipe();
+    }
+    
+    public void onClickLeft(View v) {
+       DrawView view = (DrawView)this.getCurrentFocus();
+       
+       view.onLeftSwipe();
+    }
+    
+    public void onClickDown(View v) {
+       DrawView view = (DrawView)this.getCurrentFocus();
+       
+       view.onDownSwipe();
+    }
+    
+    public void onClickRotate(View v) {
+       DrawView view = (DrawView)this.getCurrentFocus();
+       
+       view.onTapUp(true);
     }
 }
\ No newline at end of file
index 6b7981c..2955239 100644 (file)
@@ -3,6 +3,7 @@
  */
 package de.android.androidtetris;
 
+import java.text.DecimalFormat;
 import java.util.Random;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
@@ -17,9 +18,10 @@ import android.view.KeyEvent;
 import android.view.MotionEvent;
 import android.view.SurfaceHolder;
 import android.view.SurfaceView;
+import android.widget.TextView;
 
 /**
- * @author gusarapo
+ * @author
  *
  */
 public class DrawView extends SurfaceView {
@@ -35,6 +37,11 @@ public class DrawView extends SurfaceView {
     private CurrentPiece currentPiece;
     private ExecutorService exec;
     private GestureDetector gestureDetector;
+    private final Context context;
+    private int score;
+    private int level;
+    private int timeout = 1000;
+    private final DecimalFormat sixDigits = new DecimalFormat("0000");
        
     private class MainLoop implements Runnable 
        {
@@ -55,7 +62,7 @@ public class DrawView extends SurfaceView {
                        } catch (InterruptedException e) {
                                        // This code implements the thread's interruption policy. It may swallow the
                                // interruption request. Java Concurrency in Practice ยง7.1.3
-                                       // TODO: I should finish the process in this case.
+                                       break;
                                }
                        synchronized (view.getHolder())
                        {
@@ -73,16 +80,19 @@ public class DrawView extends SurfaceView {
     public DrawView(final Context context) 
     {
        super(context);
+       this.context = context;
        this.initialize(context);
     }
 
     public DrawView(Context context, AttributeSet attrs) {
         super(context, attrs);
+        this.context = context;
         initialize(context);
     }
     
     public DrawView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
+       this.context = context;
        this.initialize(context);
     }
     
@@ -127,7 +137,7 @@ public class DrawView extends SurfaceView {
                        public void surfaceChanged(final SurfaceHolder holder, final int format, final int width, final int height) {
                
                        }
-        });      
+        });
     }
     private void resetTiles(int tilecount) {
        tileArray = new Bitmap[tilecount];
@@ -193,24 +203,24 @@ public class DrawView extends SurfaceView {
        //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 + initX, y + 2);
+                drawTile(canvas, Tile.GRAY.getColor(), x + initX, y + 1);
        
        //draw the pre-piece
        for(int x=0; x < PrePiece.WIDTH; x++)
                for(int y=0; y< PrePiece.HEIGHT; y++)
                        if(prePiece.size[x][y] != Tile.NOCOLOR)
-                    drawTile(canvas, prePiece.size[x][y].getColor(), prePiece.x + x + initX, prePiece.y + y + 2);
+                    drawTile(canvas, prePiece.size[x][y].getColor(), prePiece.x + x + initX, prePiece.y + y + 1);
        
        //draw grid
        for(int x=0; x < MAPWIDTH; x++)
                for(int y=0; y < MAPHEIGHT; y++)
-                drawTile(canvas, mapMatrix[x][y].getColor(), x + initX, y + 2);
+                drawTile(canvas, mapMatrix[x][y].getColor(), x + initX, y + 1);
 
        //draw the current block
        for(int x=0; x < CurrentPiece.WIDTH; x++)
                for(int y=0; y < CurrentPiece.HEIGHT; y++)
                        if(currentPiece.size[x][y] != Tile.NOCOLOR)
-                    drawTile(canvas, currentPiece.size[x][y].getColor(), currentPiece.x + x + initX, currentPiece.y +y + 2);
+                    drawTile(canvas, currentPiece.size[x][y].getColor(), currentPiece.x + x + initX, currentPiece.y +y + 1);
     }
     
     
@@ -239,8 +249,26 @@ public class DrawView extends SurfaceView {
                {
                        if (currentPiece.y == -1)
                 {
-                    //GAMEOVER
-                               startMap();     
+                               //GAME OVER
+                               startMap();
+                               this.score = 0;
+                               ((AndroidTetrisActivity)this.context).runOnUiThread(new Runnable() {
+                                               public void run() {
+                                       ((TextView)((AndroidTetrisActivity)DrawView.this.context).
+                                                                       findViewById(R.id.score_display)).
+                                                                                       setText(getResources().getString(R.string.score) + 
+                                                                                                                       sixDigits.format(score));
+                                   }
+                                       });
+                               this.level = 0;
+                               ((AndroidTetrisActivity)this.context).runOnUiThread(new Runnable() {
+                                               public void run() {
+                                       ((TextView)((AndroidTetrisActivity)DrawView.this.context).
+                                                                       findViewById(R.id.level_display)).
+                                                                                       setText(getResources().getString(R.string.level) + 
+                                                                                                                       sixDigits.format(level));
+                                   }
+                                       });
                 }
                 else
                 {
@@ -261,6 +289,27 @@ public class DrawView extends SurfaceView {
 
                                        if(filled)
                                        {
+                                               this.score++;
+                                               ((AndroidTetrisActivity)this.context).runOnUiThread(new Runnable() {
+                                                       public void run() {
+                                               ((TextView)((AndroidTetrisActivity)DrawView.this.context).
+                                                                               findViewById(R.id.score_display)).
+                                                                                               setText(getResources().getString(R.string.score) + 
+                                                                                                                               sixDigits.format(score));
+                                           }
+                                               });
+                                               if ((this.score % 10) == 0) {
+                                                       this.level++;
+                                                       ((AndroidTetrisActivity)this.context).runOnUiThread(new Runnable() {
+                                                               public void run() {
+                                                       ((TextView)((AndroidTetrisActivity)DrawView.this.context).
+                                                                                       findViewById(R.id.level_display)).
+                                                                                                       setText(getResources().getString(R.string.level) + 
+                                                                                                                                       sixDigits.format(level));
+                                                   }
+                                                       });
+                                                       this.timeout = this.timeout + this.level;
+                                               }
                                                removeRow(j);
                                        }
                                }
@@ -398,7 +447,14 @@ public class DrawView extends SurfaceView {
     
 
     public void onLeftSwipe() {
-            //userMoveLeft();
+       synchronized (this.getHolder())
+               {
+                       Canvas c = this.getHolder().lockCanvas();
+                       this.move(-1, 0);
+                       this.drawMap(c);
+                       //view.onDraw(c);
+                       this.getHolder().unlockCanvasAndPost(c);
+               }
     }
 
 
@@ -414,12 +470,24 @@ public class DrawView extends SurfaceView {
     }
 
     public void onTapUp(boolean onGround) {
-            //userRotate();
+       synchronized (this.getHolder())
+               {
+                       Canvas c = this.getHolder().lockCanvas();
+                       this.rotateBlock();
+                       this.drawMap(c);
+                       //view.onDraw(c);
+                       this.getHolder().unlockCanvasAndPost(c);
+               }
     }
 
     public void onDownSwipe() {
-            //userFallDown();
+       synchronized (this.getHolder())
+               {
+                       Canvas c = this.getHolder().lockCanvas();
+                       this.move(0,1);
+                       this.drawMap(c);
+                       //view.onDraw(c);
+                       this.getHolder().unlockCanvasAndPost(c);
+               }
     }
-
-
 }
index f31751c..f8c157f 100644 (file)
@@ -4,6 +4,9 @@ import android.view.GestureDetector.OnGestureListener;
 import android.view.MotionEvent;
 
 public class GestureListener implements OnGestureListener {
+       private static final int SWIPE_MIN_DISTANCE = 25;
+    private static final int SWIPE_MAX_OFF_PATH = 30;
+    private static final int SWIPE_THRESHOLD_VELOCITY = 150;
        private final DrawView drawView;
 
        public GestureListener(final DrawView drawView)
@@ -13,8 +16,18 @@ public class GestureListener implements OnGestureListener {
        
        @Override
        public boolean onDown(MotionEvent e) {
-               //this.drawView.onRightSwipe();
-               return false;
+//             float prueba;
+//             float gus;
+//             prueba=e.getX();
+//             gus = prueba;
+               if (e.getX() > 650) {
+                       this.drawView.onRightSwipe();
+                       return false;
+               } else if (e.getX() < 65) {
+                       this.drawView.onLeftSwipe();
+                       return false;
+               }
+               return true;
        }
 
        @Override
@@ -45,7 +58,15 @@ public class GestureListener implements OnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                        float velocityY) {
-               this.drawView.onRightSwipe();
+               // Check movement along the Y-axis. If it exceeds SWIPE_MAX_OFF_PATH, then dismiss the swipe.
+        if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
+            return false;
+        // right to left swipe
+        if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
+               this.drawView.onLeftSwipe();
+        } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
+               this.drawView.onRightSwipe();
+        }
                return false;
        }