这是个完整的代码,部分图片我就不上传了
package day05;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Main {
public static void main(String[] args) throws Exception {
JFrame frame = new JFrame("捕鱼人");
Pool pool = new Pool();
frame.add(pool);
frame.setSize(800, 510);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
pool.action();
}
}
class Pool extends JPanel {
int score;
BufferedImage background;
Fish[] allFish;
Net net;
public Pool() throws Exception {
background = ImageIO.read(new File("bg.jpg"));
score = 100;
net = new Net();
allFish = new Fish[22];
for (int i = 0; i <= 8; i++) {
// i=0 1 2 3 4 5 6 7 8
allFish[i] = new Fish("fish0" + (i + 1));
allFish[i + 9] = new Fish("fish0" + (i + 1));
}
allFish[18] = new Fish("fish13");
allFish[19] = new Fish("fish13");
allFish[20] = new Fish("fish14");
allFish[21] = new Fish("fish14");
// 0 1 2 3 4 5 6 7 8
// allFish = {*, *, *, ^, ^, ^, ^, ^, ^,
// 9 10 11 12 13 14 15 16 17 18 19 20 21
// *, *, *, ^, ^, ^, ^, ^, ^, ^, ^, ^, ^}
//
}
/** Pool 中 action() 方法启动每条鱼,鱼自己跑 */
public void action() throws Exception {
MouseAdapter l = new MouseAdapter() {
public void mouseMoved(MouseEvent e) {// 鼠标移动
int x = e.getX();
int y = e.getY();
net.moveTo(x, y);
}
public void mouseEntered(MouseEvent e) {// 鼠标进入
net.show = true;
}
public void mouseExited(MouseEvent e) {// 鼠标离开
net.show = false;
}
public void mousePressed(MouseEvent e) {
catchFish();// 抓鱼
}
};
this.addMouseListener(l);
this.addMouseMotionListener(l);
for (int i = 0; i < allFish.length; i++) {
// i=0 1 2 3 .. 21
allFish[i].start();
}
while (true) {
repaint();
Thread.sleep(1000 / 24);
}
}
/** Pool 添加方法 */
public void catchFish() {
for (int i = 0; i < allFish.length; i++) {
// i=0 1 2 3 4 ... 21
Fish fish = allFish[i];
if (net.catched(fish)) {
fish.getOut();
score = score + fish.width / 10;
}
}
}
public void paint(Graphics g) {
g.drawImage(background, 0, 0, null);
g.setColor(Color.WHITE);
g.drawString("SCORE:" + score, 10, 20);
for (int i = 0; i < allFish.length; i++) {
Fish fish = allFish[i];
g.drawImage(fish.image, fish.x, fish.y, null);
}
if (net.show) {
g.drawImage(net.image, net.x - net.width / 2, net.y - net.height / 2,
null);
}
}
}
class Fish extends Thread {
int x;
int y;
int width;
int height;
int step;
int index;
BufferedImage image;
BufferedImage[] images;
public Fish(String name) throws Exception {
images = new BufferedImage[10];
for (int i = 0; i < images.length; i++) {
images[i] = ImageIO.read(new File(name + "_0" + i + ".png"));
}
image = images[0];
width = image.getWidth();
height = image.getHeight();
Random random = new Random();
x = random.nextInt(800 - width);
y = random.nextInt(480 - height);
step = random.nextInt(4) + 2;
index = 0;
}
/** 在Fish 中添加方法run */
public void run() {
while (true) {
move();
try {
Thread.sleep(1000 / 10);
} catch (Exception e) {
}
}
}
public void move() {
image = images[index++ % images.length];
x = x - step;
// 如果出界就滚蛋
if (x <= -width) {
getOut();
}
}
/** 滚蛋 */
public void getOut() {
x = 800;
Random r = new Random();
y = r.nextInt(480 - height);
step = r.nextInt(4) + 2;
}
}
class Net {
int x;
int y;
int width;
int height;
BufferedImage image;
boolean show;// show显示,是否3显示当前的渔网
public Net() throws Exception {
image = ImageIO.read(new File("net09.png"));
show = true;
x = 0;
y = 0;
width = image.getWidth();
height = image.getHeight();
}
public void moveTo(int x, int y) {
this.x = x;
this.y = y;
}
/**
* Net 上添加方法 用当前在网抓鱼 返回true 抓到
*/
public boolean catched(Fish fish) {
int dx = x - fish.x;
int dy = y - fish.y;
return dx >= 0 && dx < fish.width && dy >= 0 && dy < fish.height;
}
// public
}