Skip to content

Instantly share code, notes, and snippets.

@AnnaBoro
Last active October 27, 2015 19:45
Show Gist options
  • Save AnnaBoro/098d71ae4e14e4785455 to your computer and use it in GitHub Desktop.
Save AnnaBoro/098d71ae4e14e4785455 to your computer and use it in GitHub Desktop.
tankInObjects-Frame2
package lesson4.gametank;
import java.awt.*;
/**
* Created by anna on 27.10.15.
*/
public class ActionField {
BattleField bf;
public ActionField() throws Exception {
bf = new BattleField();
}
public void runTheGame() throws Exception {
initTanks();
clean();
}
public void initTanks() {
int[] randomQuadrant = getRandomQuadrant();
while (bf.getBattleField()[randomQuadrant[1]][randomQuadrant[0]].equals("B")) {
randomQuadrant = getRandomQuadrant();
}
bf.getTank().setTankX(randomQuadrant[0] * 64);
bf.getTank().setTankY(randomQuadrant[1] * 64);
}
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 boolean processInterception() {
if (isOnTheField()) {
if (removeBrick(false)) {
bf.getTank().getBullet().setBulletX(-100);
bf.getTank().getBullet().setBulletY(-100);
}
return false;
}
return true;
}
public boolean isOnTheField() {
if ((bf.getTank().getBullet().getBulletX() > 0 && bf.getTank().getBullet().getBulletX() < 575)
&& (bf.getTank().getBullet().getBulletY() > 0 && bf.getTank().getBullet().getBulletY() < 575)) {
return true;
}
return false;
}
public boolean removeBrick(boolean removeType) {
String quadrant;
if (removeType) {
quadrant = getQuadrant(bf.getTank().getTankX(), bf.getTank().getTankY());
}
else quadrant = getQuadrant(bf.getTank().getBullet().bulletX, bf.getTank().getBullet().getBulletY());
int i = Integer.parseInt(quadrant.substring(0, quadrant.indexOf("_")));
int j = Integer.parseInt(quadrant.substring(quadrant.indexOf("_") + 1, quadrant.length()));
if (bf.getBattleField()[i][j] == "B") {
bf.getBattleField()[i][j] = " ";
bf.repaint();
return true;
}
return false;
}
String getQuadrant(int v, int h) {
int x = v / 64;
int y = h / 64;
return y + "_" + x;
}
public void fire() throws InterruptedException {
bf.getTank().getBullet().setBulletX(bf.getTank().getTankX() + 25);
bf.getTank().getBullet().setBulletY(bf.getTank().getTankY() + 25);
while (isOnTheField()) {
runBullet();
}
}
public void runBullet() throws InterruptedException {
for (int i = 0; i < 64; ) {
if (bf.getTank().getTankDirection() == 1) {
bf.getTank().getBullet().setBulletY(bf.getTank().getBullet().getBulletY() - 1);
}
else if (bf.getTank().getTankDirection() == 2) {
bf.getTank().getBullet().setBulletY(bf.getTank().getBullet().getBulletY() + 1);
}
else if (bf.getTank().getTankDirection() == 3) {
bf.getTank().getBullet().setBulletX(bf.getTank().getBullet().getBulletX() - 1);
}
else if (bf.getTank().getTankDirection() == 4) {
bf.getTank().getBullet().setBulletX(bf.getTank().getBullet().getBulletX() + 1);
}
processInterception();
bf.repaint();
Thread.sleep(bf.getTank().getBullet().bulletSpeed);
break;
}
}
public String getQuadrantXY(int v, int h) {
return (v - 1) * 64 + "_" + (h - 1) * 64;
}
public void move(int direction) throws Exception {
bf.repaint();
Thread.sleep(bf.getTank().getSpeed());
turn(direction);
moveTank();
}
public void moveTank() throws InterruptedException {
for (int i = 0; i < 64; i++) {
if (bf.getTank().getTankDirection() == 1) {
if (bf.getTank().getTankY() !=0) {
bf.getTank().setTankY(bf.getTank().getTankY() - 1);
}
else System.out.println("Wrong direction");
}
else if (bf.getTank().getTankDirection() == 2) {
if (bf.getTank().getTankY() != 512) {
bf.getTank().setTankY(bf.getTank().getTankY() + 1);
}
else System.out.println("Wrong direction");
}
else if (bf.getTank().getTankDirection() == 3) {
if (bf.getTank().getTankX() != 0) {
bf.getTank().setTankX(bf.getTank().getTankX() - 1);
}
else System.out.println("Wrong direction");
}
else if (bf.getTank().getTankDirection() == 4) {
if (bf.getTank().getTankX() != 512) {
bf.getTank().setTankX(bf.getTank().getTankX() + 1);
}
else System.out.println("Wrong direction");
}
bf.repaint();
Thread.sleep(bf.getTank().getSpeed()/2);
}
System.out.println(bf.getTank().getTankY() + "_" + bf.getTank().getTankX());
}
public void turn(int direction) {
bf.getTank().setTankDirection(direction);
}
public void moveRandom() throws InterruptedException {
while (true) {
int direction = getRandomDirection();
if (direction == 1) {
if (bf.getTank().getTankY() !=0) {
bf.getTank().setTankY(bf.getTank().getTankY() - 64);
}
else System.out.println("Wrong direction");
}
else if (direction == 2) {
if (bf.getTank().getTankY() != 512) {
bf.getTank().setTankY(bf.getTank().getTankY() + 64);
}
else System.out.println("Wrong direction");
}
else if (direction == 3) {
if (bf.getTank().getTankX() != 0) {
bf.getTank().setTankX(bf.getTank().getTankX() - 64);
}
else System.out.println("Wrong direction");
}
else if (direction == 4) {
if (bf.getTank().getTankX() != 512) {
bf.getTank().setTankX(bf.getTank().getTankX() + 64);
}
else System.out.println("Wrong direction");
}
bf.repaint();
Thread.sleep(bf.getTank().getSpeed() * 2);
}
}
public int getRandomDirection(){
int direction = 0;
int[] randomNumbers = getRandomNumbers();
int randNumInt1 = randomNumbers[0];
int randNumInt2 = randomNumbers[1];
if (randNumInt1 > randNumInt2) {
if (randNumInt1 % 2 == 0) {
direction = 1;
}
else {
direction = 2;
}
}
else {
if (randNumInt2 % 2 == 0) {
direction = 3;
}
else {
direction = 4;
}
}
return direction;
}
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 void moveToQuadrant(int v, int h) throws Exception {
bf.repaint();
Thread.sleep(bf.getTank().getSpeed());
String quadrant = 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 - bf.getTank().getTankX()) > 0) {
int steps = (tankXNew - bf.getTank().getTankX()) / 64;
for (int step = 0; step < steps; step++) {
move(4);
}
}
else if ((tankXNew - bf.getTank().getTankX()) < 0) {
int steps = Math.abs((tankXNew - bf.getTank().getTankX()) / 64);
for (int step = 0; step < steps; step ++) {
move(3);
}
}
if ((tankYNew - bf.getTank().getTankY()) > 0) {
int steps = (tankYNew - bf.getTank().getTankY()) / 64;
for (int step = 0; step < steps; step++) {
move(2);
}
}
else if ((tankYNew - bf.getTank().getTankY()) < 0) {
int steps = Math.abs((bf.getTank().getTankY() - tankYNew) / 64);
for (int step = 0; step < steps; step ++) {
move(1);
}
}
}
public void clean() throws Exception {
turn(3);
while (isEmptyX()) {
fire();
}
moveToQuadrant(bf.getTank().getTankY(), 0);
turn(1);
while (isEmptyY()) {
fire();
}
moveToQuadrant(0, 0);
turn(4);
while (isEmptyX()) {
fire();
}
for (int i = 0; i < bf.getBattleField().length; i++) {
moveToQuadrant(0, i * 64);
turn(2);
while (isEmptyY()) {
fire();
}
}
}
public boolean isEmptyY() {
int index = bf.getTank().getTankX() / 64;
int firstPoint = 0;
int endPoint = bf.getTank().getTankY() / 64;
if (bf.getTank().getTankDirection() == 2) {
firstPoint = bf.getTank().getTankY() / 64;
endPoint = bf.getBattleField()[index].length;
}
for (int i = firstPoint; i < endPoint; i++) {
if (bf.getBattleField()[i][index] == "B") {
return true;
}
}
return false;
}
public boolean isEmptyX() {
int index = bf.getTank().getTankY() / 64;
int firstPoint = 0;
int endPoint = bf.getTank().getTankX() / 64;
if (bf.getTank().getTankDirection() == 4) {
firstPoint = bf.getTank().getTankX() / 64;
endPoint = bf.getBattleField()[index].length;
}
for (int i = firstPoint; i < endPoint; i++) {
if (bf.getBattleField()[index][i] == "B") {
return true;
}
}
return false;
}
}
package lesson4.gametank;
import javax.swing.*;
import java.awt.*;
/**
* Created by anna on 27.10.15.
*/
public class BattleField extends JPanel{
final boolean COLORDED_MODE = false;
final int BF_WIDTH = 576;
final 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"}};
private Tank tank;
public BattleField() throws Exception {
JFrame frame = new JFrame("BATTLE FIELD, DAY 2");
frame.setLocation(750, 150);
frame.setMinimumSize(new Dimension(BF_WIDTH, BF_HEIGHT + 22));
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(this);
frame.pack();
frame.setVisible(true);
tank = new Tank();
}
public String[][] getBattleField() {
return battleField;
}
public Tank getTank() {
return tank;
}
public void setTank(Tank tank) {
this.tank = tank;
}
public String getQuadrantXY(int v, int h) {
return (v - 1) * 64 + "_" + (h - 1) * 64;
}
@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 < battleField.length; j++) {
for (int k = 0; k < battleField.length; k++) {
if (battleField[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.getTankX(), tank.getTankY(), 64, 64);
g.setColor(new Color(0, 255, 0));
if (tank.getTankDirection() == 1) {
g.fillRect(tank.getTankX() + 20, tank.getTankY(), 24, 34);
} else if (tank.getTankDirection() == 2) {
g.fillRect(tank.getTankX() + 20, tank.getTankY() + 30, 24, 34);
} else if (tank.getTankDirection() == 3) {
g.fillRect(tank.getTankX(), tank.getTankY() + 20, 34, 24);
} else {
g.fillRect(tank.getTankX() + 30, tank.getTankY() + 20, 34, 24);
}
g.setColor(new Color(255, 255, 0));
g.fillRect(tank.getBullet().getBulletX(), tank.getBullet().getBulletY(), 14, 14);
}
}
package lesson4.gametank;
/**
* Created by anna on 27.10.15.
*/
public class Bullet {
int bulletX = -100;
int bulletY = -100;
int bulletSpeed = 5;
public Bullet() {
}
public int getBulletX() {
return bulletX;
}
public void setBulletX(int bulletX) {
this.bulletX = bulletX;
}
public int getBulletY() {
return bulletY;
}
public void setBulletY(int bulletY) {
this.bulletY = bulletY;
}
public int getBulletSpeed() {
return bulletSpeed;
}
public void setBulletSpeed(int bulletSpeed) {
this.bulletSpeed = bulletSpeed;
}
}
package lesson4.gametank;
/**
* Created by anna on 27.10.15.
*/
public class Launcher {
public static void main(String[] args) throws Exception {
ActionField af = new ActionField();
af.runTheGame();
}
}
package lesson4.gametank;
/**
* Created by anna on 27.10.15.
*/
public class Tank {
private int tankDirection = 1;
private int tankX = 128;
private int tankY = 512;
private int speed = 10;
private Bullet bullet;
public Tank() {
bullet = new Bullet();
}
public int getTankDirection() {
return tankDirection;
}
public void setTankDirection(int tankDirection) {
this.tankDirection = tankDirection;
}
public int getTankX() {
return tankX;
}
public void setTankX(int tankX) {
this.tankX = tankX;
}
public int getTankY() {
return tankY;
}
public void setTankY(int tankY) {
this.tankY = tankY;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public Bullet getBullet() {
return bullet;
}
public void setBullet(Bullet bullet) {
this.bullet = bullet;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment