Android Games: The Tank

De Aulas
Revisão de 20h19min de 30 de maio de 2017 por Admin (discussão | contribs) (Criou página com 'Links relacionados: Jogos Digitais = Recursos = * [https://{{SERVERNAME}}/~saulo/aulas/unisul/games/assets/tank.zip Assets] = Vector2D.java = <syntaxhighlight lang=jav...')
(dif) ← Edição anterior | Revisão atual (dif) | Versão posterior → (dif)

Links relacionados: Jogos Digitais

Recursos

Vector2D.java

  1package com.mygdx.game;
  2
  3class Vector2D {
  4	private double x;
  5	private double y;
  6
  7	Vector2D() {
  8		x = 0.0f;
  9		y = 0.0f;
 10	}
 11
 12	Vector2D(double x, double y) {
 13		this.x = x;
 14		this.y = y;
 15	}
 16
 17	// Retorna o tamanho do vetor
 18
 19	static double grad2radians(double grad) {
 20		return (grad / 180) * Math.PI;
 21	}
 22
 23	// Soma dois vetores e retorna um terceiro
 24
 25	public static double radians2grad(double radians) {
 26		return (radians * 180) / Math.PI;
 27	}
 28
 29	// Soma o atual vetor com os valores de um outro
 30
 31	double size() {
 32		return Math.sqrt((x * x) + (y * y));
 33	}
 34
 35	public Vector2D sum(Vector2D a, Vector2D b) {
 36		return new Vector2D(a.getX() + b.getX(), a.getY() + b.getY());
 37	}
 38
 39	void sumTo(Vector2D other) {
 40		x += other.getX();
 41		y += other.getY();
 42	}
 43
 44	public Vector2D multiply(double value) {
 45		return new Vector2D(x * value, y * value);
 46	}
 47
 48	public void multiplyTo(double value) {
 49		x *= value;
 50		y *= value;
 51	}
 52
 53	// Normaliza o vetor, transformando seu tamanho para 1
 54
 55	public Vector2D divide(double value) {
 56		return new Vector2D(x / value, y / value);
 57	}
 58
 59	// Angulos
 60
 61	private void divideBy(double value) {
 62		x /= value;
 63		y /= value;
 64	}
 65
 66	public void normalize() {
 67		divideBy(size());
 68	}
 69
 70	public Vector2D bySizeAndAngle(double size, double angle) {
 71		return new Vector2D(Math.cos(size) * size, Math.sin(size) * size);
 72	}
 73
 74	void rotate(double angle) {
 75		// Executa as operações de sin e cos apenas uma vez,
 76		// economizando processamento.
 77		double s = Math.sin(angle);
 78		double c = Math.cos(angle);
 79		double newX = (x * c) - (y * s);
 80		double newY = (x * s) + (y * c);
 81		x = newX;
 82		y = newY;
 83	}
 84
 85	private double dot(Vector2D other) {
 86		return (x * other.x) + (y * other.y);
 87	}
 88
 89	public double angleBetween(Vector2D other) {
 90		double dp = dot(other);
 91		if (dp >= 1.0) {
 92			dp = 1.0f;
 93		} else if (dp <= -1.0) {
 94			dp = -1.0f;
 95		}
 96		double angPi = (float) Math.acos(dp);
 97		return dot(other) > 0 ? -angPi : angPi;
 98	}
 99
100	double getX() {
101		return x;
102	}
103
104	void setX(double x) {
105		this.x = x;
106	}
107
108	double getY() {
109		return y;
110	}
111
112	void setY(double y) {
113		this.y = y;
114	}
115}

MyGdxGame.java

  1package com.mygdx.game;
  2
  3import com.badlogic.gdx.ApplicationAdapter;
  4import com.badlogic.gdx.Gdx;
  5import com.badlogic.gdx.Input;
  6import com.badlogic.gdx.audio.Sound;
  7import com.badlogic.gdx.graphics.GL20;
  8import com.badlogic.gdx.graphics.Texture;
  9import com.badlogic.gdx.graphics.g2d.BitmapFont;
 10import com.badlogic.gdx.graphics.g2d.Sprite;
 11import com.badlogic.gdx.graphics.g2d.SpriteBatch;
 12
 13import java.util.Locale;
 14
 15public class MyGdxGame extends ApplicationAdapter {
 16	private final int canePosX = 26;
 17	private final int groundPosY = 100;
 18	private SpriteBatch batch;
 19	private Sprite ball;
 20	private Sprite tank;
 21	private Sprite cane;
 22	private Texture background;
 23	private BitmapFont verdana;
 24	private Vector2D vecBall;
 25	private Vector2D vecGravity;
 26	private Vector2D vecWind;
 27	private Sound sndShoot;
 28	private Sound sndExplosion;
 29	private double force;
 30	private boolean shooting;
 31
 32	@Override
 33	public void create() {
 34		batch = new SpriteBatch();
 35		sndExplosion = Gdx.audio.newSound(Gdx.files.internal("explosion.wav"));
 36		sndShoot = Gdx.audio.newSound(Gdx.files.internal("shoot.wav"));
 37		verdana = new BitmapFont(Gdx.files.internal("verdana.fnt"), false);
 38		verdana.setColor(0, 0, 0, 1);
 39		background = new Texture("background.png");
 40		tank = new Sprite(new Texture("tank.png"));
 41		tank.setPosition(100, groundPosY);
 42		cane = new Sprite(new Texture("cane.png"));
 43		cane.setPosition(100 + canePosX, groundPosY + 18);
 44		cane.setOrigin(0, 1);
 45		ball = new Sprite(new Texture("ball.png"));
 46		ball.setPosition(0, groundPosY);
 47		force = 20.0f;
 48		vecBall = new Vector2D(0.0f, force);
 49		vecGravity = new Vector2D(0.0f, -0.98f);
 50		vecWind = new Vector2D(0.0f, 0.0f);
 51		shooting = false;
 52	}
 53
 54	private void execute() {
 55		if (Gdx.input.isKeyPressed(Input.Keys.ESCAPE)) {
 56			Gdx.app.exit();
 57		}
 58		if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
 59			tank.setX(tank.getX() + 1);
 60		} else if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
 61			tank.setX(tank.getX() - 1);
 62		}
 63		if (Gdx.input.isKeyJustPressed(Input.Keys.MINUS)) {
 64			force--;
 65		} else if (Gdx.input.isKeyJustPressed(Input.Keys.EQUALS)) {
 66			force++;
 67		}
 68		if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
 69			if (cane.getRotation() < 90) cane.rotate(0.5f);
 70		} else if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
 71			if (cane.getRotation() > 0) cane.rotate(-0.5f);
 72		}
 73		if (Gdx.input.isKeyJustPressed(Input.Keys.PAGE_UP)) {
 74			vecWind.setX(vecWind.getX() + 0.01f);
 75		} else if (Gdx.input.isKeyJustPressed(Input.Keys.PAGE_DOWN)) {
 76			vecWind.setX(vecWind.getX() - 0.01f);
 77		}
 78		if (Gdx.input.isKeyPressed(Input.Keys.SPACE)) {
 79			if (!shooting) {
 80				shooting = true;
 81				ball.setPosition(cane.getX(), cane.getY());
 82				vecBall.setX(force);
 83				vecBall.setY(0.0f);
 84				vecBall.rotate(Vector2D.grad2radians(cane.getRotation()));
 85				sndShoot.play();
 86			}
 87		}
 88		cane.setX(tank.getX() + canePosX);
 89		if (shooting) {
 90			vecBall.sumTo(vecGravity);
 91			vecBall.sumTo(vecWind);
 92			ball.setX(ball.getX() + (float) vecBall.getX());
 93			ball.setY(ball.getY() + (float) vecBall.getY());
 94			if (ball.getY() <= groundPosY) {
 95				shooting = false;
 96				ball.setY(groundPosY);
 97				sndExplosion.play();
 98			}
 99		}
100	}
101
102	@Override
103	public void render() {
104		execute();
105		Gdx.gl.glClearColor(1, 1, 1, 1);
106		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
107		batch.begin();
108		batch.draw(background, 0, 0);
109		verdana.draw(batch, "Angle: "
110				+ String.format(Locale.US, "%.0f", cane.getRotation()), 5, 480);
111		verdana.draw(batch, "Force: "
112				+ String.format(Locale.US, "%.0f", force), 5, 450);
113		verdana.draw(batch, "Wind : "
114				+ String.format(Locale.US, "%.0f", (vecWind.getX()) * 100), 4, 420);
115		ball.draw(batch);
116		cane.draw(batch);
117		tank.draw(batch);
118		batch.end();
119		sleep(20);
120	}
121
122	private void sleep(int timer) {
123		try {
124			Thread.sleep(timer);
125		} catch (InterruptedException e) {
126			e.printStackTrace();
127		}
128	}
129
130	@Override
131	public void dispose() {
132		batch.dispose();
133		ball.getTexture().dispose();
134		tank.getTexture().dispose();
135		cane.getTexture().dispose();
136	}
137}