Created
September 20, 2015 21:49
-
-
Save AnnaBoro/70d9e3daa740d20b650c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2013 midgardabc.com | |
*/ | |
package lesson3; | |
import java.awt.Color; | |
import java.awt.Dimension; | |
import java.awt.Graphics; | |
import java.util.Random; | |
import javax.swing.JFrame; | |
import javax.swing.JPanel; | |
import javax.swing.WindowConstants; | |
/** | |
* @version 3.0 | |
*/ | |
public class BattleFieldTemplate2 extends JPanel { | |
final boolean COLORDED_MODE = false; | |
final int BF_WIDTH = 576; | |
final int BF_HEIGHT = 576; | |
// 1 - top, 2 - bottom, 3 - left, 4 - right | |
int tankDirection = 1; | |
int tankX = 128; | |
int tankY = 512; | |
int bulletX = -100; | |
int bulletY = -100; | |
int speed = 10; | |
int bulletSpeed = 5; | |
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"} | |
}; | |
/** | |
* Write your code here. | |
*/ | |
void runTheGame() throws Exception { | |
fire(); | |
} | |
boolean processInterception() { | |
if ((bulletX > 0 && bulletX < 575) && (bulletY > 0 && bulletY < 575)) { | |
String quadrant = getQuadrant(bulletX, bulletY); | |
// System.out.println(quadrant); | |
int i = Integer.parseInt(quadrant.substring(0, quadrant.indexOf("_"))); | |
int j = Integer.parseInt(quadrant.substring(quadrant.indexOf("_") + 1, quadrant.length())); | |
// System.out.println(i + "_" + j); | |
if (battleField[i][j] == "B") { | |
battleField[i][j] = " "; | |
return true; | |
} | |
} | |
return false; | |
} | |
String getQuadrant(int v, int h) { | |
int x = v / 64; | |
int y = h / 64; | |
return y + "_" + x; | |
} | |
void fire() throws InterruptedException { | |
bulletX = tankX + 25; | |
bulletY = tankY + 25; | |
while ((bulletX > 0 && bulletX < 575) && (bulletY > 0 && bulletY < 575)) { | |
if (tankDirection == 1) { | |
for (int i = 0; i < 64; i++) { | |
bulletY -= 1; | |
// System.out.println(bulletY); | |
if (processInterception() == true) { | |
bulletX = -100; | |
bulletY = -100; | |
continue; | |
} | |
// System.out.println(bulletY); | |
repaint(); | |
Thread.sleep(bulletSpeed); | |
break; | |
} | |
} | |
else if (tankDirection == 2) { | |
for (int i = 0; i < 64; i++) { | |
bulletY += 1; | |
// System.out.println(bulletY); | |
if (processInterception() == true) { | |
bulletX = -100; | |
bulletY = -100; | |
continue; | |
} | |
repaint(); | |
Thread.sleep(bulletSpeed); | |
break; | |
} | |
} | |
else if (tankDirection == 3) { | |
for (int i = 0; i < 64; i++) { | |
bulletX -= 1; | |
// System.out.println(bulletX); | |
if (processInterception() == true) { | |
bulletX = -100; | |
bulletY = -100; | |
continue; | |
} | |
repaint(); | |
Thread.sleep(bulletSpeed); | |
break; | |
} | |
} | |
else if (tankDirection == 4) { | |
for (int i = 0; i < 64; i++) { | |
bulletX += 1; | |
// System.out.println(bulletX); | |
if (processInterception() == true) { | |
bulletX = -100; | |
bulletY = -100; | |
continue; | |
} | |
repaint(); | |
Thread.sleep(bulletSpeed); | |
break; | |
} | |
} | |
} | |
} | |
String getQuadrantXY(int v, int h) { | |
return (v - 1) * 64 + "_" + (h - 1) * 64; | |
} | |
void move(int direction) throws Exception { | |
repaint(); | |
Thread.sleep(speed); | |
turn(direction); | |
if (tankDirection == 1) { | |
if (tankY !=0) { | |
for (int i = 0; i < 64; i++) { | |
tankY -= 1; | |
repaint(); | |
Thread.sleep(speed/2); | |
} | |
System.out.println("Up: " + tankX + "_" + tankY); | |
Thread.sleep(speed * 2); | |
} | |
else System.out.println("Wrong direction"); | |
} | |
else if (tankDirection == 2) { | |
if (tankY != 512) { | |
for (int i = 0; i < 64; i++) { | |
tankY += 1; | |
// System.out.println(tankY); | |
repaint(); | |
Thread.sleep(speed/2); | |
} | |
System.out.println("Down: " + tankX + "_" + tankY); | |
Thread.sleep(speed * 2); | |
} | |
else System.out.println("Wrong direction"); | |
} | |
else if (tankDirection == 3) { | |
if (tankX != 0) { | |
for (int i = 0; i < 64; i++) { | |
tankX -= 1; | |
repaint(); | |
Thread.sleep(speed/2); | |
} | |
System.out.println("Left: " + tankX + "_" + tankY); | |
Thread.sleep(speed * 2); | |
} | |
else System.out.println("Wrong direction"); | |
} | |
else if (tankDirection == 4) { | |
if (tankX != 512) { | |
for (int i = 0; i < 64; i++) { | |
tankX += 1; | |
repaint(); | |
Thread.sleep(speed/2); | |
} | |
System.out.println("Right: " + tankX + "_" + tankY); | |
Thread.sleep(speed * 2); | |
} | |
else System.out.println("Wrong direction"); | |
} | |
} | |
void turn(int direction) { | |
tankDirection = direction; | |
} | |
void moveRandom() throws InterruptedException { | |
int direction = 0; | |
while (true) { | |
long randValue = System.currentTimeMillis(); | |
String randNum = String.valueOf(randValue); | |
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); | |
if (randNumInt1 > randNumInt2) { | |
if (randNumInt1 % 2 == 0) { | |
direction = 1; | |
} | |
else { | |
direction = 2; | |
} | |
} | |
else { | |
if (randNumInt2 % 2 == 0) { | |
direction = 3; | |
} | |
else { | |
direction = 4; | |
} | |
} | |
repaint(); | |
Thread.sleep(speed); | |
if (direction == 1) { | |
if (tankY !=0) { | |
tankY -= 64; | |
repaint(); | |
System.out.println("Up: " + tankX + "_" + tankY); | |
Thread.sleep(speed * 2); | |
} | |
else System.out.println("Wrong direction"); | |
} | |
else if (direction == 2) { | |
if (tankY != 512) { | |
tankY += 64; | |
repaint(); | |
System.out.println("Down: " + tankX + "_" + tankY); | |
Thread.sleep(speed * 2); | |
} | |
else System.out.println("Wrong direction"); | |
} | |
else if (direction == 3) { | |
if (tankX != 0) { | |
tankX -= 64; | |
repaint(); | |
System.out.println("Left: " + tankX + "_" + tankY); | |
Thread.sleep(speed * 2); | |
} | |
else System.out.println("Wrong direction"); | |
} | |
else if (direction == 4) { | |
if (tankX != 512) { | |
tankX += 64; | |
repaint(); | |
System.out.println("Right: " + tankX + "_" + tankY); | |
Thread.sleep(speed * 2); | |
} | |
else System.out.println("Wrong direction"); | |
} | |
// System.out.println(direction + " "+ randValue); | |
// Thread.sleep(1); | |
} | |
} | |
void moveToQuadrant(int v, int h) throws Exception { | |
repaint(); | |
Thread.sleep(speed); | |
String quadrant = getQuadrant(v, h); | |
// System.out.println(quadrant); | |
int lineIndex = quadrant.indexOf("_"); | |
int tankXNew = Integer.parseInt(quadrant.substring(0, lineIndex)); | |
int tankYNew = Integer.parseInt(quadrant.substring(lineIndex+1)); | |
// System.out.println(tankXNew + " " + tankYNew); | |
if ((tankXNew - tankX) > 0) { | |
// System.out.println(tankX + " " + tankXNew); | |
int steps = (tankXNew - tankX) / 64; | |
for (int step = 0; step < steps; step++) { | |
// System.out.println(tankX); | |
move(4); | |
} | |
} | |
else if ((tankXNew - tankX) < 0) { | |
int steps = (tankXNew - tankX) / 64; | |
for (int step = 0; step < steps; step ++) { | |
move(3); | |
} | |
} | |
if ((tankYNew - tankY) > 0) { | |
int steps = (tankYNew - tankY) / 64; | |
for (int step = 0; step < steps; step++) { | |
move(2); | |
} | |
} | |
else if ((tankYNew - tankY) < 0) { | |
int steps = (tankY - tankYNew) / 64; | |
for (int step = 0; step < steps; step ++) { | |
move(1); | |
} | |
} | |
} | |
// Magic bellow. Do not worry about this now, you will understand everything in this course. | |
// Please concentrate on your tasks only. | |
public static void main(String[] args) throws Exception { | |
BattleFieldTemplate2 bf = new BattleFieldTemplate2(); | |
// System.out.println(bf.getQuadrant(0, 60)); | |
// System.out.println(bf.getQuadrant(0, 64)); | |
// System.out.println(bf.getQuadrant(68, 96)); | |
bf.runTheGame(); | |
bf.turn(3); | |
bf.runTheGame(); | |
bf.turn(4); | |
bf.runTheGame(); | |
bf.turn(2); | |
bf.runTheGame(); | |
bf.turn(4); | |
bf.runTheGame(); | |
bf.turn(1); | |
bf.runTheGame(); | |
} | |
public BattleFieldTemplate2() 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); | |
} | |
@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(tankX, tankY, 64, 64); | |
g.setColor(new Color(0, 255, 0)); | |
if (tankDirection == 1) { | |
g.fillRect(tankX + 20, tankY, 24, 34); | |
} else if (tankDirection == 2) { | |
g.fillRect(tankX + 20, tankY + 30, 24, 34); | |
} else if (tankDirection == 3) { | |
g.fillRect(tankX, tankY + 20, 34, 24); | |
} else { | |
g.fillRect(tankX + 30, tankY + 20, 34, 24); | |
} | |
g.setColor(new Color(255, 255, 0)); | |
g.fillRect(bulletX, bulletY, 14, 14); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment