From 5756c9077cd700d2a9472d4bd04e9ebecf6cf575 Mon Sep 17 00:00:00 2001 From: Matt Low Date: Sun, 11 Nov 2018 15:51:39 +0400 Subject: [PATCH] Reset ball on top of battle when hitting bottom edge of screen Render paddle from bottom-left Change screen-edge reflection checks Remove use of 'this' where not needed Make BrickBuster entities private --- core/src/com/me/brickbuster/BrickBuster.java | 19 +++++++------- core/src/com/me/brickbuster/entity/Ball.java | 24 ++++++++++-------- core/src/com/me/brickbuster/entity/Block.java | 4 +-- .../src/com/me/brickbuster/entity/Paddle.java | 25 +++++++++++++------ 4 files changed, 43 insertions(+), 29 deletions(-) diff --git a/core/src/com/me/brickbuster/BrickBuster.java b/core/src/com/me/brickbuster/BrickBuster.java index 380b8b1..5859a9e 100644 --- a/core/src/com/me/brickbuster/BrickBuster.java +++ b/core/src/com/me/brickbuster/BrickBuster.java @@ -16,15 +16,13 @@ public class BrickBuster extends ApplicationAdapter { public static final int HEIGHT = 600; public static final String TITLE = "Brick Buster"; - public static final Vector2 RIGHT_EDGE = new Vector2(0, 1); - public static final Vector2 TOP_EDGE = new Vector2(-1, 0); - public static final Vector2 LEFT_EDGE = new Vector2(0, -1); - public static final Vector2 BOTTOM_EDGE = new Vector2(1, 0); + public static final Vector2 HORIZONTAL_EDGE = new Vector2(1, 0); + public static final Vector2 VERTICAL_EDGE = new Vector2(0, 1); - Ball ball; - Paddle paddle; - ArrayList blocks; - boolean playing = false; + private Ball ball; + private Paddle paddle; + private ArrayList blocks; + private boolean playing = false; @Override public void create () { @@ -66,13 +64,16 @@ public class BrickBuster extends ApplicationAdapter { @Override public void dispose () { - } public boolean isPlaying() { return playing; } + public void setPlaying(boolean playing) { + this.playing = playing; + } + public Ball getBall() { return ball; } diff --git a/core/src/com/me/brickbuster/entity/Ball.java b/core/src/com/me/brickbuster/entity/Ball.java index c23af4b..84713dd 100644 --- a/core/src/com/me/brickbuster/entity/Ball.java +++ b/core/src/com/me/brickbuster/entity/Ball.java @@ -1,7 +1,7 @@ package com.me.brickbuster.entity; import com.badlogic.gdx.graphics.Color; -import com.badlogic.gdx.graphics.glutils.ShapeRenderer; +import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType; import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.Vector2; import com.me.brickbuster.BrickBuster; @@ -16,14 +16,14 @@ public class Ball extends Entity { public float speed; public Ball(BrickBuster brickBuster) { - super(brickBuster, BrickBuster.WIDTH/2, Paddle.PADDLE_Y + RADIUS + Paddle.PADDLE_HEIGHT/2); + super(brickBuster,BrickBuster.WIDTH/2, Paddle.PADDLE_Y + Paddle.PADDLE_HEIGHT + RADIUS); } @Override public void render() { - getShapeRenderer().begin(ShapeRenderer.ShapeType.Filled); + getShapeRenderer().begin(ShapeType.Filled); getShapeRenderer().setColor(BALL_COLOR); - getShapeRenderer().circle(this.getX(), this.getY(), RADIUS); + getShapeRenderer().circle(getX(), getY(), RADIUS); getShapeRenderer().end(); } @@ -36,14 +36,12 @@ public class Ball extends Entity { int newX = getX() + (int) (direction.x * speed); int newY = getY() + (int) (direction.y * speed); - if (newX + RADIUS > BrickBuster.WIDTH) { - Utils.reflect(direction, BrickBuster.RIGHT_EDGE); + if (newX + RADIUS > BrickBuster.WIDTH || newX - RADIUS < 0) { + Utils.reflect(direction, BrickBuster.VERTICAL_EDGE); } else if (newY + RADIUS > BrickBuster.HEIGHT) { - Utils.reflect(direction, BrickBuster.TOP_EDGE); - } else if (newX - RADIUS < 0) { - Utils.reflect(direction, BrickBuster.LEFT_EDGE); + Utils.reflect(direction, BrickBuster.HORIZONTAL_EDGE); } else if (newY - RADIUS < 0) { - Utils.reflect(direction, BrickBuster.BOTTOM_EDGE); + reset(); } setX(getX() + (int) (direction.x * speed)); @@ -55,4 +53,10 @@ public class Ball extends Entity { direction = new Vector2(MathUtils.cos(angle), MathUtils.sin(angle)); speed = 5; } + + public void reset() { + setX(getBrickBuster().getPaddle().getX() + Paddle.PADDLE_WIDTH/2); + setY(Paddle.PADDLE_Y + Paddle.PADDLE_HEIGHT + RADIUS); + getBrickBuster().setPlaying(false); + } } diff --git a/core/src/com/me/brickbuster/entity/Block.java b/core/src/com/me/brickbuster/entity/Block.java index 1504f64..4b43f90 100644 --- a/core/src/com/me/brickbuster/entity/Block.java +++ b/core/src/com/me/brickbuster/entity/Block.java @@ -1,7 +1,7 @@ package com.me.brickbuster.entity; import com.badlogic.gdx.graphics.Color; -import com.badlogic.gdx.graphics.glutils.ShapeRenderer; +import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType; import com.me.brickbuster.BrickBuster; public class Block extends Entity { @@ -15,7 +15,7 @@ public class Block extends Entity { @Override public void render() { - getShapeRenderer().begin(ShapeRenderer.ShapeType.Filled); + getShapeRenderer().begin(ShapeType.Filled); getShapeRenderer().setColor(BLOCK_COLOR); getShapeRenderer().rect(getX(), getY(), BLOCK_WIDTH, BLOCK_HEIGHT); getShapeRenderer().end(); diff --git a/core/src/com/me/brickbuster/entity/Paddle.java b/core/src/com/me/brickbuster/entity/Paddle.java index 7c07201..52b6585 100644 --- a/core/src/com/me/brickbuster/entity/Paddle.java +++ b/core/src/com/me/brickbuster/entity/Paddle.java @@ -3,26 +3,28 @@ package com.me.brickbuster.entity; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input; import com.badlogic.gdx.graphics.Color; -import com.badlogic.gdx.graphics.glutils.ShapeRenderer; +import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType; +import com.badlogic.gdx.math.Vector2; import com.me.brickbuster.BrickBuster; +import net.dermetfan.utils.Pair; public class Paddle extends Entity { public static final Color PADDLE_COLOR = Color.BLACK; public static final int PADDLE_WIDTH = 100; - public static final int PADDLE_HEIGHT = 16; - public static final int PADDLE_Y = 25; + public static final int PADDLE_HEIGHT = 10; + public static final int PADDLE_Y = 15; public static final int PADDLE_SPEED = 10; public Paddle(BrickBuster brickBuster) { - super(brickBuster, BrickBuster.WIDTH/2, PADDLE_Y); + super(brickBuster, BrickBuster.WIDTH / 2 - PADDLE_WIDTH / 2, PADDLE_Y); } @Override public void render() { - getShapeRenderer().begin(ShapeRenderer.ShapeType.Filled); + getShapeRenderer().begin(ShapeType.Filled); getShapeRenderer().setColor(PADDLE_COLOR); - getShapeRenderer().rect(getX() - (PADDLE_WIDTH/2), getY() - (PADDLE_HEIGHT/2), PADDLE_WIDTH, PADDLE_HEIGHT); + getShapeRenderer().rect(getX(), getY(), PADDLE_WIDTH, PADDLE_HEIGHT); getShapeRenderer().end(); } @@ -30,7 +32,7 @@ public class Paddle extends Entity { public void update(float dt) { Ball ball = getBrickBuster().getBall(); if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) { - if (getX() - PADDLE_SPEED - PADDLE_WIDTH/2 < 0) { + if (getX() - PADDLE_SPEED < 0) { return; } setX(getX() - PADDLE_SPEED); @@ -39,7 +41,7 @@ public class Paddle extends Entity { } } if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) { - if (getX() + PADDLE_SPEED + PADDLE_WIDTH/2 > BrickBuster.WIDTH) { + if (getX() + PADDLE_SPEED + PADDLE_WIDTH > BrickBuster.WIDTH) { return; } setX(getX() + PADDLE_SPEED); @@ -49,4 +51,11 @@ public class Paddle extends Entity { } } + public Pair getTopEdge() { + return new Pair( + new Vector2(getX(), getY() + PADDLE_HEIGHT), + new Vector2(getX() + PADDLE_WIDTH, getY() + PADDLE_HEIGHT) + ); + } + }