Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
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: davedes (posted 2013-04-29 11:48:48, viewed 150 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   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183  
package mdesl.line2dx.test;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.utils.Array;

 
public class TestMenu implements ApplicationListener {

   //in LibGDX, an Image is kind of like Java2D's Label
   //it allows us to render textures and resize/reposition/etc them
   class Entity extends Image {
      
      //speed of movement
      public float speed = 85f;
      
      //the velocity of this entity
      public float vx, vy;
      
      public Entity(Texture tex) {
         super(tex);
         super.addListener(new InputListener() {
            
            //on mouse hover, tint it red...
            public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
               setColor(Color.RED);
            }
            
            //when the mouse isn't over, make it white again
            public void exit(InputEvent event, float x, float y, int pointer, Actor fromActor) {
               setColor(Color.WHITE);
            }
            
            //on click
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
               setVisible(false);               
               return true; 
            }
         });
      }
      
      //this is the 'update' method called from game loop
      public void act(float delta) {
         super.act(delta);
         
         //update the position based on velocity & delta time
         translate(vx * speed * delta, vy * speed * delta);
      }
   }

   Stage stage;
   Array<Entity> entities;
   BitmapFont font;
   
   @Override
   public void create() {
      //set up a new stage
      stage = new Stage();
      
      //make sure input goes through our stage
      Gdx.input.setInputProcessor(stage);
      
      //add a new input listener for the whole stage.. 
      stage.addListener(new MyInputListener());
      
      //first we need to create a Texture; this is what stores image data on GPU
      //Gdx.files.internal gets a resource from our class-path (i.e. inside our game's executable JAR)
      Texture tex = new Texture(Gdx.files.internal("data/lookup.png"));
      
      //Array<T> in LibGDX is the same as ArrayList<T>
      //we could also use ArrayList, but Array is a bit more optimized for Android/iOS/etc
      entities = new Array<Entity>();
      
      final int MAX_ENTITIES = 10;
      for (int i=0; i<MAX_ENTITIES; i++) {
         Entity e = new Entity(tex);
         
         //random position inside screen.. use LibGDX MathUtils for convenience
         e.setPosition( 
               MathUtils.random(0, Gdx.graphics.getWidth()), 
               MathUtils.random(0, Gdx.graphics.getHeight()));
         
         //random velocity
         e.vx = MathUtils.randomBoolean() ? 1f : -1f;
         e.vy = MathUtils.randomBoolean() ? 1f : -1f;
         
         //add to list
         entities.add(e);
         
         //we also need to add it to the stage, otherwise the entities won't get rendered
         stage.addActor(e);
      }
      
      //to render text, we need to create a bitmap font... let's use the default one:
      font = new BitmapFont();
   }
   
   @Override
   public void resize(int width, int height) {
      //resize the stage to the new window size
      stage.setViewport(width, height, false);
   }
 
   @Override
   public void render() {
      //clear the screen
      Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
      
      for (int i=0; i<entities.size; i++) {
         Entity e = entities.get(i);
         
         //if the entity hits an edge, reverse its direction...         
         if (e.getX() + e.getWidth() >= Gdx.graphics.getWidth()) {
            e.vx = -1f;
         } 
         if (e.getY() + e.getHeight() >= Gdx.graphics.getHeight()) {
            e.vy = -1f;
         } 
         if (e.getX() < 0) {
            e.vx = 1f;
         } 
         if (e.getY() < 0) {
            e.vy = 1f;
         }
      }
      
      //update and draw the stage UI
      //this will call our entity's act() method
      stage.act();
      stage.draw();
      
      //we could also have added the text to the stage as a Label widget
      //but this demonstrates another way of rendering directly to the screen :)
      SpriteBatch batch = stage.getSpriteBatch();
      batch.begin();
      font.draw(batch, "Press SPACE to reset", 10, Gdx.graphics.getHeight()-10);
      batch.end();
   }
   
   protected void reset() {
      //show all entities
      for (Entity e : entities)
         e.setVisible(true);
   }
 
   @Override
   public void pause() {
      
   }
 
   @Override
   public void resume() {
      
   }
 
   @Override
   public void dispose() {
      //dispose resources when shutting down the app
      stage.dispose();
   }
   
   class MyInputListener extends InputListener {
      public boolean keyDown(InputEvent event, int keycode) {
         if (keycode == Keys.SPACE) {
            reset();
            return true;
         } 
         return false;
      }
   }
}





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.

rwatson462 (32 views)
2014-12-15 09:26:44

Mr.CodeIt (23 views)
2014-12-14 19:50:38

BurntPizza (50 views)
2014-12-09 22:41:13

BurntPizza (84 views)
2014-12-08 04:46:31

JscottyBieshaar (45 views)
2014-12-05 12:39:02

SHC (59 views)
2014-12-03 16:27:13

CopyableCougar4 (57 views)
2014-11-29 21:32:03

toopeicgaming1999 (123 views)
2014-11-26 15:22:04

toopeicgaming1999 (114 views)
2014-11-26 15:20:36

toopeicgaming1999 (32 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

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
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!