58,441
社区成员
发帖
与我相关
我的任务
分享
// 主操控程序
package TankWar2;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
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;
import java.util.Random;
public class TankWar extends Frame {
public enum Direction {
U, UR, R, RD, D, DL, L, LU, STOP
};
public static final int GAME_WIDTH = 900, GAME_HEIGHT = 700;
public static final int GAME_SPEED = 44;//0 - 300,越小越快
public static final int KEY_UP = KeyEvent.VK_UP;
public static final int KEY_DOWN = KeyEvent.VK_DOWN;
public static final int KEY_RIGHT = KeyEvent.VK_RIGHT;
public static final int KEY_LEFT = KeyEvent.VK_LEFT;
public static final int KEY_SPACE = KeyEvent.VK_SPACE;
public static final int KEY_A = KeyEvent.VK_A;
public static final int KEY_CONTROL = KeyEvent.VK_CONTROL;
public static final int KEY_ENTER = KeyEvent.VK_ENTER;
public static boolean uKey = false;
public static boolean rKey = false;
public static boolean dKey = false;
public static boolean lKey = false;
public static boolean controlKey = false;
public static boolean pause = false;
public static int timeCounter = 0;
public static int timeCounter2 = 0;
public static int superFireAmount = 0;//大招数量
public static int playerFireRate = 5;//玩家攻击速率
public static Tank myTank = new Tank(700, 70, 0, true, Direction.STOP, Direction.D);//生成玩家坦克
public static List<Missile> missiles = new ArrayList<Missile>();//导弹数组
public static List<Tank> tanks = new ArrayList<Tank>();//坦克数组
public static List<Wall> walls = new ArrayList<Wall>();//墙数组
public static List<Collision> collisions = new ArrayList<Collision>();//爆炸数组
public static List<EatAbleThing> bloods = new ArrayList<EatAbleThing>();
public static List<EatAbleThing> speedUps = new ArrayList<EatAbleThing>();
public static List<EatAbleThing> fireRates = new ArrayList<EatAbleThing>();
public static List<EatAbleThing> superFires = new ArrayList<EatAbleThing>();
public static List<EatAbleThing> lifeAdds = new ArrayList<EatAbleThing>();
public static Image image;
public static Graphics g;
public static int playerLifes = 0;
public static int xChange = 2,xChange2 = 2;
public static int yChange = 15,yChange2 = 15;
public static int key6 = 2, key8 = 2, key10 = 700, key11 = 0, key14 = 0;//key10为敌军增援首次延时
public static boolean key1 = false, key2 = false, key3 = false, key4 = false, key5 = false, key7 = false, key9 = false, key13 = false;// key1, key2, key3用来解决移除数组产生的问题
public static Random random = new Random();
public static void main(String[] args) {
TankWar tw = new TankWar();
tw.setLocation(10, 10);
tw.setSize(GAME_WIDTH, GAME_HEIGHT);
tw.setTitle("坦克大战");
tw.setResizable(false);
tw.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
tw.setVisible(true);
tw.start();
}
public void start() {
new Thread(new KeyThread(this)).start();//按键线程
new Thread(new MainThread(this)).start();//主线程 发现线程真的比较快,但不怎么清楚原理
}
//以下为画画面
public void update(Graphics g) {
if (image == null) image = this.createImage(GAME_WIDTH, GAME_HEIGHT);
Graphics h = image.getGraphics();
h.setColor(Color.WHITE);
h.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
myTank.draw(h);
h.setColor(Color.RED);
h.drawString("superfire amount : " + superFireAmount, 15, 50);
h.drawString("fire rate : " + (6 - playerFireRate), 15, 80);
h.drawString("speed : " + (Tank.TANK_XSPEED + myTank.speedUp), 15, 110);
h.drawString("lifes : " + playerLifes, 15, 140);
h.drawString("score : " + key11, 15,170 );
h.setColor(Color.BLUE);
h.drawString("It's me!",myTank.x - 5,myTank.y - 11 );
h.setColor(Color.RED);
h.drawString("next wave : " + timeCounter2 + "/" + key10, 15,200 );
h.drawString("你能得多少分呢?", 15,230 );
key14 += 2;
if (key14 > 18) key14 = 0;
if (key13) {
h.drawString("you are going to die!!!", GAME_WIDTH / 2 - 100,70 + key14 );
}
if (key4) {
h.drawString("生 命 燃 烧", GAME_WIDTH / 2 - 40 + xChange, yChange);
xChange += key6;
yChange += 2;
if (xChange > 15)
key6 = -2;
if (xChange < 2)
key6 = 2;
if (yChange > 170) {
key4 = false;
xChange = 2;
yChange = 15;
key6 = 2;
}
}
if (key9) {
h.drawString("敌 军 增 援", GAME_WIDTH / 2 - 40 + xChange2, yChange2);
xChange2 += key8;
yChange2 += 2;
if (xChange2 > 15)
key8 = -2;
if (xChange2 < 2)
key8 = 2;
if (yChange2 > 180) {
key9 = false;
xChange2 = 2;
yChange2 = 15;
key8 = 2;
}
}
for (int i = 0; i < walls.size(); i++) {
walls.get(i).draw(h);
}
for (int i = 0; i < tanks.size(); i++) {
tanks.get(i).draw(h);
}
for (int i = 0; i < missiles.size(); i++) {
missiles.get(i).draw(h);
}
for (int i = 0; i < bloods.size(); i++) {
bloods.get(i).draw(h);
}
for (int i = 0; i < fireRates.size(); i++) {
fireRates.get(i).draw(h);
}
for (int i = 0; i < superFires.size(); i++) {
superFires.get(i).draw(h);
}
for (int i = 0; i < speedUps.size(); i++) {
speedUps.get(i).draw(h);
}
for (int i = 0; i < lifeAdds.size(); i++) {
lifeAdds.get(i).draw(h);
}
for (int i = 0; i < collisions.size(); i++) {
collisions.get(i).draw(h);
}
g.drawImage(image, 0, 0, null);
}

public void fire() {
TankWar.missiles.add(new Missile(x + TANK_WIDTH / 2
- Missile.MISSILE_WIDTH / 2, y + TANK_HEIGHT / 2
- Missile.MISSILE_HEIGHT / 2, ptDir, team));
}
public void superFire() {
TankWar.missiles.add(new Missile(x + TANK_WIDTH / 2
- Missile.MISSILE_WIDTH / 2, y + TANK_HEIGHT / 2
- Missile.MISSILE_HEIGHT / 2, Direction.U, team));
TankWar.missiles.add(new Missile(x + TANK_WIDTH / 2
- Missile.MISSILE_WIDTH / 2, y + TANK_HEIGHT / 2
- Missile.MISSILE_HEIGHT / 2, Direction.UR, team));
TankWar.missiles.add(new Missile(x + TANK_WIDTH / 2
- Missile.MISSILE_WIDTH / 2, y + TANK_HEIGHT / 2
- Missile.MISSILE_HEIGHT / 2, Direction.R, team));
TankWar.missiles.add(new Missile(x + TANK_WIDTH / 2
- Missile.MISSILE_WIDTH / 2, y + TANK_HEIGHT / 2
- Missile.MISSILE_HEIGHT / 2, Direction.RD, team));
TankWar.missiles.add(new Missile(x + TANK_WIDTH / 2
- Missile.MISSILE_WIDTH / 2, y + TANK_HEIGHT / 2
- Missile.MISSILE_HEIGHT / 2, Direction.D, team));
TankWar.missiles.add(new Missile(x + TANK_WIDTH / 2
- Missile.MISSILE_WIDTH / 2, y + TANK_HEIGHT / 2
- Missile.MISSILE_HEIGHT / 2, Direction.DL, team));
TankWar.missiles.add(new Missile(x + TANK_WIDTH / 2
- Missile.MISSILE_WIDTH / 2, y + TANK_HEIGHT / 2
- Missile.MISSILE_HEIGHT / 2, Direction.L, team));
TankWar.missiles.add(new Missile(x + TANK_WIDTH / 2
- Missile.MISSILE_WIDTH / 2, y + TANK_HEIGHT / 2
- Missile.MISSILE_HEIGHT / 2, Direction.LU, team));
}
//坦克被打到
public void beHit(Missile mis, List<Tank> tanks, List<Missile> missiles) {
if (mis.team == this.team) {
return;
}
missiles.remove(mis);
TankWar.key2 = true;
life = life - HARM;
if (life <= 0) {
if (isPlayer) {
TankWar.playerLifes --;
TankWar.superFireAmount = 0;
TankWar.playerFireRate = 0;
TankWar.myTank.speedUp = 0;
if (TankWar.playerLifes < 0) {
System.exit(0);
}
life = LIFE_MAX;
} else {
tanks.remove(this);
TankWar.key1 = true;
TankWar.collisions.add(new Collision(x, y));
}
}
}
//聊天
public String chat () {
if (random.nextInt(5000) > CHAT_RATE) {
int randomCount = random.nextInt(CHAT.length);
return CHAT[randomCount];
} else return "";
}
}
package TankWar2;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.List;
import TankWar2.TankWar.Direction;
public class Missile {
public static final int MISSILE_WIDTH = 10;
public static final int MISSILE_HEIGHT = 10;
public static final int MISSILE_XSPEED = 10;
public static final int MISSILE_YSPEED = 10;
public int x, y, team;
public Direction misDir;
public Missile(int x, int y, Direction misDir, int team) {
this.x = x;
this.y = y;
this.team = team;
this.misDir = misDir;
}
public void draw(Graphics g) {
if (team == 1) g.setColor(Color.RED);
else g.setColor(Color.BLACK);
g.fillOval(x, y, MISSILE_WIDTH, MISSILE_HEIGHT);
}
public void move(List<Tank> tanks, List<Missile> missiles, List<Wall> walls) {
switch (misDir) {
case U:
y = y - MISSILE_YSPEED;
break;
case UR:
x = x + (int) (MISSILE_XSPEED / 1.3);
y = y - (int) (MISSILE_YSPEED / 1.3);
break;
case R:
x = x + MISSILE_XSPEED;
break;
case RD:
x = x + (int) (MISSILE_XSPEED / 1.3);
y = y + (int) (MISSILE_YSPEED / 1.3);
break;
case D:
y = y + MISSILE_YSPEED;
break;
case DL:
x = x - (int) (MISSILE_XSPEED / 1.3);
y = y + (int) (MISSILE_YSPEED / 1.3);
break;
case L:
x = x - MISSILE_XSPEED;
break;
case LU:
x = x - (int) (MISSILE_XSPEED / 1.3);
y = y - (int) (MISSILE_YSPEED / 1.3);
break;
}
//是否撞坦克
for (int i = 0; i < tanks.size(); i++) {
if (new Rectangle(x, y, MISSILE_WIDTH, MISSILE_HEIGHT)
.intersects(TankWar.getRect(tanks.get(i)))) {
tanks.get(i).beHit(this, tanks, missiles);
return;
}
}
if (new Rectangle(x, y, MISSILE_WIDTH, MISSILE_HEIGHT)
.intersects(TankWar.getRect(TankWar.myTank))) {
TankWar.myTank.beHit(this, tanks, missiles);
return;
}
if (x < 0 || y < 0 || x > (TankWar.GAME_WIDTH - MISSILE_WIDTH)
|| y > (TankWar.GAME_HEIGHT - MISSILE_HEIGHT)) {
missiles.remove(this);
TankWar.key2 = true;
return;
}
//是否撞墙
for (int i = 0; i < walls.size(); i++) {
if (new Rectangle(x, y, MISSILE_WIDTH, MISSILE_HEIGHT)
.intersects(TankWar.getRect(walls.get(i)))) {
missiles.remove(this);
TankWar.key2 = true;
return;
}
}
//是否撞弹
for (int i = 0; i < missiles.size(); i++) {
if (!this.equals(missiles.get(i))
&& new Rectangle(x, y, MISSILE_WIDTH, MISSILE_HEIGHT)
.intersects(TankWar.getRect(missiles.get(i)))) {
if (missiles.indexOf(this) > missiles.indexOf(missiles.get(i)))
TankWar.key3 = true;
else
TankWar.key3 = false;
missiles.remove(missiles.get(i));
missiles.remove(this);
TankWar.key2 = true;
return;
}
}
}
}
package TankWar2;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
public class Wall {
public int x, y, width, height;
public Wall(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public void draw(Graphics g) {
g.setColor(Color.BLACK);
g.fillRect(x, y, width, height);
}
}
package TankWar2;
import java.awt.Color;
import java.awt.Graphics;
public class Collision {
public final int POS_MAX = 23;
int x, y, pos = 1;
public Collision(int x, int y) {
this.x = x;
this.y = y;
}
public void draw(Graphics g) {
g.setColor(Color.RED);
g.fillOval(x + Tank.TANK_WIDTH / 2 - pos, y + Tank.TANK_HEIGHT / 2
- pos, pos * 2, pos * 2);
pos += 5;
if (pos >= POS_MAX) {
TankWar.collisions.remove(this);
}
}
}
package TankWar2;
import java.awt.Color;
import java.awt.Graphics;
public class EatAbleThing {
public static final int EATABLETHINGAREA = 13;
public static final int EATABLETHINGCHANGESPEED = 2;
//以下为各可以吃的东西生成时间,1 - 500;越大生成时间越短
public static final int BLOOD_RATE = 12;
public static final int SPEEDUP_RATE = 2;
public static final int FIRERATE_RATE = 2;
public static final int SUPERFIRE_RATE = 20;
public static final int LIFEADD_RATE = 3;
public int x, y, pos1 = 1, pos2 = 0;
public String eatAbleThingName = "";
public Color color;
public EatAbleThing(int x, int y, Color color, String eatAbleThingName) {
this.x = x;
this.y = y;
this.eatAbleThingName = eatAbleThingName;
this.color = color;
}
public void draw(Graphics g) {
g.setColor(color);
g.drawOval(x + EATABLETHINGAREA / 2 - pos1, y + EATABLETHINGAREA / 2
- pos1, pos1 * 2, pos1 * 2);
if (pos2 > 10)
pos2 = 0;
g.drawString(eatAbleThingName, x + EATABLETHINGAREA / 2
- (int) (eatAbleThingName.length() * 2.4), y + EATABLETHINGAREA
/ 2 + pos2 - 3);
pos2 += 2;
pos1 += EATABLETHINGCHANGESPEED;
if (pos1 >= EATABLETHINGAREA) {
pos1 = 1;
}
}
}