Skip to content

Instantly share code, notes, and snippets.

@alexejVasko
Last active April 13, 2016 10:15
Show Gist options
  • Save alexejVasko/96535bfb280dd6a1ed9f to your computer and use it in GitHub Desktop.
Save alexejVasko/96535bfb280dd6a1ed9f to your computer and use it in GitHub Desktop.
package tanks;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
public class BattleFieldTemplate extends JPanel {
boolean COLORDED_MODE = true;
int tankX = 0;
int tankY = 0;
long speed = 10;
int direction = 0;
void runTheGame() throws Exception {
moveRandom();
}
void moveRandom() throws Exception {
while (true) {
move(random());
}
}
int random() throws Exception {
String str = String.valueOf(System.currentTimeMillis());
int result = Integer.parseInt(str.substring(12));
if (result==1 || result==2 || result==3){
direction = 1;
}
else if (result==4 || result==5){
direction = 2;
}
else if (result==6 || result==7){
direction = 3;
}
else if (result==8 || result==9 || result == 0){
direction = 4;
}
return direction;
}
void move(int direction) throws Exception {
int step = 1;
int moveTop = tankY - 64;
int moveRight = tankX + 64;
int moveBottom = tankY + 64;
int moveLeft = tankX - 64;
if (direction == 1) {
if (tankY <= 0) {
System.out.println("UPS! THE WALL!");
}
else {
System.out.println("Go up");
while (tankY > moveTop) {
tankY -= step;
repaint();
Thread.sleep(speed);
}
}
}
else if (direction == 2) {
if (tankY >= 8*64) {
System.out.println("UPS! THE WALL!");
}
else {
System.out.println("Go down");
while (tankY < moveBottom) {
tankY += step;
repaint();
Thread.sleep(speed);
}
}
}
else if (direction == 3) {
if (tankX <= 0) {
System.out.println("UPS! THE WALL!");
}
else {
System.out.println("to left");
while (tankX > moveLeft) {
tankX -= step;
repaint();
Thread.sleep(speed);
}
}
}
else if (direction == 4) {
if (tankX >= 8*64) {
System.out.println("UPS! THE WALL!");
}
else {
System.out.println("to right");
while (tankX < moveRight) {
tankX += step;
repaint();
Thread.sleep(speed);
}
}
}
}
final int BF_WIDTH = 576;
final int BF_HEIGHT = 576;
public static void main(String[] args) throws Exception {
BattleFieldTemplate bf = new BattleFieldTemplate();
bf.runTheGame();
}
public BattleFieldTemplate() 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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment