打飞机游戏第一天,诸神归位

Posted 琦琦爸爸

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了打飞机游戏第一天,诸神归位相关的知识,希望对你有一定的参考价值。

/*游戏第一天,诸神归位*/

 

1,主程序

package qiqi.shoot;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.imageio.ImageIO;
import java.awt.Graphics;

/* 主程序类*/
public class ShootGame extends JPanel {
public static final int WIDTH=400;
public static final int HEIGHT=645;

public static BufferedImage background;
public static BufferedImage start;
public static BufferedImage pause;
public static BufferedImage gameover;
public static BufferedImage airplane;
public static BufferedImage bee;
public static BufferedImage bullet;
public static BufferedImage hero0;
public static BufferedImage hero1;

private Hero hero = new Hero();
private FlyingObject[] flyings={};
private Bullet[] bullets={};

ShootGame(){
flyings = new FlyingObject[2];
flyings[0]=new Airplane();
flyings[1]=new Bee();
bullets = new Bullet[1];
bullets[0]=new Bullet(100,200);
}

static { //初始化静态资源
try{
background = ImageIO.read(ShootGame.class.getResource("background.png"));
start = ImageIO.read(ShootGame.class.getResource("start.png"));
pause = ImageIO.read(ShootGame.class.getResource("pause.png"));
gameover = ImageIO.read(ShootGame.class.getResource("gameover.png"));
airplane = ImageIO.read(ShootGame.class.getResource("airplane.png"));
bee = ImageIO.read(ShootGame.class.getResource("bee.png"));
bullet = ImageIO.read(ShootGame.class.getResource("bullet.png"));
hero0 = ImageIO.read(ShootGame.class.getResource("hero0.png"));
hero1 = ImageIO.read(ShootGame.class.getResource("hero1.png"));
}catch(Exception e){
e.printStackTrace();
}

}

public void paint(Graphics g){
g.drawImage(background,0,0,null);
paintHero(g);
paintFlyingObjects(g);
paintBullets(g);

}

public void paintHero(Graphics g){
g.drawImage(hero.image,hero.x,hero.y,null);
}
public void paintFlyingObjects(Graphics g){
for (int i=0;i<flyings.length;i++){
FlyingObject f = flyings[i];
g.drawImage(f.image,f.x,f.y,null);
}
}
public void paintBullets(Graphics g){
for (int i=0;i<bullets.length;i++){
Bullet b= bullets[i];
g.drawImage(b.image,b.x,b.y,null);
}
}

public static void main(String[] args) {
JFrame frame = new JFrame("Fly");
ShootGame game = new ShootGame();
frame.add(game);
frame.setSize(WIDTH, HEIGHT);
frame.setAlwaysOnTop(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);

}

}

 

 

2类 玩家飞机

 

package qiqi.shoot;
import java.awt.image.BufferedImage;
import java.util.Random;

public class Hero extends FlyingObject {
private int life;
private int doubleFire;
private BufferedImage[] images;
private int index;

public Hero(){
image=ShootGame.hero0;
width=image.getWidth();
height=image.getHeight();
x =150;
y=400;
life=3;
doubleFire=0;
images=new BufferedImage[]{ShootGame.hero0,ShootGame.hero1};
index=0;
}

}

 

 

3类  FlyingObject (所有飞行物的父类)

package qiqi.shoot;
import java.awt.image.BufferedImage;

/*
*
* 飞行物类*/
public class FlyingObject {
protected BufferedImage image;
protected int width;
protected int height;
protected int x;
protected int y;

}

 

4类 接口类 Enemy 

package qiqi.shoot;

public interface Enemy {
/*得分*/
public int getSorce();
}

 

package com.tarena.shoot;

import java.util.Random;

public class Bullet extends FlyingObject{
private int speed=3;

public Bullet(int x,int y){
image=ShootGame.bullet;
width=image.getWidth();
height=image.getHeight();
this.x=x;
this.y=y;
}

}

 

5类 小蜜蜂类

package qiqi.shoot;

import java.util.Random;

public class Bee extends FlyingObject implements Award {

private int xSpeed=1;
private int ySpeed=2;
private int awardType;

public Bee(){
image=ShootGame.bee;
width=image.getWidth();
height=image.getHeight();
Random rand= new Random();
x =rand.nextInt(ShootGame.WIDTH-this.width);
y=-this.height;
awardType=rand.nextInt(2);

}


public int getType(){
return awardType;
}
}

 

6类  奖励接口类

package qiqi.shoot;

/** 奖励*/
public interface Award {
public int DOUBLE_FIRE=0;
public int LIFE=1;
public int getType();

}

 

7类 敌机类

package qiqi.shoot;
import java.util.Random;

public class Airplane extends FlyingObject implements Enemy{

private int speed=2;

public Airplane(){
image=ShootGame.airplane;
width=image.getWidth();
height=image.getHeight();
Random rand= new Random();
x =rand.nextInt(ShootGame.WIDTH-this.width);
y=-this.height;

}

public int getSorce(){
return 5;
}

}

 

以上是关于打飞机游戏第一天,诸神归位的主要内容,如果未能解决你的问题,请参考以下文章

原生JS超级马里奥(第一天)

分享30个有趣的 Python小游戏,我能玩一天

分享30个有趣的 Python小游戏,我能玩一天(附源码)

30个Python小游戏,上班摸鱼我能玩一天内附源码

30个Python小游戏,上班摸鱼我能玩一天内附源码

30个Python小游戏,上班摸鱼我能玩一天内附源码