- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) >
- 如何使用java實(shí)現捕魚(yú)達人游戲
這篇文章給大家分享的是有關(guān)如何使用java實(shí)現捕魚(yú)達人游戲的內容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
源代碼分享:
測試類(lèi):
package game;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import javax.imageio.ImageIO;import javax.swing.JFrame;/** * 測試類(lèi) * @author Lenovo * */public class Client { public static void main(String[] args) throws IOException { //創(chuàng )建窗口 JFrame gameFrame = new JFrame("捕魚(yú)達人"); //將池塘放入到界面中去 Pool pool = new Pool(); gameFrame.setContentPane(pool); //創(chuàng )建窗口圖標,絕對路徑 BufferedImage icon = ImageIO.read(new File("E:/New_life/fish_game/resource/images/fish07_03.png")); gameFrame.setIconImage(icon); //設置窗口的尺寸 gameFrame.setSize(800, 500); //窗口的位置 gameFrame.setLocation(10, 10); //設置窗口不可拖拽 gameFrame.setResizable(false); //設置窗口可以關(guān)閉 gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //讓窗口顯示 gameFrame.setVisible(true); //調用方法 pool.action(); }}
大炮的設置:
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; //坐標值 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; } }
與魚(yú)塘的設置:
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; /** * 背景圖片 * 海王 * 魚(yú) * 大炮 * 狀態(tài)欄 */ //池塘 private BufferedImage backgroud; //單條魚(yú)// private Fish fish; //多條與 private Fish[] fishes; //狀態(tài)欄 private BufferedImage statusImage; //大炮 private Cannon cannon; //鼠標x軸 private int mouseX; //鼠標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"); //設置10條魚(yú) 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("fish23"); this.fishes[10] = new Fish("fish24"); //初始化狀態(tài)欄 this.statusImage = ImageIO.read(new File("resource/images/bottom-bar.png")); //初始化大炮 this.cannon = new Cannon(); //調用監聽(tīng)器 this.addListener(); //創(chuàng )建網(wǎng) this.net = new Net(); //數組定義 this.bullets = new LinkedList<Bullet>(); } private void addListener() { //添加監聽(tīng)器 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)線(xiàn)程 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); } }); //鼠標移動(dòng)監聽(tīng) 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); } }); } /** * 畫(huà)界面 */ @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); //畫(huà)狀態(tài)欄 arg0.drawImage(statusImage,15, 400, statusImage.getWidth(), statusImage.getHeight(), null); //畫(huà)大炮 //Graphics:不能畫(huà)旋轉的圖片,畫(huà)旋轉的圖片需要Graphics2D,創(chuàng )建畫(huà)筆 Graphics2D graphics2d = (Graphics2D) arg0.create(); //設置畫(huà)筆的角度 //計算大炮的旋轉中心 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); //畫(huà)大炮結束 //畫(huà)漁網(wǎng),drawImage是參數是int類(lèi)型,所以進(jì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); } //畫(huà)子彈 //子彈沒(méi)有發(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); } } /** * 請開(kāi)始你的表演 */ public void action() { //讓魚(yú)動(dòng)起來(lái)// this.fish.start(); for (Fish fish : this.fishes) { fish.start(); } //重新畫(huà)界面,匿名內部類(lè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; } }
魚(yú)類(lèi)的設置:
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坐標 @SuppressWarnings("unused") private int x; //y坐標 @SuppressWarnings("unused") private int y; //圖片 @SuppressWarnings("unused") private BufferedImage image; //速度 @SuppressWarnings("unused") private int step; //是否被抓 @SuppressWarnings("unused") private boolean isCatch; //魚(yú)游動(dòng)的圖片數組 @SuppressWarnings("unused") private BufferedImage[] images; //抓獲魚(yú)的圖片 private BufferedImage[] catchImages; //圖片的下標 @SuppressWarnings("unused") private int imagesIndex; /** *魚(yú)的構造方法 * @param name 魚(yú)的圖片名稱(chēng) * @throws IOException */ public Fish(String imageName) throws IOException { //魚(yú)游動(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; } //初始化圖片下標 this.imagesIndex = 0; this.image = this.images[this.imagesIndex]; //初始化魚(yú)的寬度和高度 this.width = this.image.getWidth(); this.height = this.image.getHeight(); //初始化x和y的坐標 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(); } /** * 魚(yú)的游動(dòng) */ public void move() { //坐標減去游動(dòng)的速度 this.x = this.x - this.step; //切換魚(yú)的圖片 this.image = this.images[this.imagesIndex ++ % this.images.length]; //重新游一遍,小于魚(yú)與橫坐標則返回 if (this.x < -this.width) { //重置x坐標 this.x = 800; //重置y坐標 Random random = new Random(); this.y = random.nextInt(375); //重置魚(yú)游的速度 this.step = random.nextInt(5) + 1; } //休眠 try { sleep(50); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 被捕獲時(shí)翻滾 */ public void turnOver() { //切換魚(yú)被捕獲時(shí)魚(yú)的圖片 for (int i = 0; i < 6; i++) { this.image = this.catchImages[i % this.catchImages.length]; try { sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } } //重置魚(yú)的屬性,坐標,速度,是否被抓 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(); } } } /** * 生成了魚(yú)的屬性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; }}
魚(yú)網(wǎng)的設置(這里漁網(wǎng)是靜態(tài)的,有缺陷):
package game;/** * 捕魚(yú)網(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坐標 private double x; //Y坐標 private double y; //寬度 private double width; //高度 private double height; //是否展示 private boolean isShow; /** * 漁網(wǎng)構造方法 * @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)與離鼠標的x位置 double xx = mouseX - centerX; //中心點(diǎn)與離鼠標的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; //坐標值 private int x; private int y; //大小 private int width; private int height; //是否活著(zhù) 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)行強制轉換 int xx = (int) (distance * Math.sin(this.thread)); int xxx = this.point.x + xx; //求yy坐標 int yy = (int) (distance * Math.cos(this.thread)); int yyy = this.point.y - yy; //判斷是否出界 if (xxx < 0 || xxx > 800 || yyy < 0) { //將子彈置為死亡 this.isAlive = false; //在數組中刪除子彈 this.pool.getBullets().remove(this); } //判斷是否擊中魚(yú) Fish[] fishs = pool.getFishes(); for (Fish fish : fishs) { //魚(yú)的x坐標范圍 int maxX = fish.getX() + fish.getWidth(); //魚(yú)的y坐標范圍 int mayY = fish.getY() + fish.getHeight(); if (xxx > fish.getX() && xxx < maxX && fish.getY() < yyy && yyy < mayY) { //設置魚(yú)被抓到 fish.setCatch(true); //設置讓子彈消失 this.isAlive = false; //在數組中刪除子彈 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) { //讓子彈飛一會(huì ) if (isAlive) { move(); }else { //直接結束線(xiàn)程 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è)計分板沒(méi)有寫(xiě)上,沒(méi)有開(kāi)始結束的界面,漁網(wǎng)是靜態(tài)的,這些功能都還沒(méi)有實(shí)現,日后更新。
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站