Aircraft war games
1. function description
(1) Game interface drawing: set the name of the game as "aircraft war games", and the size of the game window is 512*768 pixels, which is displayed in the middle of the screen; Add a game background. When a game is restarted, one of the uploaded four backgrounds will be randomly displayed as a battle scene.
(2) Mouse movement reading and discrimination: create a mouse monitor, create a mouse adapter, monitor the coordinates of the mouse movement, assign the coordinates of the mouse to the hero machine, so as to realize the mouse operation of the hero machine to move in the screen.
(3) Judge whether the bullet hit the enemy aircraft: judge whether the bullet hit the enemy aircraft by the location of the bullet and the location of the enemy aircraft, so as to reduce the blood volume of the enemy aircraft and judge whether it can kill the enemy aircraft.
(4) Discrimination of whether the hero aircraft collides with the enemy aircraft: the discrimination method is the same as that of the bullet and the enemy aircraft. Whether the hero aircraft collides is judged by their position relationship. After the collision, the hero aircraft's life value is reduced by one, the number of bullets is initially 1, and the enemy aircraft disappears.
(5) Fighter and missile drawing: add fighter and missile materials to randomly generate enemy aircraft and missiles. For large enemy aircraft with large pixels, its movement speed will slow down. For enemy aircraft with small pixels, its movement speed will increase and its direction can be changed. The missile is slow and falls vertically, but it cannot be killed.
(6) Score display: the score is drawn in the upper left corner. Each time an enemy aircraft is killed, 10 points will be increased. However, the difficulty of killing the three types of enemy aircraft is different. Small enemy aircraft move fast and are not easy to aim. Large enemy aircraft have thick HP and are not easy to kill. When a large enemy aircraft is killed, the score will be doubled.
(7) Life value display: the life value of the hero machine is displayed in the upper right corner, which is represented by the number of icons of the hero machine. When the hero machine collides with the enemy machine, its life value is reduced by one, and when it collides with the missile, its life value is reduced by one. If there is no life value, the game ends and jumps to the game end interface.
2. system flow chart and some codes
public GamePanel(){ bg = App.getImg("/img/bg4.png"); //Using the mouse listener // 1. create a mouse adapter MouseAdapter adapter = new MouseAdapter() { //2. determine the events of the mouse to be monitored // Mouse events // (1) mouseMoved() listens for mouse movement // (2) mouseCliked() listens for mouse click events // (3) mousePressed() listens for mouse down events // (4) mouseEntered() listens to the event that the mouse moves into the game interface // (5) mouseExited() monitors the event that the mouse moves out of the game interface // MouseEvent mouse event, which records what the mouse does @Override public void mouseClicked(MouseEvent e) { if(gameover){ hero = new Hero(); action(); gameover=false; score=0; //Clear enemy aircraft collection eps.clear(); //Empty bullet collection fs.clear(); Random rd = new Random(); int index = (rd.nextInt(4)+1); bg = App.getImg("/img/bg"+index+".png"); repaint(); } } @Override public void mouseMoved(MouseEvent e) { //Make the horizontal and vertical coordinates of the hero machine equal to the horizontal and vertical coordinates of the mouse int mx = e.getX(); int my = e.getY(); if(!gameover){ hero.moveToMouse(mx, my); } //Refresh interface rewrite drawing repaint(); } }; //3. add the adapter to the listener addMouseListener(adapter); addMouseMotionListener(adapter); }
//Check whether the bullets hit the enemy aircraft protected void shootEp() { for(int i=0;i<fs.size();i++){ Fire f = fs.get(i); //Each time you take a bullet, judge whether the bullet hits the enemy aircraft bang(f); } } //Determine whether the bullet hit the enemy aircraft private void bang(Fire f) { for(int i=0;i<eps.size();i++){ Ep e = eps.get(i); if(e.shootBy(f)){ e.hp--; if(e.hp<=0){ if(e.type==4){ power++; if(power>3){ score+=10; power=3; } } eps.remove(e); score+=10; } fs.remove(f); } } } //Bullet movement protected void fireMove() { for(int i=0;i<fs.size();i++){ Fire f = fs.get(i); f.move(); } } //Method of firing bullets int findex = 0; protected void shoot() { findex++; if(findex>=30){ if(power==1){ Fire fire2 = new Fire(hero.x+45,hero.y-20,1); fs.add(fire2); }else if(power==2){ Fire fire1 = new Fire(hero.x+10,hero.y,1); fs.add(fire1); Fire fire3 = new Fire(hero.x+80,hero.y,1); fs.add(fire3); }else{ Fire fire1 = new Fire(hero.x+10,hero.y,0); fs.add(fire1); Fire fire2 = new Fire(hero.x+45,hero.y-20,1); fs.add(fire2); Fire fire3 = new Fire(hero.x+80,hero.y,2); fs.add(fire3); } findex=0; } }
3. data dictionary
4. collision mechanism
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; }
5. operation screenshot
Note: background pictures can be designed by yourself. The first java game assignment... Please give us some advice.
To improve and perfect the future of the game: (1) continue to optimize the collision mechanism to achieve more accuracy; (2) When the bullet contacts the enemy aircraft, it will produce an explosive effect; (3) It can increase the Boss enemy aircraft and display the health of the Boss enemy aircraft; (4) For some enemy aircraft, add the function of launching bullets, add props to the enemy aircraft, and take props after killing (5) add the background music of the game and the music of killing the enemy aircraft, etc.