Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.util.Arrays;
- public class Tanks extends JPanel
- {
- final boolean COLORED_MODE = false;
- final boolean IS_GRID = true;
- final int BF_WIDTH = 576;
- final int BF_HEIGHT = 576;
- // 1 - top, 2 - right, 3 - down, 4 - left
- int tankDirection = 1;
- int tankX = 0;
- int tankY = 64*3;
- int bulletX = -100;
- int bulletY = -100;
- int tankSpeed = 10;
- int bulletSpeed = 40;
- final String BRICK = "B";
- final String BLANK = " ";
- /* String[][] battleField = {
- {"B", "B", "B", "B", "B", "B", "B", "B", "B"},
- {" ", " ", " ", " ", " ", " ", " ", " ", "B"},
- {"B", "B", "B", " ", "B", " ", "B", "B", "B"},
- {"B", "B", "B", " ", " ", " ", "B", "B", "B"},
- {"B", "B", "B", " ", "B", " ", "B", "B", "B"},
- {"B", "B", " ", "B", "B", "B", " ", "B", "B"},
- {"B", "B", " ", " ", " ", " ", " ", "B", "B"},
- {"B", " ", " ", "B", "B", "B", " ", " ", "B"},
- {"B", " ", " ", "B", "B", "B", " ", " ", "B"}
- };
- */
- String [][] battleField = {
- {" ","B"," "," "," "," "," "," ","B"},
- {" "," "," "," "," "," ","B"," ","B"},
- {" "," "," ","B","B"," ","B"," "," "},
- {" ","B"," "," "," "," "," "," ","B"},
- {" "," "," "," "," "," ","B"," "," "},
- {" "," "," "," "," "," "," "," "," "},
- {" "," "," ","B"," "," ","B"," ","B"},
- {" ","B"," "," "," "," ","B"," ","B"},
- {"B"," "," "," "," "," "," "," "," "}};
- void runTheGame() throws Exception
- {
- printCurrentBattleField();
- // moveAnz(4);
- tankDirection=3;
- /* moveAnz(4);
- moveAnz(4);
- moveAnz(4);
- moveAnz(3);
- moveAnz(3);
- moveAnz(3);
- moveAnz(3);
- moveAnz(1);
- moveAnz(1);
- moveAnz(1);*/
- // moveAnz(1);
- // moveAnz(4);
- // moveAnz(4);
- // moveAnz(4);
- // moveAnz(4);
- while (true)
- {
- fire();
- }
- }
- /**
- *
- * When called tank should produce new bullet.
- * This bullet should smoothly move to the opposite side.
- *
- * Bullet should be destroyed when rich the opposite side.
- *
- * Ignore all the objects on battle field for now.
- *
- */
- void fire() {
- bulletX = tankX + 25;
- bulletY = tankY + 25;
- int distance = 1;
- int countStep = 0;
- // while (countStep < 1000) {
- // sleep(bulletSpeed);
- // countStep++;
- System.out.println(bulletX);
- System.out.println(bulletY);
- System.out.println("------");
- while (bulletX <= (64 * 8) & bulletX >= 0 & bulletY <= (64 * 8) & bulletY >= 0){
- if (tankDirection == 1) {// up
- bulletY += distance;
- repaint();
- } else if (tankDirection == 2) {// down
- bulletY -= distance;
- repaint();
- } else if (tankDirection == 3) {// left
- bulletX -= distance;
- repaint();
- } else if (tankDirection == 4) {// right
- bulletX += distance;
- repaint();
- }
- sleep(bulletSpeed);
- System.out.println(bulletX);
- System.out.println(bulletY);
- //System.out.println(countStep);
- }
- }
- void moveAnz(int s) {
- int distance=1;
- int countStep=0;
- turn(s);//1-up,2down,3-left,4-right
- //if ((s==1)|| (s==2) ||(s==3)||(s==4)){
- while (countStep<64) {
- sleep(5);
- countStep++;
- if (s == 1) {
- if (tankX <= 64 * 8 & tankX >= 0 & (tankY - distance) <= 64 * 8 & (tankY - distance) >= 0) {
- System.out.println("UP");
- tankY-=distance;
- }
- }
- else if (s == 2) {
- if (tankX <= 64 * 8 & tankX >= 0 & (tankY + distance) <= 64 * 8 & (tankY + distance) >= 0) {
- System.out.println("down");
- tankY+=distance;
- }
- }
- else if (s == 3) {
- //System.out.println("start3"+" "+tankX+" " + tankY);
- if ((tankX-distance) <= 64 * 8 & (tankX-distance) >= 0 & (tankY) <= 64 * 8 & (tankY) >= 0) {
- System.out.println("left");
- tankX-=distance;
- }
- }
- else if (s == 4) {
- if ((tankX + distance) <= 64 * 8 & (tankX + distance) >= 0 & (tankY) <= 64 * 8 & (tankY) >= 0) {
- System.out.println("right");
- tankX+=distance;
- }
- }
- else {
- System.out.println("Incorrect input");
- break;
- }
- repaint();
- }
- //}
- }
- void turnTank(int d) { //1-up,2down,3-left,4-right
- tankDirection=d;
- }
- /*String getQuadrant(int v, int h)
- {
- return((h-1)*64+"_"+((v-1)*64));
- }
- */
- void moveToQuadrant(int hh, int vv)
- {
- String CoordinatX;
- String CoordinatY;
- String CoordXY;
- int CoordXint;
- int CoordYint;
- // get coordinates
- /* CoordXY=getQuadrant(hh,vv);
- CoordinatX=CoordXY.substring(0,CoordXY.indexOf("_"));
- CoordinatY=CoordXY.substring(CoordXY.indexOf("_")+1);
- System.out.println(CoordinatX);
- System.out.println(CoordinatY);*/
- CoordXint=getQuadrant(hh,vv)[0];
- CoordYint=getQuadrant(hh,vv)[1];
- //move the tank
- /*CoordYint=Integer.valueOf(CoordinatY);
- CoordXint=Integer.valueOf(CoordinatX);*/
- while (tankY < CoordYint){
- moveAnz(2);//down
- }
- while (tankY > CoordYint) {
- moveAnz(1); //up
- }
- while (tankX < CoordXint){
- moveAnz(4);//right
- }
- while (tankX > CoordXint) {
- moveAnz(3);//left
- }
- }
- private void printCurrentBattleField()
- {
- for (String[] row : battleField)
- {
- System.out.println(Arrays.toString(row));
- }
- }
- int[] getQuadrant(int x, int y)
- {
- return new int[] {x / 64, y / 64};
- }
- void move(int direction)
- {
- // TODO SHOULD BE ALREADY IMPLEMENTED
- }
- void turn(int direction)
- {
- tankDirection = direction;
- repaint();
- }
- // Magic bellow. Do not worry about this now, you will understand everything in this course.
- // Please concentrate on your tasks only.
- public static void main(String[] args) throws Exception
- {
- Tanks bf = new Tanks();
- /* String [][] battleField = new String [9][];
- String[] bF1 ={" ","B"," "," "," "," "," "," ","B"};
- String[] bF2 ={" "," "," "," "," "," ","B"," ","B"};
- String[] bF3 ={" "," "," ","B","B"," ","B"," "," "};
- String[] bF4 ={" ","B"," "," "," "," "," "," ","B"};
- String[] bF5 ={" "," "," "," "," "," ","B"," "," "};
- String[] bF6 ={" "," "," "," "," "," "," "," "," "};
- String[] bF7 ={" "," "," ","B"," "," ","B"," ","B"};
- String[] bF8 ={" ","B"," "," "," "," ","B"," ","B"};
- String[] bF9 ={"B"," "," "," "," "," "," "," "," "};
- battleField[0]=bF1;
- battleField[1]=bF2;
- battleField[2]=bF3;
- battleField[3]=bF4;
- battleField[4]=bF5;
- battleField[5]=bF6;
- battleField[6]=bF7;
- battleField[7]=bF8;
- battleField[8]=bF9;
- */
- bf.runTheGame();
- }
- public Tanks() throws Exception
- {
- JFrame frame = new JFrame("YOUR TANK SHOULD FIRE!!!");
- frame.setLocation(750, 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);
- }
- void sleep(int millis)
- {
- try
- {
- Thread.sleep(millis);
- }
- catch (Exception ignore)
- {
- }
- }
- @Override
- protected void paintComponent(Graphics g)
- {
- paintBF(g);
- paintBorders(g);
- g.setColor(new Color(255, 0, 0));
- g.fillRect(tankX, tankY, 64, 64);
- g.setColor(new Color(0, 255, 0));
- if (tankDirection == 1)
- {
- g.fillRect(tankX + 20, tankY, 24, 34);
- }
- else if (tankDirection == 2)
- {
- g.fillRect(tankX + 20, tankY + 30, 24, 34);
- }
- else if (tankDirection == 3)
- {
- g.fillRect(tankX, tankY + 20, 34, 24);
- }
- else
- {
- g.fillRect(tankX + 30, tankY + 20, 34, 24);
- }
- g.setColor(new Color(255, 255, 0));
- g.fillRect(bulletX, bulletY, 14, 14);
- }
- private void paintBorders(Graphics g)
- {
- for (int j = 0; j < battleField.length; j++)
- {
- for (int k = 0; k < battleField.length; k++)
- {
- if (battleField[j][k].equals("B"))
- {
- String coordinates = getQuadrantXY(j + 1, k + 1);
- int separator = coordinates.indexOf("_");
- int y = Integer.parseInt(coordinates.substring(0, separator));
- int x = Integer.parseInt(coordinates.substring(separator + 1));
- g.setColor(new Color(0, 0, 255));
- g.fillRect(x, y, 64, 64);
- if (IS_GRID)
- {
- g.setColor(new Color(0, 0, 0));
- g.drawRect(x, y, 64, 64);
- }
- }
- }
- }
- }
- private void paintBF(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 (COLORED_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);
- }
- }
- }
- private String getQuadrantXY(int v, int h)
- {
- return (v - 1) * 64 + "_" + (h - 1) * 64;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement