package 坦克遊戲第二版;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import javax.swing.*;
public class TanKe extends JFrame{
MyPanel mp = null;
public TanKe() {
// TODO Auto-generated constructor stub
mp = new MyPanel(400,400,0,2,0);
this.add(mp);
this.addKeyListener(mp);
this.setSize(500, 500); // 設置框體大小
this.setLocation(400, 150); // 設置框體顯示的位置
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 設置框體退出
this.setVisible(true); // 顯示框體
}
public static void main(String[] args) {
TanKe tk = new TanKe();
}
}
class MyPanel extends JPanel implements KeyListener{
Tank tank = null; // 採用多態的形式編寫代碼
Vector<Enemy> vc = null;
int enSize = 3;
public MyPanel(int x,int y,int color,int speed,int direct) {
tank = new Hero(x, y, speed, color, direct); // 多態
vc = new Vector<Enemy>(); // 存放敵人坦克的容器
for(int i=0;i<enSize;i++){
vc.add( new Enemy((i+1)*50,100,2,1,2) );
}
}
public void paint(Graphics g){
super.paint(g);
g.fillRect(0, 0, 500, 500);
// if(tank instanceof Hero) drawTank(tank.getX(), tank.getY(), tank.getColor(), g, tank.getDirect());
// if(tank instanceof Enemy) drawTank(tank.getX(), tank.getY(), tank.getColor(), g, tank.getDirect());
drawTank(tank.getX(), tank.getY(), tank.getColor(), g, tank.getDirect());
for(int i=0;i<vc.size();i++)
drawTank(vc.get(i).getX(), vc.get(i).getY(), vc.get(i).getColor(), g, vc.get(i).getDirect());
}
private void drawTank(int x, int y, int type, Graphics g, int direct) {
// TODO Auto-generated method stub
switch (type) {
case 0:
g.setColor(Color.yellow);
break;
case 1:
g.setColor(Color.red);
break;
}
switch (direct) {
//向上
case 0:
g.fill3DRect(x, y, 5, 30, false); // 填充矩形
g.fill3DRect(x + 15, y, 5, 30, false);
g.fill3DRect(x + 5, y + 5, 10, 20, false);
g.fillOval(x + 4, y + 10, 10, 10);
g.drawLine(x + 10, y + 15, x + 10, y);
break;
//向右
case 1:
g.fill3DRect(x, y, 30, 5, false);
g.fill3DRect(x, y+15, 30, 5, false);
g.fill3DRect(x+5, y+5, 20, 10, false);
g.fillOval(x+10, y+5, 10, 10);
g.drawLine(x+15, y+10, x+30, y+10);
break;
//向下
case 2:
g.fill3DRect(x, y, 5, 30, false); // 填充矩形
g.fill3DRect(x + 15, y, 5, 30, false);
g.fill3DRect(x + 5, y + 5, 10, 20, false);
g.fillOval(x + 4, y + 10, 10, 10);
g.drawLine(x + 10, y + 15, x + 10, y+30);
break;
//向左
case 3:
g.fill3DRect(x, y, 30, 5, false);
g.fill3DRect(x, y+15, 30, 5, false);
g.fill3DRect(x+5, y+5, 20, 10, false);
g.fillOval(x+10, y+5, 10, 10);
g.drawLine(x+15, y+10, x, y+10);
break;
}
}
// 按下鍵
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
// w表示向上,d表示向右,s表示向下,a表示想左
if(e.getKeyCode()==KeyEvent.VK_W){
this.tank.setDirect(0); // 設置方向
this.tank.y -= this.tank.speed; // 運行速度
}
if(e.getKeyCode()==KeyEvent.VK_D){
this.tank.setDirect(1);
this.tank.x += this.tank.speed;
}
if(e.getKeyCode()==KeyEvent.VK_S){
this.tank.setDirect(2);
this.tank.y += this.tank.speed;
}
if(e.getKeyCode()==KeyEvent.VK_A){
this.tank.setDirect(3);
this.tank.x -= this.tank.speed;
}
this.repaint();
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
}
class Hero extends Tank{
public Hero(int x,int y,int speed,int color,int direct) {
// TODO Auto-generated constructor stub
super(x,y,speed,color,direct);
}
}
class Enemy extends Tank{
public Enemy(int x,int y,int speed,int color,int direct) {
// TODO Auto-generated constructor stub
super(x,y,speed,color,direct);
}
}
class Tank{
int x; // 坦克的橫座標
int y; // 坦克的縱座標
int speed; // 坦克的速度
// 0代表黃色, 1代表紅色
int color; // 坦克的顏色
// 0表示向上,1表示向右,2表示向下,3表示向左
int direct;
public Tank(int x, int y,int speed,int color,int direct) {
// TODO Auto-generated constructor stub
this.x = x;
this.y = y;
this.speed = speed;
this.color = color;
this.direct = direct;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public int getColor() {
return color;
}
public void setColor(int color) {
this.color = color;
}
public int getDirect() {
return direct;
}
public void setDirect(int direct) {
this.direct = direct;
}
}
java之坦克大戰(一)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.