// === MainGame.java === package com.prajval.battleroyale;
import com.badlogic.gdx.Game;
public class MainGame extends Game { @Override public void create() { this.setScreen(new GameScreen()); } }
// === GameScreen.java === package com.prajval.battleroyale;
import com.badlogic.gdx.Screen; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
public class GameScreen implements Screen {
SpriteBatch batch;
Texture mapTexture;
Player player;
Array<Bullet> bullets;
Array<Enemy> enemies;
OrthographicCamera camera;
ShapeRenderer shapeRenderer;
float safeZoneX = 100;
float safeZoneY = 100;
float safeZoneSize = 600;
float shrinkTimer = 0;
@Override
public void show() {
batch = new SpriteBatch();
mapTexture = new Texture("map.png");
player = new Player();
bullets = new Array<>();
enemies = new Array<>();
enemies.add(new Enemy(600, 300));
shapeRenderer = new ShapeRenderer();
camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480);
}
@Override
public void render(float delta) {
shrinkTimer += delta;
if (shrinkTimer > 5f && safeZoneSize > 100) {
safeZoneSize -= 10;
safeZoneX += 5;
safeZoneY += 5;
shrinkTimer = 0;
}
player.update(delta);
if (player.shooting) {
bullets.add(new Bullet(player.x + 16, player.y + 16));
player.shooting = false;
}
for (Bullet b : bullets) {
b.update(delta);
}
for (Enemy e : enemies) {
e.update(delta);
}
for (Bullet b : bullets) {
for (Enemy e : enemies) {
if (e.alive && e.hitBy(b)) {
e.alive = false;
}
}
}
if (!player.inSafeZone(safeZoneX, safeZoneY, safeZoneSize)) {
player.health -= delta * 5;
}
camera.position.set(player.x, player.y, 0);
camera.update();
Gdx.gl.glClearColor(0, 0.3f, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(mapTexture, 0, 0);
batch.draw(player.texture, player.x, player.y);
for (Bullet b : bullets) {
batch.draw(b.texture, b.x, b.y);
}
for (Enemy e : enemies) {
if (e.alive) {
batch.draw(e.texture, e.x, e.y);
}
}
batch.end();
shapeRenderer.setProjectionMatrix(camera.combined);
shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
shapeRenderer.setColor(0, 1, 0, 1);
shapeRenderer.rect(safeZoneX, safeZoneY, safeZoneSize, safeZoneSize);
shapeRenderer.end();
}
@Override
public void dispose() {
batch.dispose();
mapTexture.dispose();
player.dispose();
shapeRenderer.dispose();
for (Bullet b : bullets) {
b.dispose();
}
for (Enemy e : enemies) {
e.dispose();
}
}
public void resize(int w, int h) {}
public void pause() {}
public void resume() {}
public void hide() {}
}
// === Player.java === package com.prajval.battleroyale;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input; import com.badlogic.gdx.graphics.Texture;
public class Player { public float x = 400, y = 240; public float speed = 200; public float health = 100; public Texture texture; public boolean shooting = false;
public Player() {
texture = new Texture("player.png");
}
public void update(float delta) {
if (Gdx.input.isKeyPressed(Input.Keys.W)) y += speed * delta;
if (Gdx.input.isKeyPressed(Input.Keys.S)) y -= speed * delta;
if (Gdx.input.isKeyPressed(Input.Keys.A)) x -= speed * delta;
if (Gdx.input.isKeyPressed(Input.Keys.D)) x += speed * delta;
if (Gdx.input.isKeyJustPressed(Input.Keys.SPACE)) shooting = true;
}
public boolean inSafeZone(float zoneX, float zoneY, float zoneSize) {
return x >= zoneX && x <= zoneX + zoneSize && y >= zoneY && y <= zoneY + zoneSize;
}
public void dispose() {
texture.dispose();
}
}
// === Bullet.java === package com.prajval.battleroyale;
import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.math.Rectangle;
public class Bullet { public float x, y; public float speed = 400; public Texture texture;
public Bullet(float x, float y) {
this.x = x;
this.y = y;
texture = new Texture("bullet.png");
}
public void update(float delta) {
x += speed * delta;
}
public Rectangle getBounds() {
return new Rectangle(x, y, texture.getWidth(), texture.getHeight());
}
public void dispose() {
texture.dispose();
}
}
// === Enemy.java === package com.prajval.battleroyale;
import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.math.Rectangle;
public class Enemy { public float x, y; public boolean alive = true; public Texture texture;
public Enemy(float x, float y) {
this.x = x;
this.y = y;
texture = new Texture("enemy.png");
}
public void update(float delta) {
// Idle behavior for now
}
public boolean hitBy(Bullet bullet) {
return getBounds().overlaps(bullet.getBounds());
}
public Rectangle getBounds() {
return new Rectangle(x, y, texture.getWidth(), texture.getHeight());
}
public void dispose() {
texture.dispose();
}
}