Skip to content

Commit

Permalink
finished, but need score correction
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita Dyomin committed May 23, 2019
1 parent bcc26d1 commit 06e9047
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 29 deletions.
22 changes: 20 additions & 2 deletions app/src/main/java/com/tappydefender/EnemyShip.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/com/tappydefender/GameActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
}
}
11 changes: 11 additions & 0 deletions app/src/main/java/com/tappydefender/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
79 changes: 52 additions & 27 deletions app/src/main/java/com/tappydefender/TDView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<SpaceDust> dustList = new ArrayList<SpaceDust>();

private float distanceRemaining;
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 +
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 06e9047

Please sign in to comment.