1、捕鱼达人实验报告山西财经大学 JAVA实用教程实验报告 年 月 日实验题目捕鱼达人选课课号学 院班 级姓 名学 号理论课教师上机指导教师实验目的及要求:捕鱼达人游戏的要求:1. 在鱼池有很多鱼且鱼各自游动2. 有一张渔网,随鼠标移动点击鼠标即可抓住渔网中的与3. 抓到鱼进行积分实验原理、详细操作步骤及程序代码:1. 理清对象之间的数据关系。对象:窗口鱼池,多条鱼,渔网;2. 数据模型:鱼和渔网的width,height,x,y;3. 类的设计4. 程序代码:import java.awt.*;import java.awt.event.*;import java.awt.image.Buffe
2、redImage;import java.io.File;import javax.imageio.ImageIO;import javax.swing.*;import java.util.Random;public class FishDemo extends Thread public static void main(String args) throws Exception FishDemo fish=new FishDemo(); fish.start(); public void run() JFrame frame=new JFrame(捕鱼达人); Pool pool; tr
3、y pool = new Pool(); frame.add(pool); frame.setSize(800,510); frame.setResizable(false); frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); frame.setVisible(true); pool.action(); catch (Exception e) e.printStackTrace(); class Pool extends JPanel BufferedImage bg; Fish allfish; Net net; int score;
4、 public Pool() throws Exception bg = ImageIO.read(new File(bg.jpg); allfish = new Fish9 + 2 + 9; for (int i = 0; i 9; i+) allfishi = new Fish(fish0 + (i + 1); allfishi + 9 = new Fish(fish0 + (i + 1); allfishallfish.length-2 = new Fish(fish13); allfishallfish.length-1 = new Fish(fish14); net=new Net(
5、); score=0; public void action() throws InterruptedException for (int i = 0; i allfish.length; i+) allfishi.start(); MouseAdapter l=new MouseAdapter() public void mouseEntered(MouseEvent e) net.show=true; public void mousePressed(MouseEvent e) for (int i = 0; i allfish.length; i+) Fish fish=allfishi
6、; if(fish.catain(net.x,net.y) fish.catchFish(); score+=fish.width/10; public void mouseMoved(MouseEvent e) int x=e.getX(); int y=e.getY(); net.x=x; net.y=y; public void mouseExited(MouseEvent e) net.show=false; ; this.addMouseListener(l); this.addMouseMotionListener(l); while (true) repaint(); Threa
7、d.sleep(1000 /24); public void paint(Graphics g) g.drawImage(bg, 0, 0, null); for (int i = 0; i allfish.length; i+) Fish fish = allfishi; 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); g.setColor(Color.white); g.setFon
8、t(new Font(黑体, Font.BOLD, 15); g.drawString(得分:+score, 10, 20); class Net int x; int y; int width; int height; boolean show; BufferedImage image; public Net() throws Exception image = ImageIO.read(new File(net09.png); width=image.getWidth(); height=image.getHeight(); x=0; y=0; show=false; class Fish
9、 extends Thread int x; int y; int height; int width; BufferedImage image; BufferedImage images; int index; int step; public Fish(String pre) throws Exception images = new BufferedImage10; for (int i = 0; i 9; i+) String filename = pre + _0 + (i + 1) + .png; imagesi = ImageIO.read(new File(filename);
10、 System.out.println(filename); images9 = ImageIO.read(new File(pre + _10.png); image = images0; width = image.getWidth(); height = image.getHeight(); Random random = new Random(); x = random.nextInt(800 - width); y = random.nextInt(480 - height); step = random.nextInt(3) + 1; public void run() while (true) x -= step; if (x 0&dx0&dyheight; 组内分工(可选)实验中发现的问题及解决方法、实验运行效果:心得体会教师评语成 绩
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1