diff options
-rw-r--r-- | AndroidManifest.xml | 24 | ||||
-rw-r--r-- | BallBounce.apk | bin | 0 -> 1216966 bytes | |||
-rw-r--r-- | java/org/neocities/autoart/ballbounce/CanvasView.java | 192 | ||||
-rw-r--r-- | java/org/neocities/autoart/ballbounce/Game.java | 28 | ||||
-rw-r--r-- | java/org/neocities/autoart/ballbounce/Menu.java | 102 | ||||
-rw-r--r-- | java/org/neocities/autoart/ballbounce/MoveBall.java | 100 | ||||
-rw-r--r-- | res/layout/activity_game.xml | 16 | ||||
-rw-r--r-- | res/layout/activity_menu.xml | 95 | ||||
-rw-r--r-- | res/mipmap-hdpi/ic_launcher.png | bin | 0 -> 436 bytes | |||
-rw-r--r-- | res/mipmap-mdpi/ic_launcher.png | bin | 0 -> 302 bytes | |||
-rw-r--r-- | res/mipmap-xhdpi/ic_launcher.png | bin | 0 -> 678 bytes | |||
-rw-r--r-- | res/mipmap-xxxhdpi/ic_launcher.png | bin | 0 -> 1563 bytes | |||
-rw-r--r-- | res/values-w820dp/dimens.xml | 6 | ||||
-rw-r--r-- | res/values/colors.xml | 6 | ||||
-rw-r--r-- | res/values/dimens.xml | 5 | ||||
-rw-r--r-- | res/values/strings.xml | 11 | ||||
-rw-r--r-- | res/values/styles.xml | 17 |
17 files changed, 602 insertions, 0 deletions
diff --git a/AndroidManifest.xml b/AndroidManifest.xml new file mode 100644 index 0000000..315434e --- /dev/null +++ b/AndroidManifest.xml @@ -0,0 +1,24 @@ +<?xml version="1.0" encoding="utf-8"?> +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="org.neocities.autoart.ballbounce"> + + <application + android:allowBackup="true" + android:icon="@mipmap/ic_launcher" + android:label="@string/app_name" + android:supportsRtl="true" + android:theme="@style/AppTheme"> + <activity android:name=".Menu"> + <intent-filter> + <action android:name="android.intent.action.MAIN" /> + + <category android:name="android.intent.category.LAUNCHER" /> + </intent-filter> + </activity> + <activity + android:name=".Game" + android:screenOrientation="portrait" + /> + </application> + +</manifest> diff --git a/BallBounce.apk b/BallBounce.apk Binary files differnew file mode 100644 index 0000000..c10e42e --- /dev/null +++ b/BallBounce.apk 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); + + + } +} 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 @@ +<?xml version="1.0" encoding="utf-8"?> +<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:tools="http://schemas.android.com/tools" + android:layout_width="match_parent" + android:layout_height="match_parent" + tools:context="org.neocities.autoart.ballbounce.NormalMode"> + + <org.neocities.autoart.ballbounce.CanvasView + android:layout_width="match_parent" + android:layout_height="match_parent" + android:id="@+id/canvas" + android:background="#ffffff" + /> + + +</RelativeLayout> 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 @@ +<?xml version="1.0" encoding="utf-8"?> +<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:tools="http://schemas.android.com/tools" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:paddingBottom="@dimen/activity_vertical_margin" + android:paddingLeft="@dimen/activity_horizontal_margin" + android:paddingRight="@dimen/activity_horizontal_margin" + android:paddingTop="@dimen/activity_vertical_margin" + tools:context="org.neocities.autoart.ballbounce.Menu"> + + <TextView + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:text="@string/menu_title" + android:textSize="25sp" + android:layout_centerHorizontal="true" + android:textColor="#000000" + android:id="@+id/menu_title" + /> + + <Button + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_below="@id/menu_title" + android:background="#b1e5ff" + android:text="@string/normal" + android:layout_marginTop="20sp" + android:id="@+id/normal_mode" + /> + + + <Button + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_below="@id/normal_mode" + android:background="#ffdfb1" + android:text="@string/crazy_gravity" + android:layout_marginTop="20sp" + android:id="@+id/crazy_gravity_mode" + /> + + <Button + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_below="@id/crazy_gravity_mode" + android:background="#ebc4ff" + android:layout_marginTop="20sp" + android:text="@string/colour_changing" + android:id="@+id/colour_changing_mode" + + /> + + <Button + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_below="@id/colour_changing_mode" + android:background="#c7ffc4" + android:layout_marginTop="20sp" + android:text="@string/oscillation" + android:id="@+id/oscillation_mode" + + /> + <Button + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_below="@id/oscillation_mode" + android:layout_marginTop="20sp" + android:text="@string/bw_mode" + android:background="#dadada" + android:id="@+id/bw_mode" + /> + + <Button + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_below="@id/bw_mode" + android:layout_marginTop="20sp" + android:text="@string/flip_mode" + android:background="#faa0a1" + android:id="@+id/flip_mode" + /> + + <Button + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_below="@id/flip_mode" + android:layout_marginTop="20sp" + android:text="@string/random_mode" + android:background="#faf7a0" + android:id="@+id/random_mode" + /> + + +</RelativeLayout> diff --git a/res/mipmap-hdpi/ic_launcher.png b/res/mipmap-hdpi/ic_launcher.png Binary files differnew file mode 100644 index 0000000..e73144f --- /dev/null +++ b/res/mipmap-hdpi/ic_launcher.png diff --git a/res/mipmap-mdpi/ic_launcher.png b/res/mipmap-mdpi/ic_launcher.png Binary files differnew file mode 100644 index 0000000..5599d93 --- /dev/null +++ b/res/mipmap-mdpi/ic_launcher.png diff --git a/res/mipmap-xhdpi/ic_launcher.png b/res/mipmap-xhdpi/ic_launcher.png Binary files differnew file mode 100644 index 0000000..a094af8 --- /dev/null +++ b/res/mipmap-xhdpi/ic_launcher.png diff --git a/res/mipmap-xxxhdpi/ic_launcher.png b/res/mipmap-xxxhdpi/ic_launcher.png Binary files differnew file mode 100644 index 0000000..7be100a --- /dev/null +++ b/res/mipmap-xxxhdpi/ic_launcher.png diff --git a/res/values-w820dp/dimens.xml b/res/values-w820dp/dimens.xml new file mode 100644 index 0000000..63fc816 --- /dev/null +++ b/res/values-w820dp/dimens.xml @@ -0,0 +1,6 @@ +<resources> + <!-- Example customization of dimensions originally defined in res/values/dimens.xml + (such as screen margins) for screens with more than 820dp of available width. This + would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). --> + <dimen name="activity_horizontal_margin">64dp</dimen> +</resources> diff --git a/res/values/colors.xml b/res/values/colors.xml new file mode 100644 index 0000000..3ab3e9c --- /dev/null +++ b/res/values/colors.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="utf-8"?> +<resources> + <color name="colorPrimary">#3F51B5</color> + <color name="colorPrimaryDark">#303F9F</color> + <color name="colorAccent">#FF4081</color> +</resources> diff --git a/res/values/dimens.xml b/res/values/dimens.xml new file mode 100644 index 0000000..47c8224 --- /dev/null +++ b/res/values/dimens.xml @@ -0,0 +1,5 @@ +<resources> + <!-- Default screen margins, per the Android Design guidelines. --> + <dimen name="activity_horizontal_margin">16dp</dimen> + <dimen name="activity_vertical_margin">16dp</dimen> +</resources> diff --git a/res/values/strings.xml b/res/values/strings.xml new file mode 100644 index 0000000..2c97fc5 --- /dev/null +++ b/res/values/strings.xml @@ -0,0 +1,11 @@ +<resources> + <string name="app_name">Ball Bounce</string> + <string name="menu_title">Main menu</string> + <string name="normal">Play normal mode</string> + <string name="crazy_gravity">Play crazy gravity mode</string> + <string name="colour_changing">Play colour changing mode</string> + <string name="oscillation">Play oscillation mode</string> + <string name="bw_mode">Play black & white mode</string> + <string name="flip_mode">Play flip mode</string> + <string name="random_mode">Play a random mode!</string> +</resources> diff --git a/res/values/styles.xml b/res/values/styles.xml new file mode 100644 index 0000000..8898e0c --- /dev/null +++ b/res/values/styles.xml @@ -0,0 +1,17 @@ +<resources> + + <!-- Base application theme. --> + <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> + <!-- Customize your theme here. --> + <item name="colorPrimary">@color/colorPrimary</item> + <item name="colorPrimaryDark">@color/colorPrimaryDark</item> + <item name="colorAccent">@color/colorAccent</item> + </style> + + <style name="ThemeNoTitleBar" parent="Theme.AppCompat.Light.NoActionBar"> + <item name="colorPrimary">@color/colorPrimary</item> + <item name="colorPrimaryDark">@color/colorPrimaryDark</item> + <item name="colorAccent">@color/colorAccent</item> + </style> + +</resources> |