Skip to content

Instantly share code, notes, and snippets.

@AnnaBoro
Last active November 23, 2015 22:11
Show Gist options
  • Save AnnaBoro/8a5debad755610161ffc to your computer and use it in GitHub Desktop.
Save AnnaBoro/8a5debad755610161ffc to your computer and use it in GitHub Desktop.
drawable+destroyable - Frame6
package lesson6.gametank;
import java.awt.*;
public abstract class AbstractTank implements Drawable, Destroyable{
private Direction direction;
private int x = 128;
private int y = 512;
protected int speed = 10;
protected Color tankColor;
protected Color towerColor;
private ActionField actionField;
private BattleField battleField;
public AbstractTank() {
}
public AbstractTank(ActionField actionField, BattleField battleField) {
this(actionField, battleField, 128, 512, Direction.UP);
towerColor = new Color(70, 70, 70);
tankColor = new Color(0, 255, 0);
}
public AbstractTank(ActionField actionField, BattleField battleField, int x, int y, Direction direction) {
this.actionField = actionField;
this.battleField = battleField;
this.x = x;
this.y = y;
this.direction = direction;
towerColor = new Color(70, 70, 70);
tankColor = new Color(0, 255, 0);
}
@Override
public void draw(Graphics g) {
g.setColor(towerColor);
g.fillRect(this.getX(), this.getY(), 64, 64);
g.setColor(tankColor);
if (this.getDirection().getId() == 1) {
g.fillRect(this.getX() + 20, this.getY(), 24, 34);
} else if (this.getDirection().getId() == 2) {
g.fillRect(this.getX() + 20, this.getY() + 30, 24, 34);
} else if (this.getDirection().getId() == 3) {
g.fillRect(this.getX(), this.getY() + 20, 34, 24);
} else {
g.fillRect(this.getX() + 30, this.getY() + 20, 34, 24);
}
}
public void destroy() {
x = -100;
y = -100;
}
public void turn(Direction direction) {
this.direction = direction;
actionField.processTurn(this);
}
public void move() throws InterruptedException {
actionField.processMove(this);
}
public void fire() throws InterruptedException {
Bullet bullet = new Bullet(x + 25, y + 25, direction);
actionField.processFire(bullet);
}
public void clean() throws InterruptedException {
turn(Direction.LEFT);
while (isEmptyX()) {
fire();
}
moveToQuadrant(getY(), 0);
turn(Direction.UP);
while (isEmptyY()) {
fire();
}
moveToQuadrant(0, 0);
turn(Direction.RIGHT);
while (isEmptyX()) {
fire();
}
for (int i = 0; i < battleField.getBattleField().length; i++) {
moveToQuadrant(0, i * 64);
turn(Direction.DOWN);
while (isEmptyY()) {
fire();
}
}
}
public boolean isEmptyY() {
int index = getX() / 64;
int firstPoint = 0;
int endPoint = getY() / 64;
if (getDirection() == Direction.DOWN) {
firstPoint = getY() / 64;
endPoint = battleField.getBattleField()[index].length;
}
for (int i = firstPoint; i < endPoint; i++) {
if (battleField.getBattleField()[i][index] == "B") {
return true;
}
}
return false;
}
public boolean isEmptyX() {
int index = getY() / 64;
int firstPoint = 0;
int endPoint = getX() / 64;
if (getDirection() == Direction.RIGHT) {
firstPoint = getX() / 64;
endPoint = battleField.getBattleField()[index].length;
}
for (int i = firstPoint; i < endPoint; i++) {
if (battleField.getBattleField()[index][i] == "B") {
return true;
}
}
return false;
}
public int[] getRandomQuadrant() {
int[] randomNumbers = getRandomNumbers();
for (int i = 0; i < randomNumbers.length; i++) {
if (randomNumbers[i] > 8) {
randomNumbers[i] = randomNumbers[i] - 1;
}
}
return randomNumbers;
}
public int[] getRandomNumbers() {
String randNum = String.valueOf(System.currentTimeMillis());
String randNum1 = randNum.substring(randNum.length()-1);
String randNum2 = randNum.substring(randNum.length()-2, randNum.length()-1);
int randNumInt1 = Integer.parseInt(randNum1);
int randNumInt2 = Integer.parseInt(randNum2);
int[] randomNumbers = {randNumInt1, randNumInt2};
return randomNumbers;
}
public Direction getRandomDirection(){
int[] randomNumbers = getRandomNumbers();
int randNumInt1 = randomNumbers[0];
int randNumInt2 = randomNumbers[1];
if (randNumInt1 > randNumInt2) {
if (randNumInt1 % 2 == 0) {
direction = Direction.DOWN;
}
else {
direction = Direction.UP;
}
}
else {
if (randNumInt2 % 2 == 0) {
direction = Direction.LEFT;
}
else {
direction = Direction.RIGHT;
}
}
return direction;
}
public void moveRandom() throws InterruptedException {
while (true) {
turn(getRandomDirection());
move();
}
}
public void moveToQuadrant(int v, int h) throws InterruptedException {
String quadrant = actionField.getQuadrant(v, h);
int lineIndex = quadrant.indexOf("_");
int tankXNew = 64 * Integer.parseInt(quadrant.substring(0, lineIndex));
int tankYNew = 64 * Integer.parseInt(quadrant.substring(lineIndex+1));
if ((tankXNew - getX()) > 0) {
int steps = (tankXNew - getX()) / 64;
for (int step = 0; step < steps; step++) {
turn(Direction.RIGHT);
if ((battleField.getBattleField()[getY() / 64][getX() / 64 + 1].equals("B") ||
(actionField.getTank().getY() / 64 == actionField.getAgressor().getY() / 64)) &&
((actionField.getTank().getX() / 64 + 1) == actionField.getAgressor().getX() / 64)) {
fire();
}
move();
}
} else if ((tankXNew - getX()) < 0) {
int steps = Math.abs((tankXNew - getX()) / 64);
for (int step = 0; step < steps; step++) {
turn(Direction.LEFT);
if ((battleField.getBattleField()[getY() / 64][getX() / 64 - 1].equals("B")) ||
(actionField.getTank().getY() / 64 == actionField.getAgressor().getY() / 64) &&
((actionField.getTank().getX() / 64 - 1) == actionField.getAgressor().getX() / 64)) {
fire();
}
move();
}
}
if ((tankYNew - getY()) > 0) {
int steps = (tankYNew - getY()) / 64;
for (int step = 0; step < steps; step++) {
turn(Direction.DOWN);
if ((battleField.getBattleField()[getY() / 64 + 1][getX()/64].equals("B")) ||
((actionField.getTank().getY() / 64 + 1) == actionField.getAgressor().getY() / 64) &&
(actionField.getTank().getX() / 64 == actionField.getAgressor().getX() / 64)) {
fire();
}
move();
}
} else if ((tankYNew - getY()) < 0) {
int steps = Math.abs((getY() - tankYNew) / 64);
for (int step = 0; step < steps; step++) {
turn(Direction.UP);
if ((battleField.getBattleField()[getY() / 64 - 1][getX()/64].equals("B")) ||
((actionField.getTank().getY() / 64 - 1) == actionField.getAgressor().getY() / 64) &&
(actionField.getTank().getX()/64 == actionField.getAgressor().getX() / 64)) {
fire();
}
move();
}
}
}
public Direction getDirection() {
return direction;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void updateX(int i) {
if (x < 0) {
x = 0;
}
x += i;
}
public void updateY(int i) {
if (y < 0) {
y = 0;
}
y += i;
}
public int getSpeed() {
return speed;
}
}
package lesson6.gametank;
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class ActionField extends JPanel{
private boolean COLORDED_MODE = false;
private BattleField bf;
private AbstractTank defender;
private AbstractTank agressor;
private Bullet bullet;
private int[][] randomArr = {{64, 64}, {64, 448}, {448, 64}};
private int randomPosition = -1;
public ActionField() throws Exception {
bf = new BattleField();
defender = new T34(this, bf);
int[] xy = randomArr[getRandomNum()];
int y2 = xy[1];
int x2 = xy[0];
agressor = new Tiger(this, bf, x2, y2, Direction.DOWN);
bullet = new Bullet(-100, -100, Direction.UP);
initFrame();
}
public void runTheGame() throws Exception {
defender.moveToQuadrant(64, 64);
defender.clean();
// defender.moveRandom();
// defender.destroy();
// defender.fire();
// defender.turn(4);
// defender.fire();
// defender.move();
// defender.move();
// defender.turn(1);
// defender.turn(2);
// defender.fire();
// defender.move();
// defender.fire();
}
private boolean processInterception() throws InterruptedException {
if (isOnTheField()) {
if (removeBrick(false)) {
bullet.destroy();
}
else if (removeTank()) {
agressor.destroy();
bullet.destroy();
repaint();
Thread.sleep(3000);
int[] xy = randomArr[getRandomNum()];
agressor.updateX(xy[0]);
agressor.updateY(xy[1]);
repaint();
((Tiger)agressor).setArmor(1);
}
return false;
}
return true;
}
public boolean isOnTheField() {
if ((bullet.getX() > 0 && bullet.getX() < 575)
&& (bullet.getY() > 0 && bullet.getY() < 575)) {
return true;
}
return false;
}
public boolean removeBrick(boolean removeType) {
String quadrant;
if (removeType) {
quadrant = getQuadrant(defender.getX(), defender.getY());
}
else quadrant = getQuadrant(bullet.getX(), bullet.getY());
int i = Integer.parseInt(quadrant.substring(0, quadrant.indexOf("_")));
int j = Integer.parseInt(quadrant.substring(quadrant.indexOf("_") + 1, quadrant.length()));
if (bf.scanQuadrant(i, j) == "B") {
bf.updateQuadrant(i, j, " ");
repaint();
return true;
}
return false;
}
public boolean removeTank() throws InterruptedException {
String quadrant = getQuadrant(bullet.getX(), bullet.getY());
String quadrant2 = getQuadrant(agressor.getX(), agressor.getY());
if (quadrant.equalsIgnoreCase(quadrant2)) {
if (((Tiger) agressor).getArmor() == 1) {
((Tiger) agressor).setArmor(((Tiger) agressor).getArmor() - 1);
bullet.destroy();
repaint();
defender.fire();
return false;
}
else return true;
}
return false;
}
String getQuadrant(int v, int h) {
int x = v / 64;
int y = h / 64;
return y + "_" + x;
}
public String getQuadrantXY(int v, int h) {
return (v - 1) * 64 + "_" + (h - 1) * 64;
}
public void initFrame() throws Exception {
JFrame frame = new JFrame("BATTLE FIELD, DAY 2");
frame.setLocation(750, 150);
frame.setMinimumSize(new Dimension(bf.getBF_WIDTH(), bf.getBF_HEIGHT() + 22));
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(this);
frame.pack();
frame.setVisible(true);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int i = 0;
Color cc;
for (int v = 0; v < 9; v++) {
for (int h = 0; h < 9; h++) {
if (COLORDED_MODE) {
if (i % 2 == 0) {
cc = new Color(252, 241, 177);
} else {
cc = new Color(233, 243, 255);
}
} else {
cc = new Color(180, 180, 180);
}
i++;
g.setColor(cc);
g.fillRect(h * 64, v * 64, 64, 64);
}
}
for (int j = 0; j < bf.getDimensionY(); j++) {
for (int k = 0; k < bf.getDimensionY(); k++) {
if (bf.scanQuadrant(j, k).equals("B")) {
String coordinates = getQuadrantXY(j + 1, k + 1);
int separator = coordinates.indexOf("_");
int y = Integer.parseInt(coordinates.substring(0, separator));
int x = Integer.parseInt(coordinates.substring(separator + 1));
g.setColor(new Color(0, 0, 255));
g.fillRect(x, y, 64, 64);
}
}
}
defender.draw(g);
agressor.draw(g);
bullet.draw(g);
}
public void processMove(AbstractTank tank) throws InterruptedException {
for (int i = 0; i < 64; i++) {
if (tank.getDirection().getId() == 1) {
if (tank.getY() !=0) {
tank.updateY(-1);
}
else System.out.println("Wrong direction");
}
else if (tank.getDirection().getId() == 2) {
if (tank.getY() != 512) {
tank.updateY(1);
}
else System.out.println("Wrong direction");
}
else if (tank.getDirection().getId() == 3) {
if (tank.getX() != 0) {
tank.updateX(- 1);
}
else System.out.println("Wrong direction");
}
else if (tank.getDirection().getId() == 4) {
if (tank.getX() != 512) {
tank.updateX(1);
}
else System.out.println("Wrong direction");
}
repaint();
Thread.sleep(tank.getSpeed()/2);
}
this.removeBrick(true);
}
public void processTurn(AbstractTank tank) {
repaint();
}
public void processFire(Bullet bullet) throws InterruptedException {
this.bullet = bullet;
while (isOnTheField()) {
for (int i = 0; i < 64; ) {
if (defender.getDirection().getId() == 1) {
bullet.updateY(-1);
}
else if (defender.getDirection().getId() == 2) {
bullet.updateY(1);
}
else if (defender.getDirection().getId() == 3) {
bullet.updateX(-1);
}
else if (defender.getDirection().getId() == 4) {
bullet.updateX(1);
}
processInterception();
repaint();
Thread.sleep(bullet.getSpeed());
break;
}
}
}
public int getRandomNum() {
Random random = new Random();
int randomInt = random.nextInt(3);
if (randomPosition == randomInt) {
return getRandomNum();
}
randomPosition = randomInt;
return randomInt;
}
public AbstractTank getTank() {
return defender;
}
public AbstractTank getAgressor() {
return agressor;
}
}
package lesson6.gametank;
import java.awt.*;
public class Bullet implements Drawable, Destroyable{
private int x = -100;
private int y = -100;
private int speed = 5;
private Direction direction;
public Bullet(int x, int y, Direction direction) {
this.x = x;
this.y = y;
this.direction = direction;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getSpeed() {
return speed;
}
public Direction getDirection() {
return direction;
}
public void updateX(int i) {
x += i;
}
public void updateY(int i) {
y += i;
}
public void destroy() {
x = -100;
y = -100;
}
@Override
public void draw(Graphics g) {
g.setColor(new Color(255, 255, 0));
g.fillRect(this.getX(), this.getY(), 14, 14);
}
}
package lesson6.gametank;
public interface Destroyable {
public void destroy();
}
package lesson6.gametank;
import java.awt.*;
public interface Drawable {
public void draw(Graphics g);
}
package lesson6.gametank;
import java.awt.*;
public class T34 extends AbstractTank {
public T34(ActionField actionField, BattleField battleField) {
super(actionField, battleField);
tankColor = new Color(0, 255, 0);
towerColor = new Color(255, 0, 0);
}
public T34(ActionField actionField, BattleField battleField, int x, int y, Direction direction) {
super(actionField, battleField, x, y, direction);
tankColor = new Color(0, 255, 0);
towerColor = new Color(255, 0, 0);
}
}
package lesson6.gametank;
import java.awt.*;
public class Tiger extends AbstractTank {
private int armor;
public Tiger(ActionField actionField, BattleField battleField, int armor) {
super(actionField, battleField);
this.armor = armor;
tankColor = new Color(255, 0, 0);
towerColor = new Color(0, 255, 0);
}
public Tiger(ActionField actionField, BattleField battleField, int x, int y, Direction direction) {
super(actionField, battleField, x, y, direction);
armor = 1;
tankColor = new Color(255, 0, 0);
towerColor = new Color(0, 255, 0);
}
public void setArmor(int armor) {
this.armor = armor;
}
public int getArmor() {
return armor;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment