The function of the game: the player operates the aircraft through the mouse, and the aircraft automatically launches bullets. The system randomly generates the enemy aircraft, and detects whether the bullets collide with the enemy aircraft and whether the hero aircraft collides with the enemy aircraft; Whether the hero plane is dead; The system updates the score at any time. When the game is over, it will remind you to click the screen to start the game
Knowledge points used:
java swing
Multithreading
aggregate
java aircraft war programming idea:
Write game panel and form ------------ write App tool class to read photos ----------- add background image in the panel---------
The panel uses the drawing method to draw pictures------------------------------
Write FlyObject class, in which the member variables represent the picture, width, height, ordinate and abscissa---------------
Creation and movement of hero machine:
Write the hero machine class, inherit the FlyObject class, and set the initial position, size and blood volume of the hero machine--------------------
Use the drawing method in the panel to draw the hero machine, so as to display the hero machine in the panel-------------------------
Customize the moveToMouse method, let the hero machine move with the mouse ------------ add a mouse listener in the form, and let the hero machine register a listener-------------
The hero machine calls the moveToMouse method to realize the function of the hero machine moving with the mouse
Creation and movement of enemy aircraft:
Write the enemy aircraft class, inherit the FlyObject class, and set the size and initial blood volume of the enemy aircraft----------------------------
The ordinate of the enemy aircraft is uniformly set as - h, and the abscissa is determined by rd.nextInt(1024-w) method, so that the enemy aircraft appear randomly---------------------------------
Set different speeds for different enemy planes to make their falling speeds different
Use the set in the panel to create the base camp of the enemy aircraft, namely: List < EP > EPS = new ArrayList < EP > ()-----------
Use the drawing method in the panel to traverse the whole base camp, draw the enemy aircraft and display the enemy aircraft-----------------------------------
Bullet collides with enemy aircraft, hero collides with enemy aircraft:
Write the bullet class, inherit the FlyObject class, and set the size of the bullet-------------------
In the panel, use the set to create the powder magazine, that is: List < fire > FS = new ArrayList < fire > ();
In the construction method, the coordinates of the bullet and the hero machine are the same---------------
Customize the move method to fix the speed and direction of bullet movement --------------------- use the drawing method in the panel to draw bullets and display bullets in the panel----
Set the initial firing of one bullet, gradually increase the bullets until three bullets, and each bullet moves at a different speed-------------------------------------
Conduct collision detection between bullets and enemy aircraft. The collision detection formula is Boolean hit = (x < = f.x + F.W) & & (x > = f.x-w) & & (y < = f.y + F.H) & & (Y > = f.y-h);
Judge whether the bullet collides with the enemy aircraft. If so, call the remove() method to remove the bullet, and if the enemy aircraft's blood volume is < = 0, remove the enemy aircraft----------------
Use a similar method to judge whether the enemy aircraft collides with the hero aircraft. If so, remove the enemy aircraft, and the blood volume of the hero aircraft is - 1. When the blood volume of the hero aircraft is < = 0, the game ends
Start the game:
Set the initial score to 0, and the score will be + 10 for each enemy plane knocked down
Set the boolean variable gameover to false as the switch of the game. When gameover is true, the game ends
Using single thread, the fixed format is public void action() {new thread() {} start();}, In which the while loop runs the game
When the hero machine dies, it will prompt "the game is over, click the screen to restart", call the mouseCliked(Event e) method, monitor the mouse click, and restart the game if the mouse click
And when you restart the game, change the background randomly
Code implementation:
import java.awt.*; import javax.swing.*; import javax.imageio.*; import java.awt.event.*; import java.awt.image.BufferedImage; import java.io.*; //Tool class for processing pictures public class App{ //Read the picture at the specified location public static BufferedImage getImg(String path){ try{ BufferedImage img = ImageIO.read(App.class.getResource(path)); return img; } catch(IOException e){ e.printStackTrace(); } return null; } }
import java.awt.image.*; import java.util.Random; import java.util.List; import java.awt.*; public class Ep extends FlyObject{ int speed; int hp = 3; int type; public Ep(){ Random rd = new Random(); int index = rd.nextInt(12)+1; type = index;//Prop machine img = App.getImg("/Img/ep"+index+".PNG"); w = img.getWidth(); h = img.getHeight(); x = rd.nextInt(1024-w); y = -h; speed = 13 - index; } public void move(){ if(type==5){ y += speed/2; x += 2; } else if(type==6){ y += speed/2; x -= 2; } else{ y += speed; } } public boolean shootBy(Fire f){ boolean hit = (x<=f.x+f.w) && (x>=f.x-w) && (y<=f.y+f.h) && (y>=f.y-h); return hit; } public boolean hitBy(Hero hero){ boolean hit = (x<=hero.x+hero.w) && (x>=hero.x-w) && (y<=hero.y+hero.h) && (y>=hero.y-h); return hit; } }
public class Fire extends FlyObject{ int dir;//Direction of bullet movement //dir=0 top left corner //dir=1 Fly up vertically //dir=2 Upper right corner public Fire(int hx, int hy, int dir){ img = App.getImg("/img/fire.PNG"); w = img.getWidth(); h = img.getHeight(); //Locate the bullet x = hx; y = hy; this.dir = dir; } public void move(){ if(dir==0){ y -= 10; x -= 1; } else if(dir==1){ y -= 10; } else if(dir==2){ y -= 10; x += 1; } } }
import java.awt.image.*; //In development, the same characteristics are extracted to form parent classes, so as to improve the reusability of code public class FlyObject{ BufferedImage img; int x;//Abscissa int y;//Ordinate int w;//width int h;//height }
import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.awt.image.*; public class GameFrame extends JFrame{ public GameFrame(){ setTitle("Aircraft war"); setSize(1024, 768);//1024 wide and 768 high //The default interface is centered setLocationRelativeTo(null); //Players are not allowed to change the page size setResizable(false); //Set default shutdown options setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String args[]){ GameFrame frame = new GameFrame(); GamePanel panel = new GamePanel(frame); panel.action(); frame.add(panel);//Add panel to form frame.setVisible(true); } }
import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.awt.image.*; import java.util.List; import java.util.ArrayList; import java.util.Random; public class GamePanel extends JPanel{ BufferedImage bg;//Background map Hero hero = new Hero(); //Ep ep = new Ep(); List<Ep> eps = new ArrayList<Ep>(); List<Fire> fs = new ArrayList<Fire>(); //Define score int score = 0; boolean gameover = false; int power = 1;//The firepower of the hero plane public void action(){ //Create and start a thread to control the activity of objects in the game scene new Thread(){ public void run(){ //Write an endless loop while(true){ if(!gameover){ epEnter(); epMove(); shoot(); fireMove(); shootEp(); hit(); try{ Thread.sleep(10); }catch(InterruptedException e){ e.printStackTrace(); } repaint(); } } } }.start(); } protected void hit(){ for(int i=0;i<eps.size();i++){ Ep e = eps.get(i); if(e.hitBy(hero)){ eps.remove(e); hero.hp--; score += 10; power = 1; if(hero.hp<=0){ gameover = true; } } } } protected void shootEp(){ //Traverse all bullets for(int i=0;i<fs.size();i++){ Fire f = fs.get(i); bang(f); } } //Determine whether the bullet hit the enemy plane private void bang(Fire f){ //Traverse all enemy aircraft for(int i=0;i<eps.size();i++){ Ep e = eps.get(i); //Determine whether the bullet hit the enemy plane if(e.shootBy(f)){ e.hp--; if(e.hp<=0){ if(e.type==8){ power++; if(power > 3){ hero.hp++; power = 3; if(hero.hp > 3){ hero.hp = 3; } } } eps.remove(e); score += 10; } fs.remove(f); } } } protected void fireMove(){ for(int i=0;i<fs.size();i++){ Fire fire = fs.get(i); fire.move(); } } int index1 = 0; protected void shoot(){ index1 ++; if(index1>=20){ if(power == 1){ Fire fire2 = new Fire(hero.x-20, hero.y-20, 1); fs.add(fire2); }else if(power == 2){ Fire fire1 = new Fire(hero.x-50, hero.y, 0); fs.add(fire1); Fire fire2 = new Fire(hero.x-20, hero.y-20, 1); fs.add(fire2); }else{ Fire fire1 = new Fire(hero.x-50, hero.y, 0); fs.add(fire1); Fire fire2 = new Fire(hero.x-20, hero.y-20, 1); fs.add(fire2); Fire fire3 = new Fire(hero.x+10, hero.y, 2); fs.add(fire3); } index1 = 0; } } protected void epMove(){ for(int i=0;i<eps.size();i++){ Ep e = eps.get(i); e.move(); } } int index = 0; protected void epEnter(){ index ++; if(index>=20){ Ep e = new Ep(); eps.add(e); index = 0; } } public GamePanel(GameFrame frame){ //Set background setBackground(Color.green); //Initialize picture bg = App.getImg("/Img/start.BMP"); //Add mouse adapter MouseAdapter adapter = new MouseAdapter(){ public void mouseClicked(MouseEvent e){ if(gameover){ //Restart the game hero = new Hero(); gameover = false; score = 0; eps.clear(); fs.clear(); //Random background map Random rd = new Random(); int index = rd.nextInt(6)+1; bg = App.getImg("/img/bg"+index+".BMP"); power = 1; action(); } } public void mouseMoved(MouseEvent e){ int mx = e.getX(); int my = e.getY(); if(!gameover){ hero.moveToMouse(mx, my); //Refresh interface repaint(); } } }; //Add adapter to listener (fixed format) addMouseListener(adapter); addMouseMotionListener(adapter); //Using the keyboard listener (fixed format) KeyAdapter kd = new KeyAdapter(){ public void keyPressed(KeyEvent e){ int keyCode = e.getKeyCode(); if(!gameover){ if(keyCode == KeyEvent.VK_UP){ hero.moveUP(); }else if(keyCode == KeyEvent.VK_DOWN){ hero.moveDOWN(); }else if(keyCode == KeyEvent.VK_LEFT){ hero.moveLEFT(); }else if(keyCode == KeyEvent.VK_RIGHT){ hero.moveRIGHT(); } repaint(); } } }; //Add adapter to form listener frame.addKeyListener(kd); } //Special drawing method //Graphics g brush public void paint(Graphics g){ super.paint(g); //Draw picture g.drawimage (picture, abscissa, ordinate, null) g.drawImage(bg, 0, 0, 1024, 768, null); g.drawImage(hero.img, hero.x, hero.y, hero.w, hero.h, null); for(int i=0;i<eps.size();i++){ Ep ep = eps.get(i); g.drawImage(ep.img, ep.x, ep.y, ep.w, ep.h, null); } for(int i=0;i<fs.size();i++){ Fire fire = fs.get(i); g.drawImage(fire.img, fire.x, fire.y, fire.w, fire.h, null); } g.setColor(Color.white); g.setFont(new Font("Regular script", Font.BOLD, 20)); g.drawString("fraction:"+score, 10, 30); //Draw the blood volume of the hero machine for(int i=0;i<hero.hp;i++){ g.drawImage(hero.img, 350+i*35, 5, 30, 30, null); } //game over if(gameover == true){ g.setColor(Color.black); g.setFont(new Font("Regular script", Font.BOLD, 35)); g.drawString("GAMEOVER! Vegetable chicken! Chicken, you are so beautiful", 20, 300); g.setColor(Color.red); g.setFont(new Font("Regular script", Font.BOLD, 35)); g.drawString("Click the screen to restart the game, sand sculpture", 40, 350); } } }
import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.awt.image.*; public class Hero extends FlyObject{ int hp; public Hero(){ img = App.getImg("/Img/me3.PNG"); x = 500; y = 500; w = img.getWidth(); h = img.getHeight(); hp = 3;//Hero machine has three lives } public void moveToMouse(int mx, int my){ x = mx-w/2; y = my-h/2; } public void moveUP(){ y = y - 10; } public void moveDOWN(){ y = y + 10; } public void moveLEFT(){ x = x - 10; } public void moveRIGHT(){ x = x + 10; } }