捕鱼小游戏
package fish;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Demo2 {
public static void main(String[] args) throws IOException {
final JFrame f = new JFrame("捕鱼达人");
Pool p = new Pool();
f.setContentPane(p);
f.setSize(700, 500);
f.setLocation(700, 500);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
int var = JOptionPane.showConfirmDialog(f, "确定离开?");
if(var == JOptionPane.YES_OPTION){
System.exit(0);
}
}
});
f.setVisible(true);
p.action();
}
}
public static void main(String[] args) throws IOException {
final JFrame f = new JFrame("捕鱼达人");
Pool p = new Pool();
f.setContentPane(p);
f.setSize(700, 500);
f.setLocation(700, 500);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
int var = JOptionPane.showConfirmDialog(f, "确定离开?");
if(var == JOptionPane.YES_OPTION){
System.exit(0);
}
}
});
f.setVisible(true);
p.action();
}
}
class Pool extends JPanel{
BufferedImage bg;
int score;
Fishs fish;
Wang net;
public Pool() throws IOException{
bg = ImageIO.read(new File("pic/bg.jpg"));
score = 100;
fish = new Fishs("fish03");
net = new Wang();
}
public void paint(Graphics g){
g.drawImage(bg, 0, 0, null);
g.setColor(Color.red);
g.setFont(new Font("", Font.BOLD, 30));
g.drawString("得分:"+score, 20, 30);
g.drawImage(fish.fishPic, fish.x, fish.y, null);
if(net.show){
g.drawImage(net.netPic, net.x - net.width/2, net.y - net.height/2, null);
}
}
public void action(){
mouseMove();
fish.start();
while(true){
repaint();
try {
Thread.sleep(1000/10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void mouseMove(){
MouseAdapter m = new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
net.show = true;
}
public void mouseExited(MouseEvent e) {
net.show = false;
}
public void mouseMoved(MouseEvent e) {
int x = e.getX();
int y = e.getY();
net.wangMove(x,y);
}
public void mousePressed(MouseEvent e) {
catchFishs();
}
};
this.addMouseListener(m);
this.addMouseMotionListener(m);
}
public void catchFishs(){
if(net.catchFish(fish)){
this.score += fish.width/2;
fish.isFishCatch = true;
}
}
}
BufferedImage bg;
int score;
Fishs fish;
Wang net;
public Pool() throws IOException{
bg = ImageIO.read(new File("pic/bg.jpg"));
score = 100;
fish = new Fishs("fish03");
net = new Wang();
}
public void paint(Graphics g){
g.drawImage(bg, 0, 0, null);
g.setColor(Color.red);
g.setFont(new Font("", Font.BOLD, 30));
g.drawString("得分:"+score, 20, 30);
g.drawImage(fish.fishPic, fish.x, fish.y, null);
if(net.show){
g.drawImage(net.netPic, net.x - net.width/2, net.y - net.height/2, null);
}
}
public void action(){
mouseMove();
fish.start();
while(true){
repaint();
try {
Thread.sleep(1000/10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void mouseMove(){
MouseAdapter m = new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
net.show = true;
}
public void mouseExited(MouseEvent e) {
net.show = false;
}
public void mouseMoved(MouseEvent e) {
int x = e.getX();
int y = e.getY();
net.wangMove(x,y);
}
public void mousePressed(MouseEvent e) {
catchFishs();
}
};
this.addMouseListener(m);
this.addMouseMotionListener(m);
}
public void catchFishs(){
if(net.catchFish(fish)){
this.score += fish.width/2;
fish.isFishCatch = true;
}
}
}
class Fishs extends Thread{
BufferedImage fishPic;
BufferedImage[] fishPics;
int x;
int y;
int width;//图片的宽度
int height;
int step;
int index = 0;
Random r;
boolean isFishCatch;
BufferedImage[] catchedFishPic;
public Fishs(String name) throws IOException{
catchedFishPic = new BufferedImage[2];
isFishCatch = false;
for (int i = 0; i < catchedFishPic.length; i++) {
catchedFishPic[i] = ImageIO.read(new File("pic/"+name+"_catch_"+(i+1)+".png"));
}
fishPics = new BufferedImage[10];
for (int i = 0; i < fishPics.length; i++) {
fishPics[i] = ImageIO.read(new File("pic/"+name+"_"+(i+1)+".png"));
}
fishPic = fishPics[0];
r = new Random();
width = fishPic.getWidth();
height = fishPic.getHeight();
x = r.nextInt(800 - width);
y = r.nextInt(500 - height);
step = r.nextInt(5) + 1;
}
public void run(){
while(true){
if(isFishCatch){
for (int i = 0; i < 5; i++) {
fishPic = catchedFishPic[0];
try {
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
fishPic = catchedFishPic[1];
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
getOut();
isFishCatch = false;
}else{
fishMove();
try {
Thread.sleep(1000/24);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void fishMove(){
fishPic = fishPics[index++%fishPics.length];
this.x = this.x - this.step;
if(this.x <= -width){
getOut();
}
}
public void getOut(){
this.x = 700;
this.y = r.nextInt(500 - height);
this.step = r.nextInt(5) + 1;
}
}
BufferedImage fishPic;
BufferedImage[] fishPics;
int x;
int y;
int width;//图片的宽度
int height;
int step;
int index = 0;
Random r;
boolean isFishCatch;
BufferedImage[] catchedFishPic;
public Fishs(String name) throws IOException{
catchedFishPic = new BufferedImage[2];
isFishCatch = false;
for (int i = 0; i < catchedFishPic.length; i++) {
catchedFishPic[i] = ImageIO.read(new File("pic/"+name+"_catch_"+(i+1)+".png"));
}
fishPics = new BufferedImage[10];
for (int i = 0; i < fishPics.length; i++) {
fishPics[i] = ImageIO.read(new File("pic/"+name+"_"+(i+1)+".png"));
}
fishPic = fishPics[0];
r = new Random();
width = fishPic.getWidth();
height = fishPic.getHeight();
x = r.nextInt(800 - width);
y = r.nextInt(500 - height);
step = r.nextInt(5) + 1;
}
public void run(){
while(true){
if(isFishCatch){
for (int i = 0; i < 5; i++) {
fishPic = catchedFishPic[0];
try {
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
fishPic = catchedFishPic[1];
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
getOut();
isFishCatch = false;
}else{
fishMove();
try {
Thread.sleep(1000/24);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void fishMove(){
fishPic = fishPics[index++%fishPics.length];
this.x = this.x - this.step;
if(this.x <= -width){
getOut();
}
}
public void getOut(){
this.x = 700;
this.y = r.nextInt(500 - height);
this.step = r.nextInt(5) + 1;
}
}
class Wang{
BufferedImage netPic;
int x;
int y;
int width;
int height;
boolean show;
public Wang() throws IOException{
netPic = ImageIO.read(new File("pic/net09.png"));
width = netPic.getWidth();
height = netPic.getHeight();
show = true;
}
public void wangMove(int x,int y){
this.x = x;
this.y = y;
}
public boolean catchFish(Fishs f){
int dx = this.x - f.x;
int dy = this.y - f.y;
if(dx >= 0 && dx < f.width && dy >= 0 && dy < f.height){
return true;
}
return false;
}
}

BufferedImage netPic;
int x;
int y;
int width;
int height;
boolean show;
public Wang() throws IOException{
netPic = ImageIO.read(new File("pic/net09.png"));
width = netPic.getWidth();
height = netPic.getHeight();
show = true;
}
public void wangMove(int x,int y){
this.x = x;
this.y = y;
}
public boolean catchFish(Fishs f){
int dx = this.x - f.x;
int dy = this.y - f.y;
if(dx >= 0 && dx < f.width && dy >= 0 && dy < f.height){
return true;
}
return false;
}
}