Skip to content

Instantly share code, notes, and snippets.

@dtb49
Created November 29, 2016 16:23
Show Gist options
  • Save dtb49/ae1e59caa89e725c06d9e3002d1b67b2 to your computer and use it in GitHub Desktop.
Save dtb49/ae1e59caa89e725c06d9e3002d1b67b2 to your computer and use it in GitHub Desktop.
DotSmasher
package com.dotsmasherdtb;
import java.util.Random;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.graphics.*;
public class DotSmasherCanvas extends View implements OnTouchListener {
int dotX, dotY, score;
protected void MoveDot()
{
Random generator = new Random();
generator.setSeed(System.currentTimeMillis());
int w = getWidth() - 60;
int h = getHeight() - 120; //leave room for dot and score
float f = generator.nextFloat();
dotX = (int)(f*w);
f = generator.nextFloat();
dotY = (int)(f*h) + 60;
}
protected boolean detectHit(int x, int y)
{
if((x >= dotX && x <= dotX + 60) && (y>= dotY && y<= dotY + 60))
{
return true;
}
return false;
}
protected void onDraw(Canvas canvas)
{
canvas.drawColor(Color.BLACK);
Paint dotPaint = new Paint();
dotPaint.setColor(Color.GREEN);
//canvas.drawRect(dotX, dotY, dotX + 60, dotY + 60, dotPaint);
canvas.drawCircle(dotX +20, dotY+20, 40, dotPaint);
dotPaint.setTextSize(60);
canvas.drawText("Score: " + score, 60, 60, dotPaint);
}
public int getDotX() {
return dotX;
}
public void setDotX(int dotX) {
this.dotX = dotX;
}
public int getDotY() {
return dotY;
}
public void setDotY(int dotY) {
this.dotY = dotY;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public DotSmasherCanvas(Context context) {
super(context);
MoveDot();
setOnTouchListener(this);
}
public DotSmasherCanvas(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public DotSmasherCanvas(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
// TODO Auto-generated constructor stub
}
@Override
public boolean onTouch(View v, MotionEvent event)
{
if(detectHit( (int)event.getX(), (int)event.getY() ) )
{
score += 1;
invalidate();
}
return false;
}
}
package com.dotsmasherdtb;
import java.util.TimerTask;
import com.dotsmasherdtb.DotSmasherCanvas;
public class DotSmasherTimerTask extends TimerTask {
DotSmasherCanvas canvas;
public DotSmasherTimerTask(DotSmasherCanvas canvas) {
this.canvas = canvas;
}
@Override
public void run() {
canvas.MoveDot();
canvas.postInvalidate();
}
}
package com.dotsmasherdtb;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.*;
import java.util.Timer;
public class MainActivity extends Activity {
private Timer timer;
private DotSmasherCanvas canvas;
private DotSmasherTimerTask task;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
setTitle("DotSmasherDTB");
canvas = new DotSmasherCanvas(getBaseContext());
timer = new Timer();
task = new DotSmasherTimerTask(canvas);
timer.schedule(task, 0, 1500);
setContentView(canvas);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.game_menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.item1:
newGame();
canvas.postInvalidate();
return true;
case R.id.item2:
quit();
return true;
case R.id.easy:
timer.cancel();
setTitle("DotSmasherDTB easy");
canvas = new DotSmasherCanvas(getBaseContext());
timer = new Timer();
task = new DotSmasherTimerTask(canvas);
timer.schedule(task, 0, 2500);
setContentView(canvas);
return true;
case R.id.normal:
timer.cancel();
setTitle("DotSmasherDTB normal");
canvas = new DotSmasherCanvas(getBaseContext());
timer = new Timer();
task = new DotSmasherTimerTask(canvas);
timer.schedule(task, 0, 1500);
setContentView(canvas);
return true;
case R.id.hard:
timer.cancel();
setTitle("DotSmasherDTB hard");
canvas = new DotSmasherCanvas(getBaseContext());
timer = new Timer();
task = new DotSmasherTimerTask(canvas);
timer.schedule(task, 0, 750);
setContentView(canvas);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
void newGame()
{
canvas.setScore(0);
}
void quit()
{
timer.cancel();
task = null;
timer = null;
canvas = null;
finish();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment