Skip to content
Snippets Groups Projects
Commit 39697a5c authored by Sander Østrem Fagernes's avatar Sander Østrem Fagernes
Browse files

Merge branch 'move_tank' into 'main'

Simple model + logic for moving tank

See merge request !7
parents 7dd78213 d8a8d53a
No related branches found
No related tags found
1 merge request!7Simple model + logic for moving tank
Pipeline #203736 passed
frontend/assets/tank-khaki.png

349 B

package com.game.tankwars;
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 com.game.tankwars.model.Tank;
public class TankWarsGame extends ApplicationAdapter {
SpriteBatch batch;
Texture img;
Tank tank;
@Override
public void create () {
batch = new SpriteBatch();
img = new Texture("badlogic.jpg");
tank = new Tank(new Vector2(50, 50), new Texture("tank-khaki.png"));
}
@Override
public void render () {
ScreenUtils.clear(1, 0, 0, 1);
if(Gdx.input.isKeyPressed(Input.Keys.D)) {
tank.moveRight();
}
if(Gdx.input.isKeyPressed(Input.Keys.A)) {
tank.moveLeft();
}
ScreenUtils.clear(0, 0, 0, 1);
batch.begin();
batch.draw(img, 0, 0);
batch.draw(tank.getTexture(), tank.getPosition().x, tank.getPosition().y, Tank.TANK_WIDTH, Tank.TANK_HEIGHT);
batch.end();
}
@Override
public void dispose () {
batch.dispose();
img.dispose();
tank.getTexture().dispose();
}
}
package com.game.tankwars.model;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
public class Tank {
public static final int TANK_MOVESPEED = 2;
public static final int TANK_WIDTH = 50;
public static final int TANK_HEIGHT = 50;
private Vector2 position;
private Rectangle bounds;
private Texture texture;
public Tank(Vector2 position, Texture texture) {
this.position = position;
this.bounds = new Rectangle(position.x, position.y, TANK_WIDTH, TANK_HEIGHT);
this.texture = texture;
}
public Vector2 getPosition() {
return position;
}
public Rectangle getBounds() {
return bounds;
}
public Texture getTexture() {
return texture;
}
public void setPosition(Vector2 position) {
this.position = position;
}
public void setBounds(Rectangle bounds) {
this.bounds = bounds;
}
public void setTexture(Texture texture) {
this.texture = texture;
}
public void moveRight() {
position.x += TANK_MOVESPEED;
}
public void moveLeft() {
position.x -= TANK_MOVESPEED;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment