用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

飞机大战

2019-10-12 作者: 云代码会员举报

[java]代码库

public class World extends JPanel {
	public static final int WIDTH = 400;//public static final int WIDTH=400;
	public static final int HEIGHT = 700;//public static final int HEIGHT=700;
	private Sky sky = new Sky();//private Sky sky=new Sky();
	private Hero hero = new Hero();//private Hero hero=new Hero();
	private FlyingObject[] enemies = {}; // 敌人数组 private FlyingObject []enemies={};
	private Bullet[] bullets = {}; // 子弹数组 private Bullet[]bullets={};

	public static final int START = 0;//public static final int START=0;
	public static final int RUNNING = 1;//public static final int RUNNING=1;
	public static final int PAUSE = 2;//public static final int PAUSE=2;
	public static final int GAME_OVER = 3;//public static final int GAME_OVER=3;
	private int state = START;//private int state=START;

	public static BufferedImage start;//public static BufferedImage start;
	public static BufferedImage pause;//public static BufferedImage pause;
	public static BufferedImage gameover;//public static BufferedImage gameover;
	static {//static{静态块
		start = Images.loadImage("start.png");//start=Images.loadImage("start.png);
		pause = Images.loadImage("pause.png");//pause=Images.loadImage("pause.png");
		gameover = Images.loadImage("gameover.png");//gameover=Images.loadImage("gameover.png");
	}

	public FlyingObject nextOne() {//public FlyingObject nextOne(){
		Random rand = new Random();//Random rand=new Random();
		int type = rand.nextInt(20);//int type=rand .nextInt(20);
		if (type < 5) {//if(type<5){
			return new Bee();//return new Bee();
		} else if (type < 20) {//else if (type<20){
			return new Airplane();//return new Airplane();
		} else {//else
			return new BigAirplane();//return new BigAirplane();
		}
	}

	int flyEnterIndex = 0;//int flyEnterIndex=0;

	public void enterAction() {//public void enterAction(){
		flyEnterIndex++;//flyEnterIndex++;
		if (flyEnterIndex % 40 == 0) {//if(flyEnterIndex%40==0){
			FlyingObject obj = nextOne();//FlyingObject obj=nextOne;
			enemies = Arrays.copyOf(enemies, enemies.length + 1);//enemies =Arrays.copyOf(enemies,enemies.length+1);
			enemies[enemies.length - 1] = obj;//enemies [enemies.length-1]=obj;
		}
	}

	public void stepAction() {//public void stepAction(){
		sky.step();//sky.step();
		for (int i = 0; i < enemies.length; i++) {//for(int i=0;i<enemies.length;i++)
			enemies[i].step();//enemies[i].step();
		}
		for (int i = 0; i < bullets.length; i++) {//for(int i=0;i<bullets.length;i++)
			bullets[i].step();//bullets[i].step();
		}
	}

	int shootIndex = 0;//int shootIndex=0;

	public void shootAction() {//public void shootAction(){
		shootIndex++;//shootIndex++;
		if (shootIndex % 30 == 0) {//if(shootIndex%30==0){
			Bullet[] bs = hero.shoot();//Bullets[]bs=hero.shoot();
			bullets = Arrays.copyOf(bullets, bullets.length + bs.length);
			System.arraycopy(bs, 0, bullets, bullets.length - bs.length, bs.length);
		}//bullets=Arrays.copyOf(bullets,bullets.length+bs.length);
	}//System.arraycopy(bs,0,bullets,bullets.length-bs.length,bs.length);

	private int score = 0;//private int score=0;

	public void bangAction() {//public void bangAction()
		for (int i = 0; i < bullets.length; i++) {//for(int i=0;i<bullets.length;i++){
			Bullet b = bullets[i];//Bullets b=bullets[i]
			for (int j = 0; j < enemies.length; j++) {//for(int j=0;j<enemies.length;j++){
				FlyingObject f = enemies[j];//FlyingObject f=enemies[j];
				if (b.isLife() && f.isLife() && b.hit(f)) {//if(b.islife()&&f.isLife()&&b.hit(f)){
					b.goDead();//b.goDead();
					f.goDead();//f.goDead();
					if (f instanceof Enemy) {//if(f instanceof Enemy){
						Enemy e = (Enemy) f;//Enemy e=(Enemy) f;
						score += e.getScore();//score+=e.getScore();
					}
					if (f instanceof Award) {//if(f instanceof Award){
						Award a = (Award) f;//Award a=(Award)f;
						int type = a.getType();//int type=a.getType();
						switch (type) {//switch(type){
						case Award.DOUBLE_FIRE://case Award.DOUBLE_FIRE:
							hero.addDoubleFire();//hero.addDoubleFire();
							break;//break;
						case Award.LIFE://case Award.LIFE:
							hero.addLife();//hero.addLife
							break;//break;
						}
					}

				}
			}
		}
	}



网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...