From c2624b01a7a7f8d3d8985613a0cd9d150b0086bf Mon Sep 17 00:00:00 2001 From: pommicket Date: Mon, 20 Jun 2016 15:27:48 -0400 Subject: Created BallBounceAndroid --- AndroidManifest.xml | 24 +++ BallBounce.apk | Bin 0 -> 1216966 bytes .../neocities/autoart/ballbounce/CanvasView.java | 192 +++++++++++++++++++++ java/org/neocities/autoart/ballbounce/Game.java | 28 +++ java/org/neocities/autoart/ballbounce/Menu.java | 102 +++++++++++ .../org/neocities/autoart/ballbounce/MoveBall.java | 100 +++++++++++ res/layout/activity_game.xml | 16 ++ res/layout/activity_menu.xml | 95 ++++++++++ res/mipmap-hdpi/ic_launcher.png | Bin 0 -> 436 bytes res/mipmap-mdpi/ic_launcher.png | Bin 0 -> 302 bytes res/mipmap-xhdpi/ic_launcher.png | Bin 0 -> 678 bytes res/mipmap-xxxhdpi/ic_launcher.png | Bin 0 -> 1563 bytes res/values-w820dp/dimens.xml | 6 + res/values/colors.xml | 6 + res/values/dimens.xml | 5 + res/values/strings.xml | 11 ++ res/values/styles.xml | 17 ++ 17 files changed, 602 insertions(+) create mode 100644 AndroidManifest.xml create mode 100644 BallBounce.apk create mode 100644 java/org/neocities/autoart/ballbounce/CanvasView.java create mode 100644 java/org/neocities/autoart/ballbounce/Game.java create mode 100644 java/org/neocities/autoart/ballbounce/Menu.java create mode 100644 java/org/neocities/autoart/ballbounce/MoveBall.java create mode 100644 res/layout/activity_game.xml create mode 100644 res/layout/activity_menu.xml create mode 100644 res/mipmap-hdpi/ic_launcher.png create mode 100644 res/mipmap-mdpi/ic_launcher.png create mode 100644 res/mipmap-xhdpi/ic_launcher.png create mode 100644 res/mipmap-xxxhdpi/ic_launcher.png create mode 100644 res/values-w820dp/dimens.xml create mode 100644 res/values/colors.xml create mode 100644 res/values/dimens.xml create mode 100644 res/values/strings.xml create mode 100644 res/values/styles.xml diff --git a/AndroidManifest.xml b/AndroidManifest.xml new file mode 100644 index 0000000..315434e --- /dev/null +++ b/AndroidManifest.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + diff --git a/BallBounce.apk b/BallBounce.apk new file mode 100644 index 0000000..c10e42e Binary files /dev/null and b/BallBounce.apk differ diff --git a/java/org/neocities/autoart/ballbounce/CanvasView.java b/java/org/neocities/autoart/ballbounce/CanvasView.java new file mode 100644 index 0000000..6c11747 --- /dev/null +++ b/java/org/neocities/autoart/ballbounce/CanvasView.java @@ -0,0 +1,192 @@ +package org.neocities.autoart.ballbounce; + +import android.content.Context; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.os.Handler; +import android.util.AttributeSet; +import android.view.MotionEvent; +import android.view.View; + +import java.util.ArrayList; + +public class CanvasView extends View +{ + public static Game activity; + public static int score = 0; + + public ArrayList ballPositions; + public double[] currentBallPosition; + public double[] paddlePosition; + + public double ballSize; + public double paddleSize; + + public int w; + public int h; + + + public static boolean started = false; + + + public ArrayList colours; + + public Handler handler = new Handler(); + + MoveBall moveBall = new MoveBall(); + + + public CanvasView(Context context) + { + super(context); + } + + public CanvasView(Context context, AttributeSet attrs) + { + super(context, attrs); + } + + public CanvasView(Context context, AttributeSet attrs, int defStyleAttr) + { + super(context, attrs, defStyleAttr); + } + + public double dist(double x1, double y1, double x2, double y2) + { + return Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)); + } + + public void init() + { + moveBall.cv = this; + w = getWidth(); + h = getHeight(); + moveBall.t = 0; + moveBall.a = Math.random()*0.00001; + moveBall.b = Math.random()*0.002; + + colours = new ArrayList<>(); + + score = 0; + moveBall.running = true; + + currentBallPosition = new double[2]; + paddlePosition = new double[2]; + + + currentBallPosition[0] = -w; + currentBallPosition[1] = -h; + + paddlePosition[0] = w/2; + paddlePosition[1] = h/2; + + ballSize = w*0.05; + paddleSize = w*0.2; + + ballPositions = new ArrayList<>(); + + + moveBall.ballVel = new double[2]; + moveBall.ballVel[0] = 0; + moveBall.ballVel[1] = 0; + + + setOnTouchListener(new OnTouchListener() + { + @Override + public boolean onTouch(View v, MotionEvent event) + { + int x = (int)event.getX(); + int y = (int)event.getY(); + paddlePosition[0] = x; + paddlePosition[1] = y; + + return true; + } + }); + + + + handler.postDelayed(moveBall, 50); + } + + @Override + public void onWindowFocusChanged(boolean hasFocus) + { + super.onWindowFocusChanged(hasFocus); + + if (started) + return; + + started = true; + + w = getWidth(); + h = getHeight(); + + currentBallPosition = new double[2]; + paddlePosition = new double[2]; + + currentBallPosition[0] = w/2; + currentBallPosition[1] = 0; + + paddlePosition[0] = -w; + paddlePosition[1] = -h; + + ballSize = w*0.05; + paddleSize = w*0.2; + + + } + + @Override + public void onDraw(Canvas canvas) + { + + if (w == 0) + return; + + Paint paint = new Paint(); + + for (int i = 0; i < ballPositions.size(); i++) + { + double[] ballPosition = ballPositions.get(i); + + if (colours.size() <= Math.ceil((double)i/100)) + { + int[] c = new int[3]; + c[0] = (int)(Math.random()*255); + c[1] = (int)(Math.random()*255); + c[2] = (int)(Math.random()*255); + + colours.add(c); + } + + paint.setStyle(Paint.Style.FILL); + if (Game.MODE == Game.NORMAL_MODE || Game.MODE == Game.CRAZY_GRAVITY_MODE || Game.MODE == Game.FLIP_MODE) + { + paint.setARGB(255, 0, 0, 0); + } + else if (Game.MODE == Game.COLOUR_CHANGING_MODE) + { + paint.setARGB(255, colours.get(i / 100)[0], colours.get(i / 100)[1], colours.get(i / 100)[2]); + } + else if (Game.MODE == Game.OSCILLATION_MODE) + { + int val = (int)(255*Math.sin(0.001*i)); + paint.setARGB(255, val, val, val); + } + else if (Game.MODE == Game.BW_MODE) + { + int val = 255*((i/500)%2); + paint.setARGB(255, val, val, val); + } + canvas.drawCircle((float) ballPosition[0], (float) ballPosition[1], (float) ballSize, paint); + } + + + paint.setStyle(Paint.Style.STROKE); + paint.setARGB(255, 0, 0, 255); + paint.setStrokeWidth(10); + canvas.drawLine((float)(paddlePosition[0]-paddleSize/2), (float)(paddlePosition[1]), (float)(paddlePosition[0]+paddleSize/2), (float)(paddlePosition[1]), paint); + } +} diff --git a/java/org/neocities/autoart/ballbounce/Game.java b/java/org/neocities/autoart/ballbounce/Game.java new file mode 100644 index 0000000..42f20b6 --- /dev/null +++ b/java/org/neocities/autoart/ballbounce/Game.java @@ -0,0 +1,28 @@ +package org.neocities.autoart.ballbounce; + +import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; + +public class Game extends AppCompatActivity +{ + public static int MODE; + public static final int NORMAL_MODE = 0; + public static final int CRAZY_GRAVITY_MODE = 1; + public static final int COLOUR_CHANGING_MODE = 2; + public static final int OSCILLATION_MODE = 3; + public static final int BW_MODE = 4; + public static final int FLIP_MODE = 5; + public static final int NUM_MODES = 6; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_game); + + CanvasView.activity = this; + CanvasView cv = (CanvasView) findViewById(R.id.canvas); + cv.init(); + + } + +} diff --git a/java/org/neocities/autoart/ballbounce/Menu.java b/java/org/neocities/autoart/ballbounce/Menu.java new file mode 100644 index 0000000..3eca4a9 --- /dev/null +++ b/java/org/neocities/autoart/ballbounce/Menu.java @@ -0,0 +1,102 @@ +package org.neocities.autoart.ballbounce; + +import android.content.Intent; +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; +import android.view.MotionEvent; +import android.view.View; +import android.widget.Button; + +public class Menu extends AppCompatActivity +{ + + private void startGame() + { + Intent intent = new Intent(getApplicationContext(), Game.class); + startActivity(intent); + } + + @Override + protected void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_menu); + + Button normal_mode_button = (Button) findViewById(R.id.normal_mode); + normal_mode_button.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Game.MODE = Game.NORMAL_MODE; + startGame(); + } + }); + + Button crazy_gravity_mode = (Button) findViewById(R.id.crazy_gravity_mode); + crazy_gravity_mode.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View v) + { + Game.MODE = Game.CRAZY_GRAVITY_MODE; + startGame(); + } + }); + + Button colour_changing_mode = (Button) findViewById(R.id.colour_changing_mode); + colour_changing_mode.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View v) + { + Game.MODE = Game.COLOUR_CHANGING_MODE; + startGame(); + } + }); + + Button oscillation_mode = (Button) findViewById(R.id.oscillation_mode); + oscillation_mode.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View v) + { + Game.MODE = Game.OSCILLATION_MODE; + startGame(); + } + }); + + Button bw_mode = (Button) findViewById(R.id.bw_mode); + bw_mode.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View v) + { + Game.MODE = Game.BW_MODE; + startGame(); + } + }); + + Button flip_mode = (Button) findViewById(R.id.flip_mode); + flip_mode.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View v) + { + Game.MODE = Game.FLIP_MODE; + startGame(); + } + }); + + Button random_mode = (Button) findViewById(R.id.random_mode); + random_mode.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View v) + { + Game.MODE = (int)Math.floor(Math.random()*Game.NUM_MODES); + startGame(); + } + }); + + + } +} diff --git a/java/org/neocities/autoart/ballbounce/MoveBall.java b/java/org/neocities/autoart/ballbounce/MoveBall.java new file mode 100644 index 0000000..cc44fc4 --- /dev/null +++ b/java/org/neocities/autoart/ballbounce/MoveBall.java @@ -0,0 +1,100 @@ +package org.neocities.autoart.ballbounce; + +import android.app.AlertDialog; +import android.content.DialogInterface; +import android.content.Intent; +import android.graphics.Canvas; + +import java.util.Date; + +public class MoveBall implements Runnable +{ + public CanvasView cv; + public double[] ballVel; + public double gravity = 1; + public boolean running = true; + public long lastBounce; + public double a; + public double b; + public int t = 0; + + @Override + public void run() + { + + + + + if (cv.w == 0 || !running) + { + cv.handler.postDelayed(this, 10); + return; + } + + if (Game.MODE != Game.CRAZY_GRAVITY_MODE && Game.MODE != Game.FLIP_MODE) + gravity = 1; + else if (Game.MODE == Game.CRAZY_GRAVITY_MODE) + gravity = Math.abs(cv.w*(t*a*Math.sin(b*t))); + else if (Game.MODE == Game.FLIP_MODE && (t/500) % 2 == 0) + gravity = 1; + else if (Game.MODE == Game.FLIP_MODE) + gravity = -1; + + cv.currentBallPosition[0] += ballVel[0]; + cv.currentBallPosition[1] += ballVel[1]; + + + if (cv.currentBallPosition[1] > cv.h || cv.currentBallPosition[1] < 0 || cv.currentBallPosition[0] > cv.w || cv.currentBallPosition[0] < 0) + { + running = false; + new AlertDialog.Builder(CanvasView.activity) + .setTitle("You lost!") + .setMessage("Score: " + CanvasView.score) + .setCancelable(false) + .setPositiveButton("OK", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) + { + CanvasView.started = false; + Intent intent = new Intent(CanvasView.activity, Menu.class); + CanvasView.activity.startActivity(intent); + } + }).create().show(); + } + + if (Math.abs(cv.currentBallPosition[0]-cv.paddlePosition[0]) < cv.paddleSize/2 && + Math.abs(cv.currentBallPosition[1]-cv.paddlePosition[1]) < 5+cv.ballSize) + { + Date d = new Date(); + + if (d.getTime()-lastBounce < 100) + { + ballVel[1] += gravity; + cv.ballPositions.add(cv.currentBallPosition.clone()); + cv.invalidate(); + cv.handler.postDelayed(this, 10); + return; + } + + ballVel[0] += cv.w*0.00005 * (cv.currentBallPosition[0]-cv.paddlePosition[0]) + Math.random()*cv.w*0.0005-cv.w*0.00025; + ballVel[1] = -gravity*cv.w*0.02; + CanvasView.score++; + + lastBounce = d.getTime(); + } + + + ballVel[1] += gravity; + + + cv.ballPositions.add(cv.currentBallPosition.clone()); + + cv.invalidate(); + + t++; + + cv.handler.postDelayed(this, 10); + + + } +} diff --git a/res/layout/activity_game.xml b/res/layout/activity_game.xml new file mode 100644 index 0000000..4de4334 --- /dev/null +++ b/res/layout/activity_game.xml @@ -0,0 +1,16 @@ + + + + + + + diff --git a/res/layout/activity_menu.xml b/res/layout/activity_menu.xml new file mode 100644 index 0000000..381b6d7 --- /dev/null +++ b/res/layout/activity_menu.xml @@ -0,0 +1,95 @@ + + + + + +