day one of the plane war
1. Abstract Class: Flying Object
public abstract class FlyingObject{ protected BufferedImage image; protected int width; //宽 protected int height; //高 protected int x; //x coordinate
protected int y;
//y coordinate}
2. Interface: Enemy
public interface Enemy { /** Score*/ public int getScore(); }
3. Interface: Reward
/** award*/ public interface Award { public int DOUBLE_FIRE = 0; //fire value public int LIFE = 1; //命 /** Get reward type (0 or 1 above) */ public int getType(); }
4. Enemy aircraft, flying objects, are also enemies
public class Airplane extends FlyingObject implements Enemy { private int speed = 2; // speed of movement /** Construction method*/ public Airplane(){ image = ShootGame.airplane; //图片 width = image.getWidth(); //宽 height = image.getHeight(); //高 Random rand = new Random(); //random number object x = rand.nextInt(ShootGame.WIDTH-this.width); //x: A random number between 0 and (window width - enemy aircraft width) y = -this.height; //y: the height of the negative enemy plane } /** Override getScore() score */ public int getScore(){ return 5; // 5 points for destroying an enemy plane }}
5. Little Bee: Flying objects are also rewards
/** Little bee: is a flying object, */ public class Bee extends FlyingObject implements Award { private int xSpeed = 1; //x coordinate movement speed private int ySpeed = 2; //y coordinate movement speed private int awardType; //Type of award (0 or 1) /** Construction method*/ public Bee(){ image = ShootGame.bee; //image width = image.getWidth(); //宽 height = image.getHeight(); //高 Random rand = new Random(); //random number object x = rand.nextInt(ShootGame.WIDTH-this.width); //x: A random number between 0 and (window width - bee width) y = -this.height; //y: the height of the negative bee awardType = rand.nextInt(2); //random number between 0 and 1 } /** Get reward type */ public int getType(){ return awardType; //return award type } }
6. Bullets: also flying objects
/** bullet: is a flying object */ public class Bullet extends FlyingObject { private int speed = 3; // speed of movement /** The initial coordinates of the construction method bullet are fixed with the hero machine */ public Bullet(int x,int y){ image = ShootGame.bullet; //image width = image.getWidth(); //宽 height = image.getHeight(); //高 this.x = x; //x: random hero this.y = y; //y: random hero machine }
7. Hero Machine: Flying Object
/** Hero machine: is a flying object */ public class Hero extends FlyingObject { private int doubleFire; //fire value private int life; //命 private BufferedImage[] images; //Switchable image array private int index; //Assist picture switching /** Construction method*/ public Hero(){ image = ShootGame.hero0; //image width = image.getWidth(); //宽 height = image.getHeight(); //高 x = 150; //x: fixed value y = 400; //y: fixed value doubleFire = 10000; //The default is 0 (single firepower) life = 3; //default 3 lives images = new BufferedImage[]{ShootGame.hero0,ShootGame.hero1}; //Switch between two images index = 0; //Assist picture switching } }
8. Main class: window
//Main window class public class ShootGame extends JPanel { public static final int WIDTH = 400; //Window width public static final int HEIGHT = 654; //Window height public static BufferedImage background; //background image public static BufferedImage start; //Startup image public static BufferedImage pause; //Pause the image public static BufferedImage gameover; //Game over image public static BufferedImage airplane; //敌机 public static BufferedImage bee; //Little bee public static BufferedImage bullet; //子弹 public static BufferedImage hero0; //Hero machine 0 public static BufferedImage hero1; //Hero machine 1 static{ //Initialize the static image 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 (); } } private Hero hero = new Hero(); //A hero machine private FlyingObject[] flyings = {}; //A bunch of enemies (enemy aircraft + little bees) private Bullet[] bullets = {}; //A bunch of bullets public static final int START = 0; //Start state public static final int RUNNING = 1; //Running state public static final int PAUSE = 2; //Pause state public static final int GAME_OVER = 3; //Game over state private int state = START; //current state (default startup state) public static void main(String[] args) { JFrame frame = new JFrame("Fly"); //Create a window object ShootGame game = new ShootGame(); //Create a panel object frame.add(game); //Add the panel to the window frame.setSize(WIDTH, HEIGHT); //Set the window size frame.setAlwaysOnTop(true); //The setting is always on top frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Set the default close operation of the window (exit the program when the window is closed) frame.setLocationRelativeTo(null); //Set the center display frame.setVisible(true); //1) Set the window to be visible 2) Call the paint() method as soon as possible game.action(); //Start the execution of the program } /** Override paint() g:brush */ public void paint(Graphics g){ g.drawImage(background,0,0,null); //Draw background image paintHero(g); //Draw the hero machine object paintFlyingObjects(g); //Draw the enemy (enemy + bee) object paintBullets(g); //Draw the bullet object paintScoreAndLife(g); //paint score and life paintState(g); //paint state } /** Draw the hero machine object */ public void paintHero(Graphics g){ g.drawImage(hero.image,hero.x,hero.y,null); //Draw the hero machine object } /** Draw the enemy (enemy + bee) object*/ public void paintFlyingObjects(Graphics g){ for(int i=0;i<flyings.length;i++){ // Traverse all enemies (enemy aircraft + bee) FlyingObject f = flyings[i]; //Get each enemy (enemy + small bee) g.drawImage(f.image,fx,fy,null); //Draw the enemy (enemy + bee) object } } /** draw the bullet object */ public void paintBullets(Graphics g){ for(int i=0;i<bullets.length;i++){ //traverse all bullets Bullet b = bullets[i]; //Get each bullet g.drawImage(b.image,bx,by,null); //Draw the bullet object } } }
9. The implementation steps of the enemy's entry:
1)main(){ game.action(); } 2)action(){ ... run(){ //10 millisecond timing execution enteredAction(); //The enemy enters the field repaint(); } } 3)int index = 0; enteredAction(){ //10ms index++; if(index%40==0){ //40*10ms FlyingObject one = nextOne(); //Create an enemy object flyings = Arrays.copyOf(flyings,flyings.length+1); //扩容 flyings[flyings.length-1] = one; //Add the enemy object to the enemy array } } 4)nextOne(){ Generate random numbers between 0 and 19 When it is 0, return new Bee(); else return new Airplane(); }