From 06e90473db99e61af8f84f20681686739c44c5e5 Mon Sep 17 00:00:00 2001 From: Nikita Dyomin Date: Fri, 24 May 2019 00:16:46 +0300 Subject: [PATCH] finished, but need score correction --- .../java/com/tappydefender/EnemyShip.java | 22 +++++- .../java/com/tappydefender/GameActivity.java | 11 +++ .../java/com/tappydefender/MainActivity.java | 11 +++ .../main/java/com/tappydefender/TDView.java | 79 ++++++++++++------- 4 files changed, 94 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/com/tappydefender/EnemyShip.java b/app/src/main/java/com/tappydefender/EnemyShip.java index 8cb32a5..e58bb62 100644 --- a/app/src/main/java/com/tappydefender/EnemyShip.java +++ b/app/src/main/java/com/tappydefender/EnemyShip.java @@ -37,8 +37,7 @@ public EnemyShip(Context context, int screenX, int screenY) bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.enemy); break; } - bitmap = bitmap.createScaledBitmap(bitmap, (int)(bitmap.getWidth()*0.6), - (int)(bitmap.getHeight()*0.6), false); + scaleBitmap(screenX); maxX = screenX; minX = 0; @@ -54,6 +53,25 @@ public EnemyShip(Context context, int screenX, int screenY) hitBox = new Rect(x, y, x+bitmap.getWidth(), y+bitmap.getHeight()); } + private void scaleBitmap(int screenX) + { + bitmap = Bitmap.createScaledBitmap(bitmap,(int)(bitmap.getWidth()*0.6), + (int)(bitmap.getHeight()*0.6),false); + if (screenX < 1000) + { + bitmap = Bitmap.createScaledBitmap(bitmap, + bitmap.getWidth() / 3, + bitmap.getHeight() / 3, + false); + } else if (screenX < 1200) + { + bitmap = Bitmap.createScaledBitmap(bitmap, + bitmap.getWidth() / 2, + bitmap.getHeight() / 2, + false); + } + } + public void update(int playerSpeed) { x -= playerSpeed; diff --git a/app/src/main/java/com/tappydefender/GameActivity.java b/app/src/main/java/com/tappydefender/GameActivity.java index 7bd00b7..05a6bcf 100644 --- a/app/src/main/java/com/tappydefender/GameActivity.java +++ b/app/src/main/java/com/tappydefender/GameActivity.java @@ -4,6 +4,7 @@ import android.graphics.Point; import android.os.Bundle; import android.view.Display; +import android.view.KeyEvent; public class GameActivity extends Activity { @@ -33,4 +34,14 @@ protected void onResume() super.onResume(); gameView.resume(); } + + public boolean onKeyDown(int keyCode, KeyEvent event) + { + if (keyCode == KeyEvent.KEYCODE_BACK) + { + finish(); + return true; + } + return false; + } } diff --git a/app/src/main/java/com/tappydefender/MainActivity.java b/app/src/main/java/com/tappydefender/MainActivity.java index e52e02f..d3c8a29 100644 --- a/app/src/main/java/com/tappydefender/MainActivity.java +++ b/app/src/main/java/com/tappydefender/MainActivity.java @@ -4,6 +4,7 @@ import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; +import android.view.KeyEvent; import android.view.View; import android.widget.Button; import android.widget.TextView; @@ -33,4 +34,14 @@ public void onClick(View v) startActivity(i); finish(); } + + public boolean onKeyDown(int keyCode, KeyEvent event) + { + if (keyCode == KeyEvent.KEYCODE_BACK) + { + finish(); + return true; + } + return false; + } } \ No newline at end of file diff --git a/app/src/main/java/com/tappydefender/TDView.java b/app/src/main/java/com/tappydefender/TDView.java index 703a3b1..c126fa6 100644 --- a/app/src/main/java/com/tappydefender/TDView.java +++ b/app/src/main/java/com/tappydefender/TDView.java @@ -28,6 +28,8 @@ public class TDView extends SurfaceView implements Runnable private EnemyShip enemy1; private EnemyShip enemy2; private EnemyShip enemy3; + public EnemyShip enemy4; + public EnemyShip enemy5; public ArrayList dustList = new ArrayList(); private float distanceRemaining; @@ -103,6 +105,15 @@ private void startGame() enemy2 = new EnemyShip(context, screenX, screenY); enemy3 = new EnemyShip(context, screenX, screenY); + if(screenX > 1000) + { + enemy4 = new EnemyShip(context, screenX, screenY); + } + if(screenX > 1200) + { + enemy5 = new EnemyShip(context, screenX, screenY); + } + int numSpecs = 150; for (int i = 0; i < numSpecs; i++) dustList.add(new SpaceDust(screenX, screenY)); @@ -150,6 +161,22 @@ private void update() enemy3.setX(-300); hitDetected = true; } + if(screenX > 1000) + { + if(Rect.intersects(player.getHitbox(), enemy4.getHitbox())) + { + hitDetected = true; + enemy4.setX(-100); + } + } + if(screenX > 1200) + { + if(Rect.intersects(player.getHitbox(), enemy5.getHitbox())) + { + hitDetected = true; + enemy5.setX(-100); + } + } if(hitDetected) { player.reduceShieldStrength(); @@ -191,41 +218,28 @@ private void draw() for (SpaceDust sd : dustList) canvas.drawPoint(sd.getX(), sd.getY(), paint); - paint.setColor(Color.argb(255, 255, 255, 255)); -// Draw Hit boxes - canvas.drawRect(player.getHitbox().left, - player.getHitbox().top, - player.getHitbox().right, - player.getHitbox().bottom, - paint); - canvas.drawRect(enemy1.getHitbox().left, - enemy1.getHitbox().top, - enemy1.getHitbox().right, - enemy1.getHitbox().bottom, - paint); - canvas.drawRect(enemy2.getHitbox().left, - enemy2.getHitbox().top, - enemy2.getHitbox().right, - enemy2.getHitbox().bottom, - paint); - canvas.drawRect(enemy3.getHitbox().left, - enemy3.getHitbox().top, - enemy3.getHitbox().right, - enemy3.getHitbox().bottom, - paint); - canvas.drawBitmap(player.getBitmap(), player.getX(), player.getY(), paint); canvas.drawBitmap(enemy1.getBitmap(), enemy1.getX(), enemy1.getY(), paint); canvas.drawBitmap(enemy2.getBitmap(), enemy2.getX(), enemy2.getY(), paint); canvas.drawBitmap(enemy3.getBitmap(), enemy3.getX(), enemy3.getY(), paint); + if(screenX > 1000) + { + canvas.drawBitmap(enemy4.getBitmap(), + enemy4.getX(), enemy4.getY(), paint); + } + if(screenX > 1200) + { + canvas.drawBitmap(enemy5.getBitmap(), + enemy5.getX(), enemy5.getY(), paint); + } if(!gameEnded) { paint.setTextAlign(Paint.Align.LEFT); paint.setColor(Color.argb(255, 255, 255, 255)); paint.setTextSize(40); - canvas.drawText("Fastest:" + fastestTime + "s", 10, 40, paint); - canvas.drawText("Time:" + timeTaken + "s", screenX / 2, 40, + canvas.drawText("Fastest:" + formatTime(fastestTime) + "s", 10, 40, paint); + canvas.drawText("Time:" + formatTime(timeTaken) + "s", screenX / 2, 40, paint); canvas.drawText("Distance:" + distanceRemaining / 1000 + @@ -243,8 +257,8 @@ private void draw() canvas.drawText("Game Over", screenX/2, 100, paint); paint.setTextSize(40); canvas.drawText("Fastest:"+ - fastestTime + "s", screenX/2, 160, paint); - canvas.drawText("Time:" + timeTaken + + formatTime(fastestTime) + "s", screenX/2, 160, paint); + canvas.drawText("Time:" + formatTime(timeTaken) + "s", screenX / 2, 200, paint); canvas.drawText("Distance remaining:" + distanceRemaining/1000 + " KM",screenX/2, 240, paint); @@ -296,4 +310,15 @@ public boolean onTouchEvent(MotionEvent motionEvent) } return true; } + + private String formatTime(long time) + { + long seconds = (time) / 1000; + long thousandths = (time) - (seconds * 1000); + String strThousandths = "" + thousandths; + if (thousandths < 100){strThousandths = "0" + thousandths;} + if (thousandths < 10){strThousandths = "0" + strThousandths;} + String stringTime = "" + seconds + "." + strThousandths; + return stringTime; + } }