Java course design Huarongdao mini game complete

Title Based on the above requirements, develop a small Huarong Dao game to meet the following functional requirements: 1. Design a Huarong Dao game interface; 2. Create objects to represent the characters in Huarong Dao; 3. Control the movement of the characters through the mouse; 4. , Increase the difficulty of multiple games. Users can control the movement of the character through the mouse and can choose different difficulty levels to increase the game challenge. To ensure that the game runs smoothly on low-configuration systems and improve the response speed of the game. The interface is required to be simple and beautiful, with reasonable layout, simple operation, simple and easy to use, and anyone can operate it easily. (Includes source code and reports)

Title functional requirements analysis

Players can perform Huarongdao game operations on the game interface, including controlling the movement of characters through the mouse, solving levels, etc., and enjoy the challenges of the game at different difficulty levels. The game interface is simple and beautiful, easy to operate, has good response speed, good logic and playability to provide a pleasant gaming experience while adapting to the requirements of low-configuration systems. The functions are as follows:
(1) Password and account verification to log in to the game, provide game background, and display the welcome statement and game rules on the command line.
(2) Game interface: Create a simple and beautiful Huarongdao game interface, including elements such as game board and operation buttons. The game board should display the current level layout and character positions, with enough space for moving characters, and a text area to display current level information.
(3) Level setting: Create multiple levels of difficulty for players to choose from. Each level should have a different layout and character initial position. Level data can be stored in arrays and other methods.
(4) Character movement: Use mouse clicks or keyboard operations to move characters. When the player clicks a certain direction button, it is judged according to the game rules whether the character can move in the corresponding direction, and the character's position is updated if legal.
(5) Judgment of passing the level: In each level, the player needs to move a specific character to a designated position in order to pass the level. It is necessary to monitor the character's position and target position in real time. When the character successfully moves to the target position, it is judged that the player has passed the level and enters the next level.
(6) Exit game function: Allow players to exit the game at any time, and provide an exit option for players to choose.
(7) Error detection and prompts: When the player performs a movement operation, it is necessary to detect whether it is legal and give corresponding error prompts, such as exceeding the boundary or moving to an occupied position.
Insert image description here
Class name: GameClient
Function: Create a client window for the game and add the game panel and help panel to the window.
Key code: public GameClient(){
helpPanel = new HelpPanel(1);//Help Panel, create a new HelpPanel object and assign it to the helpPanel variable. Pass parameter 1 to specify the type of help panel or initialization information.
gamePanel = new GamePanel(1);//Set the level
gamePanel.setLayout(new BorderLayout());//Set the layout manager of the game panel to BorderLayout
panel = this.getContentPane();//Get the content panel
panel of the window .setLayout(new BorderLayout());//Set the layout manager of the content panel to BorderLayout
panel.add(helpPanel,BorderLayout.EAST);//Add the help panel to the east (right) side of the content panel.
panel.add(gamePanel,BorderLayout.CENTER);//Add the game panel to the center of the content panel.
this.setSize(650,650);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Set the default operation when the window is closed. Exit the program
this.setTitle("Huarongdao");
this.setVisible(true);//Set the window to be visible
this.setResizable(false);//Prohibit window size adjustment
}
Class name: GamePanel (is a game panel, used Used to display the game interface and control game logic.)
Function:
Initialize the game layout: add characters and tiles to the game panel by reading the map array and icon array, and set the initial position and icon.
Implement monitoring of mouse events and keyboard events: used to control the movement of characters, calculate the movement direction based on the pixel coordinates of mouse clicks and releases, and determine whether movement is possible.
Draw the game interface: Rewrite the paint() method to draw the characters and tiles on the game panel.
Key code:
Class name: HelpPanel
Function: Used to display game level information and operation buttons.
Key code: private void initialize() { nowName.setText(names[level-1]+"");//Set the label text of the current level name to the name of the corresponding level nowStep.setText("0");//Set The label text of the current step number is 0 record.setText(""+MapFactory.getRecord(level)); //Set the label text of the historical record to the historical record of the corresponding level } Class name: login Function: Create a graphical login interface , the user can enter the user name, password and nickname, and click the confirmation button to log in. After successful login, a welcome message will be displayed and a game client window (GaneClient) will be created. Key code: public static void main(String args[]) { new login("Welcome to the Huarong Road mini game designed by Luo Yijing"); } } Class name: MapFactory













Function: It is responsible for defining the map layout of different levels, and providing related methods to obtain the level layout information and update the recorded steps of the level.
Key code:
//Return a copy of the specified level layout information (avoid direct references)
public static Node[] getMap(int level){//Definition of static method, used to return a copy of the specified level layout information, accepts a parameter level, Indicates the level of the level to be obtained.
Node[] temp = new Node[10]; //Create a temporary array temp to store layout information.
for(int i=0;i<10;i++){ //Loop through and implement copying.
temp[i] = map[level-1][i];
}
return temp;
}
//Read the record
fis = new FileInputStream(record);//Create a DataInputStream object for reading basic data types and pass Enter the FileInputStream object created earlier as a construction parameter.
dis = new DataInputStream(fis);//Read a line of the file through the DataInputStream object and store it in the string variable str.
String str = dis.readLine();//
array = str.split(“,”);

	}catch(Exception e){
		e.printStackTrace();
	}finally{
		 try {
				if(fis!=null)
				 fis.close();
				if(dis!=null)
				 dis.close();			
				if(fos!=null)
		    	 fos.close();
				if(dos!=null)
				 dos.close();				
		     } catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();//捕获可能发生的异常,并打印异常堆栈信息。
			}
	}			
	return Integer.parseInt(array[level-1]);//将 array 数组中指定关卡级别的记录转换为整数并返回。

Class name: MapUtil
Function: Used to encapsulate operations on map layout and level records, and obtain and update relevant information by calling methods of the MapFactory class. It provides a simplified and unified interface for easy use by other classes or modules.
Key code:
//Get the map layout of the specified level
public Node[] getMap(int level){ return MapFactory.getMap(level); } //Get the level history public int getRecord(int level){ return MapFactory.getRecord(level ); } //Update the specified level record public void updateRecode(int level, int record){//Update the level record MapFactory.writeRecord(level, record); } Class name: Node Function: Used to encapsulate map node information, including The node's unique identifier, direction, and position coordinates in the map array. It provides methods to get and set node properties. Key code: public Node(int id,int x,int y,boolean direction){//Defines a class named Node and declares a constructor with parameters. this.id = id;//Assign the id parameter passed in the constructor to the id attribute in the class. By using the this keyword, you can reference the instance variables of the current class.














this.x = x;
this.y = y;
this.direction = direction;
}
Class name: Person
Function: Used to create a clickable character button to display character information in the graphical interface. It encapsulates the character's number and name, and provides methods for getting and setting character attributes.
Key code:
public class Person extends JButton{ int id;//The number of the person String name;//The name of the person

public Person(int id,String str){//一个公共构造方法,用于创建 Person 类的对象
	super("");//可以访问父类的成员。
	name = str;//将构造方法中传递的 str 参数赋值给类中的 name 属性。
	this.id = id;//将构造方法中传递的 id 参数赋值给类中的 id 属性
}

public int getId() {//定义了一个公共方法 getId(),用于获取人物的编号。
	return id;
}

public void setId(int id) {
	this.id = id;
}	

}

source code:

GameClientimport java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class GameClient extends JFrame{
   
    
    

	static GamePanel gamePanel;
	static HelpPanel helpPanel;
	static Container panel;//窗口容器
	
	public GameClient(){
   
    
    

		helpPanel = new HelpPanel(1);//帮助面板
		gamePanel = new GamePanel(1);//设置关卡
		gamePanel.setLayout(new BorderLayout());
		panel = this.getContentPane();
		panel.setLayout(new BorderLayout());
		panel.add(helpPanel,BorderLayout.EAST);
		panel.add(gamePanel,BorderLayout.CENTER);
		this.setSize(650,650);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗口关闭时默认的操作为退出程序
		this.setTitle("华容道");
		this.setVisible(true);//设置窗口可见
		this.setResizable(false);//禁止窗口大小的调整
	}
}
GamePanelimport java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class GameClient extends JFrame{
   
    
    

	static GamePanel gamePanel;
	static HelpPanel helpPanel;
	static Container panel;//窗口容器
	
	public GameClient(){
   
    
    

		helpPanel = new HelpPanel(1);//帮助面板
		gamePanel = new GamePanel(1);//设置关卡
		gamePanel.setLayout(new BorderLayout());
		panel = this.getContentPane();
		panel.setLayout(new BorderLayout());
		panel.add(helpPanel,BorderLayout.EAST);
		panel.add(gamePanel,BorderLayout.CENTER);
		this.setSize(650,650);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗口关闭时默认的操作为退出程序
		this.setTitle("华容道");
		this.setVisible(true);//设置窗口可见
		this.setResizable(false);//禁止窗口大小的调整
	}
}
HelpPanelimport java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
//创建一个帮助面板显示游戏的关卡信息 当前步数和历史记录
public class HelpPanel extends JPanel{
   
    
    

	String[] names = new String[]{
   
    
    "七步成诗","横刀立马","屯兵东路","插翅难飞","巧过五关","层层设防","近在咫尺","兵临曹营","众志成城","佳人梳妆"};//十个关卡的名字
	int level;
	JPanel panelNorth = new JPanel(new GridLayout(4,2,10,30));//:一个具有4行2列的网格布局的面板,用于存放关卡信息、当前步数和历史记录的标签以及重置按钮。
	static JLabel nowStep = new JLabel("0");//显示当前步数的标签
	JLabel nowName = new JLabel("");//显示当前关卡名称的标签
	JLabel record = new JLabel("9999");//显示历史记录的标签
	static	JButton restart = new JButton("重置");//重置按钮
                 	
	public HelpPanel(int level){
   
    
    
		this.level = level;
		this.setVisible(true);
		this.setLayout(new BorderLayout());//当前面板
		panelNorth.add(new JLabel("关卡名字:"));
		panelNorth.add(nowName);
		panelNorth.add(new JLabel("当前步数:"));
		panelNorth.add(nowStep);
		panelNorth.add(new JLabel("历史记录:"));
		panelNorth.add(record);//历史记录
		panelNorth.add(restart);
		
		this.add(panelNorth,BorderLayout.NORTH);
		initialize();
	}
//更新当前关卡数
	public void setLevel(int level){
   
    
    
		this.level = level;
		initialize();//重新初始化面板
	}
	
	private void initialize() {
   
    
    
//		System.out.println("level is "+level);
//		System.out.println(names[level-1]);
		nowName.setText(names[level-

Guess you like

Origin blog.csdn.net/m0_68165821/article/details/131485824