Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright (c) 2013 midgardabc.com
- */
- package tanks;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.util.Random;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.WindowConstants;
- /**
- * @version 3.0
- */
- public class BattleFieldTemplate2 extends JPanel {
- final boolean COLORDED_MODE = false;
- final int BF_WIDTH = 576;
- final int BF_HEIGHT = 576;
- // 1 - top, 2 - bottom, 3 - left, 4 - right
- int tankDirection = 1;
- int tankX = 128;
- int tankY = 512;
- int bulletX = -100;
- int bulletY = -100;
- int speed = 3;
- int bulletSpeed = 3;
- 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", " ", " ", " "}
- };
- /**
- * Write your code here.
- */
- void runTheGame() throws Exception {
- clean();
- }
- boolean processInterception() {
- String bulletCoord = getQuadrant(bulletX, bulletY);
- int x = Integer.parseInt(bulletCoord.substring(0, bulletCoord.indexOf("_")));
- int y = Integer.parseInt(bulletCoord.substring(bulletCoord.indexOf("_") + 1));
- if( x >= 0 && x <= 8 && y >= 0 && y <= 8){
- if(battleField[x][y].equals("B")){
- battleField[x][y]=" ";
- return true;
- }
- }
- return false;
- }
- String getQuadrant(int x, int y) {
- x = x / 64;
- y = y / 64;
- return y + "_" + x;
- }
- void fire() throws Exception {
- bulletX = tankX + 25;
- bulletY = tankY + 25;
- while (bulletX >= -32 && bulletY >= -32 && bulletX < 601 && bulletY < 601) {
- if (tankDirection == 1) {
- bulletY --;
- if(processInterception() == true){
- bulletY = -100;
- bulletX = -100;
- }
- }
- if (tankDirection == 2) {
- bulletY ++;
- if(processInterception() == true){
- bulletY = -100;
- bulletX = -100;
- }
- }
- if (tankDirection == 4) {
- bulletX ++;
- if(processInterception() == true){
- bulletY = -100;
- bulletX = -100;
- }
- }
- if (tankDirection == 3) {
- bulletX --;
- if(processInterception() == true){
- bulletY = -100;
- bulletX = -100;
- }
- }
- processInterception();
- repaint();
- Thread.sleep(bulletSpeed);
- }
- }
- void clearQuad(int tankDirection) throws Exception{
- String koord = getQuadrant(tankX, tankY);
- int separator = koord.indexOf("_");
- int y = Integer.parseInt(koord.substring(0, separator));
- int x = Integer.parseInt(koord.substring(separator + 1));
- if(tankDirection == 1 && battleField[y-1][x].equals("B")){
- fire();
- }
- if(tankDirection == 2 && battleField[y+1][x].equals("B")){
- fire();
- }
- if(tankDirection == 3 && battleField[y][x-1].equals("B")){
- fire();
- }
- if(tankDirection == 4 && battleField[y][x+1].equals("B")){
- fire();
- }
- }
- void clean() throws Exception{
- int a = -1;
- for(int i = 1; i <= 9; i++){
- moveToQuadrant(9, i);
- a++;
- turn(1);
- for(int j = 1; j < battleField.length; j++)
- if(battleField[a][j] == "B"){
- fire();
- }
- }
- }
- String getQuadrantXY(int v, int h) {
- return (v - 1) * 64 + "_" + (h - 1) * 64;
- }
- void move(int direction) throws Exception {
- int step = 1;
- int covered = 0;
- // check limits x: 0, 513; y: 0, 513
- if ((direction == 1 && tankY == 0) || (direction == 2 && tankY >= 512)
- || (direction == 3 && tankX == 0) || (direction == 4 && tankX >= 512)) {
- System.out.println("[illegal move] direction: " + direction + " tankX: " + tankX + ", tankY: " + tankY);
- return;
- }
- turn(direction);
- clearQuad(direction);
- while (covered < 64) {
- if (direction == 1) {
- tankY -= step;
- System.out.println("[move up] direction: " + direction + " tankX: " + tankX + ", tankY: " + tankY);
- } else if (direction == 2) {
- tankY += step;
- System.out.println("[move down] direction: " + direction + " tankX: " + tankX + ", tankY: " + tankY);
- } else if (direction == 3) {
- tankX -= step;
- System.out.println("[move left] direction: " + direction + " tankX: " + tankX + ", tankY: " + tankY);
- } else {
- tankX += step;
- System.out.println("[move right] direction: " + direction + " tankX: " + tankX + ", tankY: " + tankY);
- }
- covered += step;
- repaint();
- Thread.sleep(speed);
- }
- }
- void turn(int direction) {
- if (tankDirection != direction) {
- tankDirection = direction;
- }
- }
- void moveRandom() throws Exception {
- Random r = new Random();
- int i;
- while (true) {
- i = r.nextInt(5);
- if (i > 0) {
- move(i);
- }
- }
- }
- void moveToQuadrant(int v, int h) throws Exception {
- String coordinates = getQuadrantXY(v, h);
- int separator = coordinates.indexOf("_");
- int y = Integer.parseInt(coordinates.substring(0, separator));
- int x = Integer.parseInt(coordinates.substring(separator + 1));
- 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);
- }
- }
- }
- // 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 {
- BattleFieldTemplate2 bf = new BattleFieldTemplate2();
- bf.runTheGame();
- }
- public BattleFieldTemplate2() throws Exception {
- JFrame frame = new JFrame("BATTLE FIELD, DAY 2");
- 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);
- }
- @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);
- }
- }
- 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);
- }
- }
- }
- 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);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement