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
This commit is contained in:
Matt Low 2018-11-11 15:51:39 +04:00
parent ec28b8bd99
commit 5756c9077c
4 changed files with 43 additions and 29 deletions

View File

@ -16,15 +16,13 @@ public class BrickBuster extends ApplicationAdapter {
public static final int HEIGHT = 600; public static final int HEIGHT = 600;
public static final String TITLE = "Brick Buster"; public static final String TITLE = "Brick Buster";
public static final Vector2 RIGHT_EDGE = new Vector2(0, 1); public static final Vector2 HORIZONTAL_EDGE = new Vector2(1, 0);
public static final Vector2 TOP_EDGE = new Vector2(-1, 0); public static final Vector2 VERTICAL_EDGE = new Vector2(0, 1);
public static final Vector2 LEFT_EDGE = new Vector2(0, -1);
public static final Vector2 BOTTOM_EDGE = new Vector2(1, 0);
Ball ball; private Ball ball;
Paddle paddle; private Paddle paddle;
ArrayList<Block> blocks; private ArrayList<Block> blocks;
boolean playing = false; private boolean playing = false;
@Override @Override
public void create () { public void create () {
@ -66,13 +64,16 @@ public class BrickBuster extends ApplicationAdapter {
@Override @Override
public void dispose () { public void dispose () {
} }
public boolean isPlaying() { public boolean isPlaying() {
return playing; return playing;
} }
public void setPlaying(boolean playing) {
this.playing = playing;
}
public Ball getBall() { public Ball getBall() {
return ball; return ball;
} }

View File

@ -1,7 +1,7 @@
package com.me.brickbuster.entity; package com.me.brickbuster.entity;
import com.badlogic.gdx.graphics.Color; 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.MathUtils;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import com.me.brickbuster.BrickBuster; import com.me.brickbuster.BrickBuster;
@ -16,14 +16,14 @@ public class Ball extends Entity {
public float speed; public float speed;
public Ball(BrickBuster brickBuster) { 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 @Override
public void render() { public void render() {
getShapeRenderer().begin(ShapeRenderer.ShapeType.Filled); getShapeRenderer().begin(ShapeType.Filled);
getShapeRenderer().setColor(BALL_COLOR); getShapeRenderer().setColor(BALL_COLOR);
getShapeRenderer().circle(this.getX(), this.getY(), RADIUS); getShapeRenderer().circle(getX(), getY(), RADIUS);
getShapeRenderer().end(); getShapeRenderer().end();
} }
@ -36,14 +36,12 @@ public class Ball extends Entity {
int newX = getX() + (int) (direction.x * speed); int newX = getX() + (int) (direction.x * speed);
int newY = getY() + (int) (direction.y * speed); int newY = getY() + (int) (direction.y * speed);
if (newX + RADIUS > BrickBuster.WIDTH) { if (newX + RADIUS > BrickBuster.WIDTH || newX - RADIUS < 0) {
Utils.reflect(direction, BrickBuster.RIGHT_EDGE); Utils.reflect(direction, BrickBuster.VERTICAL_EDGE);
} else if (newY + RADIUS > BrickBuster.HEIGHT) { } else if (newY + RADIUS > BrickBuster.HEIGHT) {
Utils.reflect(direction, BrickBuster.TOP_EDGE); Utils.reflect(direction, BrickBuster.HORIZONTAL_EDGE);
} else if (newX - RADIUS < 0) {
Utils.reflect(direction, BrickBuster.LEFT_EDGE);
} else if (newY - RADIUS < 0) { } else if (newY - RADIUS < 0) {
Utils.reflect(direction, BrickBuster.BOTTOM_EDGE); reset();
} }
setX(getX() + (int) (direction.x * speed)); setX(getX() + (int) (direction.x * speed));
@ -55,4 +53,10 @@ public class Ball extends Entity {
direction = new Vector2(MathUtils.cos(angle), MathUtils.sin(angle)); direction = new Vector2(MathUtils.cos(angle), MathUtils.sin(angle));
speed = 5; speed = 5;
} }
public void reset() {
setX(getBrickBuster().getPaddle().getX() + Paddle.PADDLE_WIDTH/2);
setY(Paddle.PADDLE_Y + Paddle.PADDLE_HEIGHT + RADIUS);
getBrickBuster().setPlaying(false);
}
} }

View File

@ -1,7 +1,7 @@
package com.me.brickbuster.entity; package com.me.brickbuster.entity;
import com.badlogic.gdx.graphics.Color; 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; import com.me.brickbuster.BrickBuster;
public class Block extends Entity { public class Block extends Entity {
@ -15,7 +15,7 @@ public class Block extends Entity {
@Override @Override
public void render() { public void render() {
getShapeRenderer().begin(ShapeRenderer.ShapeType.Filled); getShapeRenderer().begin(ShapeType.Filled);
getShapeRenderer().setColor(BLOCK_COLOR); getShapeRenderer().setColor(BLOCK_COLOR);
getShapeRenderer().rect(getX(), getY(), BLOCK_WIDTH, BLOCK_HEIGHT); getShapeRenderer().rect(getX(), getY(), BLOCK_WIDTH, BLOCK_HEIGHT);
getShapeRenderer().end(); getShapeRenderer().end();

View File

@ -3,26 +3,28 @@ package com.me.brickbuster.entity;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input; import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Color; 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 com.me.brickbuster.BrickBuster;
import net.dermetfan.utils.Pair;
public class Paddle extends Entity { public class Paddle extends Entity {
public static final Color PADDLE_COLOR = Color.BLACK; public static final Color PADDLE_COLOR = Color.BLACK;
public static final int PADDLE_WIDTH = 100; public static final int PADDLE_WIDTH = 100;
public static final int PADDLE_HEIGHT = 16; public static final int PADDLE_HEIGHT = 10;
public static final int PADDLE_Y = 25; public static final int PADDLE_Y = 15;
public static final int PADDLE_SPEED = 10; public static final int PADDLE_SPEED = 10;
public Paddle(BrickBuster brickBuster) { public Paddle(BrickBuster brickBuster) {
super(brickBuster, BrickBuster.WIDTH/2, PADDLE_Y); super(brickBuster, BrickBuster.WIDTH / 2 - PADDLE_WIDTH / 2, PADDLE_Y);
} }
@Override @Override
public void render() { public void render() {
getShapeRenderer().begin(ShapeRenderer.ShapeType.Filled); getShapeRenderer().begin(ShapeType.Filled);
getShapeRenderer().setColor(PADDLE_COLOR); 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(); getShapeRenderer().end();
} }
@ -30,7 +32,7 @@ public class Paddle extends Entity {
public void update(float dt) { public void update(float dt) {
Ball ball = getBrickBuster().getBall(); Ball ball = getBrickBuster().getBall();
if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) { if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
if (getX() - PADDLE_SPEED - PADDLE_WIDTH/2 < 0) { if (getX() - PADDLE_SPEED < 0) {
return; return;
} }
setX(getX() - PADDLE_SPEED); setX(getX() - PADDLE_SPEED);
@ -39,7 +41,7 @@ public class Paddle extends Entity {
} }
} }
if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) { 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; return;
} }
setX(getX() + PADDLE_SPEED); setX(getX() + PADDLE_SPEED);
@ -49,4 +51,11 @@ public class Paddle extends Entity {
} }
} }
public Pair<Vector2, Vector2> getTopEdge() {
return new Pair<Vector2, Vector2>(
new Vector2(getX(), getY() + PADDLE_HEIGHT),
new Vector2(getX() + PADDLE_WIDTH, getY() + PADDLE_HEIGHT)
);
}
} }