java實(shí)現(xiàn)捕魚達(dá)人游戲
本文實(shí)例為大家分享了java實(shí)現(xiàn)捕魚達(dá)人游戲的具體代碼,供大家參考,具體內(nèi)容如下
效果圖如下:
源代碼分享:
測試類:
package game;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
/**
* 測試類
* @author Lenovo
*
*/
public class Client {
public static void main(String[] args) throws IOException {
//創(chuàng)建窗口
JFrame gameFrame = new JFrame("捕魚達(dá)人");
//將池塘放入到界面中去
Pool pool = new Pool();
gameFrame.setContentPane(pool);
//創(chuàng)建窗口圖標(biāo),絕對路徑
BufferedImage icon = ImageIO.read(new File("E:/New_life/fish_game/resource/images/fish07_03.png"));
gameFrame.setIconImage(icon);
//設(shè)置窗口的尺寸
gameFrame.setSize(800, 500);
//窗口的位置
gameFrame.setLocation(10, 10);
//設(shè)置窗口不可拖拽
gameFrame.setResizable(false);
//設(shè)置窗口可以關(guān)閉
gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//讓窗口顯示
gameFrame.setVisible(true);
//調(diào)用方法
pool.action();
}
}
大炮的設(shè)置:
package game;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Cannon {
//大炮的圖片
private BufferedImage image;
//坐標(biāo)值
private int x;
private int y;
public Cannon() throws IOException {
this.image = ImageIO.read(new File("resource/images/barrel.png"));
this.x = 420;
this.y = 400;
}
public BufferedImage getImage() {
return image;
}
public void setImage(BufferedImage image) {
this.image = image;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
與魚塘的設(shè)置:
package game;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
public class Pool extends JPanel{
private static final long serialVersionUID = 1L;
/**
* 背景圖片
* 海王
* 魚
* 大炮
* 狀態(tài)欄
*/
//池塘
private BufferedImage backgroud;
//單條魚
// private Fish fish;
//多條與
private Fish[] fishes;
//狀態(tài)欄
private BufferedImage statusImage;
//大炮
private Cannon cannon;
//鼠標(biāo)x軸
private int mouseX;
//鼠標(biāo)Y軸
private int mouseY;
//漁網(wǎng)
private Net net;
//子彈發(fā)射的角度
private double theta;
//子彈
private LinkedList<Bullet> bullets;
//反射原點(diǎn)
public Pool() throws IOException {
this.backgroud = ImageIO.read(new File("resource/images/bg.jpg"));
// this.fish = new Fish("fish08");
//設(shè)置10條魚
this.fishes = new Fish[11];
for (int i = 0; i < 9; i++) {
String fishName = "fish0" + (i+1);
Fish fish = new Fish(fishName);
this.fishes[i] = fish;
}
this.fishes[9] = new Fish("fish13");
this.fishes[10] = new Fish("fish14");
//初始化狀態(tài)欄
this.statusImage = ImageIO.read(new File("resource/images/bottom-bar.png"));
//初始化大炮
this.cannon = new Cannon();
//調(diào)用監(jiān)聽器
this.addListener();
//創(chuàng)建網(wǎng)
this.net = new Net();
//數(shù)組定義
this.bullets = new LinkedList<Bullet>();
}
private void addListener() {
//添加監(jiān)聽器
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
System.out.println("發(fā)射子彈!");
try {
//創(chuàng)建子彈
Bullet bullet = new Bullet(cannon.getX(), cannon.getY(), theta, Pool.this);
//啟動(dòng)線程
bullet.start();
//將對象添加到集合中去
bullets.add(bullet);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void mouseEntered(MouseEvent arg0) {
//進(jìn)入,讓漁網(wǎng)顯示
net.setShow(true);
}
@Override
public void mouseExited(MouseEvent arg0) {
//退出,讓漁網(wǎng)消失
net.setShow(false);
}
});
//鼠標(biāo)移動(dòng)監(jiān)聽
this.addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
mouseX = e.getX() + 20;
mouseY = e.getY();
System.out.println("(" + mouseX+ "," +mouseY +")");
//漁網(wǎng)移動(dòng)
net.move(mouseX, mouseY);
}
});
}
/**
* 畫界面
*/
@Override
public void paint(Graphics arg0) {
super.paint(arg0);
arg0.drawImage(backgroud, 0, 0, backgroud.getWidth(), backgroud.getHeight(), null);
for (int i = 0; i < fishes.length; i++) {
Fish fish = this.fishes[i];
arg0.drawImage(fish.getImage(),
fish.getX(),
fish.getY(),
fish.getWidth(),
fish.getHeight(), null);
}
// arg0.drawImage(this.fish.getImage(), this.fish.getX(), this.fish.getY(), this.fish.getWidth(), this.fish.getHeight(), null);
//畫狀態(tài)欄
arg0.drawImage(statusImage,15, 400, statusImage.getWidth(), statusImage.getHeight(), null);
//畫大炮
//Graphics:不能畫旋轉(zhuǎn)的圖片,畫旋轉(zhuǎn)的圖片需要Graphics2D,創(chuàng)建畫筆
Graphics2D graphics2d = (Graphics2D) arg0.create();
//設(shè)置畫筆的角度
//計(jì)算大炮的旋轉(zhuǎn)中心
double centerX = this.cannon.getX() + this.cannon.getImage().getWidth()/2;
double centerY = this.cannon.getY() + this.cannon.getImage().getHeight()/4*3;
double xx = this.mouseX - centerX;
double yy = this.mouseY - centerY;
//求反切角度
this.theta =-Math.atan(xx/yy);
graphics2d.rotate(theta, centerX ,centerY);
graphics2d.drawImage(this.cannon.getImage(),
this.cannon.getX(),
this.cannon.getY(),
this.cannon.getImage().getWidth(),
this.cannon.getImage().getHeight(), null);
//畫大炮結(jié)束
//畫漁網(wǎng),drawImage是參數(shù)是int類型,所以進(jìn)行強(qiáng)制轉(zhuǎn)換
if (this.net.isShow()) {
arg0.drawImage(this.net.getImage(),
(int)this.net.getX(),
(int)this.net.getY(),
(int)this.net.getImage().getWidth(),
(int)this.net.getImage().getHeight(),null);
}
//畫子彈
//子彈沒有發(fā)射子彈之前
for (Bullet bullet : bullets) {
Graphics2D graphics2d2 = (Graphics2D)arg0.create();
graphics2d2.rotate(bullet.getThread(), centerX, centerY);
graphics2d2.drawImage(bullet.getImage(),
bullet.getX(),
bullet.getY(),
bullet.getWidth(),
bullet.getHeight(), null);
}
}
/**
* 請開始你的表演
*/
public void action() {
//讓魚動(dòng)起來
// this.fish.start();
for (Fish fish : this.fishes) {
fish.start();
}
//重新畫界面,匿名內(nèi)部類
new Thread() {
public void run() {
while (true) {
repaint();
}
};
}.start();
}
public LinkedList<Bullet> getBullets() {
return bullets;
}
public void setBullets(LinkedList<Bullet> bullets) {
this.bullets = bullets;
}
public Fish[] getFishes() {
return fishes;
}
public void setFishes(Fish[] fishes) {
this.fishes = fishes;
}
}
魚類的設(shè)置:
package game;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
public class Fish extends Thread{
//寬度
@SuppressWarnings("unused")
private int width;
@SuppressWarnings("unused")
private int height;
//位置
//x坐標(biāo)
@SuppressWarnings("unused")
private int x;
//y坐標(biāo)
@SuppressWarnings("unused")
private int y;
//圖片
@SuppressWarnings("unused")
private BufferedImage image;
//速度
@SuppressWarnings("unused")
private int step;
//是否被抓
@SuppressWarnings("unused")
private boolean isCatch;
//魚游動(dòng)的圖片數(shù)組
@SuppressWarnings("unused")
private BufferedImage[] images;
//抓獲魚的圖片
private BufferedImage[] catchImages;
//圖片的下標(biāo)
@SuppressWarnings("unused")
private int imagesIndex;
/**
*魚的構(gòu)造方法
* @param name 魚的圖片名稱
* @throws IOException
*/
public Fish(String imageName) throws IOException {
//魚游動(dòng)的初始化
this.images = new BufferedImage[10];
for (int i = 0; i < 10; i++) {
String fishName = imageName + "_0" + i + ".png";
BufferedImage tempImage = ImageIO.read(new File("resource/images/" + fishName));
images[i] = tempImage;
}
//初始化圖片下標(biāo)
this.imagesIndex = 0;
this.image = this.images[this.imagesIndex];
//初始化魚的寬度和高度
this.width = this.image.getWidth();
this.height = this.image.getHeight();
//初始化x和y的坐標(biāo)
this.x = 800;
Random random = new Random();
int nextInt = random.nextInt(400);
this.y = nextInt;
//初始化速度
this.step = random.nextInt(5);
//初始化是否被抓住
this.isCatch = false;
this.catchImages = new BufferedImage[2];
this.catchImages[0] = ImageIO.read(new File("resource/images/" + imageName + "_catch_01.png"));
// this.width = image.getWidth();
}
/**
* 魚的游動(dòng)
*/
public void move() {
//坐標(biāo)減去游動(dòng)的速度
this.x = this.x - this.step;
//切換魚的圖片
this.image = this.images[this.imagesIndex ++ % this.images.length];
//重新游一遍,小于魚與橫坐標(biāo)則返回
if (this.x < -this.width) {
//重置x坐標(biāo)
this.x = 800;
//重置y坐標(biāo)
Random random = new Random();
this.y = random.nextInt(375);
//重置魚游的速度
this.step = random.nextInt(5) + 1;
}
//休眠
try {
sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 被捕獲時(shí)翻滾
*/
public void turnOver() {
//切換魚被捕獲時(shí)魚的圖片
for (int i = 0; i < 6; i++) {
this.image = this.catchImages[i % this.catchImages.length];
try {
sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//重置魚的屬性,坐標(biāo),速度,是否被抓
this.x = 800;
Random random = new Random();
this.y = random.nextInt(375);
this.step = random.nextInt(5) + 1;
this.isCatch = false;
}
@Override
public void run() {
while (true) {
if (this.isCatch) {
turnOver();
}else {
move();
}
}
}
/**
* 生成了魚的屬性set和get方法
* @return
*/
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public BufferedImage getImage() {
return image;
}
public void setImage(BufferedImage image) {
this.image = image;
}
public boolean isCatch() {
return isCatch;
}
public void setCatch(boolean isCatch) {
this.isCatch = isCatch;
}
}
魚網(wǎng)的設(shè)置(這里漁網(wǎng)是靜態(tài)的,有缺陷):
package game;
/**
* 捕魚網(wǎng)
* @author Lenovo
*
*/
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Net {
//圖片
private BufferedImage image;
//X坐標(biāo)
private double x;
//Y坐標(biāo)
private double y;
//寬度
private double width;
//高度
private double height;
//是否展示
private boolean isShow;
/**
* 漁網(wǎng)構(gòu)造方法
* @throws IOException
*/
public Net() throws IOException {
//初始化圖片
this.image = ImageIO.read(new File("resource/images/net09.png"));
this.x = 100;
this.y = 100;
this.width = this.image.getWidth();
this.height = this.image.getHeight();
this.isShow = true;
}
/**
* 漁網(wǎng)的移動(dòng)
* @param mouseX
* @param mouseY
*/
public void move(double mouseX, double mouseY) {
//求漁網(wǎng)的中心點(diǎn)
double centerX = this.x + this.width/2;
double centerY = this.y + this.height/2;
//中心點(diǎn)與離鼠標(biāo)的x位置
double xx = mouseX - centerX;
//中心點(diǎn)與離鼠標(biāo)的y位置
double yy = mouseY - centerY;
//左上角點(diǎn)平移
this.x = this.x + xx;
this.y = this.y + yy;
}
public BufferedImage getImage() {
return image;
}
public void setImage(BufferedImage image) {
this.image = image;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public boolean isShow() {
return isShow;
}
public void setShow(boolean isShow) {
this.isShow = isShow;
}
}
發(fā)射的子彈
package game;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
/**
* 發(fā)射的子彈
* @author zouzhuo
*
*/
public class Bullet extends Thread{
//圖片
private BufferedImage image;
//坐標(biāo)值
private int x;
private int y;
//大小
private int width;
private int height;
//是否活著
private boolean isAlive;
//速度
private int step;
//角度
private double thread;
//子彈發(fā)射的原點(diǎn)
private Point point;
//池塘
private Pool pool;
public Bullet(int x, int y, Double thread, Pool pool) throws IOException {
this.image = ImageIO.read(new File("resource/images/bullet1.png"));
this.width = this.image.getWidth();
this.height = this.image.getHeight();
this.isAlive = true;
this.step = 10;
this.x = x;
this.y = y;
this.thread = thread;
this.point = new Point(x, y);
// this.point.x = x;
// this.point.y =y;
this.pool = pool;
}
/**
* 子彈移動(dòng)的速度
*/
public void move() {
this.y = this.y - this.step;
//判斷出界
int distance = this.point.y - this.y;
//求xx,需要進(jìn)一步進(jìn)行強(qiáng)制轉(zhuǎn)換
int xx = (int) (distance * Math.sin(this.thread));
int xxx = this.point.x + xx;
//求yy坐標(biāo)
int yy = (int) (distance * Math.cos(this.thread));
int yyy = this.point.y - yy;
//判斷是否出界
if (xxx < 0 || xxx > 800 || yyy < 0) {
//將子彈置為死亡
this.isAlive = false;
//在數(shù)組中刪除子彈
this.pool.getBullets().remove(this);
}
//判斷是否擊中魚
Fish[] fishs = pool.getFishes();
for (Fish fish : fishs) {
//魚的x坐標(biāo)范圍
int maxX = fish.getX() + fish.getWidth();
//魚的y坐標(biāo)范圍
int mayY = fish.getY() + fish.getHeight();
if (xxx > fish.getX() && xxx < maxX && fish.getY() < yyy && yyy < mayY) {
//設(shè)置魚被抓到
fish.setCatch(true);
//設(shè)置讓子彈消失
this.isAlive = false;
//在數(shù)組中刪除子彈
this.pool.getBullets().remove(this);
}
}
try {
sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void run() {
super.run();
while (true) {
//讓子彈飛一會
if (isAlive) {
move();
}else {
//直接結(jié)束線程
return;
}
}
}
public BufferedImage getImage() {
return image;
}
public void setImage(BufferedImage image) {
this.image = image;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public double getThread() {
return thread;
}
public void setThread(double thread) {
this.thread = thread;
}
}
還有一個(gè)計(jì)分板沒有寫上,沒有開始結(jié)束的界面,漁網(wǎng)是靜態(tài)的,這些功能都還沒有實(shí)現(xiàn),日后更新。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:Spring Cloud基于zuul實(shí)現(xiàn)網(wǎng)關(guān)過程解析
欄 目:Java
下一篇:java 數(shù)學(xué)計(jì)算的具體使用
本文標(biāo)題:java實(shí)現(xiàn)捕魚達(dá)人游戲
本文地址:http://www.jygsgssxh.com/a1/Java/8782.html
您可能感興趣的文章
- 01-10Java實(shí)現(xiàn)動(dòng)態(tài)模擬時(shí)鐘
- 01-10利用Java實(shí)現(xiàn)復(fù)制Excel工作表功能
- 01-10JavaWeb實(shí)現(xiàn)郵件發(fā)送功能
- 01-10java基于poi導(dǎo)出excel透視表代碼實(shí)例
- 01-10Java實(shí)現(xiàn)動(dòng)態(tài)數(shù)字時(shí)鐘
- 01-10基于Java驗(yàn)證jwt token代碼實(shí)例
- 01-10java實(shí)現(xiàn)液晶數(shù)字字體顯示當(dāng)前時(shí)間
- 01-10淺談Java中真的只有值傳遞么
- 01-10Java動(dòng)態(tài)顯示當(dāng)前日期和時(shí)間
- 01-10如何解決線程太多導(dǎo)致java socket連接池出現(xiàn)的問題


閱讀排行
本欄相關(guān)
- 01-10Java實(shí)現(xiàn)動(dòng)態(tài)模擬時(shí)鐘
- 01-10Springboot中@Value的使用詳解
- 01-10JavaWeb實(shí)現(xiàn)郵件發(fā)送功能
- 01-10利用Java實(shí)現(xiàn)復(fù)制Excel工作表功能
- 01-10Java實(shí)現(xiàn)動(dòng)態(tài)數(shù)字時(shí)鐘
- 01-10java基于poi導(dǎo)出excel透視表代碼實(shí)例
- 01-10java實(shí)現(xiàn)液晶數(shù)字字體顯示當(dāng)前時(shí)間
- 01-10基于Java驗(yàn)證jwt token代碼實(shí)例
- 01-10Java動(dòng)態(tài)顯示當(dāng)前日期和時(shí)間
- 01-10淺談Java中真的只有值傳遞么
隨機(jī)閱讀
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文


