Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.53 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.util.Arrays;
  4.  
  5. public class Tanks extends JPanel
  6. {
  7.  
  8. final boolean COLORED_MODE = false;
  9. final boolean IS_GRID = true;
  10.  
  11. final int BF_WIDTH = 576;
  12. final int BF_HEIGHT = 576;
  13.  
  14. // 1 - top, 2 - right, 3 - down, 4 - left
  15. int tankDirection = 1;
  16.  
  17. int tankX = 0;
  18. int tankY = 64*3;
  19.  
  20. int bulletX = -100;
  21. int bulletY = -100;
  22.  
  23. int tankSpeed = 10;
  24. int bulletSpeed = 40;
  25.  
  26. final String BRICK = "B";
  27. final String BLANK = " ";
  28.  
  29. /* String[][] battleField = {
  30. {"B", "B", "B", "B", "B", "B", "B", "B", "B"},
  31. {" ", " ", " ", " ", " ", " ", " ", " ", "B"},
  32. {"B", "B", "B", " ", "B", " ", "B", "B", "B"},
  33. {"B", "B", "B", " ", " ", " ", "B", "B", "B"},
  34. {"B", "B", "B", " ", "B", " ", "B", "B", "B"},
  35. {"B", "B", " ", "B", "B", "B", " ", "B", "B"},
  36. {"B", "B", " ", " ", " ", " ", " ", "B", "B"},
  37. {"B", " ", " ", "B", "B", "B", " ", " ", "B"},
  38. {"B", " ", " ", "B", "B", "B", " ", " ", "B"}
  39. };
  40. */
  41. String [][] battleField = {
  42. {" ","B"," "," "," "," "," "," ","B"},
  43. {" "," "," "," "," "," ","B"," ","B"},
  44. {" "," "," ","B","B"," ","B"," "," "},
  45. {" ","B"," "," "," "," "," "," ","B"},
  46. {" "," "," "," "," "," ","B"," "," "},
  47. {" "," "," "," "," "," "," "," "," "},
  48. {" "," "," ","B"," "," ","B"," ","B"},
  49. {" ","B"," "," "," "," ","B"," ","B"},
  50. {"B"," "," "," "," "," "," "," "," "}};
  51.  
  52.  
  53.  
  54.  
  55.  
  56. void runTheGame() throws Exception
  57. {
  58. printCurrentBattleField();
  59. // moveAnz(4);
  60. tankDirection=3;
  61. /* moveAnz(4);
  62. moveAnz(4);
  63. moveAnz(4);
  64. moveAnz(3);
  65. moveAnz(3);
  66. moveAnz(3);
  67. moveAnz(3);
  68. moveAnz(1);
  69. moveAnz(1);
  70. moveAnz(1);*/
  71. // moveAnz(1);
  72. // moveAnz(4);
  73. // moveAnz(4);
  74. // moveAnz(4);
  75. // moveAnz(4);
  76. while (true)
  77. {
  78. fire();
  79. }
  80. }
  81.  
  82. /**
  83. *
  84. * When called tank should produce new bullet.
  85. * This bullet should smoothly move to the opposite side.
  86. *
  87. * Bullet should be destroyed when rich the opposite side.
  88. *
  89. * Ignore all the objects on battle field for now.
  90. *
  91. */
  92. void fire() {
  93. bulletX = tankX + 25;
  94. bulletY = tankY + 25;
  95. int distance = 1;
  96.  
  97. int countStep = 0;
  98.  
  99. // while (countStep < 1000) {
  100. // sleep(bulletSpeed);
  101. // countStep++;
  102. System.out.println(bulletX);
  103. System.out.println(bulletY);
  104. System.out.println("------");
  105.  
  106. while (bulletX <= (64 * 8) & bulletX >= 0 & bulletY <= (64 * 8) & bulletY >= 0){
  107. if (tankDirection == 1) {// up
  108. bulletY += distance;
  109. repaint();
  110. } else if (tankDirection == 2) {// down
  111. bulletY -= distance;
  112. repaint();
  113. } else if (tankDirection == 3) {// left
  114. bulletX -= distance;
  115. repaint();
  116. } else if (tankDirection == 4) {// right
  117. bulletX += distance;
  118. repaint();
  119. }
  120.  
  121. sleep(bulletSpeed);
  122. System.out.println(bulletX);
  123. System.out.println(bulletY);
  124. //System.out.println(countStep);
  125. }
  126.  
  127. }
  128.  
  129.  
  130. void moveAnz(int s) {
  131.  
  132. int distance=1;
  133. int countStep=0;
  134.  
  135. turn(s);//1-up,2down,3-left,4-right
  136.  
  137. //if ((s==1)|| (s==2) ||(s==3)||(s==4)){
  138. while (countStep<64) {
  139. sleep(5);
  140. countStep++;
  141. if (s == 1) {
  142.  
  143. if (tankX <= 64 * 8 & tankX >= 0 & (tankY - distance) <= 64 * 8 & (tankY - distance) >= 0) {
  144. System.out.println("UP");
  145. tankY-=distance;
  146.  
  147. }
  148.  
  149. }
  150. else if (s == 2) {
  151.  
  152. if (tankX <= 64 * 8 & tankX >= 0 & (tankY + distance) <= 64 * 8 & (tankY + distance) >= 0) {
  153. System.out.println("down");
  154. tankY+=distance;
  155.  
  156. }
  157. }
  158. else if (s == 3) {
  159. //System.out.println("start3"+" "+tankX+" " + tankY);
  160. if ((tankX-distance) <= 64 * 8 & (tankX-distance) >= 0 & (tankY) <= 64 * 8 & (tankY) >= 0) {
  161. System.out.println("left");
  162. tankX-=distance;
  163.  
  164.  
  165. }
  166. }
  167. else if (s == 4) {
  168. if ((tankX + distance) <= 64 * 8 & (tankX + distance) >= 0 & (tankY) <= 64 * 8 & (tankY) >= 0) {
  169. System.out.println("right");
  170. tankX+=distance;
  171.  
  172. }
  173. }
  174. else {
  175. System.out.println("Incorrect input");
  176. break;
  177. }
  178. repaint();
  179. }
  180. //}
  181. }
  182.  
  183.  
  184.  
  185. void turnTank(int d) { //1-up,2down,3-left,4-right
  186. tankDirection=d;
  187. }
  188. /*String getQuadrant(int v, int h)
  189. {
  190. return((h-1)*64+"_"+((v-1)*64));
  191. }
  192. */
  193. void moveToQuadrant(int hh, int vv)
  194. {
  195. String CoordinatX;
  196. String CoordinatY;
  197. String CoordXY;
  198. int CoordXint;
  199. int CoordYint;
  200.  
  201. // get coordinates
  202. /* CoordXY=getQuadrant(hh,vv);
  203. CoordinatX=CoordXY.substring(0,CoordXY.indexOf("_"));
  204. CoordinatY=CoordXY.substring(CoordXY.indexOf("_")+1);
  205. System.out.println(CoordinatX);
  206. System.out.println(CoordinatY);*/
  207. CoordXint=getQuadrant(hh,vv)[0];
  208. CoordYint=getQuadrant(hh,vv)[1];
  209.  
  210.  
  211. //move the tank
  212. /*CoordYint=Integer.valueOf(CoordinatY);
  213. CoordXint=Integer.valueOf(CoordinatX);*/
  214.  
  215. while (tankY < CoordYint){
  216. moveAnz(2);//down
  217. }
  218. while (tankY > CoordYint) {
  219. moveAnz(1); //up
  220. }
  221. while (tankX < CoordXint){
  222. moveAnz(4);//right
  223. }
  224. while (tankX > CoordXint) {
  225. moveAnz(3);//left
  226. }
  227. }
  228.  
  229. private void printCurrentBattleField()
  230. {
  231. for (String[] row : battleField)
  232. {
  233. System.out.println(Arrays.toString(row));
  234. }
  235. }
  236.  
  237. int[] getQuadrant(int x, int y)
  238. {
  239. return new int[] {x / 64, y / 64};
  240. }
  241.  
  242. void move(int direction)
  243. {
  244. // TODO SHOULD BE ALREADY IMPLEMENTED
  245. }
  246.  
  247. void turn(int direction)
  248. {
  249. tankDirection = direction;
  250. repaint();
  251. }
  252.  
  253. // Magic bellow. Do not worry about this now, you will understand everything in this course.
  254. // Please concentrate on your tasks only.
  255.  
  256.  
  257. public static void main(String[] args) throws Exception
  258. {
  259. Tanks bf = new Tanks();
  260. /* String [][] battleField = new String [9][];
  261.  
  262. String[] bF1 ={" ","B"," "," "," "," "," "," ","B"};
  263. String[] bF2 ={" "," "," "," "," "," ","B"," ","B"};
  264. String[] bF3 ={" "," "," ","B","B"," ","B"," "," "};
  265. String[] bF4 ={" ","B"," "," "," "," "," "," ","B"};
  266. String[] bF5 ={" "," "," "," "," "," ","B"," "," "};
  267. String[] bF6 ={" "," "," "," "," "," "," "," "," "};
  268. String[] bF7 ={" "," "," ","B"," "," ","B"," ","B"};
  269. String[] bF8 ={" ","B"," "," "," "," ","B"," ","B"};
  270. String[] bF9 ={"B"," "," "," "," "," "," "," "," "};
  271.  
  272. battleField[0]=bF1;
  273. battleField[1]=bF2;
  274. battleField[2]=bF3;
  275. battleField[3]=bF4;
  276. battleField[4]=bF5;
  277. battleField[5]=bF6;
  278. battleField[6]=bF7;
  279. battleField[7]=bF8;
  280. battleField[8]=bF9;
  281. */
  282. bf.runTheGame();
  283. }
  284.  
  285. public Tanks() throws Exception
  286. {
  287. JFrame frame = new JFrame("YOUR TANK SHOULD FIRE!!!");
  288. frame.setLocation(750, 150);
  289. frame.setMinimumSize(new Dimension(BF_WIDTH, BF_HEIGHT + 22));
  290. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  291. frame.getContentPane().add(this);
  292. frame.pack();
  293. frame.setVisible(true);
  294. }
  295.  
  296. void sleep(int millis)
  297. {
  298. try
  299. {
  300. Thread.sleep(millis);
  301. }
  302. catch (Exception ignore)
  303. {
  304. }
  305. }
  306.  
  307. @Override
  308. protected void paintComponent(Graphics g)
  309. {
  310. paintBF(g);
  311.  
  312. paintBorders(g);
  313.  
  314. g.setColor(new Color(255, 0, 0));
  315. g.fillRect(tankX, tankY, 64, 64);
  316.  
  317. g.setColor(new Color(0, 255, 0));
  318. if (tankDirection == 1)
  319. {
  320. g.fillRect(tankX + 20, tankY, 24, 34);
  321. }
  322. else if (tankDirection == 2)
  323. {
  324. g.fillRect(tankX + 20, tankY + 30, 24, 34);
  325. }
  326. else if (tankDirection == 3)
  327. {
  328. g.fillRect(tankX, tankY + 20, 34, 24);
  329. }
  330. else
  331. {
  332. g.fillRect(tankX + 30, tankY + 20, 34, 24);
  333. }
  334.  
  335. g.setColor(new Color(255, 255, 0));
  336. g.fillRect(bulletX, bulletY, 14, 14);
  337. }
  338.  
  339. private void paintBorders(Graphics g)
  340. {
  341. for (int j = 0; j < battleField.length; j++)
  342. {
  343. for (int k = 0; k < battleField.length; k++)
  344. {
  345. if (battleField[j][k].equals("B"))
  346. {
  347. String coordinates = getQuadrantXY(j + 1, k + 1);
  348. int separator = coordinates.indexOf("_");
  349. int y = Integer.parseInt(coordinates.substring(0, separator));
  350. int x = Integer.parseInt(coordinates.substring(separator + 1));
  351. g.setColor(new Color(0, 0, 255));
  352. g.fillRect(x, y, 64, 64);
  353.  
  354. if (IS_GRID)
  355. {
  356. g.setColor(new Color(0, 0, 0));
  357. g.drawRect(x, y, 64, 64);
  358. }
  359. }
  360. }
  361. }
  362. }
  363.  
  364. private void paintBF(Graphics g)
  365. {
  366. super.paintComponent(g);
  367.  
  368. int i = 0;
  369. Color cc;
  370. for (int v = 0; v < 9; v++)
  371. {
  372. for (int h = 0; h < 9; h++)
  373. {
  374. if (COLORED_MODE)
  375. {
  376. if (i % 2 == 0)
  377. {
  378. cc = new Color(252, 241, 177);
  379. }
  380. else
  381. {
  382. cc = new Color(233, 243, 255);
  383. }
  384. }
  385. else
  386. {
  387. cc = new Color(180, 180, 180);
  388. }
  389. i++;
  390. g.setColor(cc);
  391. g.fillRect(h * 64, v * 64, 64, 64);
  392. }
  393. }
  394. }
  395.  
  396. private String getQuadrantXY(int v, int h)
  397. {
  398. return (v - 1) * 64 + "_" + (h - 1) * 64;
  399. }
  400.  
  401. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement