Class aircraft war games breakthrough version

1, Game Description

The player controls the movement of the aircraft by moving the mouse (the aircraft is always on the left, and the monster appears on the right). Click the left mouse button to launch shells. If the shells hit the monster, the monster will explode. The player will get one point. After the player's cumulative score reaches the specified score in the first level, he can pass the level. In the second level, he needs to defeat the boss to pass. When the player passes the second level, the player's local score will be displayed, and the ranking list of the 10 best scores in history will be updated and displayed. If the player is hit by a monster or boss skill during the game, the player fails and needs to start the challenge again. Players can continue to challenge the second level only after passing the first level. If the player suddenly needs to leave in the game and wants to continue playing when he has time next time, he can click the exit and archive button to archive the game. The file will be automatically read when opening the game next time, that is, continue the last game.  

2, Overall frame (in parentheses represents the object for which it was created)

Program entry and window: PlaneWar classes (Ball, Monster, BossThread, GameSaved, Mouse, MovePlane, MyThread, WipeThread).

Monster explosion: BlastThread (Fragment).

Boss skill release: boss thread (Ball).

Game Archive: GameSave.

Shell launch: Mouse (Ball).

Aircraft movement: MovePlane.

Program core: MyThread(BossThread, BlastThread).

Monster generation: WipeThread.

Other classes: Ball class is shell class, Monster class is Monster class, Fragment is Monster explosion Fragment class, and Parent is the Parent class of Ball class, Monster class and Fragment class.

3, Concrete implementation

3.1 Parent class

Note: Parent class is the Parent class of Fragment class, Ball class and Monster class to realize code reuse, which has no practical significance in the game.

//The parent class of Ball class and Monster class, code reuse, has no practical significance
public class Parent 
{
	 protected int x,y;
	
	 
	 public void setXY(int x,int y)
	 {
	     this.x=x;
	     this.y=y;
	 }
	 
	 public int getX()
	 {
	     return x;
	 }
	 
	 public int getY()
	 {
	     return y;
	 }
}

3.2 Ball type

Note: a Ball class object represents a shell. The properties and methods of this class are inherited from the Parent class. The attribute is the abscissa and ordinate of the shell.

3.3 Monster class

Description: a Monster class object represents a Monster. The properties and methods of this class are inherited from the Parent class. The attribute is the horizontal and vertical coordinates of the Monster.

3.4 Fragment class

Description: a Fragment class object represents a monster Fragment. The attributes are the abscissa and ordinate of the Fragment (inherited from the Parent class), the color and size of the Fragment.

//Debris after monster explosion
public class Fragment extends Parent
{
    private int size,color;//Fragment size and color
    
    
    //Initialize fragments
    public Fragment(int x,int y,int c)
    {
    	this.x=x;
    	this.y=y;
    	color=c;
    	Random r=new Random();
    	size=r.nextInt(10);
    }
    
    public int getSize()
    {
    	return size;
    }
    
    public int getColor()
    {
    	return color;
    }
}

3.5 BlastThread class

Description: BlastThread class is the explosion thread of the monster to realize the explosion effect of the hit monster.

3.5.1 properties

private int x0,y0;//Picture coordinates of explosion required
	private Graphics g;

3.5.2 obtaining debris

Implementation idea: create a Fragment object every 15 pixels for the monster picture that needs to produce explosion effect, and the initial coordinates and colors of the object are the same as those of the corresponding pixels. All fragments are added to the Fragment queue.

//Get all fragments and store them in alf queue
    	for(int i=0;i<img.getWidth();i++)
    	{
    		for(int j=0;j<img.getHeight();j=j+15)
    		{
    		    Fragment f=new Fragment(i,j,img.getRGB(i, j));
    		    alf.add(f);
    		}
    	}

3.5.3 debris movement

Implementation idea: move and display the fragments every certain time, and repeat 100 times. The fragment movement is realized by adding a random number to the original coordinates of the fragment and subtracting a constant. Draw all fragments to the buffer before each display of fragments, and then draw the pictures in the buffer into the window.

//Move and display the fragments every certain time and repeat 100 times
    	for(int i=0;i<100;i++)
    	{
    		try
    		{
    		    sleep(10);
    		}
    		catch(InterruptedException e)
    	 	{}
            BufferedImage bi=new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
			Graphics  br=bi.getGraphics(); 
    	    for(int j=0;j<alf.size();j++)
    	    {
        	    alf.get(j).setXY(alf.get(j).getX()+r.nextInt(11)-5,alf.get(j).getY()+r.nextInt(11)-5);
        	    br.setColor(new Color(alf.get(j).getColor()));
        	    br.fillOval(alf.get(j).getX(), alf.get(j).getY(), alf.get(j).getSize(), alf.get(j).getSize());
        	    
    	    }
    	    g.drawImage(bi, x0, y0, null);
    	}

3.6 BossThread class

Note: the BossThread class is used to release the skills of the boss (there are two skills of the boss. The first is that the boss launches 11 shells and moves like an aircraft at different slopes at the same time, and the second is that 10 shells are generated at the side of the boss at the same time, all of which move horizontally to the aircraft). Boss releases skills every certain time, and the two skills are released alternately.

3.6.1 properties

//dx=-3
	private ArrayList<Ball> ball0=new ArrayList<Ball>();//y is the initial value
	private ArrayList<Ball> upball1=new ArrayList<Ball>();//y=500+dx/11
	private ArrayList<Ball> upball2=new ArrayList<Ball>();//y=500+2dx/11
	private ArrayList<Ball> upball3=new ArrayList<Ball>();//y=500+3dx/11
	private ArrayList<Ball> upball4=new ArrayList<Ball>();//y=500+4dx/11
	private ArrayList<Ball> upball5=new ArrayList<Ball>();//y=500+9dx/22
	private ArrayList<Ball> downball1=new ArrayList<Ball>();//y=500-dx/11
	private ArrayList<Ball> downball2=new ArrayList<Ball>();//y=500-2dx/11
	private ArrayList<Ball> downball3=new ArrayList<Ball>();//y=500-3dx/11
	private ArrayList<Ball> downball4=new ArrayList<Ball>();//y=500-4dx/11
	private ArrayList<Ball> downball5=new ArrayList<Ball>();//y=500-9dx/22

3.6.2 skill I

Implementation idea: create 11 Ball objects in turn. The initial abscissa of each Ball object is 1100 and the initial ordinate is 495. Add all objects to the corresponding queue at the same time.

//Release the first skill
			Ball ball=new Ball();
		    ball.setXY(1100,495);
		    ball0.add(ball);
		    ball=new Ball();
		    ball.setXY(1100,495);
		    upball1.add(ball);
		    ball=new Ball();
		    ball.setXY(1100,495);
		    upball2.add(ball);
		    ball=new Ball();
		    ball.setXY(1100,495);
		    upball3.add(ball);
		    ball=new Ball();
		    ball.setXY(1100,495);
		    upball4.add(ball);
		    ball=new Ball();
		    ball.setXY(1100,495);
		    upball5.add(ball);
		    ball=new Ball();
		    ball.setXY(1100,495);
		    downball1.add(ball);
		    ball=new Ball();
		    ball.setXY(1100,495);
		    downball2.add(ball);
		    ball=new Ball();
		    ball.setXY(1100,495);
		    downball3.add(ball);
		    ball=new Ball();
		    ball.setXY(1100,495);
		    downball4.add(ball);
		    ball=new Ball();
		    ball.setXY(1100,495);
		    downball5.add(ball);

3.6.3 skill II

Implementation idea: create 10 Ball objects in turn. The initial abscissa of each Ball object is 1100 and the initial ordinate is a random number. Add all objects to the ball0 queue at the same time.

 //boss releases two bell skill
		    Random r=new Random();
	        for(int i=0;i<10;i++)
		    {
		        ball=new Ball();
		        ball.setXY(1100,r.nextInt(900)+75);
		        ball0.add(ball);
		    }

3.7 GameSave class

Note: when the player clicks the button "exit and archive", archive the game and exit the program.

Implementation idea: the GameSave class is the action monitoring class of the "exit and archive" button. After registering the GameSave object listener for the button, the player will exit and archive after clicking the button with the mouse. Archiving is to save the level number, player score, aircraft ordinate (abscissa is always 0), coordinates of all shells fired by the player and coordinates of all monsters into the "save.txt" document. If the current level is the second level, you also need to store the boss's blood volume and the coordinates of all shells released by the boss. When the game is over, the "sava.txt" document should be automatically emptied (Mythread Implementation). File reading (PlaneWar Implementation) is to read out all the contents of the document. You should read the file first each time you run the program.

3.7.1 properties

private ArrayList<Ball> alball;
	private ArrayList<Monster> almonster;
	private ArrayList <Ball> ball0;//y=500
	private ArrayList<Ball> upball1;//y=500+dx/11
	private ArrayList<Ball> upball2;//y=500+2dx/11
	private ArrayList<Ball> upball3;//y=500+3dx/11
	private ArrayList<Ball> upball4;//y=500+4dx/11
	private ArrayList<Ball> upball5;//y=500+9dx/22
	private ArrayList<Ball> downball1;//y=500-dx/11
	private ArrayList<Ball> downball2;//y=500-2dx/11
	private ArrayList<Ball> downball3;//y=500-3dx/11
	private ArrayList<Ball> downball4;//y=500-4dx/11
	private ArrayList<Ball> downball5;//y=500-9dx/22

3.7.2 specific implementation

int l=PlaneWar.getLevel();
		try
		{
			BufferedWriter out=new  BufferedWriter(new FileWriter("save.txt"));
			//Storage gate
			out.write(""+l);
			out.newLine();   
			//Save player score
			out.write(""+MyThread.getScore());
			out.newLine();
			//Save aircraft ordinate
			out.write(""+MyThread.getPlaneY());
			out.newLine();
			//Save shells from players
			for(int i=0;i<alball.size();i++)
			{
			    out.write(alball.get(i).getX()+","+alball.get(i).getY()+" ");
			}
			out.newLine();
			//Save monster
			for(int i=0;i<almonster.size();i++)
			{
			    out.write(almonster.get(i).getX()+","+almonster.get(i).getY()+" ");
			}
			out.newLine();
			//If it is the second pass and the boss has appeared, continue to store
			if(l==2&&MyThread.getScore()>50)
			{ 
			    //Save boss blood
			    out.write(""+MyThread.getBlood());
			    out.newLine();
			    //Save the shells from the boss
			    for(int i=0;i<ball0.size();i++)
	        	{
			        out.write(ball0.get(i).getX()+","+ball0.get(i).getY()+" ");
	        	}
			    out.newLine();
			    for(int i=0;i<upball1.size();i++)
	        	{
			        out.write(upball1.get(i).getX()+","+upball1.get(i).getY()+" ");
	        	}
			    out.newLine();
			    for(int i=0;i<upball2.size();i++)
	        	{
			        out.write(upball2.get(i).getX()+","+upball2.get(i).getY()+" ");
	        	}
			    out.newLine();
			    for(int i=0;i<upball3.size();i++)
	        	{
			        out.write(upball3.get(i).getX()+","+upball3.get(i).getY()+" ");
	        	}
			    out.newLine();
			    for(int i=0;i<upball4.size();i++)
	        	{
			        out.write(upball4.get(i).getX()+","+upball4.get(i).getY()+" ");
	        	}
			    out.newLine();
			    for(int i=0;i<upball5.size();i++)
	        	{
			        out.write(upball5.get(i).getX()+","+upball5.get(i).getY()+" ");
	        	}
			    out.newLine();
			    for(int i=0;i<downball1.size();i++)
	        	{
			        out.write(downball1.get(i).getX()+","+downball1.get(i).getY()+" ");
	        	}
			    out.newLine();
			    for(int i=0;i<downball2.size();i++)
	        	{
			        out.write(downball2.get(i).getX()+","+downball2.get(i).getY()+" ");
	        	}
			    out.newLine();
			    for(int i=0;i<downball3.size();i++)
	        	{
			        out.write(downball3.get(i).getX()+","+downball3.get(i).getY()+" ");
	        	}
			    out.newLine();
			    for(int i=0;i<downball4.size();i++)
	        	{
			        out.write(downball4.get(i).getX()+","+downball4.get(i).getY()+" ");
	        	}
			    out.newLine();
			    for(int i=0;i<downball5.size();i++)
	        	{
			        out.write(downball5.get(i).getX()+","+downball5.get(i).getY()+" ");
	        	}
			    out.newLine();
			}
			out.flush();//Save the contents of the buffer to "save.txt"
			out.close();	
		}
		catch(IOException a)
		{
			a.getStackTrace();
		}
		System.exit(0);//Safe exit procedure

3.8 Mouse class

Description: every time the player clicks inside the window, a shell will be fired at the aircraft.

Implementation idea: the Mouse class is the Mouse monitor class. Register the Mouse class object for the window as the Mouse monitor. When the player clicks the window

Create a Ball object with the initial abscissa of 25 and the initial ordinate of the mouse. Then add the object to the player's shell queue alball.

3.8.1 properties

private static ArrayList<Ball> alball;//Shell queue

3.8.2} specific implementation

 Ball ball=new Ball();
		    ball.setXY(25, e.getY()-5);
	        alball.add(ball);

3.9 MovePlane class

Note: the abscissa of the aircraft is always 0. When the mouse moves, set the center ordinate of the aircraft picture of myThread class to be the same as the mouse.

Implementation idea: the MovePlane class is the mouse movement monitoring class, and the MovePlane class object is registered for the window as the mouse movement monitor. When the player's mouse moves in the window, set the center ordinate of the aircraft picture of myThread class to be the same as the mouse ordinate. At the same time, draw the plane in the corresponding position.

3.9.1 properties

	private ImageIcon image=new ImageIcon("plane.png");
	private Graphics g;
	private int px=0,py=500;//Coordinates of upper left corner of aircraft

3.9.2 specific implementation

py=e.getY()-25;
		    if(py<=75)
		    {
			    py=75;
		    }
		    g.drawImage(image.getImage(),px,py,null);
		    MyThread.setPlaneY(py);

3.10. MyThread class

Description: MyThread class is the core of this program. It can set the step size of monsters at different levels and the movement of shells and monsters. It can cooperate with MovePlane class to realize the movement of aircraft and display the player's score. When a monster is hit by a shell from the player's aircraft, create a BlastThread class object and start the monster's explosion thread to realize its explosion effect and judge whether the game is over, If the first level of the game ends and the player wins, a dialog box will pop up to let the player choose whether to continue to the next level. If so, call the next function of PlaneWa class r to start the next level, otherwise exit the program. If the first level of the game ends and the player fails to pass, a dialog box will pop up to let the player choose whether to do it again. If it does, call the playAgain function of PlaneWar class to restart the first level, otherwise exit the program. If the second level of the game ends and the player fails to pass, a dialog box will pop up to let the player choose whether to do it again. If it does, call the playAgain function of PlaneWar class to restart the first level, otherwise exit the program. If the second level of the game ends and the player wins, the player's score of the game will be displayed, and the ranking list of the 10 best scores in history will be updated and displayed (implemented by Mysql). At the same time, a dialog box will pop up for the player to choose whether to do it again. If it does, call the playAgain function of PlaneWar to restart the first level, otherwise exit the program. In the second level, when the player scores 50, the presence of boss and the release of boss skills are realized, and the boss's blood volume is displayed. When the game is over, empty the archive.

3.10.1 properties

private Graphics g;
	private static int score;//Player score, initial value is 0
	private ArrayList<Ball> alball;
	private ArrayList<Monster> almonster;
	private JTextField jtf;
	private static int px=0,py;//The coordinate of the upper left corner of the aircraft, py with an initial value of 500
	private boolean running=true;
	private int c;//Player's choice
	private JFrame jf;
	private int step=1;//Monster step
	private static int blood;//boss blood volume, initial value is 50
	//dx=-3
	private ArrayList <Ball> ball0;//y=500
	private ArrayList<Ball> upball1;//y=500+dx/11
	private ArrayList<Ball> upball2;//y=500+2dx/11
	private ArrayList<Ball> upball3;//y=500+3dx/11
	private ArrayList<Ball> upball4;//y=500+4dx/11
	private ArrayList<Ball> upball5;//y=500+9dx/22
	private ArrayList<Ball> downball1;//y=500-dx/11
	private ArrayList<Ball> downball2;//y=500-2dx/11
	private ArrayList<Ball> downball3;//y=500-3dx/11
	private ArrayList<Ball> downball4;//y=500-4dx/11
	private ArrayList<Ball> downball5;//y=500-9dx/22

3.10.2 automatic movement of shells and monsters

Implementation idea: clear the screen every short time, then update the coordinates of the object according to some rules, and display the object at the new coordinates, so that the object looks as if it is moving. Of course, the objects removed from the interface should also be removed from the queue. For example, to realize the automatic movement of all shells fired by players is to run the following code after clearing the screen every short time.

//Update the shell position in the alball and display the shells in the alball
    		for(int i=0;i<alball.size();i++)
    		{
    			if(alball.get(i).getX()<1198)
    			{
    				alball.get(i).setXY(alball.get(i).getX()+2,alball.get(i).getY());
    				g.fillOval(alball.get(i).getX(), alball.get(i).getY(), 10, 10);
    			}
    			else 
    			{
    				alball.remove(i);
    				i--;
    			}
    		}

3.10.3 correct display of aircraft

As mentioned above, the MovePlane class enables the aircraft to move to the corresponding position when the mouse moves; But when the mouse is still, there is nothing it can do about the display of the aircraft. However, when the aircraft moves, we all pass the latest coordinates of the aircraft to the MyThread class, so we can redraw the aircraft every short time to display the stationary aircraft.

3.10.4 judging collision

That is, judge whether the two pictures overlap, for example, judge whether a shell sent by the player hits a monster.

if((almonster.get(i).getX()-alball.get(j).getX()<10&&alball.get(j).getX()-almonster.get(i).getX()<image.getIconWidth())&&(almonster.get(i).getY()-alball.get(j).getY()<10&&alball.get(j).getY()-almonster.get(i).getY()<image.getIconHeight()))
    				

When a player's shell hits a monster, the player's score should be increased by 1 and displayed, and the explosion thread of the hit monster should be started at the same time. If it is the first level, judge whether the player's cumulative score meets the requirements. If it meets the requirements, the player can pass and continue to the next level. If it is the second level and the score meets the requirements, start the boss skill release thread and redraw the boss every short time.

BlastThread bt=new BlastThread(almonster.get(i).getX(),almonster.get(i).getY(),g);
    					bt.start();
    					score++;
    					jtf.setText("Score:"+score);
    					almonster.remove(i);
    					i--;
    					alball.remove(j);
    					//Judge whether the player passes the first level			
    					if(score==10&&PlaneWar.getLevel()==1)
    					{
    						gameOverSave();
    						PlaneWar.setRunning(false);
    						running=false;
    						c=JOptionPane.showOptionDialog(jf,"", "Pass",JOptionPane.DEFAULT_OPTION,JOptionPane.PLAIN_MESSAGE,null,new Object[]{"Next level","Reluctantly leave"},"Next level");
    						if(c==0)
    						{
    							PlaneWar.next();
    						}
    						else
    						{
    							System.exit(0);
    						}
    					}
//Judge whether boss should appear
    					if(score==50&&PlaneWar.getLevel()==2)
    					{
    						score++;
    						BossThread boss=new BossThread(ball0,upball1,upball2,upball3,upball4,upball5,downball1,downball2,downball3,downball4,downball5);
    						boss.start();//boss begins to release skills
    					}
    					break;

When the player's shell hits the boss, the boss's HP should be reduced by one.

for(int k=0;k<alball.size();k++)
    		    {
    			    if((alball.get(k).getX()-1088<112&&1088-alball.get(k).getX()<10)&&(alball.get(k).getY()-430<140&&430-alball.get(k).getY()<10))
    			    {
    			    	blood--;
    			    	alball.remove(k);
    			    	k--;
    			    }
        		
    		    }
    			//Redraw boss blood volume
    			g.clearRect(1100, 40, 100, 20);
    			g.setColor(Color.red);
    			g.fillRect(1100, 40,2*blood,20);
    			g.setColor(Color.orange);			

When boss HP < = 0, the player passes the second level, outputs the player's local score, updates and outputs the score ranking list.

 if(blood<=0)
    		    {
    		    	gameOverSave();
    		    	PlaneWar.setRunning(false);
    		    	//Output the player's local score, update the leaderboard in the database and output
    		    	Connection conn = null;
    		    	PreparedStatement  ps= null;
    		    	ResultSet rs = null;
    		    	String url = "jdbc:mysql://localhost:3306/myfirstdb";
    		    	try 
    				{
    					Class.forName("com.mysql.jdbc.Driver");
    				}
    				catch (ClassNotFoundException e1) 
    				{
    					    e1.printStackTrace();
    				}   	       
    				try 
    				{
    				    //Get connection object
    					conn = DriverManager.getConnection(url ,"root","1234");
    					// Get the tool object that executes sql
    					ps = conn.prepareStatement("SELECT * FROM planewar");
    					//Call the method to execute the query and return the query result set 
    					rs = ps.executeQuery();
    				} 
    				catch (SQLException e) 
    				{
    					e.printStackTrace();
    				}
                    int s;
    				int i=1;
    				System.out.println("Your score is:"+score);
    				System.out.println("The current leaderboard is:");
    				// Traversal processing results 
    				try
    				{
    					ps=conn.prepareStatement("update planewar set score=? where id=?");
    					while (rs.next()) 
    					{
    						s=rs.getInt(2);
    						if(score>s)
    						{
    							ps.setInt(1, score);
    							ps.setInt(2,i);
    							ps.executeUpdate();
    							System.out.println(score);
    							score=s;
 
    						}
    						else
    						{
    							System.out.println(s);
    						}
    						i++;		
    					} 
    					//close resource
    					rs.close();
    					ps.close();
    				}
    				catch (SQLException e) 
    				{
    					e.printStackTrace();
    				}
    				//Let players choose to continue or exit
    		    	c=JOptionPane.showOptionDialog(jf,"", "Congratulations on passing all the levels",JOptionPane.DEFAULT_OPTION,JOptionPane.PLAIN_MESSAGE,null,new Object[]{"Once more","Reluctantly leave"},"Once more");
    		    	if(c==0)
					{
						PlaneWar.playAgain();
					}
					else
					{
						System.exit(0);
					}
    		    	break;
    		    }

If the player's plane is hit by a monster or boss skill, the player's challenge fails. The following is the code for the plane to be hit by a monster.

//Judge whether a monster hits the plane. If so, the player fails
    		for(int k=0;k<almonster.size();k++)
		    {
			    if((almonster.get(k).getX()-px<50&&px-almonster.get(k).getX()<70)&&(almonster.get(k).getY()-py<50&&py-almonster.get(k).getY()<87))
			    {
			    	running=false;
			    	defeat();
					break;
			    }
		    }

The above code is executed every 10ms in the run function. The following is the default function

 //Some operations to be performed after a player fails
    public void defeat()
    {
    	gameOverSave();
    	PlaneWar.setRunning(false);
		c=JOptionPane.showOptionDialog(jf,"", "Defeat",JOptionPane.DEFAULT_OPTION,JOptionPane.PLAIN_MESSAGE,null,new Object[]{"Once more","Reluctantly leave"},"Once more");
		if(c==0)
		{
			PlaneWar.playAgain();
		}
		else
		{
			System.exit(0);
		}
    }

gameOver function, clear "save.txt" at the end of the game

 public void gameOverSave()
    {
    	try
		{
			BufferedWriter out=new  BufferedWriter(new FileWriter("save.txt"));
		    out.write("");
			out.flush();
			out.close();
			
		}
		catch(IOException e)
		{
			e.getStackTrace();
		}
    }

3.11 PlaneWar class

Note: its Main function is the program entry. When the program starts to execute, first read the file, then display the game screen, create relevant objects, start relevant threads, and register listeners for windows and buttons. Its playAgain function realizes the restart of the game, and the next function realizes the start of the second level.

3.11.1 properties

private static ArrayList<Ball> alball=new ArrayList<Ball>();//Used to store all shells in the interface
	private static ArrayList<Monster> almonster=new ArrayList<Monster>();//Used to store all monsters in the interface
	private static JFrame jf=new JFrame();//Window object
	private static Mouse mouse=new Mouse();//Mouse listening class object
	private static JTextField jtf=new JTextField("");//Text box to display player scores
	private static MovePlane mp=new MovePlane();//Mouse movement monitoring class object
	private static Graphics g;//Window content brush
	private static boolean running=true;//Record whether the program is in game state
	private static int level=1;//Level number
	//Shell sent by boss, dx=-3
	private static ArrayList<Ball> ball0=new ArrayList<Ball>();//y=500
	private static ArrayList<Ball> upball1=new ArrayList<Ball>();//y=500+dx/11
	private static ArrayList<Ball> upball2=new ArrayList<Ball>();//y=500+2dx/11
	private static ArrayList<Ball> upball3=new ArrayList<Ball>();//y=500+3dx/11
	private static ArrayList<Ball> upball4=new ArrayList<Ball>();//y=500+4dx/11
	private static ArrayList<Ball> upball5=new ArrayList<Ball>();//y=500+9dx/22
	private static ArrayList<Ball> downball1=new ArrayList<Ball>();//y=500-dx/11
	private static ArrayList<Ball> downball2=new ArrayList<Ball>();//y=500-2dx/11
	private static ArrayList<Ball> downball3=new ArrayList<Ball>();//y=500-3dx/11
	private static ArrayList<Ball> downball4=new ArrayList<Ball>();//y=500-4dx/11
	private static ArrayList<Ball> downball5=new ArrayList<Ball>();//y=500-9dx/22

3.11.2 file reading

int score=0;
	    int py=500;
	    int blood=50;
	    //File reading
	    String s=new String();
	    String[] str;
	    try
		{
			BufferedReader in=new BufferedReader(new FileReader("save.txt"));
			s=in.readLine();
			if(s!=null)
			{
				level=Integer.parseInt(s);
				s=in.readLine();
				score=Integer.parseInt(s);
				s=in.readLine();
				py=Integer.parseInt(s);
				try
				{
				    s=in.readLine();
				    str=s.split(" ");
				    for(int i=0;i<str.length;i++)
				    {
					    String[] xy=str[i].split(",");
					    Ball b=new Ball();
					    b.setXY(Integer.parseInt(xy[0]),Integer.parseInt(xy[1]));
					    alball.add(b);
			 	    }
				}
				catch( NumberFormatException e)
				{}
				try
				{
				    s=in.readLine();
				    str=s.split(" ");
				    for(int i=0;i<str.length;i++)
				    {
					    String[] xy=str[i].split(",");
					    Monster monster=new Monster();
					    monster.setXY(Integer.parseInt(xy[0]),Integer.parseInt(xy[1]));
					    almonster.add(monster);
				    }
				}
				catch( NumberFormatException e)
				{}
				if(level==2&&score>50)
				{
					s=in.readLine();
					blood=Integer.parseInt(s);
					try
					{
					    s=in.readLine();
					    str=s.split(" ");
					    for(int i=0;i<str.length;i++)
					    {
						    String[] xy=str[i].split(",");
						    Ball b=new Ball();
						    b.setXY(Integer.parseInt(xy[0]),Integer.parseInt(xy[1]));
						    ball0.add(b);
					    }
					    s=in.readLine();
					    str=s.split(" ");
					    for(int i=0;i<str.length;i++)
					    {
						    String[] xy=str[i].split(",");
						    Ball b=new Ball();
						    b.setXY(Integer.parseInt(xy[0]),Integer.parseInt(xy[1]));
						    upball1.add(b);
					    }
					    s=in.readLine();
					    str=s.split(" ");
					    for(int i=0;i<str.length;i++)
					    {
						    String[] xy=str[i].split(",");
						    Ball b=new Ball();
						    b.setXY(Integer.parseInt(xy[0]),Integer.parseInt(xy[1]));
						    upball2.add(b);
					    }
					    s=in.readLine();
					    str=s.split(" ");
					    for(int i=0;i<str.length;i++)
					    {
						    String[] xy=str[i].split(",");
						    Ball b=new Ball();
						    b.setXY(Integer.parseInt(xy[0]),Integer.parseInt(xy[1]));
						    upball3.add(b);
					    }
					    s=in.readLine();
					    str=s.split(" ");
					    for(int i=0;i<str.length;i++)
					    {
						    String[] xy=str[i].split(",");
						    Ball b=new Ball();
						    b.setXY(Integer.parseInt(xy[0]),Integer.parseInt(xy[1]));
						    upball4.add(b);
					    }
					    s=in.readLine();
					    str=s.split(" ");
					    for(int i=0;i<str.length;i++)
					    {
						    String[] xy=str[i].split(",");
						    Ball b=new Ball();
						    b.setXY(Integer.parseInt(xy[0]),Integer.parseInt(xy[1]));
						    upball5.add(b);
					    }
					    s=in.readLine();
					    str=s.split(" ");
					    for(int i=0;i<str.length;i++)
					    {
						    String[] xy=str[i].split(",");
						    Ball b=new Ball();
						    b.setXY(Integer.parseInt(xy[0]),Integer.parseInt(xy[1]));
						    downball1.add(b);
					    }
					    s=in.readLine();
					    str=s.split(" ");
					    for(int i=0;i<str.length;i++)
					    {
						    String[] xy=str[i].split(",");
						    Ball b=new Ball();
						    b.setXY(Integer.parseInt(xy[0]),Integer.parseInt(xy[1]));
						    downball2.add(b);
					    }
					    s=in.readLine();
					    str=s.split(" ");
					    for(int i=0;i<str.length;i++)
					    {
						    String[] xy=str[i].split(",");
						    Ball b=new Ball();
						    b.setXY(Integer.parseInt(xy[0]),Integer.parseInt(xy[1]));
						    downball3.add(b);
					    }
					    s=in.readLine();
					    str=s.split(" ");
					    for(int i=0;i<str.length;i++)
					    {
						    String[] xy=str[i].split(",");
						    Ball b=new Ball();
						    b.setXY(Integer.parseInt(xy[0]),Integer.parseInt(xy[1]));
						    downball4.add(b);
					    }
					    s=in.readLine();
					    str=s.split(" ");
					    for(int i=0;i<str.length;i++)
					    {
						    String[] xy=str[i].split(",");
						    Ball b=new Ball();
						    b.setXY(Integer.parseInt(xy[0]),Integer.parseInt(xy[1]));
						    downball5.add(b);
					    }
					}
					catch( NullPointerException e)
			        {}
					BossThread boss=new BossThread(ball0,upball1,upball2,upball3,upball4,upball5,downball1,downball2,downball3,downball4,downball5);
					boss.start();//The boss began to continue his skills
				}
			}
			in.close();
		}
		catch(IOException e)
		{
			e.getStackTrace();
		}

3.11.3 initialization

Initialize the game interface, get the window brush, create relevant objects, register corresponding listeners for buttons and windows, and start relevant threads...

jf.setTitle("Aircraft war");
		jf.setSize(1200,1000);
		jf.setLayout(null);
		jf.setLocationRelativeTo(null);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.setVisible(true);
		jf.setResizable(false);
		jf.setBackground(Color.BLACK);
		g=jf.getGraphics();
		mouse.setALBall(alball);//Pass the queue alball to the mouse listening class object mouse
	    Container c=jf.getContentPane();
	    c.setBackground(Color.black);//Set the window content background to black
		jf.addMouseListener(mouse);//Register mouse listeners for windows
		//Create a text box for scoring
     	jtf.setBounds(0, 0,80,20);
     	jtf.setEditable(false);
		jf.add(jtf);
		GameSave gs=new GameSave(alball,almonster,ball0,upball1,upball2,upball3,upball4,upball5,downball1,downball2,downball3,downball4,downball5);
		JButton jb=new JButton("Exit and archive");
		jb.addActionListener(gs);
		jb.setBounds(560,0,100,30);
		jf.add(jb);
		MyThread m=new MyThread(alball,almonster,jtf,jf,g,ball0,upball1,upball2,upball3,upball4,upball5,downball1,downball2,downball3,downball4,downball5,score,py,blood);
		mp.setG(g);
		jf.addMouseMotionListener(mp);//Register a mouse movement listener for the window
		m.start();
		//Create and start the thread that produces the monster
		WipeThread wt=new WipeThread();
		wt.setALMonster(almonster);
		wt.start();

3.11.4 restart and next related function

//The next level enters here
	public static void next()
	{		
		//Empty shell list
		alball.clear();
		//Empty monster list
		almonster.clear();
		level=2;
		running=true;
		MyThread m=new MyThread(alball,almonster,jtf,jf,g,ball0,upball1,upball2,upball3,upball4,upball5,downball1,downball2,downball3,downball4,downball5,0,500,50);
		WipeThread wt=new WipeThread();
		wt.setALMonster(almonster);
		m.start();
		wt.start();	
	}
	
	//Restart and enter here
	public static void playAgain()
	{
		//Empty shell list
		alball.clear();
		//Empty monster list
		almonster.clear();
		level=1;
		running=true;	
		MyThread m=new MyThread(alball,almonster,jtf,jf,g,ball0,upball1,upball2,upball3,upball4,upball5,downball1,downball2,downball3,downball4,downball5,0,500,50);
		WipeThread wt=new WipeThread();
		wt.setALMonster(almonster);
		m.start();
		wt.start();
	}

3.12  WipeThread

Description: realize the generation of monsters.

Implementation idea: create a Monster class object every certain time. The initial abscissa of the object is 1130, and the initial ordinate is randomly generated, and then add the object to the almonster queue.

3.12.1 properties

private static ArrayList<Monster> almonster;

3.12.2 monster generation

Monster monster=new Monster();
			monster.setXY(1130,r.nextInt(835)+75);
			almonster.add(monster);

Keywords: Java MySQL Eclipse

Added by tryingtolearn on Tue, 04 Jan 2022 08:44:19 +0200