From 5edab9acbf078fee1f860557761f5aeb7e2cd3a6 Mon Sep 17 00:00:00 2001 From: edmundlui Date: Wed, 22 Jun 2016 14:29:35 -0400 Subject: [PATCH 1/4] main java file --- MaiActivity | 329 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 329 insertions(+) create mode 100644 MaiActivity diff --git a/MaiActivity b/MaiActivity new file mode 100644 index 0000000..4649704 --- /dev/null +++ b/MaiActivity @@ -0,0 +1,329 @@ +package com.example.luimi.breakout; + +import android.app.Activity; +import android.content.Context; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Paint; +import android.graphics.Point; +import android.graphics.RectF; +import android.os.Bundle; +import android.util.Log; +import android.view.Display; +import android.view.MotionEvent; +import android.view.SurfaceHolder; +import android.view.SurfaceView; + +public class MainActivity extends Activity { + + //Holds all logic for game + BreakoutView breakoutView; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + //sets the game view as a blank screen + breakoutView = new BreakoutView(this); + setContentView(breakoutView); + } + + class BreakoutView extends SurfaceView implements Runnable { + + Thread gameThread = null; + + //surface holder is there to be able to use paint + SurfaceHolder ourHolder; + + //make the game run or not run + volatile boolean playing; + + // Game is paused at the start + boolean paused = true; + + // A Canvas and a Paint object + Canvas canvas; + Paint paint; + + // calculates frames per second + long fps; + + //Helps calculate the fps + private long timeThisFrame; + + // The size of the screen + int screenX; + int screenY; + + //the paddle + Paddle paddle; + + //the ball + Ball ball; + + // sets the bricks and limits it to max 200 + Brick[] bricks = new Brick[200]; + int numBricks = 0; + + // The score + int score = 0; + + // starting lives + int lives = 3; + + public BreakoutView(Context context) { + + super(context); + + ourHolder = getHolder(); + paint = new Paint(); + + // be able to access screen details + Display display = getWindowManager().getDefaultDisplay(); + Point size = new Point(); + display.getSize(size); + + screenX = size.x; + screenY = size.y; + + //create a paddle + paddle = new Paddle(screenX, screenY); + + // Create a ball + ball = new Ball(screenX, screenY); + + } + + public void createBricksAndRestart(){ + + // Put the ball back to the start + ball.reset(screenX, screenY); + + int brickWidth = screenX / 8; + int brickHeight = screenY / 10; + + // Build a wall of bricks limiting it to 8x3 + numBricks = 0; + for(int column = 0; column < 8; column ++ ){ + for(int row = 0; row < 3; row ++ ){ + bricks[numBricks] = new Brick(row, column, brickWidth, brickHeight); + numBricks ++; + } + } + + // if game over reset scores and lives + if(lives == 0){ + score = 0; + lives = 3; + } + } + + @Override + public void run(){ + while (playing) { + + long startFrameTime = System.currentTimeMillis(); + + // Update the frame + if(!paused){ + update(); + } + + draw(); + + timeThisFrame = System.currentTimeMillis() - startFrameTime; + + if (timeThisFrame >= 1) { + fps = 1000 / timeThisFrame; + } + + } + + } + + public void update() { + + // updates all objects within the game + paddle.update(fps); + + ball.update(fps); + + // Check for ball colliding with a brick + for(int i = 0; i < numBricks; i++){ + if (bricks[i].getVisibility()){ + if(RectF.intersects(bricks[i].getRect(), ball.getRect())) { + bricks[i].setInvisible(); + ball.reverseYVelocity(); + score = score + 10; + + } + } + } + + // Check for ball colliding with paddle + if(RectF.intersects(paddle.getRect(),ball.getRect())) { + ball.setRandomXVelocity(); + ball.reverseYVelocity(); + ball.clearObstacleY(paddle.getRect().top - 2); + + } + + // Bounce the ball back when it hits the bottom of screen + if(ball.getRect().bottom > screenY){ + ball.reverseYVelocity(); + ball.clearObstacleY(screenY - 2); + + // Lose a life + lives --; + + + if(lives == 0){ + paused = true; + createBricksAndRestart(); + } + } + + // Bounce the ball back when it hits the top of screen + if(ball.getRect().top < 0){ + ball.reverseYVelocity(); + ball.clearObstacleY(12); + + + } + + // If the ball hits left wall bounce + if(ball.getRect().left < 0){ + ball.reverseXVelocity(); + ball.clearObstacleX(2); + + } + + // If the ball hits right wall bounce + if(ball.getRect().right > screenX - 10){ + ball.reverseXVelocity(); + ball.clearObstacleX(screenX - 22); + + + } + + // Pause if cleared screen + if(score == numBricks * 10){ + paused = true; + createBricksAndRestart(); + } + + } + + // Draw the new updated scene + public void draw() { + + // Make sure our drawing surface is valid or will crash + if (ourHolder.getSurface().isValid()) { + // Lock the canvas ready to draw + canvas = ourHolder.lockCanvas(); + + // Draw the background color + canvas.drawColor(Color.argb(255, 26, 128, 182)); + + // Choose the color for drawing + paint.setColor(Color.argb(255, 255, 255, 255)); + + // Draw the paddle + canvas.drawRect(paddle.getRect(), paint); + + // Draw the ball + canvas.drawRect(ball.getRect(), paint); + + // Change the color for drawing + paint.setColor(Color.argb(255, 249, 129, 0)); + + // Draw the bricks if visible + for(int i = 0; i < numBricks; i++){ + if(bricks[i].getVisibility()) { + canvas.drawRect(bricks[i].getRect(), paint); + } + } + + // Choose thecolor for drawing + paint.setColor(Color.argb(255, 255, 255, 255)); + + // Draw the score + paint.setTextSize(40); + canvas.drawText("Score: " + score + " Lives: " + lives, 10,50, paint); + + // check if player won + if(score == numBricks * 10){ + paint.setTextSize(90); + canvas.drawText("YOU HAVE WON!", 10,screenY/2, paint); + } + + // check if player lost + if(lives <= 0){ + + paint.setTextSize(90); + canvas.drawText("YOU HAVE LOST!", 10,screenY/2, paint); + } + + // Draw everything to the screen + ourHolder.unlockCanvasAndPost(canvas); + } + } + + // If SimpleGameEngine Activity is paused/stopped + // shutdown our thread. + public void pause() { + playing = false; + try { + gameThread.join(); + } + catch (InterruptedException e) { + Log.e("Error:", "joining thread"); + } + } + + public void resume() { + playing = true; + gameThread = new Thread(this); + gameThread.start(); + } + + @Override + public boolean onTouchEvent(MotionEvent motionEvent) { + switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) { + + case MotionEvent.ACTION_DOWN: + paused = false; + if(motionEvent.getX() > screenX / 2){ + paddle.setMovementState(paddle.RIGHT); + } + else{ + paddle.setMovementState(paddle.LEFT); + } + + break; + + // Player has removed finger from screen + case MotionEvent.ACTION_UP: + + paddle.setMovementState(paddle.STOPPED); + break; + } + return true; + } + + } + + @Override + protected void onResume() { + super.onResume(); + + breakoutView.resume(); + } + + @Override + protected void onPause() { + super.onPause(); + + breakoutView.pause(); + } +} From c482aae16ea50870c3d9d77ee18b0f191ce5c21d Mon Sep 17 00:00:00 2001 From: edmundlui Date: Wed, 22 Jun 2016 14:30:36 -0400 Subject: [PATCH 2/4] class for the ball --- Ball | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 Ball diff --git a/Ball b/Ball new file mode 100644 index 0000000..edceaeb --- /dev/null +++ b/Ball @@ -0,0 +1,66 @@ +package com.example.luimi.breakout; + +import android.graphics.RectF; +import java.util.Random; + +public class Ball { + RectF rect; + float xVelocity; + float yVelocity; + float ballWidth = 10; + float ballHeight = 10; + + public Ball(int screenX, int screenY){ + + xVelocity = 200; + yVelocity = -400; + + rect = new RectF(); + } + + public RectF getRect(){ + return rect; + } + + public void update(long fps){ + rect.left = rect.left + (xVelocity / fps); + rect.top = rect.top + (yVelocity / fps); + rect.right = rect.left + ballWidth; + rect.bottom = rect.top - ballHeight; + } + + public void reverseYVelocity(){ + yVelocity = -yVelocity; + } + + public void reverseXVelocity(){ + xVelocity = - xVelocity; + } + + public void setRandomXVelocity(){ + Random generator = new Random(); + int answer = generator.nextInt(2); + + if(answer == 0){ + reverseXVelocity(); + } + } + + public void clearObstacleY(float y){ + rect.bottom = y; + rect.top = y - ballHeight; + } + + public void clearObstacleX(float x){ + rect.left = x; + rect.right = x + ballWidth; + } + + public void reset(int x, int y){ + rect.left = x / 2; + rect.top = y - 20; + rect.right = x / 2 + ballWidth; + rect.bottom = y - 20 - ballHeight; + } + +} From 14ad8050e045381ee863970fe1be0ba5b3c8778b Mon Sep 17 00:00:00 2001 From: edmundlui Date: Wed, 22 Jun 2016 14:31:36 -0400 Subject: [PATCH 3/4] class for brick --- Brick | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Brick diff --git a/Brick b/Brick new file mode 100644 index 0000000..426fc91 --- /dev/null +++ b/Brick @@ -0,0 +1,34 @@ +package com.example.luimi.breakout; + +import android.graphics.RectF; + +public class Brick { + + private RectF rect; + + private boolean isVisible; + + public Brick(int row, int column, int width, int height){ + + isVisible = true; + + int padding = 1; + + rect = new RectF(column * width + padding, + row * height + padding, + column * width + width - padding, + row * height + height - padding); + } + + public RectF getRect(){ + return this.rect; + } + + public void setInvisible(){ + isVisible = false; + } + + public boolean getVisibility(){ + return isVisible; + } +} From fa0a3bd62e934fbec82bd488cdc0b0110febf301 Mon Sep 17 00:00:00 2001 From: edmundlui Date: Wed, 22 Jun 2016 14:33:51 -0400 Subject: [PATCH 4/4] class for paddle --- Paddle | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Paddle diff --git a/Paddle b/Paddle new file mode 100644 index 0000000..c2d72a4 --- /dev/null +++ b/Paddle @@ -0,0 +1,52 @@ +package com.example.luimi.breakout; + +import android.graphics.RectF; + +public class Paddle { + + private RectF rect; + private float length; + private float height; + private float x; + private float y; + private float paddleSpeed; + public final int STOPPED = 0; + public final int LEFT = 1; + public final int RIGHT = 2; + private int paddleMoving = STOPPED; + + public Paddle(int screenX, int screenY){ + + length = 130; + height = 20; + + x = screenX / 2; + y = screenY - 20; + + rect = new RectF(x, y, x + length, y + height); + + paddleSpeed = 350; + } + + public RectF getRect(){ + return rect; + } + + public void setMovementState(int state){ + paddleMoving = state; + } + + public void update(long fps){ + if(paddleMoving == LEFT){ + x = x - paddleSpeed / fps; + } + + if(paddleMoving == RIGHT){ + x = x + paddleSpeed / fps; + } + + rect.left = x; + rect.right = x + length; + } + +}