Created
September 9, 2015 21:40
-
-
Save AnnaBoro/3068009beec1b81e83a1 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) 2014 kademika.com | |
*/ | |
package lesson2; | |
import java.awt.Color; | |
import java.awt.Dimension; | |
import java.awt.Graphics; | |
import javax.swing.JFrame; | |
import javax.swing.JPanel; | |
import javax.swing.WindowConstants; | |
public class BattleFieldTemplate extends JPanel { | |
boolean COLORDED_MODE = true; | |
int tankX = 0; | |
int tankY = 0; | |
long speed = 225; | |
/** | |
* Write your code here. | |
*/ | |
void runTheGame() throws Exception { | |
while (true) { | |
for (int i = 0; i < 9; i ++) { | |
tankX += 64; | |
repaint(); | |
Thread.sleep(speed); | |
if (tankX < 512) { | |
continue; | |
} | |
break; | |
} | |
for (int i = 0; i < 8; i ++) { | |
tankX -= 64; | |
repaint(); | |
Thread.sleep(speed); | |
} | |
} | |
} | |
void move(int direction) throws Exception { | |
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"); | |
} | |
} | |
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(String v, String h) throws Exception { | |
repaint(); | |
Thread.sleep(speed); | |
String quadrant = Frame25.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. | |
final int BF_WIDTH = 576; | |
final int BF_HEIGHT = 576; | |
public static void main(String[] args) throws Exception { | |
BattleFieldTemplate bf = new BattleFieldTemplate(); | |
// bf.runTheGame(); | |
// bf.move(1); | |
// bf.move(2); | |
// bf.move(3); | |
// bf.move(4); | |
// bf.move(4); | |
// bf.move(1); | |
// bf.move(2); | |
// bf.move(2); | |
// bf.moveRandom(); | |
bf.moveToQuadrant("i", "9"); | |
} | |
public BattleFieldTemplate() throws Exception { | |
JFrame frame = new JFrame("BATTLE FIELD, DAY 2"); | |
frame.setLocation(500, 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); | |
} | |
} | |
g.setColor(new Color(255, 0, 0)); | |
g.fillRect(tankX, tankY, 64, 64); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment