The Java version of Fishing Master - built with IntelliJ IDEA - a must-see for beginners 2
1. Effect display
In the last blog post, you have already seen the preliminary display screen of Fishing Master! The picture below is:
Next, let’s implement it! ! !
2. Production of fishing nets
3. Start fishing
pool.action();
/**
* 处理鼠标事件-实现点击鼠标进行捕鱼
* @throws Exception
*/
public void action() throws Exception {
net = new Net();
/**
* 实现鼠标适配器避免出现实现鼠标监听器接口或者使用匿名内部类出现的代码冗余
* 我们可以根据需要重写自己需要的方法
*/
MouseAdapter adapter = new MouseAdapter() {
/* 鼠标进入,渔网显示;鼠标移出,渔网不显示 */
@Override
public void mouseEntered(MouseEvent e) {
net.show = true;
}
@Override
public void mouseExited(MouseEvent e) {
net.show = false;
}
/**
* 渔网的位置随着鼠标的位置变化
*/
@Override
public void mouseMoved(MouseEvent e) {
net.x = e.getX();
net.y = e.getY();
}
/**
* 当鼠标按下的时候进行捕鱼操作
*/
@Override
public void mousePressed(MouseEvent e) {
catchFish();
}
};
this.addMouseListener(adapter); // 添加鼠标监听器
this.addMouseMotionListener(adapter); // 鼠标移动监听器
while (true) {
repaint();
try {
Thread.sleep(100); // 每隔一定时间刷新屏幕,需要符合视觉暂留设置50~100ms
} catch (Exception e) {
e.printStackTrace();
}
}
}
4. Record score
score += fish.width / 10; // 不同的鱼有不同的分数
Well, that's it for now! ! ! Everyone can do it by hand! ! !