Created
January 5, 2016 08:56
-
-
Save Yur-ok/b77ef063c9ae53ce0cbd 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 Lesson3.KeyPoint4; | |
import javax.swing.*; | |
import java.awt.*; | |
public class BattleFieldTemplate_tankDirection extends JPanel { | |
boolean COLORDED_MODE = true; | |
int tankX = 0; | |
int tankY = 0; | |
long speed = 30; | |
int tankDirection = 0; | |
/** | |
* Write your code here. | |
*/ | |
void runTheGame() throws Exception { | |
move(4); | |
move(3); | |
move(2); | |
move(3); | |
move(4); | |
} | |
void move(int direction) throws Exception { | |
// задаем начальные переменные | |
int up = 1; | |
int down = 2; | |
int left = 3; | |
int right = 4; | |
int oneStep = 64; | |
//разворачиваем танк | |
if (tankDirection != direction) { | |
tankDirection = direction; | |
} | |
// проверяем корректность введенных данных и начием отрисовывать движение | |
if (direction >= 1 && direction <= 4) { | |
if (direction == up && tankY != 0) { | |
for (int i = 0; i < oneStep; i++) { | |
tankY -= 1; | |
moving(); | |
} | |
System.out.println("Move TANK by one step to the UP"); | |
} else if (direction == down && tankY < 512) { | |
for (int i = 0; i < oneStep; i++) { | |
tankY += 1; | |
moving(); | |
} | |
System.out.println("Move TANK by one step to the DOWN"); | |
} else if (direction == left && tankX != 0) { | |
for (int i = 0; i < oneStep; i++) { | |
tankX -= 1; | |
moving(); | |
} | |
System.out.println("Move TANK by one step to the LEFT"); | |
} else if (direction == right && tankX < 512) { | |
for (int i = 0; i < oneStep; i++) { | |
tankX += 1; | |
moving(); | |
} | |
System.out.println("Move TANK by one step to the RIGHT"); | |
} | |
} else { | |
System.out.println("Correct data please input!!!"); | |
} | |
} | |
//выносим повторяющиеся куски кода в отдельный метод | |
void moving() throws Exception { | |
repaint(); | |
Thread.sleep(speed); | |
} | |
void start(int number) { | |
if (number > 0) { | |
while (number > 0) { | |
System.out.println(number--); | |
if (number == 0) { | |
System.out.println(number); | |
System.out.println("Go!"); | |
} | |
} | |
} else if (number == 0) { | |
System.out.println("Go!"); | |
} else { | |
System.out.println("Start failed"); | |
} | |
} | |
void moveRandom() throws Exception { | |
long a = System.currentTimeMillis(); | |
String s = String.valueOf(a); | |
String lastChar = s.substring(s.length() - 2, s.length() - 1); | |
int lastDigit = Integer.parseInt(lastChar); | |
if (lastDigit > 0 && lastDigit < 5) { | |
move(lastDigit); | |
} | |
} | |
static String getQuadrant(int v, int h) throws Exception { | |
return (v - 1) * 64 + "_" + (h - 1) * 64; | |
} | |
void moveToQuadrant(int v, int h) throws Exception { | |
int up = 1; | |
int down = 2; | |
int left = 3; | |
int right = 4; | |
String destination = getQuadrant(v, h); //y_x | |
int destX = Integer.valueOf(destination.substring(destination.indexOf("_") + 1)); | |
int destY = Integer.valueOf(destination.substring(0, destination.indexOf("_"))); | |
if (tankX < destX) { | |
int i = (destX - tankX) / 64; | |
while (i > 0) { | |
move(right); | |
i--; | |
} | |
} else { | |
int i = (tankX - destX) / 64; | |
while (i > 0) { | |
move(left); | |
i--; | |
} | |
} | |
if (tankY < destY) { | |
int i = (destY - tankY) / 64; | |
while (i > 0) { | |
move(down); | |
i--; | |
} | |
} else { | |
int i = (tankY - destY) / 64; | |
while (i > 0) { | |
move(up); | |
i--; | |
} | |
} | |
} | |
// 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_tankDirection bf = new BattleFieldTemplate_tankDirection(); | |
bf.runTheGame(); | |
} | |
public BattleFieldTemplate_tankDirection() 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