GameStage intergration and cleanup

This commit is contained in:
Administrator
2014-02-18 15:55:17 +01:00
parent e01836e91c
commit a303e6afc6
6 changed files with 65 additions and 55 deletions
+65 -55
View File
@@ -272,10 +272,6 @@ public class CanvasView extends View
int NewBlock = 0;
int NewBlockPossition = 0;
boolean goingDown = false;
boolean NextWillLock = false;
Bitmap bitmapl1, bitmapl2, bitmapt, bitmapb, bitmaps, bitmapz, bitmapi1, bitmapi2, bitmapi3, bitmapi4,
bitmapi1r, bitmapi2r, bitmapi3r, bitmapi4r, bitmapblockline, bitmapscore, bitmapcontroller, bitmapgameover;
@@ -284,14 +280,10 @@ public class CanvasView extends View
// Score.
public int startLevel = 0;
int level = 0;
int lines = 0;
int score = 0;
// Game State.
boolean GamePlaying = false;
boolean GameOver = false;
final int GS_STOPPED = 0;
final int GS_LOADING = 1;
final int GS_PLAYING = 2;
@@ -355,28 +347,54 @@ public class CanvasView extends View
Width = outMetrics.widthPixels;
// Load Bitmap's.
LoadSprites();
LoadFont();
// Initlialize.
NewBlock = (int)(Math.random() * (7));
// Audio
mp = MediaPlayer.create(activity, R.raw.tetris_a);
// Start Game.
StartGame();
}
private void StartGame()
{
// Clear.
for ( int i=0; i<blockArray.length; i++)
{
for ( int i2=0; i2<blockArray[i].length; i2++)
{
blockArray[i][i2] = 0;
}
}
for ( int i=0; i<drawArray.length; i++)
{
for ( int i2=0; i2<drawArray[i].length; i2++)
{
drawArray[i][i2] = 0;
}
}
// Initlialize.
level = 0;
lines = 0;
score = 0;
CheckTetriminoSpawnArray();
NewBlock = Tetrimino.get(0);
Tetrimino.remove(0);
NewBlockPossition = 0;
SpawnBlock();
// Audio
mp = MediaPlayer.create(activity, R.raw.tetris_a);
mp.setLooping(true);
mp.start();
// Start Game.
GamePlaying = true;
GameState = GS_PLAYING;
handler.removeCallbacksAndMessages(null);
@@ -605,7 +623,6 @@ public class CanvasView extends View
public void onPause()
{
mp.pause();
GamePlaying = false;
GameState = GS_PAUSED;
}
@@ -741,7 +758,7 @@ public class CanvasView extends View
}
}
if (GameOver)
if (GameState == GS_GAMEOVER)
{
canvas.drawBitmap(bitmapgameover, (int)GetRealPixelSize(DIP_W, dpWidth, GameSpaceLeft),
(int)GetRealPixelSize(DIP_H, dpHeight, GameSpaceTop), null);
@@ -1240,7 +1257,7 @@ public class CanvasView extends View
if (!Released)
{
if (!GameOver)
if (GameState != GS_GAMEOVER)
{
if (moveX >= 28 && moveX <= 55 && moveY >= 143 && moveY <= 155) // Start
{
@@ -1249,19 +1266,20 @@ public class CanvasView extends View
else
{
mp.start();
GamePlaying = true;
GameState = GS_PLAYING;
}
}
}
}
if (!goingDown)
if (GameState == GS_PLAYING)
{
if (GameState == GS_PLAYING)
{
InputsGamePlaying(x, y, Released);
}
InputsGamePlaying(x, y, Released);
}
if (GameState == GS_GAMEOVER)
{
StartGame();
}
}
@@ -1332,43 +1350,30 @@ public class CanvasView extends View
{
if (GameState == GS_PLAYING)
{
if (!GameOver)
/// Can the Block go down?
if (CanBlockGoDown())
{
goingDown = true;
MoveBlockDown();
this.postInvalidate();
}
else
{
ParseBlockArrayToDrawArray();
/// Can the Block go down?
if (CanBlockGoDown())
CheckForCompletedRows();
//spawn new block
if (!SpawnBlock())
{
MoveBlockDown();
GameState = GS_GAMEOVER;
mp.stop();
this.postInvalidate();
}
else
{
NextWillLock = false;
ParseBlockArrayToDrawArray();
CheckForCompletedRows();
//spawn new block
if (!SpawnBlock())
{
// GameOver
GameOver = true;
GamePlaying = false;
GameState = GS_GAMEOVER ;
mp.stop();
this.postInvalidate();
}
else
{
this.postInvalidate();
}
this.postInvalidate();
}
goingDown = false;
}
}
}
@@ -1776,8 +1781,7 @@ public class CanvasView extends View
}
ArrayList<Integer> Tetrimino;
private boolean SpawnBlock()
private void CheckTetriminoSpawnArray()
{
// Pseudo random
if (Tetrimino == null || Tetrimino.size() <= 0)
@@ -1789,6 +1793,12 @@ public class CanvasView extends View
Tetrimino.add(newTetrimino);
}while (Tetrimino.size() < 7);
}
}
private boolean SpawnBlock()
{
CheckTetriminoSpawnArray();
CurrentBlock = NewBlock;
CurrentBlockPossition = NewBlockPossition;