欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java项目,坦克大战

程序员文章站 2022-04-07 12:27:50
...
现在坦克已经可以运动,并且可以发炮弹了


主类,启动类
---------------------------------------------------------
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
//消除闪烁,使用双缓冲
//线程重画更加均匀,更能控制重化的速度。按键重画不能解决子弹自动飞行的问题;

public class TankClient extends Frame {
public static final int GAME_WIDTH = 800;
public static final int GAME_HIGHT = 600;

Tank myTank = new Tank(50, 50,this);
//怎么打多发炮弹,使用容器装炮弹,每当按下Ctr这个键的时候往容器中添加炮弹
//画出每一个炮弹;
List<Missile> missiles =new ArrayList<Missile>(); //遍历比较快   //LinkedList<>;//链表删除快,查找慢
private Image offScreenImage = null;

// 重写
public void update(Graphics g) {
if (offScreenImage == null) {
offScreenImage = this.createImage(GAME_WIDTH, GAME_HIGHT);
}
// 拿到图片上的画笔
Graphics gOffScreen = offScreenImage.getGraphics();
Color c = gOffScreen.getColor();
gOffScreen.setColor(Color.blue);
gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HIGHT);
gOffScreen.setColor(c);
paint(gOffScreen);// 画在背后图片上
g.drawImage(offScreenImage, 0, 0, null);// 画在屏幕上

}

public void paint(Graphics g) {
//画出还有多少炮弹,画一个字符串到屏幕
g.drawString("missiles count:"+missiles.size(), 30, 50);

for(int i=0;i<missiles.size();i++){
Missile m=missiles.get(i);
m.draw(g);

}
myTank.draw(g);

}

public void lauchFrame() {
setTitle("TankWar");
setBounds(230, 100, 800, 600);
// 匿名内部类,短,不涉及将来的扩展
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

setResizable(false);
setBackground(Color.GREEN);
setVisible(true);
addKeyListener(new KeyMoniter());
new Thread(new PaintThread()).start();
}

public static void main(String[] args) {
TankClient tc = new TankClient();
tc.lauchFrame();

}

// 内部类,只为这个TankWar服务,方便的访问包装类的方法,不方便公开,
private class PaintThread implements Runnable {
public void run() {
while (true) {
repaint();// 内部调用父类的paint方法;
try {
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

// 键盘事件,内部类
private class KeyMoniter extends KeyAdapter {
public void keyReleased(KeyEvent e){
myTank.keyReleased(e);
}
public void keyPressed(KeyEvent e) {
myTank.keyPressed(e);
}
}




}
----------------------------------------


Tank类


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

public class Tank {
public static final int xspeed = 5;
public static final int yspeed = 5;
public static final int WIDTH=30;
public static final int HEIGHT=30;
TankClient tc=null;
int x, y;
private boolean bL = false, bU = false, bR = false, bD = false;

public enum Direction {
L, LU, U, RU, R, RD, LD, D, STOP
};// 枚举类型

private Direction dir = Direction.STOP;
//定义一个炮筒的方向;
private Direction ptDir=Direction.D;
public Tank(int x, int y) {

this.x = x;
this.y = y;
}

public Tank(int x,int y, TankClient tc){
this(x, y);//调用上一个构造方法
this.tc=tc;

}

public void draw(Graphics g) {
Color c = g.getColor();
g.setColor(Color.red);
g.fillOval(x, y, WIDTH, HEIGHT);
g.setColor(c);
//根据坦克的方向画一条直线当作炮筒;
switch (ptDir) {
case L:

g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2, x, y+Tank.HEIGHT/2);
break; 
case LU:
g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2, x, y);
break;
case U:
g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2, x+Tank.WIDTH/2, y);
break;
case RU:
g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2, x+Tank.WIDTH, y);
break;
case R:
g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2, x+Tank.WIDTH, y+Tank.HEIGHT/2);
break;
case RD:
g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2, x+Tank.WIDTH, y+Tank.HEIGHT);
break;
case LD:
g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2, x, y+Tank.HEIGHT);
break;
case D:
g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2, x+Tank.WIDTH/2, y+Tank.HEIGHT);
break;

}
move();
}

// 移动坦克
void move() {
switch (dir) {
case L:
x -= xspeed;
break;
case LU:
x -= xspeed;
y -= yspeed;
break;
case U:
y -= yspeed;
break;
case RU:
x += xspeed;
y -= yspeed;
break;

case R:
x += xspeed;
break;
case RD:
x += xspeed;
y += yspeed;
break;
case LD:
x -= xspeed;
y += yspeed;
break;
case D:
y += yspeed;
break;
case STOP:
break;

}
//坦克出界问题
if(x<=0)x=0;
if(y<=30)y=30;
if(x+Tank.HEIGHT>=TankClient.GAME_WIDTH)x=TankClient.GAME_WIDTH-Tank.WIDTH;
if(y+Tank.HEIGHT>=TankClient.GAME_HIGHT)y=TankClient.GAME_HIGHT-Tank.HEIGHT;

if(dir!=Direction.STOP){
ptDir=dir;
}
}

public void keyPressed(KeyEvent e) {
// 获得键的虚拟码
int key = e.getKeyCode();
// if(key==KeyEvent.VK_RIGHT){
// x+=5;
// }if(key==KeyEvent.VK_LEFT){
// x=x-5;
// }if(key==KeyEvent.VK_UP){
// y=y-5;
// }if(key==KeyEvent.VK_DOWN){
// y=y+5;
// }
switch (key) {

case KeyEvent.VK_LEFT:
bL = true;
break;
case KeyEvent.VK_UP:
bU = true;
break;
case KeyEvent.VK_RIGHT:
bR = true;
break;
case KeyEvent.VK_DOWN:
bD = true;
break;
}
locateDirection();
}

void locateDirection() {
if(bL&&!bU&&!bR&&!bD) {dir=Direction.L;}
else if(bL&&bU&&!bR&&!bD) {dir=Direction.LU;}
else if(!bL&&bU&&!bR&&!bD){ dir=Direction.U;}
else if(!bL&&bU&&bR&&!bD) {dir=Direction.RU;}
else if (!bL&&!bU&&!bR&&bD){ dir=Direction.D;}
else if(!bL&&!bU&&bR&&!bD) {dir=Direction.R;}
else if(!bL&&!bU&&bR&&bD) {dir=Direction.RD;}
else if(bL&&!bU&&!bR&&bD) {dir=Direction.LD;}
else if(!bL&&!bU&&!bR&&!bD) dir=Direction.STOP;

}

public void keyReleased(KeyEvent e) {
// 获得键的虚拟码
int key = e.getKeyCode();
switch (key) {
case KeyEvent.VK_CONTROL:
fire();
break;
case KeyEvent.VK_LEFT:
bL = false;
break;
case KeyEvent.VK_UP:
bU = false;
break;
case KeyEvent.VK_RIGHT:
bR = false;
break;
case KeyEvent.VK_DOWN:
bD = false;
break;
}
locateDirection();

}

//开火
public Missile fire(){

Missile m=new Missile(x+Tank.WIDTH/2-Missile.WIDTH/2,y+Tank.HEIGHT/2-Missile.HEIGHT/2,ptDir,tc);//传递坦克的位置和方向
tc.missiles.add(m);
return m;


}


}


---------------------------------

子弹类



import java.awt.Color;
import java.awt.Graphics;

import Frame.Tank.Direction;

public class Missile {
private static final int xspeed=8;
private static final int yspeed=8;
public static final int WIDTH=10;
public static final int HEIGHT=10;
private boolean  Live=true;
//炮弹的生死状态,默认为生,出街,打倒敌人为死
public boolean isLive() {
return Live;
}

int x,y;
Tank.Direction dir;
private TankClient tc;

public Missile(int x, int y, Direction dir) {

this.x = x;
this.y = y;
this.dir = dir;
}
public Missile(int x,int y,Direction dir , TankClient tc){
this(x, y, dir);
this.tc=tc;
}

public void draw(Graphics g){
Color c=g.getColor();
g.setColor(Color.black);
g.fillOval(x, y, WIDTH, HEIGHT);
g.setColor(c);
move();

}

private void move() {
switch (dir) {
case L:
x -= xspeed;
break;
case LU:
x -= xspeed;
y -= yspeed;
break;
case U:
y -= yspeed;
break;
case RU:
x += xspeed;
y -= yspeed;
break;

case R:
x += xspeed;
break;
case RD:
x += xspeed;
y += yspeed;
break;
case LD:
x -= xspeed;
y += yspeed;
break;
case D:
y += yspeed;
break;

}
if(x<0||y<0||x>TankClient.GAME_WIDTH||y>TankClient.GAME_HIGHT){
Live=false;
tc.missiles.remove(this);
}




}
}

上一篇: perl单元测试

下一篇: 面向对象