summaryrefslogtreecommitdiff
path: root/java
diff options
context:
space:
mode:
Diffstat (limited to 'java')
-rw-r--r--java/org/neocities/autoart/ballbounce/CanvasView.java192
-rw-r--r--java/org/neocities/autoart/ballbounce/Game.java28
-rw-r--r--java/org/neocities/autoart/ballbounce/Menu.java102
-rw-r--r--java/org/neocities/autoart/ballbounce/MoveBall.java100
4 files changed, 422 insertions, 0 deletions
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<double[]> 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<int[]> 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);
+
+
+ }
+}