Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.mygdx.game;
- import com.badlogic.gdx.ApplicationAdapter;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.graphics.GL20;
- import com.badlogic.gdx.graphics.g2d.SpriteBatch;
- public class MyGdxGame extends ApplicationAdapter {
- private Counter counter = new Counter();
- private Map map = new Map();
- private Player player = new Player();
- private SpriteBatch batch;
- public void create () {
- batch = new SpriteBatch();
- map.createMap();
- player.createPlayer();
- counter.createCounter();
- }
- public void render () {
- Gdx.gl.glClearColor(1, 1, 1, 1);
- Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
- batch.begin();
- map.renderMap(batch);
- player.renderPlayer(batch);
- counter.renderCounter(batch);
- batch.end();
- }
- }
- package com.mygdx.game;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.Input.Keys;
- import com.badlogic.gdx.graphics.Texture;
- import com.badlogic.gdx.graphics.g2d.Sprite;
- import com.badlogic.gdx.graphics.g2d.SpriteBatch;
- import com.badlogic.gdx.math.Circle;
- import com.badlogic.gdx.math.Intersector;
- import com.badlogic.gdx.math.Rectangle;
- public class Player {
- private Rectangle[] elements;
- private Map map;
- private static int speed = 5;
- public int getSpeed(){
- return speed;
- }
- public void setSpeed(){
- speed+=3;
- }
- private float width;
- private float height;
- private float x;
- private float y;
- private float radius;
- private Sprite player;
- private Circle playerShape;
- public void setElements(Rectangle[] elements){
- this.elements = elements;
- }
- public void createPlayer(){
- map = new Map();
- width = Gdx.graphics.getWidth()/19.2f;
- height = Gdx.graphics.getHeight()/10.8f;
- player = new Sprite(new Texture(Gdx.files.internal("player.png")));
- if(Gdx.graphics.getWidth()>Gdx.graphics.getHeight()){
- radius = width;
- }
- else{
- radius = height;
- }
- x = Gdx.graphics.getWidth()/2-radius/2;
- y = Gdx.graphics.getHeight()/2-radius/2;
- playerShape = new Circle();
- playerShape.setRadius(radius);
- playerShape.setPosition(x, y);
- }
- public void renderPlayer(SpriteBatch batch){
- batch.draw(player, x, y, player.getOriginX(), player.getOriginY(), width, height, player.getScaleX(), player.getScaleY(), player.getRotation());
- move();
- collisionDetection();
- }
- private void collisionDetection(){
- for(int i = 0; i<map.getNumber(); i++){
- if(Intersector.overlaps(playerShape, elements[i])){
- //Kollision
- }
- }
- }
- private void move(){
- if(Gdx.input.isKeyPressed(Keys.A)){
- x-=speed;
- }
- if(Gdx.input.isKeyPressed(Keys.D)){
- x+=speed;
- }
- }
- }
- package com.mygdx.game;
- import java.util.Random;
- import java.util.Timer;
- import java.util.TimerTask;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.graphics.Texture;
- import com.badlogic.gdx.graphics.g2d.Sprite;
- import com.badlogic.gdx.graphics.g2d.SpriteBatch;
- import com.badlogic.gdx.math.Rectangle;
- public class Map {
- private int z;
- private Random r;
- private float width;
- private float height;
- private final int numberOfElements = 20;
- private Player player;
- private Timer timer;
- private TimerTask timerTask;
- private int y[];
- private int x[];
- private Sprite[] elements;
- private Rectangle[] elementsShape;
- public void createMap(){
- z = 0;
- r = new Random();
- width = Gdx.graphics.getWidth()/7.68f;
- height = Gdx.graphics.getHeight()/4.32f;
- player = new Player();
- elements = new Sprite[numberOfElements];
- elementsShape = new Rectangle[numberOfElements];
- y = new int[numberOfElements];
- x = new int[numberOfElements];
- timer = new Timer();
- timerTask = new TimerTask(){
- public void run(){
- for(int i = 0; i<numberOfElements; i++){
- y[i]-=(player.getSpeed()-3);
- }
- }
- };
- timer.schedule(timerTask, 0, 10);
- spawnElements();
- }
- private void spawnElements(){
- for(int i = 0; i<numberOfElements; i++){
- Sprite sprite = new Sprite(new Texture(Gdx.files.internal("block.png")));
- y[i] = i*(Gdx.graphics.getHeight()/2)+(Gdx.graphics.getHeight()+Gdx.graphics.getHeight()/2);
- int a = (int) (Gdx.graphics.getWidth()-(width+30));
- int b = 30;
- x[i] = r.nextInt(a)+b;
- elements[i] = sprite;
- elementsShape[i] = new Rectangle();
- elementsShape[i].setSize(elements[i].getWidth(), elements[i].getHeight());
- elementsShape[i].setPosition(x[i], y[i]);
- }
- }
- public void renderMap(SpriteBatch batch){
- player.setElements(elementsShape);
- for(int i = 0; i<numberOfElements; i++){
- batch.draw(elements[i], x[i], y[i], elements[i].getOriginX(), elements[i].getOriginY(), width,
- height, elements[i].getScaleX(), elements[i].getScaleY(), elements[i].getRotation());
- counting();
- }
- }
- private void counting(){
- for(int i = z; i<numberOfElements; i++){
- if(y[i]<=Gdx.graphics.getHeight()/2-height/2){
- z++;
- //Score up
- if(z==numberOfElements-1){
- z = 0;
- player.setSpeed();
- spawnElements();
- }
- }
- }
- }
- public int getNumber(){
- return numberOfElements;
- }
- }
- package com.mygdx.game;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.graphics.Color;
- import com.badlogic.gdx.graphics.g2d.BitmapFont;
- import com.badlogic.gdx.graphics.g2d.SpriteBatch;
- public class Counter {
- private float x;
- private float y;
- private BitmapFont font;
- private int levelCounter;
- private int scoreCounter;
- private String text;
- public void createCounter(){
- text = "Level: %s\nScore: %s";
- x = 10;
- y = 80;
- font = new BitmapFont(Gdx.files.internal("font.fnt"));
- font.setColor(Color.BLUE);
- font.getData().setScale((float)Gdx.graphics.getHeight()/2160, (float)Gdx.graphics.getWidth()/3840);
- levelCounter = 1;
- scoreCounter = 0;
- }
- public void renderCounter(SpriteBatch batch){
- font.draw(batch, String.format(text, levelCounter, scoreCounter), x, y);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement