Added blocks and paddle and part of ball movement.

This commit is contained in:
BlueNutterfly 2018-11-11 11:40:03 +04:00
parent 6c6d5b34bf
commit 74b96990c5
5 changed files with 107 additions and 11 deletions

View File

@ -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<Block> 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<Block>();
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;
}
}

View File

@ -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) {
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;
}
}

View File

@ -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) {
}
}

View File

@ -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;
}

View File

@ -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);
}
}
}
}