Created
December 12, 2015 16:37
-
-
Save Yur-ok/fbf21b746d3766d10705 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.KeyPoint3; | |
import javax.swing.*; | |
import java.awt.*; | |
public class BattleFieldTemplate_MoveMethod 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) { | |
while (tankX < 512) { | |
tankX += 64; | |
System.out.println(tankX); | |
repaint(); | |
Thread.sleep(speed); | |
} | |
while (tankX != 0) { | |
tankX -= 64; | |
System.out.println(tankX); | |
repaint(); | |
Thread.sleep(speed); | |
} | |
} | |
// Вариант Олега | |
// int i = 0; | |
// int direction = 0; | |
// while (true) { | |
// if (direction == 0 && i < 8) { | |
// tankX += 64; | |
// i++; | |
// } else { | |
// tankX -= 64; | |
// i--; | |
// } | |
// if (i == 8) { | |
// direction = 1; | |
// } else if (i == 0) { | |
// direction = 0; | |
// } | |
// repaint(); | |
// Thread.sleep(speed); | |
// } | |
} | |
void move(int direction) throws Exception { | |
if (direction >= 1 && direction <= 4) { | |
if (direction == 1 && tankY != 0) { | |
tankY -= 64; | |
System.out.println("Move TANK by one step to the UP"); | |
repaint(); | |
Thread.sleep(speed); | |
} else if (direction == 2 && tankY <= 448) { | |
tankY += 64; | |
System.out.println("Move TANK by one step to the DOWN"); | |
repaint(); | |
Thread.sleep(speed); | |
} else if (direction == 3 && tankX != 0) { | |
tankX -= 64; | |
System.out.println("Move TANK by one step to the LEFT"); | |
repaint(); | |
Thread.sleep(speed); | |
} else if (direction == 4 && tankX <= 448) { | |
tankX += 64; | |
System.out.println("Move TANK by one step to the RIGHT"); | |
repaint(); | |
Thread.sleep(speed); | |
} | |
} else { | |
System.out.println("Correct data please input!!!"); | |
} | |
} | |
// 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_MoveMethod bf = new BattleFieldTemplate_MoveMethod(); | |
// bf.runTheGame(); | |
bf.move(5); | |
bf.move(4); | |
bf.move(2); | |
bf.move(4); | |
} | |
public BattleFieldTemplate_MoveMethod() 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); | |
} | |
} |
Данный метод я вызываю строки 103 - 106.
448 это координата 8-го квадрата, почему Вас смущает это число?
Если запустить метод, то все работает прекрасно!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Где демонстрация работы метода move(). Вы его не вызываете, значит не поняли задания.
И от куда взялись число 448?