第五天完整案例,抓魚遊戲

這是個完整的代碼,部分圖片我就不上傳了



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

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章