Summary of Java Mini Game Production

to sum up

One, first analyze

	分析对象,
	寻找属性,
	发现方法,
	难点分析, 
    记录难点 

Java game notes one

Second, insert a picture
First call a tool class

/**
 * 常用工具类
 */
public class CommonUtils {
    
    
        /**
         * 读取图片资源, 转变为Java对象 Image
         * @param imgPath  图片路径
         * @return Image对象
         */
        public static Image getImage(String imgPath) {
    
    
            ImageIcon imageIcon = new ImageIcon(imgPath);
            return imageIcon.getImage();
        }

}

Then create the object and window class (inherited Frame), enter the method to open the window in the window class, and the private property code of the window is shown in Note 2
(there is only one background from beginning to end)
. After the address of the background image is set, it should be called to the static block. In, create a brush class to draw the background.
The object class also inserts the picture address in the same way.

Java game notes two

Third, the closing of the program and the movement of characters

Going back to the window class, the operation found that it cannot be closed, so we need to add a listener class to close the listener (see note 3). The
listener can not only close the program, but the movement of the character also needs the keyboard to monitor the trigger event to move. The method of this.addKeyListener performs trigger judgment. (See Part 2 of Note 3)
Note: Movement is character movement, so add it in the character class

Java game notes three

创建方法使用参数判断按键的方向
 public void okDirPressed(int keyCode)
 使用switch判断
 然后在测试类中获取
    //监听键盘事件
        this.addKeyListener(new KeyAdapter() {
    
    
            //当键盘按下的时候触发
            // Var 获取被按下的键的数值 如:a-67,B-68
            @Override
            public void keyPressed(KeyEvent e) {
    
    
                int keyCode = e.getKeyCode();
                buffoon.okDirPressed(keyCode);
                buffoon.move(buffoon.getDir());
            }

            //当键盘松开的时候触发
            @Override
            public void keyReleased(KeyEvent e) {
    
    
                int keyCode=e.getKeyCode();
                buffoon.OkDirReleased(keyCode);


            }
        });

And create a method to determine the direction of movement in the character class,
and change the values ​​of the X and Y axes.
After setting the 4 direction attributes to boolean type to judge, when I press two buttons, it triggers diagonal walking. (See note four)

Bullet launch bug fix and optimization

The bullet is launched by the character class, so set a launch method and write a judgment in the trigger method, use the button to launch the bullet (see Note 4)

After the bullet is launched, it is found that if the movement attribute of the character is STOP, the bullet will also stop, so create a variable toki of the same type as the direction in the character class, and assign the value of the direction to this variable in determining the direction. The direction variable in the parameter method passed to the bullet,
so that the direction variable of the bullet is only up, down, left, and right, and toki is assigned a default direction attribute in the character, and the bullet will move in this direction in the initial state, and the solution is solved The retention of the bullet once the direction is at rest.

Java game notes four

Border setting

Create a new method to determine whether the distance between X and Y exceeds the width and height currently set. If it exceeds, change the value of X and Y to the value of the four-side margin, and use IF judgment (note that each judgment is IF , Because the else if is only judged once, and pay attention to the size of the character picture).
(See note five for the code).

Data optimization

The steps have been completed so far, but it is found that there are many couplings in the code. At this time, we can optimize. We found that the attributes of each class in the attribute are the same, so we can set a parent class to replace it, reducing code redundancy and increasing complexity Usability. Use inheritance and then use the super keyword in the constructor to bring in the parameters, the
code is as follows:

public Buffoon(int x,int y,GameClinet gameClinet){
    
    
        super(x,y,
                DataPropertiesUtils.BUFFOON_SIZE,
                DataPropertiesUtils.BUFFOON_SIZE,
                DataPropertiesUtils.BUFFOON_SPEED
                ,gameClinet
        );

Java notes five

Producer Xu Yang

The idea may be a little off, it should be not bad in general

									时间:2020/11/17

Guess you like

Origin blog.csdn.net/MyonlyloveLAX/article/details/109739398