GitBucket
4.20.0
Toggle navigation
Sign in
Files
Branches
1
Tags
Issues
Pull requests
Labels
Priorities
Milestones
Wiki
Forks
Jonathan
/
BallJumper
Browse code
Moved SoundController out of Ball.java.
master
1 parent
bf5a264
commit
585cf8f379448c2f89e5a146819793fc8894eab1
Jonathan Ström
authored
on 23 Aug 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.Canvas; import android.graphics.Color; import android.graphics.Point; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.SurfaceView; import jonathan.balljumper.classes.Ball; import jonathan.balljumper.classes.HighscoreHandler; import jonathan.balljumper.classes.Panel; import jonathan.balljumper.classes.PanelHandler; import jonathan.balljumper.classes.SoundController; /** * Created by Jonathan on 27/07/2017. */ public class GameSurfaceView extends SurfaceView implements Runnable { private boolean isRunning = false; private Thread gameThread; private final SurfaceHolder holder; private final static Point screenSize = new Point(); public static Point getScreenSize() { return screenSize; } private boolean isTouchDown = false; private long touchStartTime; private float touchMoveDeltaX, touchMoveDeltaY; private final Ball ball; private final PanelHandler panelHandler; private final HighscoreHandler highscoreHandler; private final SoundController soundController; 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. ((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) { } }); this.soundController = new SoundController(context); // Initialize ball. int ballRadius = 35; ball = new Ball(screenSize.x / 2 - ballRadius, (screenSize.y / 3) * 2 - ballRadius, ballRadius, 0.2f, // Velocity 0.5f, // Gravity 17f, // Speed Color.argb(255, 200, 34, 34) ); // Initialize panelHandler. panelHandler = new PanelHandler(10, 150, 20, 5f); highscoreHandler = new HighscoreHandler(); } /** * 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 // Delta value. If this is set, then move the screen accordingly. float ballHeightLimit = -1; // If the ball goes below the screen, you lose. if (ball.getBottom() >= screenSize.y) { lose(); } ball.move(); highscoreHandler.addHeight(panelHandler.getPanelSpeed()); // Check if the ball is going too high, if so move the screen with the ball. if (ball.getVelocity() < ball.getSpeed() && ball.getTop() < (screenSize.y / 3) * 2) { // Move the ball first to set the new velocity and to get the delta values. // Reset the ball's Y position. ballHeightLimit = (ball.getDeltaY() * (1f / (Math.max((ball.getY() - 150), 100) / 100))); ball.setY(ball.getY() - ballHeightLimit); ball.setDeltaY(ball.getDeltaY() - ballHeightLimit); // Give extra score for speeding up. highscoreHandler.addHeight(-ballHeightLimit); } for (int i = 0; i < panelHandler.getPanelList().length; ++i) { Panel panel = panelHandler.getPanelList()[i]; // Move the panel at normal speed and reset it if it comes below the screen. panelHandler.move(i); // When the ball moves upwards too much, move with the ball. if (ballHeightLimit != -1) { panelHandler.move(i, -ballHeightLimit); } if (!ball.getHasJumped()) { // Bounce if the ball intersects with a panel. if (ball.intersects(panel.getX(), panel.getY(), panel.getWidth(), panel.getHeight())) { // Do not bounce if you're above the screen. if (ball.getBottom() > 0) { soundController.playBounce(); ball.bounce(); highscoreHandler.addBounce(); // If the ball is falling. if (ball.getDeltaY() > 0) { ball.setY(ball.getY() - (ball.getBottom() - panel.getTop())); } } } } } } /** * Render everything to the screen. * @param canvas Canvas to be used when rendering. */ protected final void render(Canvas canvas) { // Reset background canvas.drawColor(Color.GREEN); // Render panels panelHandler.draw(canvas); // Render ball ball.draw(canvas); // Render score and highscore. highscoreHandler.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: // Calculate delta x and y. touchMoveDeltaX = event.getX() - touchMoveDeltaX; touchMoveDeltaY = event.getY() - touchMoveDeltaY; // Skip first time you press down. if (!isTouchDown) { isTouchDown = true; } else { // Correct delta here. // Move the ball as you move the finger. ball.setX(ball.getX() + touchMoveDeltaX * 1.5f); } touchMoveDeltaX = event.getX(); touchMoveDeltaY = event.getY(); break; case MotionEvent.ACTION_DOWN: touchStartTime = System.currentTimeMillis(); break; case MotionEvent.ACTION_UP: // If you quickly press with your finger, move the ball to that position. if ((System.currentTimeMillis() - touchStartTime) < 125) { ball.setX(event.getX()); } touchStartTime = 0; isTouchDown = false; break; } return true; } public void lose() { isRunning = false; try { gameThread.sleep(1000); } catch (InterruptedException e) { } highscoreHandler.resetAndSave(); ball.bounce(); ball.setX(screenSize.x / 2 - ball.getWidth() / 2); ball.setY((screenSize.y / 3) * 2 - ball.getWidth() / 2); ball.setDeltaX(0); ball.setDeltaY(0); panelHandler.resetAllPanels(); isRunning = true; } }
package jonathan.balljumper; import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Point; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.SurfaceView; import jonathan.balljumper.classes.Ball; import jonathan.balljumper.classes.HighscoreHandler; import jonathan.balljumper.classes.Panel; import jonathan.balljumper.classes.PanelHandler; import jonathan.balljumper.classes.SoundController; /** * Created by Jonathan on 27/07/2017. */ public class GameSurfaceView extends SurfaceView implements Runnable { private boolean isRunning = false; private Thread gameThread; private final SurfaceHolder holder; private final static Point screenSize = new Point(); public static Point getScreenSize() { return screenSize; } private boolean isTouchDown = false; private long touchStartTime; private float touchMoveDeltaX, touchMoveDeltaY; private final Ball ball; private final PanelHandler panelHandler; private final HighscoreHandler highscoreHandler; private final SoundController soundController; 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. ((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) { } }); this.soundController = new SoundController(context); // Initialize ball. int ballRadius = 35; ball = new Ball(screenSize.x / 2 - ballRadius, (screenSize.y / 3) * 2 - ballRadius, ballRadius, 0.2f, // Velocity 0.5f, // Gravity 17f, // Speed Color.argb(255, 200, 34, 34), soundController ); // Initialize panelHandler. panelHandler = new PanelHandler(10, 150, 20, 5f); highscoreHandler = new HighscoreHandler(); } /** * 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 // Delta value. If this is set, then move the screen accordingly. float ballHeightLimit = -1; // If the ball goes below the screen, you lose. if (ball.getBottom() >= screenSize.y) { lose(); } ball.move(); highscoreHandler.addHeight(panelHandler.getPanelSpeed()); // Check if the ball is going too high, if so move the screen with the ball. if (ball.getVelocity() < ball.getSpeed() && ball.getTop() < (screenSize.y / 3) * 2) { // Move the ball first to set the new velocity and to get the delta values. // Reset the ball's Y position. ballHeightLimit = (ball.getDeltaY() * (1f / (Math.max((ball.getY() - 150), 100) / 100))); ball.setY(ball.getY() - ballHeightLimit); ball.setDeltaY(ball.getDeltaY() - ballHeightLimit); // Give extra score for speeding up. highscoreHandler.addHeight(-ballHeightLimit); } for (int i = 0; i < panelHandler.getPanelList().length; ++i) { Panel panel = panelHandler.getPanelList()[i]; // Move the panel at normal speed and reset it if it comes below the screen. panelHandler.move(i); // When the ball moves upwards too much, move with the ball. if (ballHeightLimit != -1) { panelHandler.move(i, -ballHeightLimit); } if (!ball.getHasJumped()) { // Bounce if the ball intersects with a panel. if (ball.intersects(panel.getX(), panel.getY(), panel.getWidth(), panel.getHeight())) { // Do not bounce if you're above the screen. if (ball.getBottom() > 0) { ball.bounce(); highscoreHandler.addBounce(); // If the ball is falling. if (ball.getDeltaY() > 0) { ball.setY(ball.getY() - (ball.getBottom() - panel.getTop())); } } } } } } /** * Render everything to the screen. * @param canvas Canvas to be used when rendering. */ protected final void render(Canvas canvas) { // Reset background canvas.drawColor(Color.GREEN); // Render panels panelHandler.draw(canvas); // Render ball ball.draw(canvas); // Render score and highscore. highscoreHandler.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: // Calculate delta x and y. touchMoveDeltaX = event.getX() - touchMoveDeltaX; touchMoveDeltaY = event.getY() - touchMoveDeltaY; // Skip first time you press down. if (!isTouchDown) { isTouchDown = true; } else { // Correct delta here. // Move the ball as you move the finger. ball.setX(ball.getX() + touchMoveDeltaX * 1.5f); } touchMoveDeltaX = event.getX(); touchMoveDeltaY = event.getY(); break; case MotionEvent.ACTION_DOWN: touchStartTime = System.currentTimeMillis(); break; case MotionEvent.ACTION_UP: // If you quickly press with your finger, move the ball to that position. if ((System.currentTimeMillis() - touchStartTime) < 125) { ball.setX(event.getX()); } touchStartTime = 0; isTouchDown = false; break; } return true; } public void lose() { isRunning = false; try { gameThread.sleep(1000); } catch (InterruptedException e) { } highscoreHandler.resetAndSave(); ball.bounce(); ball.setX(screenSize.x / 2 - ball.getWidth() / 2); ball.setY((screenSize.y / 3) * 2 - ball.getWidth() / 2); ball.setDeltaX(0); ball.setDeltaY(0); panelHandler.resetAllPanels(); isRunning = 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; import jonathan.balljumper.GameSurfaceView; /** * Created by Jonathan on 27/07/2017. */ public class Ball extends Sprite { private final int animationSizeMax = 6; private final Paint paint; private float radius; private float velocity, startVelocity; private float gravity; private float deltaX, deltaY; private int jumpBoost; private boolean isAnimationRunning; private float animationSize; public Ball(float x, float y, float radius, float velocity, float gravity, float speed, int color) { super(x, y, radius * 2, radius * 2); this.radius = radius; this.gravity = gravity; this.startVelocity = velocity; this.velocity = velocity; this.speed = speed; this.color = color; this.jumpBoost = -1; paint = new Paint(); ColorFilter filter = new LightingColorFilter(color, 90); paint.setColorFilter(filter); paint.setColor(color); } public final void draw(Canvas canvas) { canvas.drawOval( getLeft() - animationSize / 2, getTop() + animationSize * 2, getRight() + animationSize / 2, getBottom() - animationSize, paint ); } /** * Bounce the ball. */ public void bounce() { if (jumpBoost < 0) { jumpBoost = (int)speed; // Give the ball a boost. velocity = this.startVelocity; // Reset velocity } isAnimationRunning = true; } public void move() { this.deltaY = y; this.deltaX = x; if (jumpBoost >= 0) { int change = 0; if (jumpBoost > ((int)speed / 3) * 2) { change = 4; } else if (jumpBoost > ((int)speed / 3)) { change = 2; } else { change = 1; } jumpBoost -= change; y -= change; } velocity = velocity + gravity; y = y - speed + velocity; this.deltaY = y - this.deltaY; this.deltaX = x - this.deltaX; if (isAnimationRunning) { animationSize += velocity * 2; if (animationSize >= animationSizeMax) { isAnimationRunning = false; } } else if (animationSize > 0) { animationSize -= velocity * 2; if (animationSize < 0) animationSize = 0; } } public final 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(); } @Override public float getTop() { return y - radius; } @Override public final float getBottom() { return y + radius; } @Override public final float getLeft() { return x - radius; } @Override public final float getRight() { return x + radius; } @Override public void setX(float x) { this.x = x; if (getLeft() < 0) { this.x = 0; } if (getRight() > GameSurfaceView.getScreenSize().x) { this.x = GameSurfaceView.getScreenSize().x; } } public void setDeltaX(float newDeltaX) { this.deltaX = newDeltaX; } public final float getDeltaX() { return deltaX; } public void setDeltaY(float newDeltaY) { this.deltaY = newDeltaY; } public final float getDeltaY() { return deltaY; } public final float getVelocity() { return velocity; } public final boolean getHasJumped() { return jumpBoost > 0; } }
package jonathan.balljumper.classes; import android.graphics.Canvas; import android.graphics.ColorFilter; import android.graphics.LightingColorFilter; import android.graphics.Paint; import jonathan.balljumper.GameSurfaceView; /** * Created by Jonathan on 27/07/2017. */ public class Ball extends Sprite { private final int animationSizeMax = 6; private final SoundController soundController; private final Paint paint; private float radius; private float velocity, startVelocity; private float gravity; private float deltaX, deltaY; private int jumpBoost; private boolean isAnimationRunning; private float animationSize; public Ball(float x, float y, float radius, float velocity, float gravity, float speed, int color, SoundController soundController) { super(x, y, radius * 2, radius * 2); this.radius = radius; this.gravity = gravity; this.startVelocity = velocity; this.velocity = velocity; this.speed = speed; this.color = color; this.jumpBoost = -1; paint = new Paint(); ColorFilter filter = new LightingColorFilter(color, 90); paint.setColorFilter(filter); paint.setColor(color); this.soundController = soundController; } public final void draw(Canvas canvas) { canvas.drawOval( getLeft() - animationSize / 2, getTop() + animationSize * 2, getRight() + animationSize / 2, getBottom() - animationSize, paint ); } /** * Bounce the ball. */ public void bounce() { if (jumpBoost < 0) { jumpBoost = (int)speed; // Give the ball a boost. velocity = this.startVelocity; // Reset velocity } isAnimationRunning = true; } public void move() { this.deltaY = y; this.deltaX = x; if (jumpBoost >= 0) { int change = 0; if (jumpBoost > ((int)speed / 3) * 2) { change = 4; } else if (jumpBoost > ((int)speed / 3)) { change = 2; } else { change = 1; } jumpBoost -= change; y -= change; } velocity = velocity + gravity; y = y - speed + velocity; this.deltaY = y - this.deltaY; this.deltaX = x - this.deltaX; if (isAnimationRunning) { animationSize += velocity * 2; if (animationSize >= animationSizeMax) { isAnimationRunning = false; soundController.playBounce(); } } else if (animationSize > 0) { animationSize -= velocity * 2; if (animationSize < 0) animationSize = 0; } } public final 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(); } @Override public float getTop() { return y - radius; } @Override public final float getBottom() { return y + radius; } @Override public final float getLeft() { return x - radius; } @Override public final float getRight() { return x + radius; } @Override public void setX(float x) { this.x = x; if (getLeft() < 0) { this.x = 0; } if (getRight() > GameSurfaceView.getScreenSize().x) { this.x = GameSurfaceView.getScreenSize().x; } } public void setDeltaX(float newDeltaX) { this.deltaX = newDeltaX; } public final float getDeltaX() { return deltaX; } public void setDeltaY(float newDeltaY) { this.deltaY = newDeltaY; } public final float getDeltaY() { return deltaY; } public final float getVelocity() { return velocity; } public final boolean getHasJumped() { return jumpBoost > 0; } }
Show line notes below