Last active
April 13, 2016 10:10
-
-
Save alexejVasko/67b274ffb1b68308aa35 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
package tanks; | |
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 BattleFieldMoveToQuadrant extends JPanel { | |
boolean COLORDED_MODE = true; | |
int tankX = 0; | |
int tankY = 0; | |
long speed = 10; | |
int direction = 0; | |
void runTheGame() throws Exception { | |
moveToQuadrant("b", "3"); | |
moveToQuadrant("c", "4"); | |
moveToQuadrant("c", "9"); | |
moveToQuadrant("i", "9"); | |
moveToQuadrant("b", "2"); | |
} | |
void moveToQuadrant(String v, String h) throws Exception{ | |
String quadrantCoordin = getQuadrant(v, h); | |
int slash = quadrantCoordin.indexOf("_"); | |
int x = Integer.valueOf(quadrantCoordin.substring(slash + 1)); | |
int y = Integer.valueOf(quadrantCoordin.substring(0, slash)); | |
if(tankX < x) { | |
while(tankX != x) { | |
move(4); | |
} | |
}else { | |
while(tankX != x) { | |
move(3); | |
} | |
} | |
if(tankY < y) { | |
while(tankY != y) { | |
move(2); | |
} | |
} | |
else { | |
while(tankY != y) { | |
move(1); | |
} | |
} | |
Thread.sleep(500); | |
} | |
static String getQuadrant(String vert, String hor) { | |
String h = "123456789"; | |
String v = "abcdefghi"; | |
int quadrantSize = 64; | |
int x = h.indexOf(hor) * quadrantSize; | |
int y = v.indexOf(vert) * quadrantSize; | |
return String.valueOf(y) + "_" + String.valueOf(x); | |
} | |
void move(int direction) throws Exception { | |
int step = 1; | |
if (direction == 1) { | |
tankY -= step; | |
System.out.println("(move up) direction " + direction + " tankX " + tankX + "tankY " + tankY); | |
repaint(); | |
Thread.sleep(speed); | |
} | |
else if(direction == 2){ | |
tankY += step; | |
System.out.println("(move down) direction " + direction + " tankX " + tankX + "tankY " + tankY); | |
repaint(); | |
Thread.sleep(speed); | |
} | |
else if(direction == 3){ | |
tankX -= step; | |
System.out.println("(move left) direction " + direction + " tankX " + tankX + "tankY " + tankY); | |
repaint(); | |
Thread.sleep(speed); | |
} | |
else if(direction == 4){ | |
tankX += step; | |
System.out.println("(move right) direction " + direction + " tankX " + tankX + "tankY " + tankY); | |
repaint(); | |
Thread.sleep(speed); | |
} | |
} | |
final int BF_WIDTH = 576; | |
final int BF_HEIGHT = 576; | |
public static void main(String[] args) throws Exception { | |
BattleFieldMoveToQuadrant bf = new BattleFieldMoveToQuadrant(); | |
bf.runTheGame(); | |
} | |
public BattleFieldMoveToQuadrant() 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
У Вас путаница с координатами. Первое движение в квадрат moveToQuadrant("f", "2");, а передвигаете танк в квадрант b6