Skip to content

Commit

Permalink
second commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita Dyomin committed May 23, 2019
1 parent 01065a7 commit bcc26d1
Show file tree
Hide file tree
Showing 11 changed files with 218 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added app/src/main/assets/bump.ogg
Binary file not shown.
Binary file added app/src/main/assets/destroyed.ogg
Binary file not shown.
Binary file added app/src/main/assets/start.ogg
Binary file not shown.
Binary file added app/src/main/assets/win.ogg
Binary file not shown.
18 changes: 16 additions & 2 deletions app/src/main/java/com/tappydefender/EnemyShip.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,29 @@ public class EnemyShip

public EnemyShip(Context context, int screenX, int screenY)
{
bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.enemy);
Random generator = new Random();
int whichBitmap = generator.nextInt(3);
switch (whichBitmap)
{
case 0:
bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.enemy3);
break;
case 1:
bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.enemy2);
break;
case 2:
bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.enemy);
break;
}
bitmap = bitmap.createScaledBitmap(bitmap, (int)(bitmap.getWidth()*0.6),
(int)(bitmap.getHeight()*0.6), false);

maxX = screenX;
minX = 0;

maxY = screenY;
minY = 0;

Random generator = new Random();
speed = generator.nextInt(6)+10;
x = screenX;
y = generator.nextInt(maxY) - bitmap.getHeight();
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/java/com/tappydefender/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements View.OnClickListener
{
Expand All @@ -15,6 +17,13 @@ protected void onCreate(Bundle savedInstanceState)
setContentView(R.layout.activity_main);
Button playBtn = findViewById(R.id.playBtn);
playBtn.setOnClickListener(this);

SharedPreferences prefs;
SharedPreferences.Editor editor;
prefs = getSharedPreferences("HiScores", MODE_PRIVATE);
final TextView textFastestTime = (TextView)findViewById(R.id.scoreTextView);
long fastestTime = prefs.getLong("fastestTime", 1000000);
textFastestTime.setText("Fastest Time:" + fastestTime);
}

@Override
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/com/tappydefender/PlayerShip.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class PlayerShip
private int x, y;
private int speed;
private boolean boosting;
private int shieldStrength;

private final int GRAVITY = -12;

Expand Down Expand Up @@ -91,4 +92,14 @@ public void stopBoosting()
{
boosting = false;
}

public int getShieldStrength()
{
return shieldStrength;
}

public void reduceShieldStrength()
{
shieldStrength --;
}
}
180 changes: 176 additions & 4 deletions app/src/main/java/com/tappydefender/TDView.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package com.tappydefender;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.media.AudioManager;
import android.media.SoundPool;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.io.IOException;
import java.util.ArrayList;

public class TDView extends SurfaceView implements Runnable
Expand All @@ -22,26 +30,86 @@ public class TDView extends SurfaceView implements Runnable
private EnemyShip enemy3;
public ArrayList<SpaceDust> dustList = new ArrayList<SpaceDust>();

private float distanceRemaining;
private long timeTaken;
private long timeStarted;
private long fastestTime;
private boolean gameEnded;

// For drawing
private Paint paint;
private Canvas canvas;
private SurfaceHolder ourHolder;

private int screenX;
private int screenY;

private Context context;

private SoundPool soundPool;
int start = -1;
int bump = -1;
int destroyed = -1;
int win = -1;

private SharedPreferences prefs;
private SharedPreferences.Editor editor;

public TDView(Context context, int screenX, int screenY)
{
super(context);
this.context = context;
ourHolder = getHolder();
paint = new Paint();

prefs = context.getSharedPreferences("HiScores", context.MODE_PRIVATE);
editor = prefs.edit();
fastestTime = prefs.getLong("fastestTime", 1000000);

this.screenX = screenX;
this.screenY = screenY;

soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC,0);
try
{
AssetManager assetManager = context.getAssets();
AssetFileDescriptor descriptor;

descriptor = assetManager.openFd("start.ogg");
start = soundPool.load(descriptor, 0);
descriptor = assetManager.openFd("win.ogg");
win = soundPool.load(descriptor, 0);
descriptor = assetManager.openFd("bump.ogg");
bump = soundPool.load(descriptor, 0);
descriptor = assetManager.openFd("destroyed.ogg");
destroyed = soundPool.load(descriptor, 0);
}
catch(IOException e)
{
Log.e("error", "failed to load sound files");
}

startGame();
}

private void startGame()
{
gameEnded = false;

soundPool.play(start, 1, 1, 0, 0, 1);

player = new PlayerShip(context, screenX, screenY);
enemy1 = new EnemyShip(context, screenX, screenY);
enemy2 = new EnemyShip(context, screenX, screenY);
enemy3 = new EnemyShip(context, screenX, screenY);

for (int i = 0; i < 150; i++)
{
dustList.add(new SpaceDust(screenX,screenY));
}
int numSpecs = 150;
for (int i = 0; i < numSpecs; i++)
dustList.add(new SpaceDust(screenX, screenY));

distanceRemaining = 10000;// 10 km
timeTaken = 0;
timeStarted = System.currentTimeMillis();
}

@Override
Expand All @@ -65,6 +133,51 @@ private void update()
{
sd.update(player.getSpeed());
}

boolean hitDetected = false;
if(Rect.intersects(player.getHitbox(), enemy1.getHitbox()))
{
enemy1.setX(-300);
hitDetected = true;
}
if(Rect.intersects(player.getHitbox(), enemy2.getHitbox()))
{
enemy2.setX(-300);
hitDetected = true;
}
if(Rect.intersects(player.getHitbox(), enemy3.getHitbox()))
{
enemy3.setX(-300);
hitDetected = true;
}
if(hitDetected)
{
player.reduceShieldStrength();
soundPool.play(bump, 1, 1, 0, 0, 1);
if (player.getShieldStrength() < 0)
{
soundPool.play(destroyed, 1, 1, 0, 0, 1);
gameEnded = true;
}
}
if(!gameEnded)
{
distanceRemaining -= player.getSpeed();
timeTaken = System.currentTimeMillis() - timeStarted;
}
if(distanceRemaining < 0)
{
soundPool.play(win, 1, 1, 0, 0, 1);
if (timeTaken < fastestTime)
{
fastestTime = timeTaken;
editor.putLong("fastestTime", timeTaken);
editor.commit();
}
distanceRemaining = 0;
gameEnded = true;
}

}

private void draw()
Expand All @@ -78,10 +191,67 @@ 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(!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,
paint);
canvas.drawText("Distance:" +
distanceRemaining / 1000 +
" KM", screenX / 3, screenY - 20, paint);
canvas.drawText("Shield:" +
player.getShieldStrength(), 10, screenY - 20, paint);
canvas.drawText("Speed:" +
player.getSpeed() * 60 +
" MPS", (screenX / 3) * 2, screenY - 20, paint);
}
else
{
paint.setTextSize(100);
paint.setTextAlign(Paint.Align.CENTER);
canvas.drawText("Game Over", screenX/2, 100, paint);
paint.setTextSize(40);
canvas.drawText("Fastest:"+
fastestTime + "s", screenX/2, 160, paint);
canvas.drawText("Time:" + timeTaken +
"s", screenX / 2, 200, paint);
canvas.drawText("Distance remaining:" +
distanceRemaining/1000 + " KM",screenX/2, 240, paint);
paint.setTextSize(100);
canvas.drawText("Tap to replay!", screenX/2, 350, paint);
}

ourHolder.unlockCanvasAndPost(canvas);
}
}
Expand Down Expand Up @@ -120,6 +290,8 @@ public boolean onTouchEvent(MotionEvent motionEvent)
break;
case MotionEvent.ACTION_DOWN:
player.setBoosting();
if(gameEnded)
startGame();
break;
}
return true;
Expand Down
Binary file added app/src/main/res/drawable/enemy2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/enemy3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit bcc26d1

Please sign in to comment.