Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (552)
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: Nanor (posted 2014-05-08 11:16:25, viewed 59 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   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199   200   201   202   203   204   205   206   207   208   209   210   211   212   213   214   215   216   217   218   219   220   221   222   223   224   225   226   227   228   229   230   231   232   233   234   235   236   237   238   239   240   241   242   243   244   245   246   247   248   249   250   251   252   253   254   255   256   257   258   259   260   261   262   263   264   265   266   267   268   269   270   271   272   273   274   275   276   277   278   279   280   281   282   283   284   285   286   287   288   289   290   291   292   293   294   295   296   297   298   299   300   301   302   303   304   305   306   307   308   309   310   311   312   313   314   315   316   317   318   319   320   321   322   323   324  
package com.gamefromscratch.helloworld;
 
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.input.GestureDetector;
import com.badlogic.gdx.input.GestureDetector.GestureListener;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.List;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.StretchViewport;
import com.badlogic.gdx.utils.viewport.Viewport;

import java.util.ArrayList;
import java.util.Iterator;
 
public class HelloWorld implements ApplicationListener, GestureListener {

   private OrthographicCameraWithVirtualViewport camera;
   
   private SpriteBatch spriteBatch;
   private Sprite minimumAreaSprite, maximumAreaSprite, floatingButtonSprite;
   private Texture playerTexture, diamondTexture;
   private Sprite playerSprite;
   private Viewport viewport;
   private Player player;
   private BitmapFont font;
   //private OrthographicCamera camera;
   private MultipleVirtualViewportBuilder multipleVirtualViewportBuilder;
   
   private float gravity = -0.3f,
         timeToJump = 0,
         timeToDiamond = 0;
   
   ArrayList<Diamond> diamonds = new ArrayList<Diamond>();
   ArrayList<Sprite> diamondSprites = new ArrayList<Sprite>();
   
   
   private boolean ignoreCollisions = false, started = false;
   
   private int screenWidth, screenHeight, playerWidth, playerHeight, scale = 1, deltaTime;
   
   private enum states{
      menu,
      running,
      dead;
   }
   
   private states gameState;
      
   @Override
   public void create() {
      gameState = states.menu;
      multipleVirtualViewportBuilder = new MultipleVirtualViewportBuilder(800, 480, 1280, 1024);  
        VirtualViewport virtualViewport = multipleVirtualViewportBuilder.getVirtualViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());  
        
      camera = new OrthographicCameraWithVirtualViewport(virtualViewport);
      camera.position.set(0f, 0f, 0f);
      
      font = new BitmapFont();
      font.setColor(Color.WHITE);
      
        Pixmap pixmap = new Pixmap(64, 64, Format.RGBA8888);  
        pixmap.setColor(Color.WHITE);  
        pixmap.fillRectangle(0, 0, 64, 64);  
              
      spriteBatch = new SpriteBatch();
      
      playerTexture = new Texture(Gdx.files.internal("data/cube.png"));
      playerTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
      
      diamondTexture = new Texture(Gdx.files.internal("data/diamond.png"));
      diamondTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
      
      player = new Player((int) (Gdx.graphics.getWidth()/2 - playerTexture.getWidth()), 0);      

      screenWidth = Gdx.graphics.getWidth();
      screenHeight = Gdx.graphics.getHeight();
      
      playerSprite = new Sprite(playerTexture);
      playerSprite.setOrigin(0, 0);
      playerSprite.setPosition(player.getPosX(), player.getPosY());
      playerSprite.scale(scale);
      
      playerWidth = (int) (playerSprite.getWidth() * scale);
      playerHeight = (int) (playerSprite.getHeight() * scale);
      
      Gdx.input.setInputProcessor(new GestureDetector(this));
      
   }
   
   @Override
   public boolean tap(float x, float y, int count, int button) {
      started = true;
      
      if(gameState == states.menu)
         gameState = states.running;
      else if (gameState == states.dead)
         gameState = states.menu;
      
      if(player.canJump()){
         if(player.isFacingLeft())
            player.setVelX(-36.0f);
         else
            player.setVelX(36.0f);
         player.setVelY(25.0f);
         player.setCanJump(false);
      }
      
      return false;
   }

   @Override
   public void render() {
      camera.update();
      Gdx.gl.glClearColor(0.204f, 0.255f, 0.255f, 1);
      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
      spriteBatch.setProjectionMatrix(camera.combined);
      spriteBatch.begin();   
      
      if(gameState == states.menu){
         font.draw(spriteBatch, "Tap anywhere to begin!", screenWidth/2, screenHeight-100);
      }else if(gameState == states.running){
         updateRunning();
         playerSprite.draw(spriteBatch);
         
         for(Iterator<Sprite> i = diamondSprites.iterator(); i.hasNext();){
            Sprite item = i.next();
            item.draw(spriteBatch);
         }
         
      }else if(gameState == states.dead){
         font.drawMultiLine(spriteBatch, "You have died!\nTap Anywhere to start again", screenWidth/2, screenHeight-100);
      }
      
      
      
      spriteBatch.end();
      
   }

   private void updateRunning() {      
      //if on ground
      if(player.getPosY() < 0 && player.canJump()){
         player.setPosY(0);
         player.setVelY(0);
         if(started){
            gameState = states.dead;
         }
      }
      else{
         player.setVelY(player.getVelY() + gravity);
      }
      
      //If collides with a wall, switch direction and set velocities
      if(wallCollision() == "left"){
         player.setPosX(0);
         player.setVelX(0);
         ignoreCollisions = true;
         player.setCanJump(true);
         player.setFacingLeft(false);
      } else if (wallCollision() == "right"){
         player.setPosX(screenWidth-playerWidth*2);
         player.setVelX(0);
         ignoreCollisions = true;
         player.setCanJump(true);
         player.setFacingLeft(true);
      } else{
         player.setVelY(player.getVelY() + gravity);
      } 
      
      updateDiamonds();
      
      player.setPosX(player.getPosX() + player.getVelX());
      player.setPosY(player.getPosY() + player.getVelY());
      playerSprite.setPosition(player.getPosX(), player.getPosY());
   }

   private void updateDiamonds() {
      //If between 10-15 seconds have passed between last diamond, spawn one
      if(timeToDiamond > 5.0f){
         timeToDiamond = 0;
         Diamond diamond = new Diamond(screenHeight, screenWidth);
         Sprite dSprite = new Sprite(diamondTexture);
         dSprite.setOrigin(0, 0);
         diamonds.add(diamond);
         diamondSprites.add(dSprite);
      }
      else{
         timeToDiamond += Gdx.graphics.getDeltaTime();
      }
      
      int counter = 0;
      for(Iterator<Diamond> i = diamonds.iterator(); i.hasNext();){
         Diamond item = i.next();
         if(item.getPosY() < -20){
            i.remove();
            diamondSprites.remove(counter);
         }
         else{
            item.setVelY(item.getVelY() + gravity);
            item.setPosY(item.getVelY() + item.getPosY());
            diamondSprites.get(counter).setPosition(item.getPosX(), item.getPosY());
         }
         counter++;
      }
      
      
   }

   private String wallCollision(){
      if(player.getPosX() < playerWidth && !ignoreCollisions)
         return "left";
      else if(player.getPosX() > screenWidth - playerWidth*2 && !ignoreCollisions)
         return "right";
      else{      
         timeToJump += Gdx.graphics.getDeltaTime();
         if(timeToJump > 0.5f){
            timeToJump = 0;
            ignoreCollisions = false;
         }
         return "jumping";
      }
   }

   /*
    * 
    * HERE BE USELESS SHITE
    * 
    * 
    */
   
   
   @Override
   public boolean touchDown(float x, float y, int pointer, int button) {
      // TODO Auto-generated method stub
      return false;
   }

   @Override
   public boolean longPress(float x, float y) {
      // TODO Auto-generated method stub
      return false;
   }

   @Override
   public boolean fling(float velocityX, float velocityY, int button) {
      // TODO Auto-generated method stub
      return false;
   }

   @Override
   public boolean pan(float x, float y, float deltaX, float deltaY) {
      // TODO Auto-generated method stub
      return false;
   }

   @Override
   public boolean panStop(float x, float y, int pointer, int button) {
      // TODO Auto-generated method stub
      return false;
   }

   @Override
   public boolean zoom(float initialDistance, float distance) {
      // TODO Auto-generated method stub
      return false;
   }

   @Override
   public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2,
         Vector2 pointer1, Vector2 pointer2) {
      // TODO Auto-generated method stub
      return false;
   }

   @Override
   public void resize(int width, int height) {
      
      VirtualViewport virtualViewport = multipleVirtualViewportBuilder.getVirtualViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
      camera.setVirtualViewport(virtualViewport);
      
      camera.updateViewport();
      // centers the camera at 0, 0 (the center of the virtual viewport)
      camera.position.set(0f, 0f, 0f);
      
      // relocate floating stuff
      // Button button = actorsFactory.createSoundsButton(localResourceManager, 1f, audioPlayer.isSoundMuted(), -427f + virtualViewport.getVirtualWidth() * 0.5f, virtualViewport.getVirtualHeight() * 0.5f, 1.2f, 1.2f, clickListener);
      //floatingButtonSprite.setPosition(virtualViewport.getVirtualWidth() * 0.5f - 80, virtualViewport.getVirtualHeight() * 0.5f - 80);
   }

   @Override
   public void pause() {
      // TODO Auto-generated method stub
      
   }

   @Override
   public void resume() {
      // TODO Auto-generated method stub
      
   }

   @Override
   public void dispose() {
      font.dispose();
      spriteBatch.dispose();
      playerTexture.dispose();
   }
   
}





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.

CopyableCougar4 (22 views)
2014-08-22 19:31:30

atombrot (34 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (25 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (26 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (31 views)
2014-08-08 02:01:56

Norakomi (41 views)
2014-08-06 19:49:38
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!