package com.mygdx.game;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
public class Rock {
Texture texture;
Vector2 vector2;
public Rock(Texture texture, Vector2 vector2) {
this.texture = texture;
this.vector2 = vector2;
}
public void draw(SpriteBatch batch){
batch.draw(texture, vector2.x, vector2.y);
}
}
package com.mygdx.game;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
public class Gumba {
Texture texture;
Vector2 vector2;
Rectangle rectangle;
public Gumba(Texture texture, Vector2 vector2) {
this.texture = texture;
this.vector2 = vector2;
rectangle = new Rectangle(vector2.x, vector2.y, texture.getWidth(), texture.getHeight());
}
public void draw(SpriteBatch batch){
batch.draw(texture, vector2.x, vector2.y);
}
}
package com.mygdx.game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
public class Mario {
Texture texture;
Vector2 vector2;
TextureRegion [][] frames;
int curFrameX = 0, curFrameY = 0;
float dy = 0;
float curFrame = 0;
Rectangle rectangle;
public Mario(Texture texture, Vector2 vector2) {
this.texture = texture;
this.vector2 = vector2;
frames = TextureRegion.split(texture, texture.getWidth() / 4, texture.getHeight() / 4);
rectangle = new Rectangle(vector2.x, vector2.y, texture.getWidth() / 4, texture.getHeight() / 4);
}
public void draw(SpriteBatch batch){
batch.draw(frames[curFrameY][curFrameX], vector2.x, vector2.y);
}
public void update(){
dy -= 15;
dy = Math.max(-600, dy);
vector2.y += dy * Gdx.graphics.getDeltaTime();
if (vector2.y < 100) vector2.y = 100;
if (vector2.y == 100){
curFrameY = 1;
} else {
curFrameY = 2;
}
curFrame += Gdx.graphics.getDeltaTime();
if (curFrame >= 0.1f){
curFrameX = (curFrameX + 1) % frames[curFrameY].length;
curFrame = 0;
}
rectangle.x = vector2.x;
rectangle.y = vector2.y;
}
}
package com.mygdx.game;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.ScreenUtils;
import java.util.ArrayList;
import java.util.Random;
public class MyGdxGame extends ApplicationAdapter {
final float ROCK_SPEED = 300;
final int MAX_GUMBAS = 3;
final float GUMBA_TIME = 0.5f;
float preGumba = 0;
boolean isGameOver = false;
SpriteBatch batch;
Texture mario, rock, back, gumba, gameOver;
ArrayList<Rock> rocks = new ArrayList<>();
ArrayList<Gumba> gumbas = new ArrayList<>();
Mario hero;
@Override
public void create () {
batch = new SpriteBatch();
mario = new Texture("mario-spritesheet.png");
rock = new Texture("rock.png");
back = new Texture("back.png");
gumba = new Texture("gumba.png");
gameOver = new Texture("game_over.png");
hero = new Mario(mario, new Vector2(100, rock.getHeight()));
for (int i = 0; i < 20; i++){
rocks.add(new Rock(rock, new Vector2(i * rock.getWidth(), 0)));
}
}
@Override
public void render () {
ScreenUtils.clear(1, 0, 0, 1);
hero.update();
if (Gdx.input.isKeyPressed(Input.Keys.UP)){
if (hero.vector2.y == 100){
hero.dy = 600;
}
}
if (Gdx.input.isKeyPressed(Input.Keys.DOWN)){
hero.curFrameY = 3;
}
// удаление гумбы
if (gumbas.size() > 0){
if (gumbas.get(0).vector2.x < -gumba.getWidth() * 2){
gumbas.remove(0);
}
}
// спавн гумбы
preGumba += Gdx.graphics.getDeltaTime();
if (gumbas.size() < MAX_GUMBAS && preGumba >= GUMBA_TIME){
Random random = new Random();
if (random.nextInt(5) == 1){
gumbas.add(new Gumba(gumba, new Vector2(Gdx.graphics.getWidth(), rock.getHeight())));
}
preGumba = 0;
}
// перемещение гумбы
for (Gumba gumba: gumbas){
gumba.vector2.x -= ROCK_SPEED * Gdx.graphics.getDeltaTime();
gumba.rectangle.x = gumba.vector2.x;
if (gumba.rectangle.overlaps(hero.rectangle)){
isGameOver = true;
}
}
// перемещение камней
for (Rock rock: rocks){
rock.vector2.x -= ROCK_SPEED * Gdx.graphics.getDeltaTime();
}
if (rocks.get(0).vector2.x < -rock.getWidth() * 2){
int last = rocks.size() - 1;
Rock lastRock = rocks.get(last);
rocks.get(0).vector2.x = lastRock.vector2.x + lastRock.texture.getWidth();
rocks.add(rocks.remove(0));
}
// отрисовка
batch.begin();
batch.draw(back, 0, 0);
for (Rock rock: rocks){
rock.draw(batch);
}
if (isGameOver){
batch.draw(gameOver, 0, 0);
} else {
for (Gumba gumba: gumbas){
gumba.draw(batch);
}
hero.draw(batch);
}
batch.end();
}
@Override
public void dispose () {
batch.dispose();
mario.dispose();
}
}