Java aircraft logging games (with complete source code) and java aircraft Logging
Preface
The technology comes from sharing, so today I am taking the time to sort out small games I have used java for your reference. Java is indeed not suitable for writing desktop applications. Here we only use this game to let everyone understand the process of oop object-oriented programming, which is purely entertaining. The code is easy to write, easy to understand, and the comments are clearly written. If there are still problems, you should make up your lessons in private.
Complete code: enemy aircraft
Import java. util. random; Enemy plane: it is a flying object and also an Enemy public class Airplane extends FlyingObject implements Enemy {private int speed = 3; // move steps/** initialize data */public Airplane () {this. image = ShootGame. airplane; width = image. getWidth (); height = image. getHeight (); y =-height; Random rand = new Random (); x = rand. nextInt (ShootGame. WIDTH-width);}/** get score */@ Override public int getScore () {return 5 ;} /** // cross-border processing */@ Override public boolean outOfBounds () {return y> ShootGame. HEIGHT;}/** move */@ Override public void step () {y + = speed ;}}Score rewards
/*** Reward */public interface Award {int DOUBLE_FIRE = 0; // double firepower int LIFE = 1; // 1 message/** reward type (0 or 1 above) */int getType ();}Bee
Import java. util. random;/** Bee */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; // reward type/** initialization data */public Bee () {this. image = ShootGame. bee; width = image. getWidth (); height = image. getHeight (); y =-height; Random rand = new Random (); x = rand. nextInt (ShootGame. WIDTH-width); awardType = rand. nextInt (2); // reward at initialization}/** reward type */public int getType () {return awardType ;} /** cross-border processing */@ Override public boolean outOfBounds () {return y> ShootGame. HEIGHT;}/** move, fly diagonally */@ Override public void step () {x + = xSpeed; y + = ySpeed; if (x> ShootGame. WIDTH-width) {xSpeed =-1;} if (x <0) {xSpeed = 1 ;}}}Bullets: Flying Objects
/*** Bullet class: flying object */public class Bullet extends FlyingObject {private int speed = 3; // speed of movement/** initialization data */public Bullet (int x, int y) {this. x = x; this. y = y; this. image = ShootGame. bullet;}/** move */@ Override public void step () {y-= speed;}/** cross-border processing */@ Override public boolean outOfBounds () {return y <-height ;}}Enemy score
/*** Enemies, scores available */public interface Enemy {/** Enemy scores */int getScore ();}Flight object (enemy plane, Bee, bullet, hero machine)
Import java. awt. image. bufferedImage;/*** flying object (enemy, Bee, bullet, Hero) */public abstract class FlyingObject {protected int x; // x coordinate protected int y; // y coordinate protected int width; // width protected int height; // high protected BufferedImage image; // picture public int getX () {return x ;} public void setX (int x) {this. x = x;} public int getY () {return y;} public void setY (int y) {this. y = y;} public int getWidth () {return width;} public void setWidth (int width) {this. width = width;} public int getHeight () {return height;} public void setHeight (int height) {this. height = height;} public BufferedImage getImage () {return image;} public void setImage (BufferedImage image) {this. image = image;}/*** check whether it is out of bounds * @ return true or not */public abstract boolean outOfBounds (); /*** move a flying object step */public abstract void step ();/*** check whether the current flying object is hit by a bullet (x, y) (shoot) * @ param Bullet object * @ return true indicates the object has been hit */public boolean shootBy (bullet) {int x = Bullet. x; // The x coordinate of the bullet int y = bullet. y; // return this. x <x & x <this. x + width & this. y <y & y <this. y + height ;}}Hero Machine
Import java. awt. image. bufferedImage;/*** Hero: flying object */public class Hero extends FlyingObject {private BufferedImage [] images ={}; // Hero machine image private int index = 0; // switch the index private int doubleFire on the Hero machine image; // double firepower private int life; // life/** initialization data */public Hero () {life = 3; // three initial doubleFire = 0; // The initial fire is 0 images = new BufferedImage [] {ShootGame. hero0, ShootGame. hero1}; // Hero image array image = ShootGame. hero0; // The image width is image. getWidth (); height = image. getHeight (); x = 150; y = 400;}/** get double firepower */public int isDoubleFire () {return doubleFire ;} /** set double firepower */public void setDoubleFire (int doubleFire) {this. doubleFire = doubleFire;}/** add firepower */public void addDoubleFire () {doubleFire = 40;}/** add life */public void addLife () {// life + +;}/** life reduction */public void subtractLife () {// life reduction --;} /** get life */public int getLife () {return life;}/** the current object has been moved, relative distance, x, y cursor position */public void moveTo (int x, int y) {this. x = x-width/2; this. y = y-height/2;}/** cross-border processing */@ Override public boolean outOfBounds () {return false ;} /** fired bullets */public Bullet [] shoot () {int xStep = width/4; // 4 half int yStep = 20; // step if (doubleFire> 0) {// double-Fire Bullet [] bullets = new Bullet [2]; bullets [0] = new Bullet (x + xStep, y-yStep ); // y-yStep (The position of the Bullet from the plane) bullets [1] = new Bullet (x + 3 * xStep, y-yStep); return bullets ;} else {// Bullet [] bullets = new Bullet [1]; bullets [0] = new Bullet (x + 2 * xStep, y-yStep ); return bullets ;}}/** move */@ Override public void step () {if (images. length> 0) {image = images [index ++/10% images. length]; // switch between images hero0, hero1}/** collision algorithm */public boolean hit (FlyingObject other) {int x1 = other. x-this. width/2; // the minimum distance between x and int x2 = other. x + this. width/2 + other. width; // the maximum distance between x and int y1 = other. y-this. height/2; // min distance from y coordinate int y2 = other. y + this. height/2 + other. height; // the maximum distance between y and int herox = this. x + this. width/2; // distance from x coordinate center of hero machine to int heroy = this. y + this. height/2; // return herox> x1 & herox <x2 & heroy> y1 & heroy <y2; // hit within the range }}Main game startup class
Import java. awt. font; import java. awt. color; import java. awt. graphics; import java. awt. event. mouseAdapter; import java. awt. event. mouseEvent; import java. util. arrays; import java. util. random; import java. util. timer; import java. util. timerTask; import java. awt. image. bufferedImage; import javax. imageio. imageIO; import javax. swing. imageIcon; import javax. swing. JFrame; import javax. swing. JPanel; public Class ShootGame extends JPanel {public static final int WIDTH = 400; // panel WIDTH public static final int HEIGHT = 654; // panel HEIGHT/** current game status: start running pause GAME_OVER */private int state; private static final int START = 0; private static final int RUNNING = 1; private static final int PAUSE = 2; private static final int GAME_OVER = 3; private int score = 0; // score private Timer timer; // Timer privat E int intervel = 1000/100; // time interval (MS) public static BufferedImage background; public static BufferedImage start; public static BufferedImage airplane; public static BufferedImage bullet; public static BufferedImage hero0; public static BufferedImage hero1; public static BufferedImage pause; public static BufferedImage gameover; private FlyingObject [] flyings = {}; // Enemy array private Bullet [] bullets ={}; // array of bullets private Hero hero = new Hero (); // The static code block of the Hero machine {// static code block, initialize image resource try {background = ImageIO. read (ShootGame. class. getResource ("background.png"); start = ImageIO. read (ShootGame. class. getResource ("start.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"); pause = ImageIO. read (ShootGame. class. getResource ("pause.png"); gameover = ImageIO. read (ShootGame. class. getResource ("gameover.png");} catch (Exception e) {e. printStackTrace () ;}/ ** image */@ Override public void Paint (Graphics g) {g. drawImage (background, 0, 0, null); // draw the background paintHero (g); // draw the hero machine paintBullets (g); // draw the bullet paintFlyingObjects (g ); // draw the flying object paintScore (g); // draw the score paintState (g); // draw the game status}/** draw the hero machine */public void paintHero (Graphics g) {g. drawImage (hero. getImage (), hero. getX (), hero. getY (), null);}/** draw bullet */public void paintBullets (Graphics g) {for (int I = 0; I <bullets. length; I ++) {Bulle T B = bullets [I]; g. drawImage (B. getImage (), B. getX ()-B. getWidth ()/2, B. getY (), null) ;}}/** image flying object */public void paintFlyingObjects (Graphics g) {for (int I = 0; I <flyings. length; I ++) {FlyingObject f = flyings [I]; g. drawImage (f. getImage (), f. getX (), f. getY (), null) ;}/ ** draw score */public void paintScore (Graphics g) {int x = 10; // x coordinate int y = 25; // y coordinate Font font = new Font (Font. SANS_SER IF, Font. BOLD, 22); // font g. setColor (new Color (0xFF0000); g. setFont (font); // set the font g. drawString ("SCORE:" + score, x, y); // SCORE y = y + 20; // y coordinates increase by 20g. drawString ("LIFE:" + hero. getLife (), x, y); // draw life}/** draw game status */public void paintState (Graphics g) {switch (state) {case START: // start status g. drawImage (start, 0, 0, null); break; case PAUSE: // PAUSE status g. drawImage (pause, 0, 0, null); break; case GAME_OVER: // Game termination status g. drawImage (gameover, 0, 0, null); break ;}} public static void main (String [] args) {JFrame frame = new JFrame ("Fly "); shootGame game = new ShootGame (); // frame of the Panel object. add (game); // add the panel to the JFrame frame. setSize (WIDTH, HEIGHT); // you can specify the frame size. setAlwaysOnTop (true); // you can specify the top frame. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); // The frame operation is disabled by default. setIconImage (new ImageIcon ("images/icon.jpg" ). GetImage (); // sets the icon frame of the form. setLocationRelativeTo (null); // sets the initial frame position of the form. setVisible (true); // call the paint game as soon as possible. action (); // start execution}/** start Execution Code */public void action () {// mouse listener event MouseAdapter l = new MouseAdapter () {@ Override public void mouseMoved (MouseEvent e) {// move the mouse if (state = RUNNING) {// move the hero machine in the RUNNING state -- int x = e with the mouse position. getX (); int y = e. getY (); hero. moveTo (x, y) ;}@override public vo Id mouseEntered (MouseEvent e) {// enter if (state = PAUSE) {// run state = RUNNING;} @ Override public void mouseExited (MouseEvent e) in the paused state) {// exit if (state = RUNNING) {// if the game is not over, set it to suspend state = PAUSE; }}@ Override public void mouseClicked (MouseEvent e) {// click switch (state) {case START: state = RUNNING; // run break in startup state; case GAME_OVER: // the game ends, clear on-site flyings = new FlyingObject [0]; // clear Flying Object bullets = new Bullet [0]; // clear the Bullet hero = new Hero (); // recreate the hero machine score = 0; // clear the score state = START; // The status is set to start break; }}; this. addMouseListener (l); // process the mouse and click this. addMouseMotionListener (l); // handle the mouse sliding operation timer = new Timer (); // master Flow Control timer. schedule (new TimerTask () {@ Override public void run () {if (state = RUNNING) {// run Status enterAction (); // stepAction () for flying object entry (); // take the shootAction () step by step; // The Hero shot Strike bangAction (); // The outOfBoundsAction () of the bullet flying object; // Delete the cross-border flying object and the bullet checkGameOverAction (); // check the game end} repaint (); // re-paint, call the paint () method }}, intervel, intervel);} int flyEnteredIndex = 0; // air object entry count/** air object entry */public void enterAction () {flyEnteredIndex ++; if (flyEnteredIndex % 40 = 0) {// generates a flying object in milliseconds -- 10*40 FlyingObject obj = nextOne (); // randomly generate a flying object flyings = Arrays. copyOf (flyings, flyings. length + 1); Flyings [flyings. length-1] = obj ;}}/** take a step */public void stepAction () {for (int I = 0; I <flyings. length; I ++) {// take the FlyingObject f = flyings [I]; f. step () ;}for (int I = 0; I <bullets. length; I ++) {// take the Bullet step Bullet B = bullets [I]; B. step ();} hero. step (); // take one step on the hero machine}/** take one step on the flying object */public void flyingStepAction () {for (int I = 0; I <flyings. length; I ++) {FlyingObject f = flyings [I]; f. step () ;}} int shootIndex = 0; // shooting count/** shooting */public void shootAction () {shootIndex ++; if (shootIndex % 30 = 0) {// send a Bullet [] bs = hero in 300 milliseconds. shoot (); // The hero plays the bullet bullets = Arrays. copyOf (bullets, bullets. length + bs. length); // resize the System. arraycopy (bs, 0, bullets, bullets. length-bs. length, bs. length); // append array}/** collision detection between bullets and flying objects */public void bangAction () {for (int I = 0; I <bullet S. length; I ++) {// traverse all bullets Bullet B = bullets [I]; bang (B ); // collision check between bullets and flying objects}/** Delete cross-border flying objects and bullets */public void outOfBoundsAction () {int index = 0; // index FlyingObject [] flyingLives = new FlyingObject [flyings. length]; // a living flying object for (int I = 0; I <flyings. length; I ++) {FlyingObject f = flyings [I]; if (! F. outOfBounds () {flyingLives [index ++] = f; // keep out of bounds} flyings = Arrays. copyOf (flyingLives, index); // keep index = 0 for all flying objects that do not cross the border; // reset the index to 0 Bullet [] bulletLives = new Bullet [bullets. length]; for (int I = 0; I <bullets. length; I ++) {Bullet B = bullets [I]; if (! B. outOfBounds () {bulletLives [index ++] = B ;}} bullets = Arrays. copyOf (bulletLives, index); // keep the bullets that do not cross the border}/** check the end of the game */public void checkGameOverAction () {if (isGameOver () = true) {state = GAME_OVER; // change status}/** check whether the game is over */public boolean isGameOver () {for (int I = 0; I <flyings. length; I ++) {int index =-1; FlyingObject obj = flyings [I]; if (hero. hit (obj) {// check whether the hero and flying object collide with hero. subtra CtLife (); // hero. setDoubleFire (0); // double Fire Relief index = I; // record the flying object index} if (index! =-1) {FlyingObject t = flyings [index]; flyings [index] = flyings [flyings. length-1]; flyings [flyings. length-1] = t; // exchange flyings = Arrays with the last flying object. copyOf (flyings, flyings. length-1); // Delete the flying object} return hero. getLife () <= 0;}/** collision check between bullets and flying objects */public void bang (Bullet bullet) {int index =-1; // index of the hit flight object for (int I = 0; I <flyings. length; I ++) {FlyingObject obj = flyings [I]; if (obj. ShootBy (bullet) {// determine whether to hit index = I; // record the index break of the hit flying object;} if (index! =-1) {// The FlyingObject one that has been hit = flyings [index]; // record the FlyingObject temp = flyings [index]; // exchange flyings [index] = flyings [flyings. length-1]; flyings [flyings. length-1] = temp; flyings = Arrays. copyOf (flyings, flyings. length-1); // Delete the last flying object (I .e. hit) // check the type of one (Enemy points, rewards) if (one instanceof Enemy) {// check type. If it is an Enemy, add Enemy e = (Enemy) one. // force type conversion score + = e. getScore (); // extra score} else {// if it is a reward, set Award a = (Award) one; int type =. getType (); // obtain the reward type switch (type) {case Award. DOUBLE_FIRE: hero. addDoubleFire (); // set double-fire break; case Award. LIFE: hero. addLife (); // set the life-saving break; }}}/*** randomly generated flying object ** @ return flying object */public static FlyingObject nextOne () {Random random = new Random (); int type = random. nextInt (20); // [0, 20) if (type <4) {return new Bee () ;}else {return new Airplane ();}}}Conclusion
The above is the complete code I have compiled for this game. Because there are about nine images, the images are not uploaded. If you need images, please trust me briefly. Finally, I posted a mind map to help you better understand the process of OOP object-oriented programming.
Ps: the code word is very tired. You can give a thumbs up or comment. Thank you ~~ The resource has been uploaded (including images). Please stamp it here
I have a public account, and I often share some Java-related dry goods. If you like my share, you can search for "Java head" or "javatuanzhang" to follow up.