Now, with score and level.
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;
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;
}
}
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>
<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>
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;
//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
*/
package de.android.androidtetris;
+import java.text.DecimalFormat;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
+import android.widget.TextView;
/**
- * @author gusarapo
+ * @author
*
*/
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
{
} 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())
{
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);
}
public void surfaceChanged(final SurfaceHolder holder, final int format, final int width, final int height) {
}
- });
+ });
}
private void resetTiles(int tilecount) {
tileArray = new Bitmap[tilecount];
//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);
}
{
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
{
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);
}
}
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);
+ }
}
}
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);
+ }
}
-
-
}
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)
@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
@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;
}