JAVA连连看课程设计报告.doc_第1页
JAVA连连看课程设计报告.doc_第2页
JAVA连连看课程设计报告.doc_第3页
JAVA连连看课程设计报告.doc_第4页
JAVA连连看课程设计报告.doc_第5页
已阅读5页,还剩17页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

滨江学院实 验 报 告| 实验名称 JAVA小游戏(连连看)设计 课程名称 智能手机程序设计 | 专业班级:信息工程1班 学生姓名:车宇翔 学 号:20112309002 指导教师:高超 学 期:2013-2014(2) 成 绩: 【选题背景】: 连连看游戏经验,玩法简单,休闲,益智,趣味,广受欢迎。【选题目的】:学会JAVA程序开发的环境搭建与配置,并在实际运用中学习和掌握JAVA程序开发的全过程。进一步熟悉掌握JAVA程序设计语音的基础内容,如用户图形界面设计、JAVA多线程编程、JAVA数据库编程等。通过亲自动手写程序,拓展知识面,锻炼调试能力。【系统分析与设计】:功能分析:实现连连看的基本游戏功能和重置、提示、消除功能设计:通过对图片的调用以及设置是否可见来完成连连看的效果【课程设计中碰到的问题及解决方案】:1.不知道如何进行对数组中两个元素是否可以消除的判断2.时间条的动态表现解决方案:1. 对每个相同图案进行循环判断,直到找出满足条件的情况boolean verticalMatch(Point a, Point b) / 竖线上的判断 boolean horizonMatch(Point a, Point b) / 横线上的判断2.为了保证动画过程和游戏过程的平行运行,因此将动画分离成一个独立的控件,并且要保证动画有自己单独的线程来运行。当每次用户的分数发生变化时,我们可以使用 setScore(int l, int c) 方法同步分数显示的动画效果。【程序输出结果】:游戏开始【程序代码】:ImageFactorypackage nicholas.game.kyodai;import javax.swing.ImageIcon;import .*;public class ImageFactory private static ImageFactory imagefactory; private static ImageIcon images; private ImageFactory() images = new ImageIcon54; URLClassLoader loader = (URLClassLoader)getClass().getClassLoader(); for(int i=0;i39;i+) imagesi = new ImageIcon(getClass().getResource(images/+i+.gif); images39 = new ImageIcon(getClass().getResource(images/dots.gif); images40 = new ImageIcon(getClass().getResource(images/ico.gif); images41 = new ImageIcon(getClass().getResource(images/topbar.gif); images42 = new ImageIcon(getClass().getResource(images/splash.gif); images43 = new ImageIcon(getClass().getResource(images/sico.gif); public ImageIcon getImageicon(int i) return imagesi; public static synchronized ImageFactory getInstance() if(imagefactory != null) return imagefactory; else imagefactory = new ImageFactory(); return imagefactory; KyodaiGridpackage nicholas.game.kyodai;import java.awt.*;import javax.swing.*;public class KyodaiGrid extends JLabel private int xpos;private int ypos;public KyodaiGrid(int x, int y) xpos = x;ypos = y;this.setHorizontalAlignment(SwingConstants.CENTER);public int getXpos() return xpos;public int getYpos() return ypos;public boolean isPassable() return !isVisible();LevelInfopackage nicholas.game.kyodai;import java.io.Serializable;public class LevelInfo implements Serializable /xBound为行号,yBound为列号private int xBound;private int yBound;public LevelInfo() xBound = 16;yBound = 9;public LevelInfo(int x, int y)xBound = x;yBound = y;public int getXBound() return xBound;public int getYBound() return yBound;MainFrame.javapackage nicholas.game.kyodai;import java.awt.*;import java.awt.event.*;import java.io.*;import javax.swing.*;import nicholas.swing.AboutDialog;import nicholas.swing.JSplashWindow;public class MainFrame extends JFrame implements ActionListener private JMenuItem aboutItem;/菜单栏private JMenuItem exitItem;private JMenuItem startItem;private JMenuItem optionItem;private JMenuItem tipItem;private JMenuItem refreshItem;private JMenuItem logItem;private JMenuItem bombItem;private JMenuItem pauseItem;private MainPanel mainPanel;/完成主要功能private LevelInfo levelInfo;public MainFrame() super(连连看);levelInfo = new LevelInfo();/设定游戏大小setMenuBar();/设置菜单setUI();setIconImage(ImageFactory.getInstance().getImageicon(43).getImage();setSize(650,520);Dimension screen = getToolkit().getScreenSize();setLocation(screen.width-getSize().width)/2, (screen.height-getSize().height)/2);this.setVisible(true);addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0););private void setMenuBar() JMenu fileMenu = new JMenu(游戏(G);JMenu helpMenu = new JMenu(帮助(H);JMenu contMenu = new JMenu(辅助(C);fileMenu.setMnemonic(G);helpMenu.setMnemonic(H);contMenu.setMnemonic(C);startItem = new JMenuItem(开局(N);startItem.setMnemonic(N);startItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F2,0);pauseItem = new JMenuItem(暂停(P);pauseItem.setMnemonic(P);pauseItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_PAUSE,0);refreshItem = new JMenuItem(刷新(R);refreshItem.setMnemonic(R);refreshItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F3,0);tipItem = new JMenuItem(提示(T);tipItem.setMnemonic(T);tipItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5,0);optionItem = new JMenuItem(选项(O).);optionItem.setMnemonic(O);logItem = new JMenuItem(排行榜(B).);logItem.setMnemonic(B);exitItem = new JMenuItem(退出(X);exitItem.setMnemonic(X);aboutItem = new JMenuItem(关于(A).);aboutItem.setMnemonic(A);aboutItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1,0);bombItem = new JMenuItem(炸弹(M);bombItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4,0);bombItem.setMnemonic(M);startItem.addActionListener(this);pauseItem.addActionListener(this);refreshItem.addActionListener(this);tipItem.addActionListener(this);optionItem.addActionListener(this);logItem.addActionListener(this);exitItem.addActionListener(this);aboutItem.addActionListener(this);bombItem.addActionListener(this);fileMenu.add(startItem);fileMenu.add(pauseItem);contMenu.add(refreshItem);contMenu.add(bombItem);contMenu.add(tipItem);fileMenu.addSeparator();fileMenu.add(exitItem);helpMenu.add(aboutItem);helpMenu.add(contMenu);JMenuBar bar = new JMenuBar();bar.add(fileMenu);bar.add(helpMenu);setJMenuBar(bar);private void setUI() mainPanel = new MainPanel(levelInfo);getContentPane().add(mainPanel,BorderLayout.CENTER);public static void main(String args) MainFrame application = new MainFrame();private void showAboutDialog() String s1=作者: 车宇翔;String s2=邮箱: 531608022;String s3=Have Fun!;TextArea ta=new TextArea();ta.setText(s1+n+n+n+s2+n+n+n+s3);ta.setEditable(false);JFrame f=new JFrame(关于);f.setLocation(300, 300);f.setSize(200,200);f.add(ta);f.setBackground(new Color(200,120,150);f.setResizable(false);f.setVisible(true);public void actionPerformed(ActionEvent ae) if(ae.getSource()=startItem) mainPanel.restart(); else if(ae.getSource()=pauseItem) mainPanel.setPaused(!mainPanel.isPaused(); else if(ae.getSource()=exitItem) System.exit(0); else if(ae.getSource()=aboutItem) showAboutDialog(); else if(ae.getSource()=bombItem) mainPanel.useBomb(); else if(ae.getSource()=refreshItem) mainPanel.refresh(); else if(ae.getSource()=tipItem) mainPanel.showNext();MainPanel.javapackage nicholas.game.kyodai;import java.awt.*;import java.awt.event.*;import java.util.Vector;import javax.swing.*;import javax.swing.border.Border;import nicholas.game.kyodai.*;public class MainPanel extends JPanel private int BOMB = 5;private int BOMBP = 200;private int REFRESH = 4;private int REFRP = 250;private int TIP = 7;private int TIPP = 120;private int PROGRESS = 1200;private int xBound;private int yBound;private int pcount;private int score;private int refreshcount;private int bombcount;private int tipcount;private LevelInfo levelInfo;private GridMouseAdapter gma;private KyodaiGrid grid;private KyodaiGrid nexts, nexte;private Border selectedBorder;private Border opaqueBorder;private Border tipBorder;private Vector path;private Thread pthread;private JProgressBar progress;private JLabel scoreLabel;private JLabel refreshLabel;private JLabel bombLabel;private JLabel tipLabel;private JPanel gridPanel;private boolean wingame;public MainPanel(LevelInfo li) super(new BorderLayout();levelInfo = li;path = new Vector3;path0 = new Vector();path1 = new Vector();path2 = new Vector();setBackground(Color.black);gma = new GridMouseAdapter();opaqueBorder = BorderFactory.createLineBorder(getBackground();/selectedBorder = BorderFactory.createLineBorder(Color.red);selectedBorder = BorderFactory.createLineBorder(Color.red);tipBorder = BorderFactory.createLineBorder(Color.green); setGridPanel(); setStatusPanel();/* *设置状态面板 */private void setStatusPanel() wingame = false;JPanel panel = new JPanel();panel.setBackground(Color.black);JLabel label = new JLabel(剩余时间:);label.setForeground(Color.white);panel.add(label);progress = new JProgressBar(0,PROGRESS);/时间条显示progress.setValue(PROGRESS);progress.setPreferredSize(new Dimension(400,20);progress.setForeground(Color.blue);progress.setBorderPainted(false);panel.add(progress);score = 0;scoreLabel = new JLabel(+score);scoreLabel.setForeground(Color.yellow);scoreLabel.setFont(new Font(Dialog,Font.BOLD,25);scoreLabel.setHorizontalAlignment(SwingConstants.RIGHT);scoreLabel.setPreferredSize(new Dimension(100,20);panel.add(scoreLabel);add(panel,BorderLayout.NORTH);panel = new JPanel();panel.setBackground(Color.black);label = new JLabel(剩余提示:);label.setForeground(Color.yellow);panel.add(label);tipcount = TIP;tipLabel = new JLabel(+tipcount);tipLabel.setForeground(Color.green);panel.add(tipLabel);label = new JLabel(剩余炸弹:);label.setForeground(Color.yellow);panel.add(label);bombcount = BOMB;bombLabel = new JLabel(+bombcount);bombLabel.setForeground(Color.green);panel.add(bombLabel);label = new JLabel(可用刷新:);label.setForeground(Color.yellow);panel.add(label);refreshcount = REFRESH;refreshLabel = new JLabel(+refreshcount);refreshLabel.setForeground(Color.green);panel.add(refreshLabel);add(panel,BorderLayout.SOUTH);pthread = new ProgressThread();pthread.start();private void setGridPanel() /完成布局gridPanel = new JPanel();gridPanel.setBackground(getBackground();xBound = levelInfo.getXBound()+2;yBound = levelInfo.getYBound()+2;gridPanel.setLayout(new GridLayout(yBound,xBound,0,0);grid = new KyodaiGridyBoundxBound;int count = 0;int sub = levelInfo.getXBound()*levelInfo.getYBound()/4;KyodaiGrid temp = new KyodaiGridxBound*yBound;for(int y=0;yyBound;y+) for(int x=0;xxBound;x+) gridyx = new KyodaiGrid(x, y);if(x=0|x=(xBound-1)|y=0|y=(yBound-1) gridyx.setIcon(ImageFactory.getInstance().getImageicon(39);gridyx.setVisible(false); else gridyx.setIcon(ImageFactory.getInstance().getImageicon(count%sub);gridyx.setBorder(opaqueBorder);gridyx.addMouseListener(gma);tempcount = gridyx;count+;gridPanel.add(gridyx);JPanel t = new JPanel();t.setBackground(Color.black);t.add(gridPanel);add(t,BorderLayout.CENTER);shuffle(temp, count);/* *开始新游戏 */public void restart() /重新开始resetStatusPanel();resetGridPanel();/* *重置面板状态和游戏图标 */private void resetStatusPanel() wingame = false;score = 0;scoreLabel.setText(+score);bombcount = BOMB;bombLabel.setText(+bombcount);refreshcount = REFRESH;refreshLabel.setText(+refreshcount);tipcount = TIP;tipLabel.setText(+tipcount);progress.setValue(PROGRESS);pthread.resume();private void resetGridPanel() int count = 0;int sub = (xBound-2)*(yBound-2)/4;KyodaiGrid temp = new KyodaiGridxBound*yBound;for(int y=1;yyBound-1;y+) for(int x=1;xxBound-1;x+)gridyx.setIcon(ImageFactory.getInstance().getImageicon(count%sub);gridyx.setBorder(opaqueBorder);gridyx.setVisible(true);tempcount =gridyx;count+;shuffle(temp,count);/* *暂停 */public void setPaused(boolean p) if(p) pthread.suspend();gridPanel.setVisible(false); else pthread.resume();gridPanel.setVisible(true);/* *是否暂停 */public boolean isPaused() return !gridPanel.isVisible();/* *没有布局存在时胜利 *计算得分 */private void win() wingame = true;pthread.suspend();score += progress.getValue()/20+bombcount*BOMBP+refreshcount*REFRP+tipcount*TIPP;scoreLabel.setText(+score);private void shuffle(KyodaiGrid array, int count) if(wingame) return;do setVisible(false);int j,k;Icon temp;for(int i=0;icount;i+) j = (int)(Math.random()*count);k = (int)(Math.random()*count);temp = arrayk.getIcon();arrayk.setIcon(arrayj.getIcon();arrayj.setIcon(temp);setVisible(true); while(!findPair();public void refresh() if(wingame|progress.getValue()=0|refreshcount=0) return;KyodaiGrid temp = new KyodaiGridxBound*yBound;int count = 0;for(int y=1;yyBound-1;y+) for(int x=1;xend.getXpos() direct = -1;path.removeAllElements();for(int x=start.getXpos()+direct;x!=end.getXpos()&x=0;x+=direct) if(gridstart.getYpos()x.isVisible() return false;path.add(gridstart.getYpos()x);path.add(end);return true;private boolean ydirect(KyodaiGrid start, KyodaiGrid end,Vector path) if(start.getXpos()!=end.getXpos() return false;int direct = 1;if(start.getYpos()end.getYpos() direct = -1;path.removeAllElements();for(int y=start.getYpos()+direct;y!=end.getYpos()&y=0;y+=direct) if(gridystart.getXpos().isVisible() return false;path.add(gridystart.getXpos();path.add(end);return true;private int findPath(KyodaiGrid start, KyodaiGrid end) /0 connerif(xdirect(start,end,path0) return 1;if(ydirect(start,end,path0) return 1;/1 connerKyodaiGrid xy = gridstart.getYpos()end.getXpos();if(!xy.isVisible()&xdirect(start,xy,path0)&ydirect(xy,end,path1) return 2;KyodaiGrid yx = gridend.getYpos()start.getXpos();if(!yx.isVisible()&ydirect(start,yx,path0)&xdirect(yx,end,path1) return 2;/2 conner/uppath0.removeAllElements();for(int y=start.getYpos()-1;y=0;y-) xy = gridystart.getXpos();yx = gridyend.getXpos();if(xy.isVisible() break;path0.add(xy);if(!yx.isVisible()&xdirect(xy,yx,path1)&ydirect(yx,end,path2) return 3;/downpath0.removeAllElements();for(int y=start.getYpos()+1;y=0;x-) yx = gridstart.getYpos()x;xy = gridend.getYpos()x;if(yx.isVisible() break;path0.add(yx);if(!xy.isVisible()&ydirect(yx,xy,path1)&xdirect(xy,end,path2) return 3;/rightpath0.removeAllElements();for(int x=start.getXpos()+1;xxBound;x+) yx = gridstart.getYpos()x;xy = gridend.getYpos()x;if(yx.isVisible() break;path0.add(yx);if(!xy.isVisible()&ydirect(yx,xy,path1)&xdirect(xy,end,path2) return 3;return 0;/* *在布局中消除配对 */private void deletePair(KyodaiGrid prev, KyodaiGrid current) /尝试寻找路径/如果找到路径/animateVector temp = new Vector();temp.add(prev);for(int i=0;ipcount;i+) temp.addAll(pathi);pathi.removeAllElements();AnimateThread thread = new AnimateThread(temp);thread.start();score += progress.getValue()/20;scoreLabel.setText(+score);progress.setValue(progress.getValue()+60);/* *展示找到的配对 */public void showNext() if(wingame|progress.getValue()=0|tipcount=0) return;tipcount-;tipLabel.setText(+tipcount);if(nexts!=null&nexte!=null) nexts.setBorder(tipBorder);nexte.setBorder(tipBorder);/* *删除找到的配对 */public void useBomb() if(wingame|progress.getValue()=0|bombcount=0) return;bombcount-;bombLabel.setText(+bombcount);if(nexts!=null&nexte!=null) deletePair(nexts,nexte);/* *发现有连接路径的配对 *返回是否发现

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论