Mudanças entre as edições de "Android Games: Walkers"

De Aulas
 
(Uma revisão intermediária pelo mesmo usuário não está sendo mostrada)
Linha 72: Linha 72:
 
         sprite.setPosition(
 
         sprite.setPosition(
 
                 clamp(sprite.getX(), 0, game.w - w - 1),
 
                 clamp(sprite.getX(), 0, game.w - w - 1),
                 clamp(sprite.getY(), 0, game.w - h - 1));
+
                 clamp(sprite.getY(), 0, game.h - h - 1));
 
     }
 
     }
  
Linha 174: Linha 174:
 
import com.badlogic.gdx.graphics.g2d.BitmapFont;
 
import com.badlogic.gdx.graphics.g2d.BitmapFont;
 
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
 
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
 +
import com.badlogic.gdx.utils.ScreenUtils;
 
import com.badlogic.gdx.utils.TimeUtils;
 
import com.badlogic.gdx.utils.TimeUtils;
  
Linha 181: Linha 182:
  
 
public class MyGdxGame extends ApplicationAdapter {
 
public class MyGdxGame extends ApplicationAdapter {
    int w, h;
+
int w, h;
    SpriteBatch batch;
+
SpriteBatch batch;
    Random rand = new Random();
+
Random rand = new Random();
    List<Actor> actors = new ArrayList<Actor>();
+
List<Actor> actors = new ArrayList<Actor>();
    private Texture avatarTexture;
+
private Texture avatarTexture;
    private Texture monsterTexture;
+
private Texture monsterTexture;
    private Texture industrialTexture;
+
private BitmapFont font;
    private BitmapFont font;
+
private Avatar avatar;
    private Avatar avatar;
 
    private long lastTimeCounted;
 
    private float sinceChange;
 
    private float frameRate;
 
  
    @Override
+
@Override
    public void create() {
+
public void create() {
        w = Gdx.graphics.getWidth();
+
w = Gdx.graphics.getWidth();
        h = Gdx.graphics.getHeight();
+
h = Gdx.graphics.getHeight();
        batch = new SpriteBatch();
+
batch = new SpriteBatch();
        avatarTexture = new Texture("avatar.png");
+
avatarTexture = new Texture("avatar.png");
        monsterTexture = new Texture("monster.png");
+
monsterTexture = new Texture("monster.png");
        industrialTexture = new Texture("industrial.png");
+
for (int i = 0; i < 10; i++) {
        for (int i = 0; i < 10; i++) {
+
actors.add(new Monster(monsterTexture, this));
            actors.add(new Monster(monsterTexture, this));
+
}
        }
+
actors.add(avatar = new Avatar(100, 100, avatarTexture, this));
        avatar = new Avatar(100, 100, avatarTexture, this);
+
font = new BitmapFont(
        actors.add(avatar);
+
Gdx.files.internal("verdana.fnt"),
        font = new BitmapFont(
+
Gdx.files.internal("verdana.png"), false);
                Gdx.files.internal("verdana.fnt"),
+
font.getData().setScale(0.75f, 0.75f);
                Gdx.files.internal("verdana.png"), false);
+
font.setColor(1, 1, 0, 1);
        font.setColor(1, 1, 1, 1);
+
}
        lastTimeCounted = TimeUtils.millis();
 
        sinceChange = 0;
 
        frameRate = Gdx.graphics.getFramesPerSecond();
 
    }
 
  
    private void execute() {
+
private void execute() {
        if (Gdx.input.isKeyPressed(Input.Keys.ESCAPE)) {
+
if (Gdx.input.isKeyPressed(Input.Keys.ESCAPE)) {
            Gdx.app.exit();
+
Gdx.app.exit();
        }
+
}
        for (Actor a : actors) a.run();
+
for (Actor a : actors) a.run();
        long delta = TimeUtils.timeSinceMillis(lastTimeCounted);
+
}
        lastTimeCounted = TimeUtils.millis();
 
        sinceChange += delta;
 
        if (sinceChange >= 1000) {
 
            sinceChange = 0;
 
            frameRate = Gdx.graphics.getFramesPerSecond();
 
        }
 
    }
 
  
    @Override
+
@Override
    public void render() {
+
public void render() {
        execute();
+
execute();
        batch.begin();
+
ScreenUtils.clear(0, 0.5f, 0, 1);
        batch.draw(industrialTexture, 0, 0);
+
float delta = Gdx.graphics.getDeltaTime();
        for (Actor a : actors) a.draw();
+
float frameRate = Gdx.graphics.getFramesPerSecond();
        if (avatar.collided) {
+
batch.begin();
            font.draw(batch, "Collided!  " + (int) frameRate + " fps", 1, h + 1);
+
for (Actor a : actors) a.draw();
        } else {
+
String text = avatar.collided ? "Collided!  " : "I'm free... ";
            font.draw(batch, "I'm free... " + (int) frameRate + " fps", 1, h + 1);
+
font.draw(batch, text + (int) frameRate + " fps" + " delta: " + delta, 1, h + 1);
        }
+
batch.end();
        batch.end();
+
}
    }
 
  
    @Override
+
@Override
    public void dispose() {
+
public void dispose() {
        batch.dispose();
+
batch.dispose();
        avatarTexture.dispose();
+
avatarTexture.dispose();
        monsterTexture.dispose();
+
monsterTexture.dispose();
        industrialTexture.dispose();
+
}
    }
 
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Edição atual tal como às 10h11min de 29 de outubro de 2022

Links relacionados: Jogos Digitais

Vídeo Demonstrativo

Recursos

Actor.class

package com.mygdx.game;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Intersector;

class Actor {
    static final int DOWN = 0;
    static final int RIGHT = 1;
    static final int UP = 2;
    static final int LEFT = 3;
    private static final int SPEED = 2;
    int direction = DOWN;
    MyGdxGame game;
    Boolean collided = false;
    private final Sprite sprite;
    public int w = 64;
    public int h = 64;
    private int step = 0;
    private int counter;
    private static final int counterMax = 10;
    private boolean moved = false;

    Actor(float x, float y, Texture texture, MyGdxGame game) {
        this.game = game;
        this.sprite = new Sprite(new TextureRegion(texture, 0, 0, w, h));
        sprite.setPosition(x, y);
    }

    void execute() {
    }

    void run() {
        execute();
        counter++;
        if (counter > counterMax) {
            counter = 0;
            if (moved) step = (step == 1) ? 0 : 1;
        }
        moved = false;
    }

    void move(int dir) {
        direction = dir;
        if (direction == UP) {
            sprite.translateY(SPEED);
            moved = true;
        } else if (direction == DOWN) {
            sprite.translateY(-SPEED);
            moved = true;
        } else if (direction == RIGHT) {
            sprite.translateX(SPEED);
            moved = true;
        } else if (direction == LEFT) {
            sprite.translateX(-SPEED);
            moved = true;
        }
        sprite.setPosition(
                clamp(sprite.getX(), 0, game.w - w - 1),
                clamp(sprite.getY(), 0, game.h - h - 1));
    }

    public float clamp(float val, float min, float max) {
        if (val < min) return min;
        if (val > max) return max;
        return val;
    }

    Boolean collision(Actor other) {
        return this != other &&
                Intersector.overlaps(
                        sprite.getBoundingRectangle(),
                        other.sprite.getBoundingRectangle());
    }

    void draw() {
        sprite.setRegion(step * w, direction * h, w, h);
        sprite.draw(game.batch);
    }
}

Avatar.class

package com.mygdx.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Texture;

class Avatar extends Actor {
    Avatar(float x, float y, Texture texture, MyGdxGame game) {
        super(x, y, texture, game);
    }

    @Override
    public void execute() {
        if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
            move(UP);
        } else if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
            move(DOWN);
        } else if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
            move(RIGHT);
        } else if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
            move(LEFT);
        }
        collided = false;
        for (Actor a : game.actors) {
            if (collision(a)) {
                collided = true;
                break;
            }
        }
    }
}

Monster.class

package com.mygdx.game;

import com.badlogic.gdx.graphics.Texture;

class Monster extends Actor {
    private int count = 0;
    private int max;

    Monster(Texture texture, MyGdxGame game) {
        super(game.rand.nextInt(game.w - 64),
                game.rand.nextInt(game.h - 64), texture, game);
        new_goal();
    }

    private void new_goal() {
        direction = game.rand.nextInt(4);
        count = 0;
        max = game.rand.nextInt(20) + 1;
    }

    @Override
    public void execute() {
        count++;
        if (count > max) new_goal();
        move(direction);
    }
}

MyGdxGame.class

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.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.ScreenUtils;
import com.badlogic.gdx.utils.TimeUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MyGdxGame extends ApplicationAdapter {
	int w, h;
	SpriteBatch batch;
	Random rand = new Random();
	List<Actor> actors = new ArrayList<Actor>();
	private Texture avatarTexture;
	private Texture monsterTexture;
	private BitmapFont font;
	private Avatar avatar;

	@Override
	public void create() {
		w = Gdx.graphics.getWidth();
		h = Gdx.graphics.getHeight();
		batch = new SpriteBatch();
		avatarTexture = new Texture("avatar.png");
		monsterTexture = new Texture("monster.png");
		for (int i = 0; i < 10; i++) {
			actors.add(new Monster(monsterTexture, this));
		}
		actors.add(avatar = new Avatar(100, 100, avatarTexture, this));
		font = new BitmapFont(
				Gdx.files.internal("verdana.fnt"),
				Gdx.files.internal("verdana.png"), false);
		font.getData().setScale(0.75f, 0.75f);
		font.setColor(1, 1, 0, 1);
	}

	private void execute() {
		if (Gdx.input.isKeyPressed(Input.Keys.ESCAPE)) {
			Gdx.app.exit();
		}
		for (Actor a : actors) a.run();
	}

	@Override
	public void render() {
		execute();
		ScreenUtils.clear(0, 0.5f, 0, 1);
		float delta = Gdx.graphics.getDeltaTime();
		float frameRate = Gdx.graphics.getFramesPerSecond();
		batch.begin();
		for (Actor a : actors) a.draw();
		String text = avatar.collided ? "Collided!   " : "I'm free... ";
		font.draw(batch, text + (int) frameRate + " fps" + " delta: " + delta, 1, h + 1);
		batch.end();
	}

	@Override
	public void dispose() {
		batch.dispose();
		avatarTexture.dispose();
		monsterTexture.dispose();
	}
}