当前位置: 代码迷 >> Eclipse >> 第一回发个小游戏,还请大家来看看
  详细解决方案

第一回发个小游戏,还请大家来看看

热度:47   发布时间:2016-04-23 12:49:48.0
第一次发个小游戏,还请大家来看看
Java code
//   主操控程序 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);    }
  相关解决方案