Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.shootthefacedontdie.game;
- import com.badlogic.gdx.Game;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.graphics.GL20;
- import com.badlogic.gdx.graphics.Texture;
- import com.badlogic.gdx.graphics.g2d.SpriteBatch;
- public class MainClass extends Game {
- private SpriteBatch batch;
- private Texture img;
- private MovableObject movableObject, movableObject2;
- private Input userInput;
- @Override
- public void create () {
- batch = new SpriteBatch();
- img = new Texture("Pictures/badlogic.jpg");
- movableObject=new MovableObject(img);
- movableObject.setOnScreenCentre();
- movableObject2=new MovableObject(img);
- userInput=new Input();
- Gdx.input.setInputProcessor(userInput);
- }
- @Override
- public void render () {
- Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
- Gdx.gl.glClearColor(0, 0, 0, 0);
- if(Gdx.input.justTouched())
- {
- System.out.println("Screen has been touched");
- System.out.println("x position:"+userInput.getXposition()+" y position:"+userInput.getYposition());
- movableObject.x+=10;
- }
- batch.begin();
- movableObject.texture.draw(batch);
- if(movableObject.overlaps(movableObject2))
- {
- System.out.println("Colision detected");
- }
- batch.end();
- }
- @Override
- public void dispose () {
- batch.dispose();
- img.dispose();
- }
- }
- //Movable object Class
- package com.shootthefacedontdie.game;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.graphics.Texture;
- import com.badlogic.gdx.graphics.g2d.Sprite;
- import com.badlogic.gdx.math.Rectangle;
- public class MovableObject extends Rectangle{
- public Texture image;
- public Sprite texture=new Sprite(image);
- public MovableObject(Texture atexture)
- {
- this.image=atexture;
- texture.setPosition(0,0);
- }
- public Sprite getTexture()
- {
- return texture;
- }
- void setOnScreenCentre()
- {
- texture.setPosition(Gdx.graphics.getWidth()/2 - image.getWidth()/2,Gdx.graphics.getHeight()/2 - image.getHeight()/2);
- }
- }
- //Input Class
- package com.shootthefacedontdie.game;
- import com.badlogic.gdx.InputAdapter;
- public class Input extends InputAdapter {
- private int xposition;
- private int yposition;
- @Override
- public boolean touchDown(int screenX, int screenY, int pointer, int button)
- {
- xposition=screenX;
- yposition=screenY;
- return true;
- }
- @Override
- public boolean touchUp(int screenX, int screenY, int pointer, int button)
- {
- return true;
- }
- int getXposition()
- {
- return xposition;
- }
- public int getYposition() {
- return yposition;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement