From bb6ecf292332c816a7d89079fc96eaebb143c96e Mon Sep 17 00:00:00 2001 From: BlueNutterfly Date: Sat, 10 Nov 2018 22:30:23 +0400 Subject: [PATCH] Add ball and paddle --- core/src/com/me/brickbuster/BrickBuster.java | 29 +++++++------- core/src/com/me/brickbuster/entity/Ball.java | 30 ++++++++++++++ .../src/com/me/brickbuster/entity/Entity.java | 40 +++++++++++++++++++ .../src/com/me/brickbuster/entity/Paddle.java | 30 ++++++++++++++ 4 files changed, 114 insertions(+), 15 deletions(-) create mode 100644 core/src/com/me/brickbuster/entity/Ball.java create mode 100644 core/src/com/me/brickbuster/entity/Entity.java create mode 100644 core/src/com/me/brickbuster/entity/Paddle.java diff --git a/core/src/com/me/brickbuster/BrickBuster.java b/core/src/com/me/brickbuster/BrickBuster.java index c23d84a..d21beca 100644 --- a/core/src/com/me/brickbuster/BrickBuster.java +++ b/core/src/com/me/brickbuster/BrickBuster.java @@ -2,22 +2,23 @@ package com.me.brickbuster; import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.Gdx; -import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.GL20; -import com.badlogic.gdx.graphics.glutils.ShapeRenderer; -import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType; +import com.me.brickbuster.entity.Ball; +import com.me.brickbuster.entity.Paddle; public class BrickBuster extends ApplicationAdapter { - public static final int WIDTH = 800; - public static final int HEIGHT = 600; - public static final String TITLE = "Brick Buster"; + public static final int WIDTH = 800; + public static final int HEIGHT = 600; + public static final String TITLE = "Brick Buster"; + + Ball ball; + Paddle paddle; - ShapeRenderer shapeRenderer; - @Override public void create () { - this.shapeRenderer = new ShapeRenderer(); + ball = new Ball(); + paddle = new Paddle(); } @Override @@ -26,16 +27,14 @@ public class BrickBuster extends ApplicationAdapter { Gdx.gl.glClearColor(0.5f,1,1,1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); - this.shapeRenderer.begin(ShapeType.Filled); - this.shapeRenderer.setColor(Color.CHARTREUSE); - this.shapeRenderer.circle(0,0, 100, 50); - this.shapeRenderer.end(); + ball.render(); + paddle.render(); } - @Override public void update(float dt) { - + ball.update(dt); + paddle.update(dt); } @Override diff --git a/core/src/com/me/brickbuster/entity/Ball.java b/core/src/com/me/brickbuster/entity/Ball.java new file mode 100644 index 0000000..1eee533 --- /dev/null +++ b/core/src/com/me/brickbuster/entity/Ball.java @@ -0,0 +1,30 @@ +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 Ball extends Entity { + + public static final int RADIUS = 25; + public static final Color BALL_COLOR = Color.CHARTREUSE; + + public Ball() { + super(400, 300); + } + + @Override + public void render() { + getShapeRenderer().begin(ShapeRenderer.ShapeType.Filled); + getShapeRenderer().setColor(BALL_COLOR); + getShapeRenderer().circle(this.getX(), this.getY(), RADIUS, 50); + getShapeRenderer().end(); + } + + @Override + public void update(float dt) { + if (this.getX() + RADIUS < BrickBuster.WIDTH) { + this.setX(this.getX() + 1); + } + } +} diff --git a/core/src/com/me/brickbuster/entity/Entity.java b/core/src/com/me/brickbuster/entity/Entity.java new file mode 100644 index 0000000..620fccc --- /dev/null +++ b/core/src/com/me/brickbuster/entity/Entity.java @@ -0,0 +1,40 @@ +package com.me.brickbuster.entity; + +import com.badlogic.gdx.graphics.glutils.ShapeRenderer; + +public abstract class Entity { + + private ShapeRenderer shapeRenderer; + private int x, y; + + public Entity(int x, int y) { + this.shapeRenderer = new ShapeRenderer(); + this.x = x; + this.y = y; + } + + public abstract void render(); + + public abstract void update(float dt); + + public ShapeRenderer getShapeRenderer() { + return shapeRenderer; + } + + public int getX() { + return x; + } + + public void setX(int x) { + this.x = x; + } + + public int getY() { + return y; + } + + public void setY(int y) { + this.y = y; + } + +} diff --git a/core/src/com/me/brickbuster/entity/Paddle.java b/core/src/com/me/brickbuster/entity/Paddle.java new file mode 100644 index 0000000..9b25e2b --- /dev/null +++ b/core/src/com/me/brickbuster/entity/Paddle.java @@ -0,0 +1,30 @@ +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 Paddle extends Entity { + + public static final Color PADDLE_COLOR = Color.BLACK; + public static final int PADDLE_WIDTH = 50; + public static final int PADDLE_HEIGHT = 15; + + public Paddle() { + super(BrickBuster.WIDTH/2, 50); + } + + @Override + public void render() { + getShapeRenderer().begin(ShapeRenderer.ShapeType.Filled); + getShapeRenderer().setColor(PADDLE_COLOR); + getShapeRenderer().rect(getX() - (PADDLE_WIDTH/2), getY() - (PADDLE_HEIGHT/2), PADDLE_WIDTH, PADDLE_HEIGHT); + getShapeRenderer().end(); + } + + @Override + public void update(float dt) { + + } + +}