Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- enum Direction {
- UP,
- RIGHT,
- DOWN,
- LEFT
- }
- class Bullet {
- public float x;
- public float y;
- public int size;
- public int dirX;
- public int dirY;
- public float speed;
- public Tank origin;
- public boolean toDelete;
- public Bullet(float x, float y, Tank origin, int dirX, int dirY) {
- speed = 7;
- this.x = x;
- this.y = y;
- this.origin = origin;
- size = bulletSize;
- this.dirX = dirX;
- this.dirY = dirY;
- toDelete = false;
- }
- }
- class Tank {
- public float x;
- public float y;
- public int hp;
- public int dmg;
- public int size;
- public float speed;
- public int dirX;
- public int dirY;
- public Direction shootDir;
- public boolean shoot;
- public float startX;
- public float startY;
- public int startHP;
- public Tank(boolean p1, int hp, int dmg, int size, float speed) {
- if (p1) {
- x = 0 + 10;
- } else {
- x = width - size - 10;
- }
- y = hudHeight + (height-hudHeight)/2;
- startX = x;
- startY = y;
- this.hp = hp;
- startHP = hp;
- this.dmg = dmg;
- this.size = size;
- this.speed = speed;
- dirX = 0;
- dirY = 0;
- shootDir = Direction.UP;
- shoot = false;
- }
- }
- Tank p1;
- Tank p2;
- int bulletSize = 4;
- float barrelSize = 5;
- ArrayList<Bullet> bulletList = new ArrayList<Bullet>();
- int hudHeight = 100;
- int p1Points = 0;
- int p2Points = 0;
- void setup() {
- size(600, 600);
- p1 = new Tank(true, 3, 1, 20, 5);
- p2 = new Tank(false, 3, 1, 20, 5);
- }
- void draw() {
- background(0);
- updateTank(p1);
- updateTank(p2);
- updateBullets();
- drawTank(p1);
- drawTank(p2);
- drawBullets();
- drawHUD();
- }
- void takeDamage(Tank t, Bullet b) {
- t.hp -= b.origin.dmg;
- if (t.hp <= 0) {
- newRound();
- }
- }
- void drawHUD() {
- fill(255);
- stroke(0, 0, 255);
- rect(0, 0, width, hudHeight);
- fill(255, 0, 0);
- rect(10, 10, 100, 80);
- float p1HPLength = ((float)p1.hp / (float)p1.startHP) * 100;
- fill(0, 255, 0);
- rect(10, 10, p1HPLength, 80);
- fill(255, 0, 0);
- rect(width-110, 10, 100, 80);
- float p2HPLength = ((float)p2.hp / (float)p2.startHP) * 100;
- fill(0, 255, 0);
- rect(width-110, 10, p2HPLength, 80);
- textSize(32);
- fill(0, 0, 255);
- text(p1Points, 238, 60);
- fill(0, 0, 255);
- text(p2Points, 328, 60);
- }
- void updateTank(Tank t) {
- t.x += t.dirX * t.speed;
- t.y += t.dirY * t.speed;
- if (t.x + t.size > width) {
- t.x = width - t.size;
- }
- if (t.x < 0) {
- t.x = 0;
- }
- if (t.y + t.size > height) {
- t.y = height - t.size;
- }
- if (t.y < hudHeight) {
- t.y = hudHeight;
- }
- shootBullet(t);
- }
- void keyPressed() {
- if (key == 'w') {
- p1.dirY = -1;
- p1.shootDir = Direction.UP;
- p1.dirX = 0;
- } else if (key == 's') {
- p1.dirY = 1;
- p1.shootDir = Direction.DOWN;
- p1.dirX = 0;
- } else if (key == 'a') {
- p1.dirX = -1;
- p1.shootDir = Direction.LEFT;
- p1.dirY = 0;
- } else if (key == 'd') {
- p1.dirX = 1;
- p1.shootDir = Direction.RIGHT;
- p1.dirY = 0;
- } else if (key == 'c') {
- p1.shoot = true;
- }
- if (key == CODED) {
- if (keyCode == UP) {
- p2.dirY = -1;
- p2.shootDir = Direction.UP;
- p2.dirX = 0;
- } else if (keyCode == DOWN) {
- p2.dirY = 1;
- p2.shootDir = Direction.DOWN;
- p2.dirX = 0;
- } else if (keyCode == LEFT) {
- p2.dirX = -1;
- p2.shootDir = Direction.LEFT;
- p2.dirY = 0;
- } else if (keyCode == RIGHT) {
- p2.dirX = 1;
- p2.shootDir = Direction.RIGHT;
- p2.dirY = 0;
- }
- }
- if (key == 'p') {
- p2.shoot = true;
- }
- }
- void keyReleased() {
- if (key == 'w') {
- if (p1.dirY == -1) {
- p1.dirY = 0;
- }
- } else if (key == 's') {
- if (p1.dirY == 1) {
- p1.dirY = 0;
- }
- } else if (key == 'a') {
- if (p1.dirX == -1) {
- p1.dirX = 0;
- }
- } else if (key == 'd') {
- if (p1.dirX == 1) {
- p1.dirX = 0;
- }
- }
- if (key == CODED) {
- if (keyCode == UP) {
- if (p2.dirY == -1) {
- p2.dirY = 0;
- }
- } else if (keyCode == DOWN) {
- if (p2.dirY == 1) {
- p2.dirY = 0;
- }
- } else if (keyCode == LEFT) {
- if (p2.dirX == -1) {
- p2.dirX = 0;
- }
- } else if (keyCode == RIGHT) {
- if (p2.dirX == 1) {
- p2.dirX = 0;
- }
- }
- }
- }
- void newRound() {
- if(p1.hp <= 0) {
- p2Points++;
- }
- else {
- p1Points++;
- }
- p1.hp = p1.startHP;
- p1.x = p1.startX;
- p1.y = p1.startY;
- p1.shootDir = Direction.UP;
- p2.hp = p2.startHP;
- p2.x = p2.startX;
- p2.y = p2.startY;
- p2.shootDir = Direction.UP;
- bulletList.clear();
- }
- void updateBullets() {
- int i = 0;
- for (i = 0; i< bulletList.size(); i++) {
- Bullet b = bulletList.get(i);
- if(b.toDelete) {
- continue;
- }
- b.x += b.dirX * b.speed;
- b.y += b.dirY * b.speed;
- if (b.x < 0 ||
- b.x > width ||
- b.y < 0 ||
- b.y > height)
- {
- b.toDelete = true;
- }
- boolean player1Hit = checkCollision(b, p1);
- boolean player2Hit = checkCollision(b, p2);
- if (player1Hit) {
- b.toDelete = true;
- takeDamage(p1, b);
- }
- if (player2Hit) {
- b.toDelete = true;
- takeDamage(p2, b);
- }
- }
- }
- boolean checkCollision(Bullet b, Tank t) {
- if (b.toDelete || b.origin == t) {
- return false;
- }
- if (t.x < b.x + b.size &&
- t.x + t.size > b.x &&
- t.y < b.y + b.size &&
- t.y + t.size > b.y)
- {
- return true;
- }
- return false;
- }
- void drawBullets() {
- int i;
- for (i = 0; i<bulletList.size(); i++) {
- Bullet b = bulletList.get(i);
- if (b.toDelete) {
- continue;
- }
- fill(255, 255, 0);
- stroke(255, 255, 0);
- rect(b.x, b.y, bulletSize, bulletSize);
- }
- }
- void drawTank(Tank t) {
- fill(255, 255, 255);
- stroke(255, 255, 255);
- rect(t.x, t.y, t.size, p1.size);
- if (t.shootDir == Direction.UP) {
- rect(t.x + t.size/2 - barrelSize/2, t.y - barrelSize, barrelSize, barrelSize);
- } else if (t.shootDir == Direction.RIGHT) {
- rect(t.x + t.size, t.y + t.size/2 - barrelSize/2, barrelSize, barrelSize);
- } else if (t.shootDir == Direction.DOWN) {
- rect(t.x + t.size/2 - barrelSize/2, t.y + t.size, barrelSize, barrelSize);
- } else {
- rect(t.x - barrelSize, t.y + t.size/2 - barrelSize/2, barrelSize, barrelSize);
- }
- }
- void shootBullet(Tank t) {
- if (!t.shoot) {
- return;
- }
- float x, y;
- int dirX = 0, dirY = 0;
- if (t.shootDir == Direction.UP) {
- x = t.x + t.size/2 - barrelSize/2;
- y = t.y - barrelSize - bulletSize;
- dirY = -1;
- } else if (t.shootDir == Direction.RIGHT) {
- x = t.x + t.size + barrelSize;
- y = t.y + t.size/2 - barrelSize/2;
- dirX = 1;
- } else if (t.shootDir == Direction.DOWN) {
- x = t.x + t.size/2 - barrelSize/2;
- y = t.y + t.size + barrelSize;
- dirY = 1;
- } else {
- x = t.x - barrelSize - bulletSize;
- y = t.y + t.size/2 - barrelSize/2;
- dirX = -1;
- }
- bulletList.add(new Bullet(x, y, t, dirX, dirY));
- t.shoot = false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement