Aircraft war games

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
			public void mouseClicked(MouseEvent e) {
					hero = new Hero();
					//Clear enemy aircraft collection
					//Empty bullet collection
					Random rd = new Random();
					int index = (rd.nextInt(4)+1);
					bg = App.getImg("/img/bg"+index+".png");
			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();
					hero.moveToMouse(mx, my);
				//Refresh interface rewrite drawing
		//3. add the adapter to the listener

//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
	//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);
	//Bullet movement
	protected void fireMove() {
		for(int i=0;i<fs.size();i++){
			Fire f = fs.get(i);
	//Method of firing bullets
	int findex = 0;
	protected void shoot() {
				Fire fire2 = new Fire(hero.x+45,hero.y-20,1);
			}else if(power==2){
				Fire fire1 = new Fire(hero.x+10,hero.y,1);
				Fire fire3 = new Fire(hero.x+80,hero.y,1);
				Fire fire1 = new Fire(hero.x+10,hero.y,0);
				Fire fire2 = new Fire(hero.x+45,hero.y-20,1);
				Fire fire3 = new Fire(hero.x+80,hero.y,2);

3. data dictionary

4. collision mechanism

public boolean shootBy(Fire f) {
		boolean hit =   x<=f.x+f.w
		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.

Posted by zymosan on Wed, 01 Jun 2022 17:35:31 +0530