實作思路:
界面-->池塘Pool-->池塘的環境-->魚-->漁網
1、完成捕魚達人遊戲的窗體界面
2、完成捕魚達人的背景(環境)功能
3、完成一條靜止不動的魚
(1)定義了Fish類
(2)在Fish類添加了5個特征
(3)在Fish類中添加了構造方法,在構造方法中對5個特征指派
(4)在Pool的特征處,定義了一條魚
Fish f;
(5)在Pool類的構造方法中,建立一條魚,給f指派。 f=new Fish();
(6)在Pool類的paint方法中,使用畫筆
畫出魚f
4、完成一條能夠移動的魚(遊)
(1)讓Fish類繼承Thread
(2)在Fish類的行為處,調出run方法
在run()方法中實作x—
(3)在Pool類的構造方法中,通過f調用start()方法啟動線程
(4)在Pool類中自定義action方法,在
action方法中實作不停的執行paint
方法
(5)在Start類中通過pl調用action()方法
5、魚遊出池塘,在重新進入池塘
6、重新進入池塘,改變y坐标值(不在一條線上遊)
7、魚的速度 1-5随機
8、魚重新進入池塘,速度也發生改變(魚别一直一個速度)
9、魚初始位置的設定
10、完成一條魚的動畫效果
fish08_00.png fish08_01.png … fish08_09.png
10張圖檔 -- 一組魚遊的動畫
11、完成多條同種類型的魚
12、完成多條不同種類型的魚
13、漁網
14、漁網的功能
(1)漁網移動的功能
(2)漁網的隐藏功能
(3)漁網的顯示功能
(4)漁網的捕魚功能
Start.java
package com.lddx.projects;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
//啟動捕魚達人遊戲的主類
public class Start {
public static void main(String[] args) throws Exception{
Start s=new Start();
s.Init();
}
//初始化
public void Init() throws Exception{
JFrame f=new JFrame();
JPanel p=new JPanel();
Pool pl= new Pool();
/*ImageIcon fish=new ImageIcon("images/fish05_00.png");
JLabel label=new JLabel(fish);
p.add(label);
f.add(p);*/
f.add(pl);
f.setTitle("捕魚達人破解版");
f.setVisible(true);
f.setSize(2000, 1000);
f.setLocation(3, 6);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setIconImage(ImageIO.read(new File("images/fish09_00.png")));
pl.action();
}
}
池塘Pool.java
package com.lddx.projects;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
public class Pool extends JPanel{
//背景
BufferedImage bg;
//漁網圖檔
//11種魚
Fish[] fish=new Fish[11];
//漁網
Fishnet net=new Fishnet();
//無參的預設構造函數
public Pool() throws Exception{
bg=ImageIO.read(new File("images/bg.jpg"));
for(int i=0;i<fish.length;i++)
{
fish[i]=new Fish(i+1);//11種不同的魚
fish[i].start();//通過start()方法啟動魚
}
}
//畫筆
public void paint(Graphics g){
g.drawImage(bg, 0, 0,2000, 1000,null);//畫背景
//畫11種不同的魚
for(int i=0;i<fish.length;i++)
{
g.drawImage(fish[i].type,fish[i].x,fish[i].y,fish[i].width,fish[i].height,null);
}
if(net.show)//表示顯示漁網
{
g.drawImage(net.net_image,net.x,net.y,net.width,net.height,null);//畫漁網
}
}
//自定義方法,讓paint()重複執行
public void action(){
//使用匿名内部類繼承MouseAdapter
MouseAdapter ma=new MouseAdapter(){
//滑鼠移動事件的監聽,完成漁網移動的功能
public void mouseMoved(MouseEvent e) {
//System.out.println("滑鼠移動了");
net.change(e.getX(), e.getY());//漁網跟着滑鼠移動
}
//滑鼠點選事件的監聽,完成捕魚的功能
public void mouseClicked(MouseEvent e) {
//System.out.println("滑鼠點選了");
//當監聽到滑鼠單擊了,進行捕魚功能
//将數組中每一條魚取出,檢測這些fish是否被捕捉
for(int i=0;i<fish.length;i++)
{
//表示漁網中線(滑鼠位置)在魚身上,說明捕捉到魚,讓魚消失,重新進入池塘
if(e.getX()>=fish[i].x&&e.getX()<=fish[i].x+fish[i].width
&&e.getY()>=fish[i].y&&e.getY()<=fish[i].y+fish[i].height)
{
fish[i].x=2000;
Random r=new Random();//重新進入池塘,改變y坐标。
fish[i].y=r.nextInt(800-fish[i].height);//随機産生y坐标
fish[i].speed=r.nextInt(10)+1;//随機改變魚的速度
}
}
}
//滑鼠離開事件的監聽,完成漁網隐藏的功能
public void mouseExited(MouseEvent e) {
//System.out.println("滑鼠離開了");
net.show=false;
}
//滑鼠進入事件的監聽,完成漁網顯示的功能
public void mouseEntered(MouseEvent e) {
//System.out.println("滑鼠進來了");
net.show=true;
}
};
//将定義好的匿名内部類綁定到池塘
//使用關鍵字this,表示目前池塘Pool類
this.addMouseListener(ma);
this.addMouseMotionListener(ma);//滑鼠移動
while(true)
{
//repaint()繼承父類JPanel,
repaint();//将paint()調用一次;
}
}
}
魚類Fish.java
package com.lddx.projects;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Random;
import javax.imageio.ImageIO;
//魚類
public class Fish extends Thread{
//種類
BufferedImage type;
//大小
int width,height;
//位置
int x,y;
//速度
int speed;
//狀态
BufferedImage[] state=new BufferedImage[10];
public Fish() throws Exception{
Random r=new Random();
//對魚的特征初始化
type=ImageIO.read(new File("images/fish06_00.png"));
width=type.getWidth();
height=type.getHeight();
x=2000;
y=r.nextInt(800-height);
speed=r.nextInt(6)+1;
for(int i=0;i<state.length;i++)
{
state[i]=ImageIO.read(new File("images/fish06_0"+i+".png"));
}
}
public Fish(int num) throws Exception{
Random r=new Random();
//對魚的特征初始化
type=ImageIO.read(new File("images/fish0"+num+"_00.png"));
width=type.getWidth();
height=type.getHeight();
x=2000;
y=r.nextInt(800-height);
speed=r.nextInt(6)+1;
for(int i=0;i<state.length;i++)
{
state[i]=ImageIO.read(new File("images/fish0"+num+"_0"+i+".png"));
}
}
//行為
//在run方法中實作魚遊動的行為
public void run() {
int index=0;
while(true)
{
x=x-speed;//改變魚的x坐标位置
type=state[(index++)%10];//不停改變魚的狀态
if(x<=-width)
{
x=2000;
Random r=new Random();//重新進入池塘,改變y坐标。
y=r.nextInt(800-height);//随機産生坐标
speed=r.nextInt(10)+1;//随機改變魚的速度
}
try {
Thread.sleep(60);//線程睡眠
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
漁網Fishnet.java
package com.lddx.projects;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class Fishnet{
//漁網
BufferedImage net_image;
//大小
int width,height;
//位置
int x,y;
//漁網的顯示隐藏
boolean show;//為true表示顯示;
//無參的預設構造函數
public Fishnet() throws Exception{
net_image=ImageIO.read(new File("images/net09.png"));
width=net_image.getWidth();
height=net_image.getHeight();
x=100;
y=100;
show=true;
}
//定義修改漁網坐标值的方法
public void change(int x1,int y1){
x=x1-width/2;//漁網的x坐标-漁網寬度的一半
y=y1-height/2;
}
}