Created
December 6, 2015 17:03
-
-
Save edinak1/4ef9d366b09772da95a6 to your computer and use it in GitHub Desktop.
This file contains 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
Enter file contents here | |
package tank; | |
import java.awt.Color; | |
import java.awt.Dimension; | |
import java.awt.Graphics; | |
import java.util.concurrent.TimeUnit; | |
import javax.swing.JFrame; | |
import javax.swing.JPanel; | |
import javax.swing.WindowConstants; | |
public class BattleFieldTemplate2 extends JPanel { | |
final boolean COLORDED_MODE = false; | |
final int BF_WIDTH = 576; | |
final int BF_HEIGHT = 576; | |
int bulletX=-100; | |
int bulletY=-100; | |
int bulletSpeed = 5; | |
int tankSpeed=10; | |
int tankX =0; | |
int tankY =0; | |
int up=1; | |
int down=2; | |
int left=3; | |
int right=4; | |
int tankDirection; | |
int pixel=64; | |
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"}}; | |
///////////////////////////////////////////////////////// | |
void runTheGame() throws Exception | |
{ | |
game(); | |
} | |
///////////////////////////////////////////////////////// | |
void game()throws Exception | |
{ | |
startCvadrant(); | |
for(int y=pixelToCoordinat(tankY);y< battleField.length;y++) | |
for(int x=pixelToCoordinat(tankX);x< battleField[y].length;x++) | |
moveToQuadrant(x,y); | |
for(int y=battleField.length-1; y>=0; y--) | |
{ | |
if(y%2==0) | |
for(int x=battleField[y].length-1; x>=0; x--) | |
moveToQuadrant(x,y); | |
else | |
for(int x=0; x<battleField[y].length; x++) | |
moveToQuadrant(x,y); | |
} | |
} | |
///////////////////////////////////////////////////////// | |
int coordinatToPixel(int x) | |
{ | |
return x*pixel; | |
} | |
///////////////////////////////////////////////////////// | |
int pixelToCoordinat(int x) | |
{ | |
return x/pixel; | |
} | |
///////////////////////////////////////////////////////// | |
void startCvadrant()throws Exception | |
{ | |
tankX=coordinatToPixel(random()); | |
tankY=coordinatToPixel(random()); | |
battleField[pixelToCoordinat(tankY)][pixelToCoordinat(tankX)]=" "; | |
//printCoordinat(pixelToCoordinat(tankX),pixelToCoordinat(tankY)); | |
} | |
///////////////////////////////////////////////////////// | |
void moveToQuadrant(int x,int y)throws Exception | |
{ | |
//printCoordinat(x,y); | |
while(tankX!=coordinatToPixel(x) || tankY!=coordinatToPixel(y)) | |
{ | |
if(tankX<coordinatToPixel(x)) | |
deleteAndMove(right,x,y); | |
else if(tankX>coordinatToPixel(x)) | |
deleteAndMove(left,x,y); | |
else if(tankY<coordinatToPixel(y)) | |
deleteAndMove(down,x,y); | |
else if(tankY>coordinatToPixel(y)) | |
deleteAndMove(up,x,y); | |
} | |
} | |
//////////////////////////////////////////////////////////// | |
void deleteAndMove(int direction,int x,int y)throws Exception | |
{ | |
turn(direction); | |
cleanCvadrant(x,y); | |
move(direction); | |
} | |
//////////////////////////////////////////////////////////// | |
void printCoordinat(int x,int y) | |
{ | |
System.out.println("x="+x+" y="+y); | |
System.out.println(getQuadrantXY(x,y)); | |
} | |
/////////////////////////////////////////////////////////// | |
void cleanCvadrant(int x,int y)throws Exception | |
{ | |
if(battleField[y][x]=="B") | |
fire(); | |
} | |
//////////////////////////////////////////////////////////// | |
void turn(int direction) | |
{ | |
tankDirection=direction; | |
} | |
//////////////////////////////////////////////////////////// | |
void fire()throws Exception | |
{ | |
bulletX=tankX+25; | |
bulletY=tankY+25; | |
while( bulletY>-14 && bulletY<575 && bulletX>-14 && bulletX<575) | |
{ | |
if(tankDirection==up) | |
bulletY--; | |
else if(tankDirection==down) | |
bulletY++; | |
else if(tankDirection==left) | |
bulletX--; | |
else if(tankDirection==right) | |
bulletX++; | |
pauza(bulletSpeed); | |
repaint(); | |
processInterception(); | |
} | |
} | |
////////////////////////////////////////////////////////////// | |
void processInterception() | |
{ | |
if(battleField[pixelToCoordinat(bulletY)][pixelToCoordinat(bulletX)]=="B") | |
{ | |
battleField[pixelToCoordinat(bulletY)][pixelToCoordinat(bulletX)]=" "; | |
bulletX=-14; | |
bulletY=-14; | |
} | |
} | |
///////////////////////////////////////////////////////////// | |
void move(int direction)throws Exception | |
{ | |
int i=1; | |
while(i++<=pixel &&( tankY>0 || tankY<512 || tankX>0 || tankX<512)) | |
{ | |
if(direction==up) | |
tankY--; | |
else | |
if(direction==down) | |
tankY++; | |
else | |
if(direction==left) | |
tankX--; | |
else | |
if(direction==right) | |
tankX++; | |
repaint(); | |
pauza(tankSpeed); | |
} | |
} | |
////////////////////////////////////////////////////////////// | |
void pauza(int time)throws Exception | |
{ | |
TimeUnit.MILLISECONDS.sleep(time); | |
} | |
///////////////////////////////////////////////////////////// | |
String getQuadrantXY(int x,int y) | |
{ | |
return coordinatToPixel(x)+"_"+coordinatToPixel(y); | |
} | |
///////////////////////////////////////////////////////////// | |
String getQuadrant(int x,int y) | |
{ | |
return pixelToCoordinat(y)+"_"+pixelToCoordinat(x); | |
} | |
///////////////////////////////////////////////////////////// | |
int random ()throws Exception | |
{ | |
TimeUnit.NANOSECONDS.sleep(3); | |
return(int)(System.nanoTime()%9); | |
} | |
// 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+15, BF_HEIGHT + 35)); | |
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, k); | |
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment