GitBucket
4.20.0
Toggle navigation
Sign in
Files
Branches
1
Tags
Issues
Pull requests
Labels
Priorities
Milestones
Wiki
Forks
Jonathan
/
BallJumper
Browse code
Changed to better variable names.
master
1 parent
20c1503
commit
e8726127c87f428d58405123c31a9e4ea904009b
Jonathan Ström
authored
on 31 Jul 2017
Patch
Showing
2 changed files
app/src/main/java/jonathan/balljumper/GameSurfaceView.java
app/src/main/java/jonathan/balljumper/classes/Ball.java
Ignore Space
Show notes
View
app/src/main/java/jonathan/balljumper/GameSurfaceView.java
package jonathan.balljumper; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Point; import android.graphics.Rect; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.SurfaceView; import jonathan.balljumper.classes.Ball; import jonathan.balljumper.classes.Panel; import jonathan.balljumper.classes.PanelHandler; /** * Created by Jonathan on 27/07/2017. */ public class GameSurfaceView extends SurfaceView implements Runnable { private boolean isRunning = false; private Thread gameThread; private SurfaceHolder holder; private Point screenSize; private Ball ball; private PanelHandler panelHandler; private final static int MAX_FPS = 40; private final static int FRAME_PERIOD = 1000 / MAX_FPS; public GameSurfaceView(Context context) { super(context); // Get and set window size. screenSize = new Point(); ((Activity)getContext()).getWindowManager() .getDefaultDisplay() .getSize(screenSize); holder = getHolder(); holder.addCallback(new SurfaceHolder.Callback() { @Override public void surfaceCreated(SurfaceHolder holder) { } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { screenSize.x = width; screenSize.y = height; } @Override public void surfaceDestroyed(SurfaceHolder holder) { } }); // Initialize ball. int ballRadius = 35; ball = new Ball(screenSize.x / 2 - ballRadius, (screenSize.y / 3) * 2 - ballRadius, ballRadius, -.3f, 0.4f, Color.argb(255, 200, 34, 34)); ball.bounce(); // Initialize panelHandler. panelHandler = new PanelHandler(10, screenSize); } /** * Start or resume the game. */ public void resume() { isRunning = true; gameThread = new Thread(this); gameThread.start(); } /** * Pause the game loop */ public void pause() { isRunning = false; boolean retry = true; while (retry) { try { gameThread.join(); retry = false; } catch (InterruptedException e) { // Try shutting down the thread again. } } } /** * Updates each frame. */ protected void update() { // Game mechanics /* if (ball.getBottom() >= screenSize.y) { ball.bounce(); } else if (ball.getTop() <= 0) { ball.bounce(); } */ if (ball.getBottom() >= screenSize.y) { pause(); } ball.setY((ball.getY() + ball.getSpeed() * ball.getDirection().y) + ball.getVelocity()); ball.setVelocity(ball.getVelocity() + ball.getGravity()); for (int i = 0; i < panelHandler.getPanelList().length; ++i) { Panel panel = panelHandler.getPanelList()[i]; // When ball moves upwards, move panels down. if (ball.getDirection().y == -1) { // If the panel is outside of the screen... if (panel.getY() + panel.getSpeed() > screenSize.y) { panelHandler.resetPanel(i); } else { // Move the panel down. panel.setY(panel.getY() + panel.getSpeed()); } } if (ball.intersects(panel.getX(), panel.getY(), panel.getWidth(), panel.getHeight())) { ball.bounce(); } } } /** * Render everything to the screen. * @param canvas Canvas to be used when rendering. */ protected void render(Canvas canvas) { // Reset background canvas.drawColor(Color.GREEN); // Render ball ball.draw(canvas); // Render panels panelHandler.draw(canvas); } @Override public void run() { while(isRunning) { // Make sure the surface is ready if (! holder.getSurface().isValid()) { continue; } long started = System.currentTimeMillis(); // Update objects. update(); // Render objects to screen. Canvas canvas = holder.lockCanvas(); if (canvas != null) { render(canvas); holder.unlockCanvasAndPost(canvas); } // Make sure update, updates accordingly to the FPS. float deltaTime = (System.currentTimeMillis() - started); int sleepTime = (int) ((FRAME_PERIOD - deltaTime) / 2); if (sleepTime > 0) { try { gameThread.sleep(sleepTime); } catch (InterruptedException e) { } } while (sleepTime < 0) { update(); sleepTime += FRAME_PERIOD; } } } @Override public boolean onTouchEvent(MotionEvent event) { super.onTouchEvent(event); float x = event.getX(); float y = event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_MOVE: ball.setX(x); break; } return true; } }
package jonathan.balljumper; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Point; import android.graphics.Rect; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.SurfaceView; import jonathan.balljumper.classes.Ball; import jonathan.balljumper.classes.Panel; import jonathan.balljumper.classes.PanelHandler; /** * Created by Jonathan on 27/07/2017. */ public class GameSurfaceView extends SurfaceView implements Runnable { private boolean isRunning = false; private Thread gameThread; private SurfaceHolder holder; private Point screenSize; private Ball ball; private PanelHandler panelHandler; private final static int MAX_FPS = 40; private final static int FRAME_PERIOD = 1000 / MAX_FPS; public GameSurfaceView(Context context) { super(context); // Get and set window size. screenSize = new Point(); ((Activity)getContext()).getWindowManager() .getDefaultDisplay() .getSize(screenSize); holder = getHolder(); holder.addCallback(new SurfaceHolder.Callback() { @Override public void surfaceCreated(SurfaceHolder holder) { } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { screenSize.x = width; screenSize.y = height; } @Override public void surfaceDestroyed(SurfaceHolder holder) { } }); // Initialize ball. int ballRadius = 35; ball = new Ball(screenSize.x / 2 - ballRadius, (screenSize.y / 3) * 2 - ballRadius, ballRadius, 0.4f, Color.argb(255, 200, 34, 34)); ball.bounce(); // Initialize panelHandler. panelHandler = new PanelHandler(10, screenSize); } /** * Start or resume the game. */ public void resume() { isRunning = true; gameThread = new Thread(this); gameThread.start(); } /** * Pause the game loop */ public void pause() { isRunning = false; boolean retry = true; while (retry) { try { gameThread.join(); retry = false; } catch (InterruptedException e) { // Try shutting down the thread again. } } } /** * Updates each frame. */ protected void update() { // Game mechanics /* if (ball.getBottom() >= screenSize.y) { ball.bounce(); } else if (ball.getTop() <= 0) { ball.bounce(); } */ if (ball.getBottom() >= screenSize.y) { pause(); } ball.setY((ball.getY() + ball.getSpeed() * ball.getDirection().y) + ball.velocity); ball.velocity += ball.getResistance(); for (int i = 0; i < panelHandler.getPanelList().length; ++i) { Panel panel = panelHandler.getPanelList()[i]; // When ball moves upwards, move panels down. if (ball.getDirection().y == -1) { // If the panel is outside of the screen... if (panel.getY() + panel.getSpeed() > screenSize.y) { panelHandler.resetPanel(i); } else { // Move the panel down. panel.setY(panel.getY() + panel.getSpeed()); } } if (ball.intersects(panel.getX(), panel.getY(), panel.getWidth(), panel.getHeight())) { ball.bounce(); } } } /** * Render everything to the screen. * @param canvas Canvas to be used when rendering. */ protected void render(Canvas canvas) { // Reset background canvas.drawColor(Color.GREEN); // Render ball ball.draw(canvas); // Render panels panelHandler.draw(canvas); } @Override public void run() { while(isRunning) { // Make sure the surface is ready if (! holder.getSurface().isValid()) { continue; } long started = System.currentTimeMillis(); // Update objects. update(); // Render objects to screen. Canvas canvas = holder.lockCanvas(); if (canvas != null) { render(canvas); holder.unlockCanvasAndPost(canvas); } // Make sure update, updates accordingly to the FPS. float deltaTime = (System.currentTimeMillis() - started); int sleepTime = (int) ((FRAME_PERIOD - deltaTime) / 2); if (sleepTime > 0) { try { gameThread.sleep(sleepTime); } catch (InterruptedException e) { } } while (sleepTime < 0) { update(); sleepTime += FRAME_PERIOD; } } } @Override public boolean onTouchEvent(MotionEvent event) { super.onTouchEvent(event); float x = event.getX(); float y = event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_MOVE: ball.setX(x); break; } return true; } }
Ignore Space
Show notes
View
app/src/main/java/jonathan/balljumper/classes/Ball.java
package jonathan.balljumper.classes; import android.graphics.Canvas; import android.graphics.ColorFilter; import android.graphics.LightingColorFilter; import android.graphics.Paint; /** * Created by Jonathan on 27/07/2017. */ public class Ball extends Sprite { private float radius; private float velocity; private float gravity; public Ball(float x, float y, float radius, float velocity, float gravity, int color) { super(x, y, radius * 2, radius * 2); this.radius = radius; this.gravity = gravity; this.velocity = velocity; setColor(color); setSpeed(17f); } public void draw(Canvas canvas) { Paint p = new Paint(); ColorFilter filter = new LightingColorFilter(getColor(), 0); p.setColorFilter(filter); p.setColor(getColor()); canvas.drawCircle(getX(), getY(), getRadius(), p); } /** * Bounce the ball. */ public void bounce() { // Simple bounce. y-=speed; // Give it some speed up, so it doesn't get stuck. velocity = -.3f; // Reset velocity getDirection().y *= -1; // Change ball direction. } public boolean intersects(float x, float y, float w, float h) { return x < this.getLeft() + this.width && x + w > this.getLeft() && y < this.getTop() + this.height && y + h > this.getTop(); } public float getRadius() { return radius; } public void setRadius(float radius) { this.radius = radius; } public float getGravity() { return gravity; } public void setGravity(float gravity) { this.gravity = gravity; } public float getVelocity() { return velocity; } public void setVelocity(float velocity) { this.velocity = velocity; } @Override public float getTop() { return y - radius; } @Override public float getBottom() { return y + radius; } @Override public float getLeft() { return x - radius; } @Override public float getRight() { return x + radius; } }
package jonathan.balljumper.classes; import android.graphics.Canvas; import android.graphics.ColorFilter; import android.graphics.LightingColorFilter; import android.graphics.Paint; /** * Created by Jonathan on 27/07/2017. */ public class Ball extends Sprite { private float radius; private float resistance; public float velocity; public Ball(float x, float y, float radius, float resistance, int color) { super(x, y, radius * 2, radius * 2); this.radius = radius; this.resistance = resistance; this.velocity = -.3f; setColor(color); setSpeed(17f); } public void draw(Canvas canvas) { Paint p = new Paint(); ColorFilter filter = new LightingColorFilter(getColor(), 0); p.setColorFilter(filter); p.setColor(getColor()); canvas.drawCircle(getX(), getY(), getRadius(), p); } public void bounce() { // Simple bounce. getDirection().y *= -1; y-=speed; velocity = -.3f; } public boolean intersects(float x, float y, float w, float h) { return x < this.getLeft() + this.width && x + w > this.getLeft() && y < this.getTop() + this.height && y + h > this.getTop(); } public float getRadius() { return radius; } public void setRadius(float radius) { this.radius = radius; } public float getResistance() { return resistance; } public void setResistance(float resistance) { this.resistance = resistance; } @Override public float getTop() { return y - radius; } @Override public float getBottom() { return y + radius; } @Override public float getLeft() { return x - radius; } @Override public float getRight() { return x + radius; } }
Show line notes below