GitBucket
4.20.0
Toggle navigation
Sign in
Files
Branches
1
Tags
Issues
Pull requests
Labels
Priorities
Milestones
Wiki
Forks
Jonathan
/
BallJumper
Browse code
Added panelSpeed to panelHandler.
master
1 parent
dd8ca75
commit
79e9791a68c2bb176c4d530ce454d9f2a674694f
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/PanelHandler.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.util.Log; 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; /** * 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 HighscoreHandler highscoreHandler; 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.2f, // Velocity 0.5f, // Gravity 17f, // Speed Color.argb(255, 200, 34, 34)); // Initialize panelHandler. panelHandler = new PanelHandler(10, 150, 20, 5f, screenSize); 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); } // 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(); // 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 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: ball.setX(x); 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.util.Log; 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; /** * 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 HighscoreHandler highscoreHandler; 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.2f, // Velocity 0.5f, // Gravity 17f, // Speed Color.argb(255, 200, 34, 34)); // Initialize panelHandler. panelHandler = new PanelHandler(10, 150, 20, 5f, screenSize); 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(5f); // 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); } // 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(); // 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 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: ball.setX(x); 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/PanelHandler.java
package jonathan.balljumper.classes; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Point; import java.util.Random; /** * Created by Jonathan on 29/07/2017. */ public class PanelHandler { private final int panelFirstPosition = 1500; // Y-position (pixel) of the first panel. private float panelSpeed; // The normal panel speed. private Point screenSize; private Random random; private Panel[] panelList; private int lastPanelIndex = -1; public PanelHandler(int panelCount, int panelWidth, int panelHeight, float panelSpeed, Point screenSize) { this.screenSize = screenSize; this.panelSpeed = panelSpeed; // Initialize all panels. panelList = new Panel[panelCount]; random = new Random(); for (int i = 0; i < panelList.length; ++i) { panelList[i] = new Panel( random.nextInt(screenSize.x - panelWidth), getNewPanelHeight(lastPanelIndex), panelWidth, panelHeight, Color.GRAY, panelSpeed ); lastPanelIndex = i; } } private void resetPanel(int index) { panelList[index].setY(getNewPanelHeight(lastPanelIndex)); lastPanelIndex = index; } private float getNewPanelHeight(int index) { float newPanelHeight; if (index == -1) { newPanelHeight = panelFirstPosition; } else { float diff = (random.nextInt(screenSize.y / 5) + 250); newPanelHeight = panelList[index].getY() - diff; } return newPanelHeight; } public void resetAllPanels() { lastPanelIndex = -1; for (int i = 0; i < panelList.length; ++i) { panelList[i].setX(screenSize.x - panelList[i].getWidth()); panelList[i].setY(getNewPanelHeight(lastPanelIndex)); } } public void draw(Canvas canvas) { for (Panel panel : panelList) { Paint pPanel = new Paint(); pPanel.setColor(panel.getColor()); canvas.drawRect(panel.getX(), panel.getY(), panel.getWidth() + panel.getX(), panel.getHeight() + panel.getY(), pPanel); } } /** * Move the panel. * @param index The panel index to move. */ public void move(int index) { move(index, panelList[index].getSpeed()); } /** * * @param index The panel index to move. * @param speed The speed to move the panel. */ public void move(int index, float speed) { // If a panel is outside of the screen reset it at the top. if (panelList[index].getY() + speed > screenSize.y) { resetPanel(index); } else { // Move the panel down. panelList[index].setY(panelList[index].getY() + speed); } } public Panel[] getPanelList() { return this.panelList; } public float getPanelSpeed() { return this.panelSpeed; } }
package jonathan.balljumper.classes; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Point; import java.util.Random; /** * Created by Jonathan on 29/07/2017. */ public class PanelHandler { private final int panelFirstPosition = 1500; // Y-position (pixel) of the first panel. private Point screenSize; private Random random; private Panel[] panelList; private int lastPanelIndex = -1; public PanelHandler(int panelCount, int panelWidth, int panelHeight, float panelSpeed, Point screenSize) { this.screenSize = screenSize; random = new Random(); // Initialize all panels. panelList = new Panel[panelCount]; Random random = new Random(); for (int i = 0; i < panelList.length; ++i) { panelList[i] = new Panel( random.nextInt(screenSize.x - panelWidth), getNewPanelHeight(lastPanelIndex), panelWidth, panelHeight, Color.GRAY, panelSpeed ); lastPanelIndex = i; } } private void resetPanel(int index) { panelList[index].setY(getNewPanelHeight(lastPanelIndex)); lastPanelIndex = index; } private float getNewPanelHeight(int index) { float newPanelHeight; if (index == -1) { newPanelHeight = panelFirstPosition; } else { float diff = (random.nextInt(screenSize.y / 5) + 250); newPanelHeight = panelList[index].getY() - diff; } return newPanelHeight; } public void resetAllPanels() { lastPanelIndex = -1; for (int i = 0; i < panelList.length; ++i) { panelList[i].setX(screenSize.x - panelList[i].getWidth()); panelList[i].setY(getNewPanelHeight(lastPanelIndex)); } } public Panel[] getPanelList() { return panelList; } public void draw(Canvas canvas) { for (Panel panel : panelList) { Paint pPanel = new Paint(); pPanel.setColor(panel.getColor()); canvas.drawRect(panel.getX(), panel.getY(), panel.getWidth() + panel.getX(), panel.getHeight() + panel.getY(), pPanel); } } /** * Move the panel. * @param index The panel index to move. */ public void move(int index) { move(index, panelList[index].getSpeed()); } /** * * @param index The panel index to move. * @param speed The speed to move the panel. */ public void move(int index, float speed) { // If a panel is outside of the screen reset it at the top. if (panelList[index].getY() + speed > screenSize.y) { resetPanel(index); } else { // Move the panel down. panelList[index].setY(panelList[index].getY() + speed); } } }
Show line notes below