diff --git a/core/src/com/me/brickbuster/BrickBuster.java b/core/src/com/me/brickbuster/BrickBuster.java index d21beca..229e99f 100644 --- a/core/src/com/me/brickbuster/BrickBuster.java +++ b/core/src/com/me/brickbuster/BrickBuster.java @@ -4,8 +4,11 @@ import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.GL20; import com.me.brickbuster.entity.Ball; +import com.me.brickbuster.entity.Block; import com.me.brickbuster.entity.Paddle; +import java.util.ArrayList; + public class BrickBuster extends ApplicationAdapter { public static final int WIDTH = 800; @@ -14,11 +17,21 @@ public class BrickBuster extends ApplicationAdapter { Ball ball; Paddle paddle; + ArrayList blocks; + boolean playing = false; @Override public void create () { - ball = new Ball(); - paddle = new Paddle(); + ball = new Ball(this); + paddle = new Paddle(this); + blocks = new ArrayList(); + for (int col = 0; col < 13; col++) { + for (int row = 0; row < 10; row++) { + int x = 15 + (col * (Block.BLOCK_WIDTH + 10)); + int y = 15 + Block.BLOCK_HEIGHT + (row * (Block.BLOCK_HEIGHT + 5)); + blocks.add(new Block(this, x, HEIGHT - y)); + } + } } @Override @@ -30,9 +43,17 @@ public class BrickBuster extends ApplicationAdapter { ball.render(); paddle.render(); + + for (Block block : blocks) { + block.render(); + } } public void update(float dt) { + if (Gdx.input.justTouched()) { + playing = true; + ball.launch(); + } ball.update(dt); paddle.update(dt); } @@ -41,4 +62,16 @@ public class BrickBuster extends ApplicationAdapter { public void dispose () { } + + public boolean isPlaying() { + return playing; + } + + public Ball getBall() { + return ball; + } + + public Paddle getPaddle() { + return paddle; + } } diff --git a/core/src/com/me/brickbuster/entity/Ball.java b/core/src/com/me/brickbuster/entity/Ball.java index 1eee533..494cca2 100644 --- a/core/src/com/me/brickbuster/entity/Ball.java +++ b/core/src/com/me/brickbuster/entity/Ball.java @@ -2,6 +2,7 @@ package com.me.brickbuster.entity; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; +import com.badlogic.gdx.math.Vector2; import com.me.brickbuster.BrickBuster; public class Ball extends Entity { @@ -9,8 +10,11 @@ public class Ball extends Entity { public static final int RADIUS = 25; public static final Color BALL_COLOR = Color.CHARTREUSE; - public Ball() { - super(400, 300); + public Vector2 direction; + public float speed; + + public Ball(BrickBuster brickBuster) { + super(brickBuster, 400, 83); } @Override @@ -23,8 +27,18 @@ public class Ball extends Entity { @Override public void update(float dt) { - if (this.getX() + RADIUS < BrickBuster.WIDTH) { - this.setX(this.getX() + 1); + if (!getBrickBuster().isPlaying()) { + return; } + setX(getX() + (int) (direction.x * speed)); + setY(getY() + (int) (direction.y * speed)); + /*if (this.getX() + RADIUS < BrickBuster.WIDTH) { + this.setX(this.getX() + 1); + }*/ + } + + public void launch() { + direction = new Vector2(0, 1); + speed = 5; } } diff --git a/core/src/com/me/brickbuster/entity/Block.java b/core/src/com/me/brickbuster/entity/Block.java new file mode 100644 index 0000000..8ba9d7e --- /dev/null +++ b/core/src/com/me/brickbuster/entity/Block.java @@ -0,0 +1,28 @@ +package com.me.brickbuster.entity; + +import com.badlogic.gdx.graphics.Color; +import com.badlogic.gdx.graphics.glutils.ShapeRenderer; +import com.me.brickbuster.BrickBuster; + +public class Block extends Entity { + public static final Color BLOCK_COLOR = Color.FOREST; + public static final int BLOCK_WIDTH = 50; + public static final int BLOCK_HEIGHT = 15; + + public Block(BrickBuster brickBuster, int x, int y) { + super(brickBuster, x, y); + } + + @Override + public void render() { + getShapeRenderer().begin(ShapeRenderer.ShapeType.Filled); + getShapeRenderer().setColor(BLOCK_COLOR); + getShapeRenderer().rect(getX(), getY(), BLOCK_WIDTH, BLOCK_HEIGHT); + getShapeRenderer().end(); + } + + @Override + public void update(float dt) { + + } +} diff --git a/core/src/com/me/brickbuster/entity/Entity.java b/core/src/com/me/brickbuster/entity/Entity.java index 620fccc..6791e90 100644 --- a/core/src/com/me/brickbuster/entity/Entity.java +++ b/core/src/com/me/brickbuster/entity/Entity.java @@ -1,14 +1,17 @@ package com.me.brickbuster.entity; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; +import com.me.brickbuster.BrickBuster; public abstract class Entity { private ShapeRenderer shapeRenderer; + private BrickBuster brickBuster; private int x, y; - public Entity(int x, int y) { + public Entity(BrickBuster brickBuster, int x, int y) { this.shapeRenderer = new ShapeRenderer(); + this.brickBuster = brickBuster; this.x = x; this.y = y; } @@ -17,6 +20,10 @@ public abstract class Entity { public abstract void update(float dt); + public BrickBuster getBrickBuster() { + return brickBuster; + } + public ShapeRenderer getShapeRenderer() { return shapeRenderer; } diff --git a/core/src/com/me/brickbuster/entity/Paddle.java b/core/src/com/me/brickbuster/entity/Paddle.java index 9b25e2b..0451f59 100644 --- a/core/src/com/me/brickbuster/entity/Paddle.java +++ b/core/src/com/me/brickbuster/entity/Paddle.java @@ -1,5 +1,7 @@ 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.me.brickbuster.BrickBuster; @@ -7,11 +9,11 @@ import com.me.brickbuster.BrickBuster; public class Paddle extends Entity { public static final Color PADDLE_COLOR = Color.BLACK; - public static final int PADDLE_WIDTH = 50; + public static final int PADDLE_WIDTH = 100; public static final int PADDLE_HEIGHT = 15; - public Paddle() { - super(BrickBuster.WIDTH/2, 50); + public Paddle(BrickBuster brickBuster) { + super(brickBuster, BrickBuster.WIDTH/2, 50); } @Override @@ -24,7 +26,19 @@ public class Paddle extends Entity { @Override public void update(float dt) { - + Ball ball = getBrickBuster().getBall(); + if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) { + setX(getX() - 5); + if (!getBrickBuster().isPlaying()) { + ball.setX(ball.getX() - 5); + } + } + if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) { + setX(getX() + 5); + if (!getBrickBuster().isPlaying()) { + ball.setX(ball.getX() + 5); + } + } } }