Skip to content

Commit

Permalink
pushing existing version v1, v2, v3
Browse files Browse the repository at this point in the history
  • Loading branch information
zebraxlj committed Sep 10, 2017
1 parent 4251218 commit 15120af
Show file tree
Hide file tree
Showing 13 changed files with 1,460 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/ReadMe.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
v1 ==============
Create Game Panel
Create user tank
Can move

v2 ==============
Create computer tanks (Dummies)
Can move
Can be destoried

Update user tank
Can fire

Create Shell


v3 ==============
Plan:
make computer tanks run independently and fire
terminate the game when user tank is destoried
29 changes: 29 additions & 0 deletions src/game_v1/Game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package game_v1;

import java.awt.BorderLayout;

import javax.swing.JFrame;

public class Game{
JFrame jf = null;
GamePanel gp = null;

public Game() {
jf = new JFrame("War");
gp = new GamePanel();

jf.add(gp, BorderLayout.CENTER);

jf.addKeyListener(gp);

jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setLocation(0, 0);
jf.setResizable(false);
jf.setSize(800, 600);
jf.setVisible(true);
}

public static void main(String[] args) {
new Game();
}
}
115 changes: 115 additions & 0 deletions src/game_v1/GamePanel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package game_v1;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JPanel;

public class GamePanel extends JPanel implements KeyListener{

Player p1 = new Player(10, 10, Tank.UP, Tank.SPEED_PLAYER);
public GamePanel() {
this.setBackground(Color.BLACK);
}

@Override
public void paint(Graphics g) {
super.paint(g);
drawTank(g, p1);
}

void drawTank(Graphics g, Tank t) {
int x = t.getX();
int y = t.getY();
int[] xPoints = null;
int[] yPoints = null;
int nPoints = 3;
g.setColor(t.getColor());
switch (t.getDirect()) {
case Tank.UP:
xPoints = new int[]{x, x+30, x+15};
yPoints = new int[]{y+30, y+30, y};
g.fillPolygon(xPoints, yPoints, nPoints);
break;
case Tank.DOWN:
xPoints = new int[]{x, x+30, x+15};
yPoints = new int[]{y, y, y+30};
g.fillPolygon(xPoints, yPoints, nPoints);
break;
case Tank.LEFT:
xPoints = new int[]{x+30, x+30, x};
yPoints = new int[]{y, y+30, y+15};
g.fillPolygon(xPoints, yPoints, nPoints);
break;
case Tank.RIGHT:
xPoints = new int[]{x, x, x+30};
yPoints = new int[]{y, y+30, y+15};
g.fillPolygon(xPoints, yPoints, nPoints);
break;
default:
break;
}
}
void drawTank(Graphics g, int x, int y, char direct) {
int[] xPoints = null;
int[] yPoints = null;
int nPoints = 3;
switch (direct) {
case 'u':
xPoints = new int[]{x, x+30, x+15};
yPoints = new int[]{y+30, y+30, y};
g.fillPolygon(xPoints, yPoints, nPoints);
break;
case 'd':
xPoints = new int[]{x, x+30, x+15};
yPoints = new int[]{y, y, y+30};
g.fillPolygon(xPoints, yPoints, nPoints);
break;
case 'l':
xPoints = new int[]{x+30, x+30, x};
yPoints = new int[]{y, y+30, y+15};
g.fillPolygon(xPoints, yPoints, nPoints);
break;
case 'r':
xPoints = new int[]{x, x, x+30};
yPoints = new int[]{y, y+30, y+15};
g.fillPolygon(xPoints, yPoints, nPoints);
break;
default:
break;
}
}

@Override
public void keyTyped(KeyEvent e) {}

@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()){
case KeyEvent.VK_UP:
p1.setDirect(Tank.UP);
p1.moveUp();
break;
case KeyEvent.VK_DOWN:
p1.setDirect(Tank.DOWN);
p1.moveDown();
break;
case KeyEvent.VK_LEFT:
p1.setDirect(Tank.LEFT);
p1.moveLeft();
break;
case KeyEvent.VK_RIGHT:
p1.setDirect(Tank.RIGHT);
p1.moveRight();
break;
}
// System.out.println(p1.getX() + " " + p1.getY());

repaint();
}

@Override
public void keyReleased(KeyEvent e) {}
}
93 changes: 93 additions & 0 deletions src/game_v1/Tank.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package game_v1;

import java.awt.Color;
/**
*
* @author lxia
* Tank position
* Tank color
*/
interface Const{
final int UP = 0;
final int DOWN = 1;
final int LEFT = 2;
final int RIGHT = 3;

final int SPEED_PLAYER = 1;

final Color COLOR_PLAYER = Color.RED;
}
public abstract class Tank implements Const{
protected int x, y;
protected int direct, speed;
protected Color color;
int w, h; // width, height of the tank
public Tank(int x, int y, int direct, int speed) {
this.x = x;
this.y = y;
this.direct = direct;
this.speed = speed;
}

public int getX() {
return x;
}
public int getY() {
return y;
}
public int getDirect() {
return direct;
}
public int getSpeed() {
return speed;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public void setDirect(int direct) {
this.direct = direct;
}
public void setSpeed(int speed) {
this.speed = speed;
}

public void moveUp() {
y -= speed;
}
public void moveDown() {
y += speed;
}
public void moveLeft() {
x -= speed;
}
public void moveRight() {
x += speed;
}

public abstract Color getColor();
public abstract void setColor(Color color);
}

class Player extends Tank {
public Color color = Color.RED;
public static final int speed = 5;
public static final int W = 30, H = 30;
public Player(int x, int y, int direct, int speed) {
super(x, y, direct, speed);
}
@Override
public Color getColor() {
return color;
}
@Override
public void setColor(Color color) {
this.color = color;
}
@Override
public void moveUp() {
super.moveUp();
}
}
8 changes: 8 additions & 0 deletions src/game_v1/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
*
*/
/**
* @author lxia
*
*/
package game_v1;
22 changes: 22 additions & 0 deletions src/game_v2/Const.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package game_v2;

import java.awt.Color;

public interface Const {
final static int UP = 0;
final static int DOWN = 1;
final static int LEFT = 2;
final static int RIGHT = 3;

final static int SPEED_PLAYER = 1;
final static int SPEED_SHELL = 3;
final static int SPEED_TANK1 = 1;

final static Color COLOR_PLAYER = Color.RED;

final static int WINDOW_W = 800;
final static int WINDOW_H = 600;
final static int PANEL_W = WINDOW_H;
final static int PANEL_H = WINDOW_H - 30;

}
Loading

0 comments on commit 15120af

Please sign in to comment.