dermetfan

PortedExample

Aug 19th, 2013
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.77 KB | None | 0 0
  1. package net.dermetfan.someLibgdxTests;
  2.  
  3. import com.badlogic.gdx.ApplicationListener;
  4. import com.badlogic.gdx.Gdx;
  5. import com.badlogic.gdx.Input.Keys;
  6. import com.badlogic.gdx.InputAdapter;
  7. import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
  8. import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
  9. import com.badlogic.gdx.graphics.Color;
  10. import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
  11. import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
  12. import com.badlogic.gdx.utils.Array;
  13.  
  14. /**
  15.  * ported http://jsfiddle.net/PQr3K/3/
  16.  * @author dermetfan
  17.  */
  18. public class PortedExample implements ApplicationListener {
  19.  
  20.     private ShapeRenderer sr;
  21.     private Player p1;
  22.     private Boss b1;
  23.     private boolean rightDown, leftDown, upDown, downDown, spaceDown;
  24.  
  25.     @Override
  26.     public void create() {
  27.         sr = new ShapeRenderer();
  28.         p1 = new Player();
  29.         b1 = new Boss();
  30.  
  31.         Gdx.input.setInputProcessor(new InputAdapter() {
  32.  
  33.             @Override
  34.             public boolean keyDown(int keycode) {
  35.                 switch(keycode) {
  36.                 case Keys.RIGHT:
  37.                     rightDown = true;
  38.                     break;
  39.                 case Keys.LEFT:
  40.                     leftDown = true;
  41.                     break;
  42.                 case Keys.UP:
  43.                     upDown = true;
  44.                     break;
  45.                 case Keys.DOWN:
  46.                     downDown = true;
  47.                     break;
  48.                 case Keys.SPACE:
  49.                     spaceDown = true;
  50.                 }
  51.                 return true;
  52.             }
  53.  
  54.             @Override
  55.             public boolean keyUp(int keycode) {
  56.                 switch(keycode) {
  57.                 case Keys.RIGHT:
  58.                     rightDown = false;
  59.                     break;
  60.                 case Keys.LEFT:
  61.                     leftDown = false;
  62.                     break;
  63.                 case Keys.UP:
  64.                     upDown = false;
  65.                     break;
  66.                 case Keys.DOWN:
  67.                     downDown = false;
  68.                     break;
  69.                 case Keys.SPACE:
  70.                     spaceDown = false;
  71.                 }
  72.                 return true;
  73.             }
  74.  
  75.         });
  76.     }
  77.  
  78.     @Override
  79.     public void resize(int width, int height) {
  80.     }
  81.  
  82.     @Override
  83.     public void render() {
  84.         sr.setColor(Color.BLACK); // #000000
  85.         sr.begin(ShapeType.Filled);
  86.         sr.rect(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
  87.         sr.end();
  88.         p1.get_input(Gdx.graphics.getDeltaTime());
  89.         b1.move(Gdx.graphics.getDeltaTime());
  90.         p1.update_shots(Gdx.graphics.getDeltaTime());
  91.         p1.draw(sr);
  92.         b1.draw(sr);
  93.     }
  94.  
  95.     @Override
  96.     public void pause() {
  97.     }
  98.  
  99.     @Override
  100.     public void resume() {
  101.     }
  102.  
  103.     @Override
  104.     public void dispose() {
  105.         sr.dispose();
  106.     }
  107.  
  108.     public class Boss {
  109.  
  110.         private float x, y, width;
  111.         private String direction;
  112.  
  113.         public Boss() {
  114.             x = Gdx.graphics.getWidth() / 2;
  115.             y = 100;
  116.             width = 50;
  117.             direction = "right";
  118.         }
  119.  
  120.         public void draw(ShapeRenderer sr) {
  121.             sr.setColor(Color.PINK); // similar to #CC0099
  122.             sr.begin(ShapeType.Filled);
  123.             sr.circle(x, y, width / 2);
  124.             sr.end();
  125.         }
  126.  
  127.         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)
  128.             if(x > Gdx.graphics.getWidth() - width)
  129.                 direction = "left";
  130.             if(x < 0 + width)
  131.                 direction = "right";
  132.             if(direction == "right")
  133.                 x += 100 * delta;
  134.             if(direction == "left")
  135.                 x -= 100 * delta;
  136.         }
  137.  
  138.     }
  139.  
  140.     public class Player {
  141.  
  142.         private float x, y, width;
  143.         private Array<Shot> shots;
  144.  
  145.         public Player() {
  146.             this.x = Gdx.graphics.getWidth() / 2;
  147.             this.y = Gdx.graphics.getHeight() - 100;
  148.             this.width = 10;
  149.             this.shots = new Array<Shot>();
  150.         }
  151.  
  152.         public void get_input(float delta) {
  153.             if(rightDown)
  154.                 this.x += 250 * delta;
  155.             if(leftDown)
  156.                 this.x -= 250 * delta;
  157.             if(upDown)
  158.                 this.y += 250 * delta;
  159.             if(downDown)
  160.                 this.y -= 250 * delta;
  161.             if(spaceDown)
  162.                 this.shoot();
  163.             if(this.x > Gdx.graphics.getWidth() - this.width)
  164.                 this.x = Gdx.graphics.getWidth() - this.width;
  165.             else if(this.x < 0 + this.width)
  166.                 this.x = 0 + this.width;
  167.         }
  168.  
  169.         public void shoot() {
  170.             float s_x = this.x;
  171.             float s_y = this.y;
  172.             Shot s = new Shot(s_x, s_y);
  173.             this.shots.add(s);
  174.         }
  175.  
  176.         public void update_shots(float delta) {
  177.             for(Shot shot : shots)
  178.                 if(shot.y < 0)
  179.                     shots.removeValue(shot, true);
  180.  
  181.             for(Shot s : shots) {
  182.                 s.y -= 1500 * delta;
  183.                 s.draw(sr);
  184.             }
  185.         }
  186.  
  187.         public void draw(ShapeRenderer sr) {
  188.             sr.setColor(Color.WHITE); // similar to #CCCCCC
  189.             sr.begin(ShapeType.Filled);
  190.             sr.circle(x, y, width / 2);
  191.             sr.end();
  192.         }
  193.  
  194.     }
  195.  
  196.     public class Shot {
  197.  
  198.         public float x, y, size;
  199.  
  200.         public Shot(float x, float y) {
  201.             this.x = x;
  202.             this.y = y;
  203.             this.size = 5;
  204.         }
  205.  
  206.         public void draw(ShapeRenderer sr) {
  207.             sr.setColor(Color.RED); // #FF0000
  208.             sr.begin(ShapeType.Filled);
  209.             sr.circle(x, y, size / 2);
  210.             sr.end();
  211.         }
  212.  
  213.     }
  214.    
  215.     public static void main(String[] args) {
  216.         LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
  217.         cfg.vSyncEnabled = true;
  218.         cfg.width = 1280;
  219.         cfg.height = 720;
  220.  
  221.         new LwjglApplication(new PortedExample(), cfg);
  222.     }
  223.  
  224. }
Advertisement
Add Comment
Please, Sign In to add comment