Skip to content

Instantly share code, notes, and snippets.

@AnnaBoro
Created November 16, 2015 17:51
Show Gist options
  • Save AnnaBoro/c0791c0b964ac963722c to your computer and use it in GitHub Desktop.
Save AnnaBoro/c0791c0b964ac963722c to your computer and use it in GitHub Desktop.
frame8 - destroy agressor
package lesson4.game;
import javax.swing.*;
import java.awt.*;
import java.util.Random;
/**
* Created by anna on 27.10.15.
*/
public class ActionField extends JPanel{
private boolean COLORDED_MODE = false;
private BattleField bf;
private Tank1 tank;
private Tank1 agressor;
private Bullet bullet;
private int[][] randomArr = {{64, 64}, {448, 64}, {64, 448}};
private int randomPosition = -1;
public ActionField() throws Exception {
bf = new BattleField();
tank = new Tank1(this, bf);
int[] xy = randomArr[getRandomNum()];
int y2 = xy[1];
int x2 = xy[0];
agressor = new Tank1(this, bf, x2, y2, Direction.DOWN);
bullet = new Bullet(-100, -100, Direction.UP);
initFrame();
}
public void runTheGame() throws Exception {
tank.moveToQuadrant(64, 64);
tank.clean();
// tank.moveRandom();
// tank.destroy();
// tank.fire();
// tank.turn(4);
// tank.fire();
// tank.move();
// tank.move();
// tank.turn(1);
// tank.turn(2);
// tank.fire();
// tank.move();
// tank.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();
}
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(tank.getX(), tank.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)) {
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);
}
}
}
g.setColor(new Color(255, 0, 0));
g.fillRect(tank.getX(), tank.getY(), 64, 64);
g.setColor(new Color(0, 255, 0));
if (tank.getDirection().getId() == 1) {
g.fillRect(tank.getX() + 20, tank.getY(), 24, 34);
} else if (tank.getDirection().getId() == 2) {
g.fillRect(tank.getX() + 20, tank.getY() + 30, 24, 34);
} else if (tank.getDirection().getId() == 3) {
g.fillRect(tank.getX(), tank.getY() + 20, 34, 24);
} else {
g.fillRect(tank.getX() + 30, tank.getY() + 20, 34, 24);
}
g.setColor(new Color(255, 255, 0));
g.fillRect(bullet.getX(), bullet.getY(), 14, 14);
/////////////////////
g.setColor(new Color(255, 0, 0));
g.fillRect(agressor.getX(), agressor.getY(), 64, 64);
g.setColor(new Color(0, 255, 0));
if (agressor.getDirection().getId() == 1) {
g.fillRect(agressor.getX() + 20, agressor.getY(), 24, 34);
} else if (tank.getDirection().getId() == 2) {
g.fillRect(agressor.getX() + 20, agressor.getY() + 30, 24, 34);
} else if (agressor.getDirection().getId() == 3) {
g.fillRect(agressor.getX(), agressor.getY() + 20, 34, 24);
} else {
g.fillRect(agressor.getX() + 30, agressor.getY() + 20, 34, 24);
}
g.setColor(new Color(255, 255, 0));
g.fillRect(bullet.getX(), bullet.getY(), 14, 14);
////////////////////
}
public void processMove(Tank1 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(Tank1 tank) {
repaint();
}
public void processFire(Bullet bullet) throws InterruptedException {
this.bullet = bullet;
while (isOnTheField()) {
for (int i = 0; i < 64; ) {
if (tank.getDirection().getId() == 1) {
bullet.updateY(-1);
System.out.println(bullet.getY());
}
else if (tank.getDirection().getId() == 2) {
bullet.updateY(1);
System.out.println(bullet.getY());
}
else if (tank.getDirection().getId() == 3) {
bullet.updateX(-1);
System.out.println(bullet.getX());
}
else if (tank.getDirection().getId() == 4) {
bullet.updateX(1);
System.out.println(bullet.getX());
}
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 Tank1 getTank() {
return tank;
}
public Tank1 getAgressor() {
return agressor;
}
}
package lesson4.game;
/**
* Created by anna on 27.10.15.
*/
public class BattleField {
private int BF_WIDTH = 576;
private int BF_HEIGHT = 576;
private String[][] battleField = {
{"B", " ", "B", "B", "B", "B", "B", "B", "B"},
{"B", " ", " ", " ", " ", " ", " ", " ", "B"},
{"B", "B", " ", " ", "B", " ", " ", "B", "B"},
{"B", " ", "B", " ", " ", " ", "B", " ", "B"},
{"B", " ", "B", " ", " ", " ", " ", " ", "B"},
{" ", " ", " ", "B", " ", "B", " ", "B", "B"},
{" ", "B", " ", " ", " ", " ", " ", "B", "B"},
{" ", " ", " ", "B", "B", "B", " ", " ", "B"},
{"B", " ", " ", "B", " ", " ", " ", " ", "B"}};
public BattleField() {
}
public String[][] getBattleField() {
return battleField;
}
public int getBF_WIDTH() {
return BF_WIDTH;
}
public int getBF_HEIGHT() {
return BF_HEIGHT;
}
public String scanQuadrant(int v, int h) {
return battleField[v][h];
}
public void updateQuadrant(int v, int h, String str) {
battleField[v][h] = str;
}
public int getDimensionX() {
return battleField[0][0].length();
}
public int getDimensionY() {
return battleField[0].length;
}
}
package lesson4.game;
public class BT7 extends Tank1 {
private int speed;
public BT7() {
}
public BT7(ActionField actionField, BattleField battleField, int x, int y, Direction direction) {
super(actionField, battleField, x, y, direction);
speed = super.getSpeed() / 2;
}
}
package lesson4.game;
/**
* Created by anna on 27.10.15.
*/
public class Bullet {
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;
}
}
package lesson4.game;
/**
* Created by anna on 10.11.15.
*/
public enum Direction {
UP(1), DOWN(2), LEFT(3), RIGHT(4);
private int id;
private Direction(int id) {
this.id = id;
}
public int getId() {
return id;
}
}
package lesson4.game;
import lesson4.game.ActionField;
/**
* Created by anna on 28.10.15.
*/
public class Launcher {
public static void main(String[] args) throws Exception {
ActionField af = new ActionField();
af.runTheGame();
}
}
package lesson4.game;
import lesson4.game.ActionField;
import lesson4.game.BattleField;
import lesson4.game.Bullet;
/**
* Created by anna on 27.10.15.
*/
public class Tank1 {
private Direction direction;
private int x = 128;
private int y = 512;
protected int speed = 10;
private ActionField actionField;
private BattleField battleField;
public Tank1() {
}
public Tank1(ActionField actionField, BattleField battleField) {
this(actionField, battleField, 128, 512, Direction.UP);
}
public Tank1(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;
}
public void destroy() {
x = -100;
y = -100;
actionField.repaint();
}
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 lesson4.game;
/**
* Created by anna on 10.11.15.
*/
public class Tiger extends Tank1{
private int armor;
public Tiger() {
}
public Tiger(ActionField actionField, BattleField battleField, int x, int y, Direction direction, int armor) {
super(actionField, battleField, x, y, direction);
armor = 1;
}
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