Skip to content
package com.example.aritetrisburg;
class Coordinate {
int y, x;
Coordinate(int r, int c) {
this.y = r;
this.x = c;
}
static Coordinate add(Coordinate A, Coordinate B) {
return new Coordinate(A.y + B.y, A.x + B.x);
}
static Coordinate sub(Coordinate A, Coordinate B) {
return new Coordinate(A.y - B.y, A.x - B.x);
}
static Coordinate rotateAntiClock(Coordinate X) {
return new Coordinate(-X.x, X.y);
}
static boolean isEqual(Coordinate A, Coordinate B) {
return A.y == B.y && A.x == B.x;
}
}
package com.example.aritetrisburg;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
class DrawView extends View {
int yOffset;
Paint paint;
GameState gameState;
public DrawView(Context context, final GameState gameState) {
super(context);
paint = new Paint();
paint.setColor(Color.BLUE);
yOffset = 300;
this.gameState = gameState;
}
private int getBlockColorCode(int color) {
switch (color) {
case 1:
return Color.BLUE;
case 2:
return Color.YELLOW;
case 3:
return Color.RED;
case 4:
return Color.GREEN;
case 5:
return Color.CYAN;
case 6:
return Color.MAGENTA;
case 7:
return Color.DKGRAY;
default:
return Color.TRANSPARENT;
}
}
private void DrawMatrix(BasicBlock[][] matrix, Canvas canvas) {
for (int i = 0; i < 24; i++) {
for (int j = 0; j < 20; j++) {
if (matrix[i][j].state == BasicBlockState.ON_EMPTY)
continue;
int color = this.getBlockColorCode(matrix[i][j].colour);
Paint p = new Paint();
p.setColor(color);
canvas.drawRect(42 + j * 50, yOffset + i * 50 + 2, 88 + j * 50, yOffset + (i + 1) * 50 - 2, p);
}
}
}
private void Clear(BasicBlock[][] matrix, Canvas canvas) {
Paint p = new Paint();
p.setColor(Color.TRANSPARENT);
for (int i = 0; i < 24; i++) {
for (int j = 0; j < 20; j++) {
canvas.drawRect(42 + j * 50, yOffset + i * 50 + 2, 88 + j * 50, yOffset + (i + 1) * 50 - 2, p);
}
}
}
private void DrawTetramino(Tetramino tetramino, Canvas canvas) {
for (BasicBlock block : tetramino.blocks) {
int color = this.getBlockColorCode(block.colour);
Paint p = new Paint();
p.setColor(color);
canvas.drawRect(42 + block.coordinate.x * 50, yOffset + block.coordinate.y * 50 + 2, 88 + block.coordinate.x * 50, yOffset + (block.coordinate.y + 1) * 50 - 2, p);
}
}
private void Boundary(Canvas canvas) {
paint.setColor(Color.WHITE);
paint.setStrokeWidth(5f);
canvas.drawLine(40, yOffset, 40, yOffset + 1200, paint);
canvas.drawLine(40, yOffset, 1040, yOffset, paint);
canvas.drawLine(1040, yOffset, 1040, yOffset + 1200, paint);
canvas.drawLine(1040, yOffset + 1200, 40, yOffset + 1200, paint);
}
private void grid(Canvas canvas) {
paint.setStrokeWidth(2f);
for (int i = 90; i < 1040; i = i + 50) {
canvas.drawLine(i, yOffset, i, yOffset + 1200, paint);
}
for (int j = 50; j < 1200; j = j + 50) {
canvas.drawLine(40, yOffset + j, 1040, yOffset + j, paint);
}
}
private void PrintScore(int score, Canvas canvas) {
Paint paint = new Paint();
paint.setColor(Color.TRANSPARENT);
canvas.drawRect(0, 100, 200, 200, paint);
paint.setColor(Color.BLACK);
paint.setTextSize(100);
canvas.drawText(Integer.toString(score), 80, 170, paint);
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setColor(Color.BLACK);
paint.setStrokeWidth(5f);
Boundary(canvas);
grid(canvas);
if (gameState.status) {
Clear(gameState.board, canvas);
DrawMatrix(gameState.board, canvas);
DrawTetramino(gameState.falling, canvas);
PrintScore(gameState.score, canvas);
} else {
Paint paint = new Paint();
DrawMatrix(gameState.board, canvas);
DrawTetramino(gameState.falling, canvas);
paint.setColor(Color.BLACK);
paint.setTextSize(200);
canvas.drawText(getResources().getString(R.string.game_over), 60, 800, paint);
// Intent intent = new Intent(DrawView.class, GameOverScreen.class);
// startActivity(intent);
// finish();
PrintScore(gameState.score, canvas);
}
}
}
package com.example.aritetrisburg;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class EnterNameScreen extends AppCompatActivity {
Button toPlayButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.enter_name_screen);
toPlayButton = findViewById(R.id.btnName);
toPlayButton.setOnClickListener(v -> {
Intent intent = new Intent(EnterNameScreen.this, MainActivity.class);
startActivity(intent);
});
}
}
package com.example.aritetrisburg;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class GameOverScreen extends AppCompatActivity {
Button toHomeButton, toLeaderboard, toRestart;
// TextView first_place_points, second_place_points, third_place_points;
// TextView tvHighest;
// SharedPreferences sharedPreferences;
// ImageView ivNewHighest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game_over_screen);
toHomeButton = (Button) findViewById(R.id.backButton);
toHomeButton.setOnClickListener(v -> {
Intent intent1 = new Intent(GameOverScreen.this, HomeScreen.class);
startActivity(intent1);
});
toLeaderboard = (Button) findViewById(R.id.leaderboardButon);
toLeaderboard.setOnClickListener(v -> {
Intent intent2 = new Intent(GameOverScreen.this,ScoreLeaderboardScreen.class);
startActivity(intent2);
});
toRestart = (Button) findViewById(R.id.restartButon);
toRestart.setOnClickListener(v -> {
Intent intent3 = new Intent(GameOverScreen.this,MainActivity.class);
startActivity(intent3);
});
}
}
\ No newline at end of file
package com.example.aritetrisburg;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.SparseArray;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import com.example.aritetrisburg.SettingsScreen;
public class GameState extends SettingsScreen{
boolean status;
int score;
int scoreMultiplier;
// public int scoreLevel;
boolean pause;
//public CheckBox checkBoxLow, checkBoxMedium, checkBoxHigh;
// public RadioGroup radioGroupScore;
BasicBlock[][] board;
Tetramino falling;
boolean difficultMode;
private int rows;
private int columns;
private Integer ctr;
private SparseArray<Tetramino> tetraminos;
public SharedPreferences sp;
@Override
public void onCreate(Bundle savedInstanceState) {
scoreLevel = sharedPreferencesPoint.getInt("pointMagic",0);
// sp = getSharedPreferences("speedController", MODE_PRIVATE);
// scoreLevel = sharedPreferencesPoint.getInt("pointMagic", 99);
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_screen);
// checkBoxLow = findViewById(R.id.pointLow);
// checkBoxMedium = findViewById(R.id.pointMedium);
// checkBoxHigh = findViewById(R.id.pointHigh);
radioGroupScore = findViewById(R.id.pointLevel);
// int checkedButton = radioGroupScore.getCheckedRadioButtonId();
// switch (checkedButton) {
// case R.id.pointLow:
// Toast.makeText(GameState.this, "Married", Toast.LENGTH_SHORT).show();
// break;
// case R.id.pointMedium:
// Toast.makeText(GameState.this, "Single", Toast.LENGTH_SHORT).show();
// break;
// case R.id.pointHigh:
// Toast.makeText(GameState.this, "In a Relationship", Toast.LENGTH_SHORT).show();
// break;
// default:
// break;
// }
// radioGroupScore.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
// @Override
// public void onCheckedChanged(RadioGroup group, int checkedId) {
// switch (checkedId) {
// case R.id.pointLow:
// SharedPreferences.Editor editor1 = sp.edit();
// editor1.putInt("pointMagic", 1);
//// scoreLevel = sp.getInt("pointMagic",0);
// System.out.println(scoreLevel + " DICK");
//
// break;
// case R.id.pointMedium:
// SharedPreferences.Editor editor2 = sp.edit();
// editor2.putInt("pointMagic", 2);
//// scoreLevel = sp.getInt("speed", 1);
// System.out.println(scoreLevel + " DICK");
//
// break;
// case R.id.pointHigh:
// SharedPreferences.Editor editor3 = sp.edit();
// editor3.putInt("pointMagic", 3);
//// scoreLevel = sp.getInt("speed", 3);
// System.out.println(scoreLevel + " DICK");
// break;
// default:
// SharedPreferences.Editor editor4 = sp.edit();
// editor4.putInt("pointMagic", 9);
// System.out.println(scoreLevel + " DEFAULT");
//
// break;
// }
// }
// });
};
public GameState(int rows, int columns, TetraminoType fallingTetraminoType) {
this.rows = rows;
this.columns = columns;
this.pause = false;
ctr = 0;
score = 0;
this.status = true;
difficultMode = false;
board = new BasicBlock[rows][columns];
for (int row = 0; row < rows; row++) {
for (int column = 0; column < columns; column++) {
board[row][column] = new BasicBlock(row, column);
}
}
tetraminos = new SparseArray<>();
falling = new Tetramino(fallingTetraminoType, this.ctr);
tetraminos.put(this.ctr, falling);
}
private BasicBlock getCoordinateBlock(Coordinate coordinate) {
return this.board[coordinate.y][coordinate.x];
}
private boolean isConflicting(Coordinate coordinate) {
if (coordinate.x < 0 || coordinate.x >= this.columns || coordinate.y < 0 || coordinate.y >= this.rows)
return true;
return this.getCoordinateBlock(coordinate).state == BasicBlockState.ON_TETRAMINO;
}
private boolean canTetraminoDisplace(Tetramino tetramino, Coordinate displacement) {
for (BasicBlock block : tetramino.blocks) {
if (block.state == BasicBlockState.ON_TETRAMINO) {
Coordinate shifted = Coordinate.add(block.coordinate, displacement);
if (isConflicting(shifted)) {
return false;
}
}
}
return true;
}
boolean moveFallingTetraminoDown() {
if (canTetraminoDisplace(falling, new Coordinate(1, 0))) {
falling.moveDown();
return true;
} else {
return false;
}
}
boolean moveFallingTetraminoLeft() {
if (canTetraminoDisplace(falling, new Coordinate(0, -1))) {
falling.moveLeft();
return true;
} else {
return false;
}
}
boolean moveFallingTetraminoRight() {
if (canTetraminoDisplace(falling, new Coordinate(0, 1))) {
falling.moveRight();
return true;
} else {
return false;
}
}
boolean rotateFallingTetraminoAntiClock() {
if (falling.type == TetraminoType.SQUARE_SHAPED) {
return true;
} else {
for (BasicBlock block : falling.blocks) {
if (block.state == BasicBlockState.ON_EMPTY)
continue;
BasicBlock referenceBlock = falling.blocks[0];
Coordinate baseCoordinate = Coordinate.sub(block.coordinate, referenceBlock.coordinate);
if (isConflicting(Coordinate.add(Coordinate.rotateAntiClock(baseCoordinate), referenceBlock.coordinate))) {
return false;
}
}
falling.performClockWiseRotation();
return true;
}
}
void paintTetramino(Tetramino tetramino) {
for (BasicBlock block : tetramino.blocks) {
if (block.state == BasicBlockState.ON_EMPTY)
continue;
this.getCoordinateBlock(block.coordinate).set(block);
}
}
void pushNewTetramino(TetraminoType tetraminoType) {
this.ctr++;
falling = new Tetramino(tetraminoType, this.ctr);
this.tetraminos.put(this.ctr, falling);
for (BasicBlock block : falling.blocks) {
if (this.getCoordinateBlock(block.coordinate).state == BasicBlockState.ON_TETRAMINO)
this.status = false;
}
}
public int incrementScore() {
//scoreLevel = sharedPreferencesPoint.getInt("pointMagic",0);
// radioGroupScore.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
// @Override
// public void onCheckedChanged(RadioGroup group, int checkedId) {
// switch (checkedId) {
// case R.id.pointLow:
// scoreLevel = 1;
// break;
// case R.id.pointMedium:
// scoreLevel = 2;
// break;
// case R.id.pointHigh:
// scoreLevel = 3;
// break;
// default:
// break;
// }
// }
// });
// System.out.println(pointX_int);
System.out.println(scoreLevel);
if (scoreLevel == 0) {
return this.score += 100;
}
// return this.score+=(100 * pointX_int);
return this.score+=(100 * scoreLevel);
}
void lineRemove() {
boolean removeLines;
do {
removeLines = false;
for (int row = this.rows - 1; row >= 0; row--) {
boolean rowIsALine = true;
for (int column = 0; column < this.columns; column++) {
if (this.board[row][column].state != BasicBlockState.ON_TETRAMINO) {
rowIsALine = false;
break;
}
}
if (!rowIsALine) {
continue;
}
for (int column = 0; column < this.columns; column++) {
Tetramino tetramino = this.tetraminos.get((this.board[row][column].tetraId));
BasicBlock blockToClear = this.board[row][column];
blockToClear.setEmptyBlock(blockToClear.coordinate);
if (tetramino == null) {
continue;
}
for (BasicBlock block : tetramino.blocks) {
if (block.state == BasicBlockState.ON_EMPTY) {
continue;
}
if (block.coordinate.y == row && block.coordinate.x == column) {
block.state = BasicBlockState.ON_EMPTY;
this.ctr++;
Tetramino upperTetramino = tetramino.copy(this.ctr);
this.tetraminos.put(this.ctr, upperTetramino);
for (BasicBlock upperBlock : upperTetramino.blocks) {
if (upperBlock.coordinate.y >= block.coordinate.y) {
upperBlock.state = BasicBlockState.ON_EMPTY;
} else {
this.getCoordinateBlock(upperBlock.coordinate).tetraId = upperBlock.tetraId;
}
}
this.ctr++;
Tetramino lowerTetramino = tetramino.copy(this.ctr);
this.tetraminos.put(this.ctr, lowerTetramino);
for (BasicBlock lowerBlock : lowerTetramino.blocks) {
if (lowerBlock.coordinate.y <= block.coordinate.y) {
lowerBlock.state = BasicBlockState.ON_EMPTY;
} else {
this.getCoordinateBlock(lowerBlock.coordinate).tetraId = lowerBlock.tetraId;
}
}
this.tetraminos.remove(block.tetraId);
break;
}
}
}
this.adjustTheMatrix();
this.incrementScore();
removeLines = true;
break;
}
} while (removeLines);
}
private void adjustTheMatrix() {
for (int row = this.rows - 1; row >= 0; row--) {
for (int column = 0; column < this.columns; column++) {
Tetramino T = (this.tetraminos).get((this.board[row][column].tetraId));
if (T != null)
this.shiftTillBottom(T);
}
}
}
private void shiftTillBottom(Tetramino tetramino) {
boolean shiftTillBottom;
do {
boolean shouldShiftDown = true;
shiftTillBottom = false;
for (BasicBlock block : tetramino.blocks) {
if (block.state == BasicBlockState.ON_EMPTY)
continue;
Coordinate newCoordinate = Coordinate.add(block.coordinate, new Coordinate(1, 0));
if (isTetraPresent(newCoordinate, tetramino))
continue;
if (isConflicting(newCoordinate))
shouldShiftDown = false;
}
if (shouldShiftDown) {
for (BasicBlock block : tetramino.blocks) {
if (block.state == BasicBlockState.ON_EMPTY)
continue;
this.getCoordinateBlock(block.coordinate).setEmptyBlock(block.coordinate);
block.coordinate.y++;
}
for (BasicBlock block : tetramino.blocks) {
if (block.state == BasicBlockState.ON_EMPTY)
continue;
this.getCoordinateBlock(block.coordinate).set(block);
}
shiftTillBottom = true;
}
} while (shiftTillBottom);
}
private boolean isTetraPresent(Coordinate coordinate, Tetramino tetramino) {
for (BasicBlock block : tetramino.blocks) {
if (block.state == BasicBlockState.ON_EMPTY)
continue;
if (Coordinate.isEqual(block.coordinate, coordinate))
return true;
}
return false;
}
}
package com.example.aritetrisburg;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class HomeScreen extends AppCompatActivity {
Button toPlayButton, toSettingsButton, toRulesButton, toScoreLeaderBoardButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_screen);
toPlayButton = (Button) findViewById(R.id.playButton);
toSettingsButton = (Button) findViewById(R.id.settingsButton);
toRulesButton = (Button) findViewById(R.id.rulesButton);
toScoreLeaderBoardButton = (Button) findViewById(R.id.scoreLeaderboardButton);
toPlayButton.setOnClickListener(v -> {
Intent intent1 = new Intent(HomeScreen.this, EnterNameScreen.class);
startActivity(intent1);
});
toSettingsButton.setOnClickListener(v -> {
Intent intent2 = new Intent(HomeScreen.this, SettingsScreen.class);
startActivity(intent2);
});
toRulesButton.setOnClickListener(v -> {
Intent intent3 = new Intent(HomeScreen.this, RulesScreen.class);
startActivity(intent3);
});
toScoreLeaderBoardButton.setOnClickListener(v -> {
Intent intent4 = new Intent(HomeScreen.this, ScoreLeaderboardScreen.class);
startActivity(intent4);
});
}
}
\ No newline at end of file
package com.example.aritetrisburg;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements View.OnClickListener, GestureDetector.OnGestureListener {
DrawView drawView;
GameState gameState;
RelativeLayout gameButtons;
Button left;
Button right;
Button rotateAc;
FrameLayout game;
Button pause;
TextView score;
Button difficultyToggle;
//Button restart;
Handler handler;
Runnable loop;
int delayFactor;
int delay;
int scoreCounter;
int delayLowerLimit;
private static final String TAG = "Swipe Position";
private float x1, x2, y1, y2;
private static int MIN_DISTANCE = 400;
private GestureDetector gestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.song2);
mediaPlayer.start();
mediaPlayer.setLooping(true);
super.onCreate(savedInstanceState);
this.gestureDetector = new GestureDetector(MainActivity.this, this);
gameState = new GameState(24, 20, TetraminoType.getRandomTetramino());
drawView = new DrawView(this, gameState);
drawView.setBackgroundResource(R.drawable.background);
//drawView.setBackgroundColor(Color.BLACK);
game = new FrameLayout(this);
gameButtons = new RelativeLayout(this);
delay = 250;
delayLowerLimit = 200;
delayFactor = 2;
pause = new Button(this);
pause.setText(R.string.pause);
pause.setId(R.id.pause);
score = new TextView(this);
score.setText(R.string.score);
score.setId(R.id.score);
score.setTextSize(30);
difficultyToggle = new Button(this);
difficultyToggle.setText(R.string.easy);
difficultyToggle.setId(R.id.difficulty);
// restart = new Button(this);
// restart.setText(R.string.restart);
// restart.setId(R.id.restart);
RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);RelativeLayout.LayoutParams leftButton = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams pausebutton = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams scoretext = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams speedbutton = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
//RelativeLayout.LayoutParams restartbutton = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
gameButtons.setLayoutParams(rl);
gameButtons.addView(pause);
gameButtons.addView(score);
gameButtons.addView(difficultyToggle);
//gameButtons.addView(restart);
pausebutton.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
pausebutton.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
scoretext.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
scoretext.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
speedbutton.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
speedbutton.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
//restartbutton.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
//restartbutton.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
pause.setLayoutParams(pausebutton);
score.setLayoutParams(scoretext);
difficultyToggle.setLayoutParams(speedbutton);
//restart.setLayoutParams(restartbutton);
game.addView(drawView);
game.addView(gameButtons);
setContentView(game);
View pauseButtonListener = findViewById(R.id.pause);
pauseButtonListener.setOnClickListener(this);
View speedButtonListener = findViewById(R.id.difficulty);
speedButtonListener.setOnClickListener(this);
//__________________________________________________________________________________________________
// View restartButtonListener = findViewById(R.id.restart);
// restartButtonListener.setOnClickListener(this);
handler = new Handler(Looper.getMainLooper());
loop = new Runnable() {
public void run() {
if (gameState.status) {
if (!gameState.pause) {
boolean success = gameState.moveFallingTetraminoDown();
if (!success) {
gameState.paintTetramino(gameState.falling);
gameState.lineRemove();
gameState.pushNewTetramino(TetraminoType.getRandomTetramino());
if (gameState.score % 10 == 9 && delay >= delayLowerLimit) {
delay = delay / delayFactor + 1;
}
scoreCounter = gameState.incrementScore();
// /_____________________________________________________________________
// Intent intent1 = new Intent(MainActivity.this, ScoreLeaderboardScreen.class);
// intent1.putExtra("points", score);
// MainActivity.this.startActivity(intent1);
// ((Activity) MainActivity.this).finish();
}
drawView.invalidate();
handler.postDelayed(this, delay);
}
else {
handler.postDelayed(this, delay);
}
}
else {
//pause.setText(R.string.start_new_game);
Intent intent2 = new Intent(MainActivity.this, GameOverScreen.class);
startActivity(intent2);
mediaPlayer.stop();
finish();
}
}
};
loop.run();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
gestureDetector.onTouchEvent(event);
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
x1 = event.getX();
y1 = event.getY();
break;
case MotionEvent.ACTION_UP:
x2 = event.getX();
y2 = event.getY();
float valueX = x2-x1;
float valueY = y2-y1;
if(Math.abs(valueX)>MIN_DISTANCE){
if(x2>=x1 + MIN_DISTANCE){
//Toast.makeText(this, "Right Swipe", Toast.LENGTH_SHORT).show();
Log.d(TAG, "Right Swipe log");
gameState.moveFallingTetraminoRight();
} else if (x2<=x1 - MIN_DISTANCE){
//Toast.makeText(this, "Left Swipe", Toast.LENGTH_SHORT).show();
Log.d(TAG, "Left Swipe log");
gameState.moveFallingTetraminoLeft();
}
} else if (Math.abs(valueY)>MIN_DISTANCE){
if (y2>=y1 + MIN_DISTANCE){
//Toast.makeText(this, "Down Swipe", Toast.LENGTH_SHORT).show();
if (!gameState.difficultMode) {
delay = delay / delayFactor;
gameState.difficultMode = true;
difficultyToggle.setText(R.string.hard);
} else {
delay = delay * delayFactor;
difficultyToggle.setText(R.string.easy);
gameState.difficultMode = false;
}
Log.d(TAG, "Down Swipe log");
} else if (y2<=y1 - MIN_DISTANCE){
//Toast.makeText(this, "Up Swipe", Toast.LENGTH_SHORT).show();
Log.d(TAG, "Up Swipe log");
gameState.rotateFallingTetraminoAntiClock();
}
}
}
return super.onTouchEvent(event);
}
@Override
public boolean onDown(MotionEvent motionEvent) {
return false;
}
@Override
public void onShowPress(MotionEvent motionEvent) {
}
@Override
public boolean onSingleTapUp(MotionEvent motionEvent) {
return false;
}
@Override
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
return false;
}
@Override
public void onLongPress(MotionEvent motionEvent) {
}
@Override
public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
return false;
}
//_____________________________________________________________________________________________
// public void restart(View view){
// Intent intent3 = new Intent(MainActivity.this, MainActivity.class);
// startActivity(intent3);
// finish();
// }
@Override
public void onClick(View action) {
if (action == left) {
gameState.moveFallingTetraminoLeft();
} else if (action == right) {
gameState.moveFallingTetraminoRight();
} else if (action == rotateAc) {
gameState.rotateFallingTetraminoAntiClock();
} else if (action == pause) {
if (gameState.status) {
if (gameState.pause) {
gameState.pause = false;
pause.setText(R.string.pause);
} else {
pause.setText(R.string.play);
gameState.pause = true;
}
} else {
pause.setText(R.string.start_new_game);
Toast.makeText(getApplicationContext(), "Play Again or Restart", Toast.LENGTH_SHORT).show();
// ///RESTART THE GAME///
// // Restart game when start_new_game is pressed
// View startNewGameButtonListener = findViewById(R.id.pause);
// startNewGameButtonListener.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// // Stop the game loop
//// handler.removeCallbacks(loop);
////
//// // Reset the game state
//// gameState = new GameState(24, 20, TetraminoType.getRandomTetramino());
//// //drawView.setGameState(gameState);
////
//// //drawView = new DrawView(this, gameState);
//// drawView.setBackgroundResource(R.drawable.img);
////
//// // Reset the delay
//// delay = 50;
//// delayLowerLimit = 200;
//// delayFactor = 2;
////
//// // Reset the score
//// gameState.score = 0;
//// //score.setText(getString(R.string.score, 0);
////
//// // Restart the game loop
//// handler.postDelayed(loop, delay);
// }
// });
//
}
} else if (action == difficultyToggle) {
if (!gameState.difficultMode) {
delay = delay / delayFactor;
gameState.difficultMode = true;
difficultyToggle.setText(R.string.hard);
} else {
delay = delay * delayFactor;
difficultyToggle.setText(R.string.easy);
gameState.difficultMode = false;
}
}
// else if (action == restart){
// Intent intent4 = new Intent(MainActivity.this, MainActivity.class);
// startActivity(intent4);
// finish();
// ___________________________
// handler.removeCallbacks(loop);
//
// // Reset the game state
// gameState = new GameState(24, 20, TetraminoType.getRandomTetramino());
// //drawView.setGameState(gameState);
//
// //drawView = new DrawView(this, gameState);
// drawView.setBackgroundResource(R.drawable.img);
//
// // Reset the delay
// delay = 50;
// delayLowerLimit = 200;
// delayFactor = 2;
//
// // Reset the score
// gameState.score = 0;
// //score.setText(getString(R.string.score, 0);
//
// // Restart the game loop
// handler.postDelayed(loop, delay);
}
}
package com.example.aritetrisburg;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class PlayScreen extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.play_screen);
}
}
\ No newline at end of file
package com.example.aritetrisburg;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class RulesScreen extends AppCompatActivity {
Button toHomeButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rules_screen);
toHomeButton = (Button) findViewById(R.id.backButton);
toHomeButton.setOnClickListener(v -> {
Intent intent = new Intent(RulesScreen.this,HomeScreen.class);
startActivity(intent);
});
}
}
\ No newline at end of file
package com.example.aritetrisburg;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class ScoreLeaderboardScreen extends AppCompatActivity {
Button toHomeButton;
TextView first_place_points, second_place_points, third_place_points;
//TextView tvHighest;
SharedPreferences sharedPreferences;
//ImageView ivNewHighest;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.score_leaderboard_screen);
/////
// first_place_points = findViewById(R.id.txtScore1);
// second_place_points = findViewById(R.id.txtScore2);
// third_place_points = findViewById(R.id.txtScore3);
//
//
// int points = getIntent().getExtras().getInt("points");
// second_place_points.setText("" + points);
//
// sharedPreferences = getSharedPreferences("my_pref", 0);
// int highest = sharedPreferences.getInt("highest", 0);
// if (points > highest){
// second_place_points.setVisibility(View.VISIBLE);
// highest = points;
// SharedPreferences.Editor editor = sharedPreferences.edit();
// editor.putInt("highest", highest);
// editor.commit();
// }
//
// second_place_points.setText("" + highest);
////
toHomeButton = findViewById(R.id.btnLeaderboard);
toHomeButton.setOnClickListener(v -> {
Intent intent = new Intent(ScoreLeaderboardScreen.this, HomeScreen.class);
startActivity(intent);
});
}
}
\ No newline at end of file
package com.example.aritetrisburg;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import androidx.appcompat.app.AppCompatActivity;
public class SettingsScreen extends AppCompatActivity {
Button toHomeButton;
//public SeekBar pointX, speedX;
//public int pointX_int;
public static int scoreLevel;
public int speedX_int;
public RadioGroup radioGroupScore;
public int scoreMultiplier;
public SharedPreferences sharedPreferencesPoint, sharedPreferencesSpeed;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_screen);
radioGroupScore = findViewById(R.id.pointLevel);
toHomeButton = (Button) findViewById(R.id.btnSetting);
// pointX = (SeekBar) findViewById(R.id.seekPoint);
// speedX = (SeekBar) findViewById(R.id.seekSpeed);
// pointX.setMax(100);
// speedX.setMax(100);
sharedPreferencesPoint = getSharedPreferences("speedController", Context.MODE_PRIVATE);
scoreLevel = sharedPreferencesPoint.getInt("pointMagic", 99);
sharedPreferencesSpeed = getPreferences(Context.MODE_PRIVATE);
// pointX_int = sharedPreferencesPoint.getInt("last_position_point", 1);
// speedX_int = sharedPreferencesSpeed.getInt("last_position_speed", 1);
//
// pointX.setProgress(pointX_int);
// speedX.setProgress(speedX_int);
radioGroupScore.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.pointLow:
SharedPreferences.Editor editor1 = sharedPreferencesPoint.edit();
editor1.putInt("pointMagic", 1);
editor1.apply();
// scoreLevel = sp.getInt("pointMagic",0);
System.out.println(scoreLevel);
break;
case R.id.pointMedium:
SharedPreferences.Editor editor2 = sharedPreferencesPoint.edit();
editor2.putInt("pointMagic", 2);
editor2.apply();
// scoreLevel = sp.getInt("speed", 1);
System.out.println(scoreLevel);
break;
case R.id.pointHigh:
SharedPreferences.Editor editor3 = sharedPreferencesPoint.edit();
editor3.putInt("pointMagic", 3);
editor3.apply();
// scoreLevel = sp.getInt("speed", 3);
System.out.println(scoreLevel);
break;
default:
SharedPreferences.Editor editor4 = sharedPreferencesPoint.edit();
editor4.putInt("pointMagic", 9);
editor4.apply();
System.out.println(scoreLevel);
break;
}
}
});
// pointX.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
// @Override
// public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// //System.out.println(store_input_point(progress));
// pointX_int = progress;
// System.out.println(pointX_int + " first");
// }
//
// @Override
// public void onStartTrackingTouch(SeekBar seekBar) {
// pointX_int = seekBar.getProgress();
// scoreMultiplier = pointX_int;
// System.out.println(pointX_int + " second");
//
// }
//
// @Override
// public void onStopTrackingTouch(SeekBar seekBar) {
// SharedPreferences.Editor editor = sharedPreferencesPoint.edit();
// editor.putInt("last_position_point", seekBar.getProgress());
// editor.apply();
//// pointX_int = seekBar.getProgress();
//// scoreMultiplier = pointX_int;
//// System.out.println(pointX_int + " third");
//
//
// }
// });
// speedX.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
// @Override
// public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// //System.out.println(store_input_point(progress));
//
// }
//
// @Override
// public void onStartTrackingTouch(SeekBar seekBar) {
//
// }
//
// @Override
// public void onStopTrackingTouch(SeekBar seekBar) {
// SharedPreferences.Editor editor = sharedPreferencesSpeed.edit();
// editor.putInt("last_position_speed", seekBar.getProgress());
// editor.apply();
// speedX_int = seekBar.getProgress();
// System.out.println(speedX_int);
// }
// });
toHomeButton.setOnClickListener(v -> {
Intent intent = new Intent(SettingsScreen.this, HomeScreen.class);
startActivity(intent);
// System.out.println(pointX_int);
});
}
// public int store_input_point(int i)
// {
// return i;
// }
// @Override
// protected void onDestroy() {
// super.onDestroy();
//
// // Store the last position in shared preferences when the activity is destroyed
// SharedPreferences.Editor editorPoint = sharedPreferencesPoint.edit();
// editorPoint.putInt("last_position_point", pointX.getProgress());
// editorPoint.apply();
// System.out.println(pointX_int);
//
//
// SharedPreferences.Editor editorSpeed = sharedPreferencesSpeed.edit();
// editorSpeed.putInt("last_position_speed", pointX.getProgress());
// editorSpeed.apply();
// }
}
\ No newline at end of file
package com.example.aritetrisburg;
import java.util.Random;
enum TetraminoType {
SQUARE_SHAPED,
CHERRY,//
BANANA, //
STRAWBERRY, //
CUCUMBER , //
ORANGE, //
PEAR;
private static final TetraminoType[] VALUES = values();
private static final int SIZE = VALUES.length;
private static final Random RANDOM = new Random();
public static TetraminoType getRandomTetramino() {
return VALUES[RANDOM.nextInt(SIZE)];
}
}
class Tetramino {
BasicBlock[] blocks;
TetraminoType type;
Tetramino(TetraminoType type, int tetraId) {
Coordinate[] coordinates;
switch (type) {
case SQUARE_SHAPED:
coordinates = new Coordinate[]{
new Coordinate(0, 10),
new Coordinate(1, 10),
new Coordinate(1, 11),
new Coordinate(0, 11)
};
blocks = this.blocksGenerator(tetraId, 1, coordinates);
break;
case CHERRY:
coordinates = new Coordinate[]{
new Coordinate(0, 10),
new Coordinate(1, 10),
new Coordinate(2, 10),
new Coordinate(3, 10),
new Coordinate(4, 10),
new Coordinate(0, 9),
new Coordinate(3, 9),
new Coordinate(4, 9),
new Coordinate(3, 11),
new Coordinate(4, 11),
};
blocks = this.blocksGenerator(tetraId, 2, coordinates);
break;
case CUCUMBER:
coordinates = new Coordinate[]{
new Coordinate(0, 10),
new Coordinate(1, 10),
new Coordinate(2, 10),
new Coordinate(3, 10),
new Coordinate(4, 10),
};
blocks = this.blocksGenerator(tetraId, 3, coordinates);
break;
case STRAWBERRY:
coordinates = new Coordinate[]{
new Coordinate(0, 10),
new Coordinate(1, 10),
new Coordinate(2, 10),
new Coordinate(1, 9),
new Coordinate(2, 9),
new Coordinate(1, 11),
new Coordinate(2, 11),
new Coordinate(3, 10),
};
blocks = this.blocksGenerator(tetraId, 4, coordinates);
break;
case ORANGE:
coordinates = new Coordinate[]{
new Coordinate(0, 10),
new Coordinate(1, 10),
new Coordinate(2, 10),
new Coordinate(0, 9),
new Coordinate(0, 11),
new Coordinate(1, 9),
new Coordinate(1, 11),
new Coordinate(2, 9),
new Coordinate(2, 11),
};
blocks = this.blocksGenerator(tetraId, 5, coordinates);
break;
case BANANA:
coordinates = new Coordinate[]{
new Coordinate(0, 10),
new Coordinate(0, 9),
new Coordinate(1, 9),
new Coordinate(2, 9),
new Coordinate(3, 9),
new Coordinate(4, 9),
new Coordinate(4, 10),
new Coordinate(5, 10),
};
blocks = this.blocksGenerator(tetraId, 6, coordinates);
break;
case PEAR:
coordinates = new Coordinate[]{
new Coordinate(0, 10),
new Coordinate(1, 10),
new Coordinate(2, 10),
new Coordinate(1, 9),
new Coordinate(2, 9),
new Coordinate(1, 11),
new Coordinate(2, 11),
};
blocks = this.blocksGenerator(tetraId, 7, coordinates);
break;
}
}
private Tetramino(BasicBlock[] blocks) {
this.blocks = blocks;
}
private BasicBlock[] blocksGenerator(int tetraId, int colour, Coordinate[] coordinates) {
BasicBlock[] blocks = new BasicBlock[coordinates.length];
for (int itr = 0; itr < coordinates.length; itr++) {
blocks[itr] = new BasicBlock(colour, tetraId, coordinates[itr], BasicBlockState.ON_TETRAMINO);
}
return blocks;
}
Tetramino copy(int tetraId) {
BasicBlock[] newBlocks = new BasicBlock[this.blocks.length];
for (int itr = 0; itr < this.blocks.length; itr++) {
newBlocks[itr] = this.blocks[itr].copy();
newBlocks[itr].tetraId = tetraId;
}
return new Tetramino(newBlocks);
}
void moveDown() {
for (BasicBlock block : blocks) {
block.coordinate.y++;
}
}
void moveLeft() {
for (BasicBlock block : blocks) {
block.coordinate.x--;
}
}
void moveRight() {
for (BasicBlock block : blocks) {
block.coordinate.x++;
}
}
void performClockWiseRotation() {
BasicBlock referenceBlock = blocks[0];
for (BasicBlock block : blocks) {
Coordinate baseCoordinate = Coordinate.sub(block.coordinate, referenceBlock.coordinate);
block.coordinate = Coordinate.add(Coordinate.rotateAntiClock(baseCoordinate), referenceBlock.coordinate);
}
}
}
package com.example.toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
String[] toasts = {"Wanna hear a joke?", "It's quiz time! Let's meet at PHO.", "You guys look tired, let's take a break."};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onStart() {
super.onStart();
int ii = 0;
while (true) {
try {
Thread.sleep(5000);
Toast.makeText(this,toasts[ii],Toast.LENGTH_SHORT).show();
if (ii<2) {
ii++;
} else {
ii = 0;
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
</LinearLayout>
\ No newline at end of file
tools:context=".MainActivity"
android:background="@drawable/background"
>
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/peach_background"
tools:context=".EnterNameScreen"
android:layout_gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/dbxlnightfever_normal"
android:text="ENTER YOUR NAME"
android:textSize="50dp"
android:textColor="@color/black"
android:layout_marginTop="150dp"
android:layout_gravity="center" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="name"
android:id="@+id/edtTxtName"
android:textStyle="italic|bold"
android:fontFamily="@font/dbxlnightfever_normal"
android:textSize="25dp"
android:textColor="@color/black"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layout_marginTop="100dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnName"
android:fontFamily="@font/dbxlnightfever_normal"
android:text="Start Game"
android:textSize="30dp"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:backgroundTint="@color/lavender_btn"/>
</LinearLayout>
\ No newline at end of file