Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.dermetfan.someLibgdxTests;
- import com.badlogic.gdx.ApplicationListener;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.Input.Keys;
- import com.badlogic.gdx.InputAdapter;
- import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
- import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
- 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.utils.Array;
- /**
- * ported http://jsfiddle.net/PQr3K/3/
- * @author dermetfan
- */
- public class PortedExample implements ApplicationListener {
- private ShapeRenderer sr;
- private Player p1;
- private Boss b1;
- private boolean rightDown, leftDown, upDown, downDown, spaceDown;
- @Override
- public void create() {
- sr = new ShapeRenderer();
- p1 = new Player();
- b1 = new Boss();
- Gdx.input.setInputProcessor(new InputAdapter() {
- @Override
- public boolean keyDown(int keycode) {
- switch(keycode) {
- case Keys.RIGHT:
- rightDown = true;
- break;
- case Keys.LEFT:
- leftDown = true;
- break;
- case Keys.UP:
- upDown = true;
- break;
- case Keys.DOWN:
- downDown = true;
- break;
- case Keys.SPACE:
- spaceDown = true;
- }
- return true;
- }
- @Override
- public boolean keyUp(int keycode) {
- switch(keycode) {
- case Keys.RIGHT:
- rightDown = false;
- break;
- case Keys.LEFT:
- leftDown = false;
- break;
- case Keys.UP:
- upDown = false;
- break;
- case Keys.DOWN:
- downDown = false;
- break;
- case Keys.SPACE:
- spaceDown = false;
- }
- return true;
- }
- });
- }
- @Override
- public void resize(int width, int height) {
- }
- @Override
- public void render() {
- sr.setColor(Color.BLACK); // #000000
- sr.begin(ShapeType.Filled);
- sr.rect(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
- sr.end();
- p1.get_input(Gdx.graphics.getDeltaTime());
- b1.move(Gdx.graphics.getDeltaTime());
- p1.update_shots(Gdx.graphics.getDeltaTime());
- p1.draw(sr);
- b1.draw(sr);
- }
- @Override
- public void pause() {
- }
- @Override
- public void resume() {
- }
- @Override
- public void dispose() {
- sr.dispose();
- }
- public class Boss {
- private float x, y, width;
- private String direction;
- public Boss() {
- x = Gdx.graphics.getWidth() / 2;
- y = 100;
- width = 50;
- direction = "right";
- }
- public void draw(ShapeRenderer sr) {
- sr.setColor(Color.PINK); // similar to #CC0099
- sr.begin(ShapeType.Filled);
- sr.circle(x, y, width / 2);
- sr.end();
- }
- public void move(float delta) { // delta is the time that passed since the last frame in seconds (to keep the same movement speed across all framerates)
- if(x > Gdx.graphics.getWidth() - width)
- direction = "left";
- if(x < 0 + width)
- direction = "right";
- if(direction == "right")
- x += 100 * delta;
- if(direction == "left")
- x -= 100 * delta;
- }
- }
- public class Player {
- private float x, y, width;
- private Array<Shot> shots;
- public Player() {
- this.x = Gdx.graphics.getWidth() / 2;
- this.y = Gdx.graphics.getHeight() - 100;
- this.width = 10;
- this.shots = new Array<Shot>();
- }
- public void get_input(float delta) {
- if(rightDown)
- this.x += 250 * delta;
- if(leftDown)
- this.x -= 250 * delta;
- if(upDown)
- this.y += 250 * delta;
- if(downDown)
- this.y -= 250 * delta;
- if(spaceDown)
- this.shoot();
- if(this.x > Gdx.graphics.getWidth() - this.width)
- this.x = Gdx.graphics.getWidth() - this.width;
- else if(this.x < 0 + this.width)
- this.x = 0 + this.width;
- }
- public void shoot() {
- float s_x = this.x;
- float s_y = this.y;
- Shot s = new Shot(s_x, s_y);
- this.shots.add(s);
- }
- public void update_shots(float delta) {
- for(Shot shot : shots)
- if(shot.y < 0)
- shots.removeValue(shot, true);
- for(Shot s : shots) {
- s.y -= 1500 * delta;
- s.draw(sr);
- }
- }
- public void draw(ShapeRenderer sr) {
- sr.setColor(Color.WHITE); // similar to #CCCCCC
- sr.begin(ShapeType.Filled);
- sr.circle(x, y, width / 2);
- sr.end();
- }
- }
- public class Shot {
- public float x, y, size;
- public Shot(float x, float y) {
- this.x = x;
- this.y = y;
- this.size = 5;
- }
- public void draw(ShapeRenderer sr) {
- sr.setColor(Color.RED); // #FF0000
- sr.begin(ShapeType.Filled);
- sr.circle(x, y, size / 2);
- sr.end();
- }
- }
- public static void main(String[] args) {
- LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
- cfg.vSyncEnabled = true;
- cfg.width = 1280;
- cfg.height = 720;
- new LwjglApplication(new PortedExample(), cfg);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment