Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.66 KB | None | 0 0
  1. package com.shootthefacedontdie.game;
  2.  
  3. import com.badlogic.gdx.Game;
  4. import com.badlogic.gdx.Gdx;
  5. import com.badlogic.gdx.graphics.GL20;
  6. import com.badlogic.gdx.graphics.Texture;
  7. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  8.  
  9. public class MainClass extends Game {
  10.     private SpriteBatch batch;
  11.     private Texture img;
  12.     private MovableObject movableObject, movableObject2;
  13.     private Input userInput;
  14.    
  15.     @Override
  16.     public void create () {
  17.         batch = new SpriteBatch();
  18.         img = new Texture("Pictures/badlogic.jpg");
  19.         movableObject=new MovableObject(img);
  20.         movableObject.setOnScreenCentre();
  21.         movableObject2=new MovableObject(img);
  22.         userInput=new Input();
  23.         Gdx.input.setInputProcessor(userInput);
  24.     }
  25.  
  26.     @Override
  27.     public void render () {
  28.         Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  29.         Gdx.gl.glClearColor(0, 0, 0, 0);
  30.         if(Gdx.input.justTouched())
  31.         {
  32.             System.out.println("Screen has been touched");
  33.             System.out.println("x position:"+userInput.getXposition()+" y position:"+userInput.getYposition());
  34.             movableObject.x+=10;
  35.         }
  36.         batch.begin();
  37.         movableObject.texture.draw(batch);
  38.         if(movableObject.overlaps(movableObject2))
  39.         {
  40.             System.out.println("Colision detected");
  41.         }
  42.         batch.end();
  43.     }
  44.    
  45.     @Override
  46.     public void dispose () {
  47.         batch.dispose();
  48.         img.dispose();
  49.     }
  50. }
  51.  
  52.  
  53.  
  54. //Movable object Class
  55.  
  56. package com.shootthefacedontdie.game;
  57.  
  58. import com.badlogic.gdx.Gdx;
  59. import com.badlogic.gdx.graphics.Texture;
  60. import com.badlogic.gdx.graphics.g2d.Sprite;
  61. import com.badlogic.gdx.math.Rectangle;
  62.  
  63. public class MovableObject extends Rectangle{
  64.     public Texture image;
  65.     public Sprite texture=new Sprite(image);
  66.  
  67.     public MovableObject(Texture atexture)
  68.     {
  69.         this.image=atexture;
  70.         texture.setPosition(0,0);
  71.     }
  72.  
  73.     public Sprite getTexture()
  74.     {
  75.         return texture;
  76.     }
  77.  
  78.     void setOnScreenCentre()
  79.     {
  80.         texture.setPosition(Gdx.graphics.getWidth()/2 - image.getWidth()/2,Gdx.graphics.getHeight()/2 - image.getHeight()/2);
  81.     }
  82.  
  83. }
  84.  
  85.  
  86. //Input Class
  87.  
  88. package com.shootthefacedontdie.game;
  89.  
  90. import com.badlogic.gdx.InputAdapter;
  91.  
  92. public class Input extends InputAdapter {
  93.  
  94.     private int xposition;
  95.     private int yposition;
  96.  
  97.     @Override
  98.     public boolean touchDown(int screenX, int screenY, int pointer, int button)
  99.     {
  100.         xposition=screenX;
  101.         yposition=screenY;
  102.         return true;
  103.     }
  104.  
  105.     @Override
  106.     public boolean touchUp(int screenX, int screenY, int pointer, int button)
  107.     {
  108.         return true;
  109.     }
  110.  
  111.     int getXposition()
  112.     {
  113.         return xposition;
  114.     }
  115.  
  116.     public int getYposition() {
  117.         return yposition;
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement