Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (517)
Games in Android Showcase (123)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: BurntPizza (posted 2014-08-08 02:01:56, viewed 60 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101  
public class GameScreen implements Screen {
   
   MyGame mg;
           
   SpriteBatch batch;
           
   Vector2 turret = new Vector2(10, 10);

   List<Bullet> bullets;
           
   public GameScreen(MyGame mg) {
      this.mg = mg;
      bullets = new ArrayList<>();
      bullets.add(new Bullet(turret)); // start with 1 bullet at the turret position
   }
 
   @Override
   public void render(float delta) {
      Gdx.gl.glClearColor(1F, 1F, 1F, 1F);
      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
      
      update(delta);
           
      batch.begin();
      
      for(Bullet b : bullets)
         b.render(batch);
       
      batch.end();
   }
           
   public void update(float delta) {
        for(Bullet b : bullets)
           b.update(delta);
   }
 
   @Override
   public void resize(int width, int height) {
      // TODO Auto-generated method stub
             
   }
 
   @Override
   public void show() {
      batch = new SpriteBatch();    
   }
 
   @Override
   public void hide() {
      // TODO Auto-generated method stub
             
   }
 
   @Override
   public void pause() {
      // TODO Auto-generated method stub
             
   }
 
   @Override
   public void resume() {
      // TODO Auto-generated method stub
             
   }
   
   @Override
   public void dispose() {
      // TODO Auto-generated method stub
             
   }
 
}

class Bullet {
   
   public static final float BULLET_SPEED = 10; // modify to your liking
   public static final Sprite BULLET_SPRITE = new Sprite(new Texture(Gdx.files.internal("HUDS/button_Back.png")));

   Vector2 velocity = new Vector2();
   boolean velocityHasBeenSet = false;
   Vector2 position = new Vector2();

   public Bullet(Vector2 initPosition) {
      position.set(initPosition);
   }

   public void update(float delta) {
      if(Gdx.input.isButtonPressed(0) && !velocityHasBeenSet) {
         Vector2 dir = new Vector2(Gdx.input.getX(), Gdx.graphics.getHeight() - 1 - Gdx.input.getY());
         dir.sub(position).nor().scl(BULLET_SPEED);
         velocity.set(dir);
         velocityHasBeenSet = true;
      }

      position.add(velocity.scl(delta));
   }

   public void render(SpriteBatch batch) {
      batch.draw(BULLET_SPRITE, position.x, position.y);
   }
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (34 views)
2014-10-27 03:28:38

TehJavaDev (27 views)
2014-10-27 03:27:51

DarkCart (41 views)
2014-10-26 19:37:11

Luminem (22 views)
2014-10-26 10:17:50

Luminem (27 views)
2014-10-26 10:14:04

theagentd (33 views)
2014-10-25 15:46:29

Longarmx (61 views)
2014-10-17 03:59:02

Norakomi (59 views)
2014-10-16 15:22:06

Norakomi (48 views)
2014-10-16 15:20:20

lcass (43 views)
2014-10-15 16:18:58
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!