中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何使用java實現捕魚達人游戲

發布時間:2021-09-26 18:03:38 來源:億速云 閱讀:195 作者:小新 欄目:編程語言

這篇文章給大家分享的是有關如何使用java實現捕魚達人游戲的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

源代碼分享:

測試類:

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 { //創建窗口 JFrame gameFrame = new JFrame("捕魚達人");  //將池塘放入到界面中去 Pool pool = new Pool(); gameFrame.setContentPane(pool);  //創建窗口圖標,絕對路徑 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); //設置窗口可以關閉 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; } }

與魚塘的設置:

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; /** * 背景圖片 * 海王 * 魚 * 大炮 * 狀態欄 */ //池塘 private BufferedImage backgroud;  //單條魚// private Fish fish; //多條與 private Fish[] fishes; //狀態欄 private BufferedImage statusImage; //大炮 private Cannon cannon; //鼠標x軸 private int mouseX; //鼠標Y軸 private int mouseY; //漁網 private Net net; //子彈發射的角度 private double theta; //子彈 private LinkedList<Bullet> bullets; //反射原點   public Pool() throws IOException { this.backgroud = ImageIO.read(new File("resource/images/bg.jpg"));// this.fish = new Fish("fish08"); //設置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("fish23");  this.fishes[10] = new Fish("fish24");  //初始化狀態欄 this.statusImage = ImageIO.read(new File("resource/images/bottom-bar.png")); //初始化大炮 this.cannon = new Cannon(); //調用監聽器 this.addListener(); //創建網 this.net = new Net(); //數組定義 this.bullets = new LinkedList<Bullet>(); } private void addListener() { //添加監聽器   this.addMouseListener(new MouseAdapter() {  @Override  public void mouseClicked(MouseEvent arg0) {  System.out.println("發射子彈!");  try {   //創建子彈   Bullet bullet = new Bullet(cannon.getX(), cannon.getY(), theta, Pool.this);   //啟動線程   bullet.start();   //將對象添加到集合中去   bullets.add(bullet);  } catch (IOException e) {   // TODO Auto-generated catch block   e.printStackTrace();  }  }    @Override  public void mouseEntered(MouseEvent arg0) {  //進入,讓漁網顯示  net.setShow(true);  }  @Override  public void mouseExited(MouseEvent arg0) {  //退出,讓漁網消失  net.setShow(false);  } });  //鼠標移動監聽 this.addMouseMotionListener(new MouseAdapter() {  @Override  public void mouseMoved(MouseEvent e) {  mouseX = e.getX() + 20;  mouseY = e.getY();  System.out.println("(" + mouseX+ "," +mouseY +")");  //漁網移動  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); //畫狀態欄 arg0.drawImage(statusImage,15, 400, statusImage.getWidth(), statusImage.getHeight(), null);  //畫大炮 //Graphics:不能畫旋轉的圖片,畫旋轉的圖片需要Graphics2D,創建畫筆 Graphics2D graphics2d = (Graphics2D) arg0.create(); //設置畫筆的角度 //計算大炮的旋轉中心 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);  //畫大炮結束  //畫漁網,drawImage是參數是int類型,所以進行強制轉換 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); }  //畫子彈 //子彈沒有發射子彈之前 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() { //讓魚動起來// this.fish.start(); for (Fish fish : this.fishes) {  fish.start(); } //重新畫界面,匿名內部類 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; } }

魚類的設置:

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;  //魚游動的圖片數組 @SuppressWarnings("unused") private BufferedImage[] images; //抓獲魚的圖片 private BufferedImage[] catchImages; //圖片的下標 @SuppressWarnings("unused") private int imagesIndex;  /** *魚的構造方法 * @param name 魚的圖片名稱 * @throws IOException  */ public Fish(String imageName) throws IOException { //魚游動的初始化 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];  //初始化魚的寬度和高度 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(); }  /** * 魚的游動 */ public void move() { //坐標減去游動的速度 this.x = this.x - this.step;  //切換魚的圖片 this.image = this.images[this.imagesIndex ++ % this.images.length];  //重新游一遍,小于魚與橫坐標則返回 if (this.x < -this.width) {  //重置x坐標  this.x = 800;  //重置y坐標  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(); } } /** * 被捕獲時翻滾 */ public void turnOver() { //切換魚被捕獲時魚的圖片 for (int i = 0; i < 6; i++) {  this.image = this.catchImages[i % this.catchImages.length];  try {  sleep(50);  } catch (InterruptedException e) {  e.printStackTrace();  } } //重置魚的屬性,坐標,速度,是否被抓 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; }}

魚網的設置(這里漁網是靜態的,有缺陷):

package game;/** * 捕魚網 * @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; /** * 漁網構造方法 * @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;   } /** * 漁網的移動 * @param mouseX * @param mouseY */ public void move(double mouseX, double mouseY) { //求漁網的中心點 double centerX = this.x + this.width/2; double centerY = this.y + this.height/2;  //中心點與離鼠標的x位置 double xx = mouseX - centerX; //中心點與離鼠標的y位置 double yy = mouseY - centerY;  //左上角點平移 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; }}

發射的子彈

package game;import java.awt.Point;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import javax.imageio.ImageIO;/** * 發射的子彈 * @author zouzhuo * */public class Bullet extends Thread{ //圖片 private BufferedImage image; //坐標值 private int x; private int y; //大小 private int width; private int height; //是否活著 private boolean isAlive; //速度 private int step; //角度 private double thread; //子彈發射的原點 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; } /** * 子彈移動的速度 */ public void move() { this.y = this.y - this.step;  //判斷出界 int distance = this.point.y - this.y; //求xx,需要進一步進行強制轉換 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); }  //判斷是否擊中魚 Fish[] fishs = pool.getFishes(); for (Fish fish : fishs) {  //魚的x坐標范圍  int maxX = fish.getX() + fish.getWidth();  //魚的y坐標范圍  int mayY = fish.getY() + fish.getHeight();  if (xxx > fish.getX() && xxx < maxX && fish.getY() < yyy && yyy < mayY) {  //設置魚被抓到  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) {  //讓子彈飛一會  if (isAlive) {  move();  }else {  //直接結束線程  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; } }

還有一個計分板沒有寫上,沒有開始結束的界面,漁網是靜態的,這些功能都還沒有實現,日后更新。

感謝各位的閱讀!關于“如何使用java實現捕魚達人游戲”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

合肥市| 湖南省| 禄劝| 湟中县| 连山| 忻城县| 措美县| 沁阳市| 囊谦县| 墨脱县| 安多县| 南江县| 六枝特区| 水富县| 洪湖市| 浑源县| 手机| 开封县| 玛曲县| 安义县| 安塞县| 武夷山市| 贵港市| 博湖县| 贺兰县| 克拉玛依市| 油尖旺区| 通化市| 中卫市| 沙雅县| 中西区| 三穗县| 南溪县| 孝感市| 湖南省| 桓台县| 星子县| 磴口县| 贡嘎县| 平湖市| 伽师县|