JAVA实验报告3-“连连看”游戏程序设计
- 格式:doc
- 大小:200.00 KB
- 文档页数:20
基于java的连连看游戏设计与实现的研究报告随着信息化时代的发展和互联网的普及,网络游戏成为现代生活的重要组成部分之一,连连看游戏作为其中的佼佼者一直深受玩家们的喜爱。
本次研究以Java语言为基础,设计并实现了一款基于图形用户界面的连连看游戏,从游戏设计、编程实现以及测试三个方面进行详细介绍。
一、游戏设计该连连看游戏设计的基础是二维数组。
玩家通过鼠标点击选择两个同样的图片,连接它们的线路不能经过其他图标,且路径短优先。
游戏共18个关卡,每个关卡的难度依次递增,并拥有不同主题的图片素材。
二、编程实现该游戏使用Java Swing绘制图形用户界面,整个游戏主要包括五个类:Main、Game、Node、Action、Panel。
1.Main类:主要作用为程序入口。
通过创建Game对象启动游戏。
2.Game类:游戏类。
在该类中主要完成游戏的初始化、监听器、关卡切换、连通性判断等操作。
3.Node类:节点类。
主要用于表示每一个图标,包括坐标以及图标种类等信息。
4.Action类:动作类。
主要用于计算玩家所选的两点是否可以连通。
5.Panel类:面板类。
界面主要交由Panel完成。
在该类中完成了界面布局、游戏信息展示等工作。
三、测试在游戏的测试过程中,主要测试了游戏的各个关卡能否正常切换和连通性判断能否正常运行。
此外,还通过在不同操作系统和不同分辨率下的测试,保证了游戏适用性和兼容性。
四、总结本次研究成功地设计并实现了一款基于Java的连连看游戏。
通过该游戏的设计与实现过程,进一步深入了解了Java编程语言以及Swing界面库的应用与优势。
最终测试结果表明,该游戏在各种操作系统和分辨率下均表现出良好的兼容性和适用性,可供广大玩家使用。
但仍有一些不足之处,需要进一步完善和优化。
在本次基于Java的连连看游戏设计与实现研究中,我们对玩家游戏数据进行了收集与分析。
具体数据如下:1. 游戏总胜利次数:1022. 最高得分:97633. 游戏失败次数:464. 游戏累计时间:20小时15分通过对这些游戏数据的分析,我们可以得出以下结论:1. 游戏整体受欢迎度较高,玩家胜利次数较多。
课程设计2013 ~ 2014学年第二学期设计题目连连看游戏程序院(系)专业软件工程班级学号学生姓名设计时间 2014年6月24日~2014年6月27日指导教师提交日期 2014年6月27日目录目录 (2)1.课程设计的目的与要求 (3)1.1课程设计目的与要求 (3)1.2课程设计内容 (3)1.3课程设计的实验环境 (3)1.4课程设计的预备知识 (3)2.系统模块结构图 (4)2.1 模块设计 (4)2.1.1 菜单控制模块 (4)2.1.2 算法模块 (5)2.1.3 界面显示模块 (7)3.详细设计 (9)3.1总体算法思路 (9)3.2代码实现 (9)4.小结 (9)1.课程设计的目的与要求1.1课程设计目的与要求1.掌握JA V A语言中面向对象的概念,并能在程序中熟练运用。
2.了解面向对象程序设计(JA V A)的基本概念以及运用面向对象技术进行程序设计的基本思想。
3.能正确编写和调试JA V A程序。
4.了解在JA V A环境下进行程序设计的基本思想和方法。
1.2课程设计内容1.设计一个连连看的游戏程序。
2.在JA V A环境下,将上述程序使用GUI、数据结构等内容完成3.设计思路:(1)界面设计1)初始测试界面可以考虑使用简单的按钮来代表连连看游戏中的图标。
2)布局可以考虑使用GRID方式。
3)设计菜单选择连连看难度等内容,具体可参照QQ连连看,也可自定义。
4)考虑完善界面,例如动画等。
(2)代码设计1)本设计的核心为练练看算法的设计,可以考虑数据结构中的图的遍历章节,合理设计算法,将界面中各按钮的分布映射入数学矩阵进行路径规划。
(可以上网搜索相关算法)。
2)完成连连看游戏的图形、算法等代码的设计。
4.实验步骤(包括操作方法、数据处理)(1)界面设计(2)算法设计(3)代码设计1.3课程设计的实验环境硬件要求能运行Windows xp/7操作系统的微机系统。
JAVA程序设计语言及相应的集成开发环境, ECLIPSE开发工具。
java课程设计报告 连连看一、课程目标知识目标:1. 让学生掌握Java基础语法,包括变量声明、数据类型、运算符等。
2. 使学生了解面向对象编程的基本概念,如类、对象、继承、封装等。
3. 帮助学生掌握Java中常用类库和API的使用,如Array、ArrayList等。
4. 让学生学会运用Java编写图形用户界面(GUI)程序。
技能目标:1. 培养学生运用Java语言解决实际问题的能力,特别是在连连看游戏设计中的逻辑思维和编程技巧。
2. 提高学生分析问题、设计解决方案的能力,学会使用Java实现游戏的基本功能。
3. 培养学生团队合作精神,学会在项目中分工合作、共同推进项目进度。
情感态度价值观目标:1. 培养学生对编程的兴趣和热情,激发他们主动探索新技术、新方法的积极性。
2. 培养学生面对问题时的耐心和毅力,让他们体会到编程过程中的成就感。
3. 引导学生树立正确的价值观,认识到编程对于社会发展的重要性,激发他们为我国信息技术产业的发展贡献力量的责任感。
课程性质分析:本课程为Java编程课程设计,旨在通过实际项目——连连看游戏的设计与实现,巩固和拓展学生所学Java知识,提高编程实践能力。
学生特点分析:学生已具备一定的Java基础,了解基本语法和面向对象编程,但实际编程经验不足,需要通过本课程加强实践操作。
教学要求:1. 注重理论与实践相结合,引导学生将所学知识运用到实际项目中。
2. 鼓励学生积极参与讨论,培养解决问题的能力和团队合作精神。
3. 关注学生个体差异,提供个性化指导,确保每位学生都能在课程中取得进步。
二、教学内容1. Java基础语法回顾:变量声明、数据类型、运算符、控制流程(条件语句、循环语句)。
教材章节:第一章至第三章2. 面向对象编程:类与对象、构造方法、封装、继承、多态。
教材章节:第四章至第六章3. Java常用类库和API:String类、Array、ArrayList、泛型。
基于Java的图片连连看游戏设计研究基于Java的图片连连看游戏设计研究摘要:连连看(Link Up)游戏是一款经典的益智游戏,通过消除相同的图案来获得分数。
本文基于Java编程语言,对连连看游戏的设计进行深入研究。
通过实现游戏的基本功能和特色功能,对游戏进行优化和改进,提升游戏的用户体验。
通过实践验证,本设计具有一定的可行性和实用性。
1. 引言连连看游戏是一种非常受欢迎的益智游戏,其规则简单,操作容易上手。
随着移动互联网的快速发展,游戏市场越来越庞大。
设计一款基于Java的图片连连看游戏,可以满足用户对游戏的需求,同时也是提升自己编程能力的学习机会。
2. 游戏的基本功能设计2.1 游戏界面设计游戏主界面分为游戏区和得分区。
游戏区由多个方格组成,每个方格里面有一张图案,玩家需要通过点击两个相同的图案来消除它们。
得分区显示玩家的得分、剩余时间和游戏关卡等信息。
2.2 游戏规则设计游戏开始时,系统会随机生成一定数量的图案并填充到游戏区中。
玩家需要通过连接相同的图案来消除它们,直到清空游戏区中所有的图案。
连接两个图案的连线只能是水平或垂直直线,并且在连线路径上不能有其他图案阻挡。
玩家每消除一对图案,得分增加,游戏关卡随之升级。
如果无法连接两个图案,玩家可以使用提示功能获取一定的帮助。
3. 游戏特色功能设计3.1 计时功能游戏设置了一个倒计时器,玩家需要在规定的时间内完成游戏。
当倒计时为0时,游戏结束,系统会显示玩家的得分和游戏结束画面。
3.2 关卡功能游戏设计了多个关卡,每个关卡的游戏难度和图案数量都不同。
玩家需要通过完成前一关卡的要求才能解锁下一关卡。
3.3 提示功能当玩家遇到无法连接的图案时,可以使用提示功能,系统会自动给出可以连接的图案,并帮助玩家找到解决方案。
4. 游戏的优化与改进4.1 性能优化在游戏的设计过程中,需要考虑到游戏的性能问题。
通过合理的算法设计和资源管理,减少游戏的内存占用和运行时的开销,提高游戏的运行速度和流畅度。
JAVA课程设计连连看游戏的开发湖南涉外经济学院课程设计报告课程名称:程序设计实训报告题目:连连看游戏的开发学生姓名:所在学院:专业班级:学生学号:指导教师:2013年6月20日课程设计任务书报告题目连连看游戏的开发完成时间2013-6学生姓名专业班级职称讲师总体设计要求和技术要点设计一个连连看游戏程序,游戏程序的功能需求如下:游戏区是一个包含了多种不同花色游戏牌的二维棋盘,棋盘的行数和列数可以自行定义。
每一种花色的游戏牌的数量都是偶数。
两张花色相同的游戏牌的之间的路径如果满足以下条件,这两张游戏牌即可消去:路径连线由不多于3条的水平和垂直的线段构成,任何一条线段都不能穿越其他的游戏牌,但可以不在棋盘中,路径两端的游戏牌必须具有相同的花色。
游戏只要用鼠标即可进行操作。
最初棋盘里布满了游戏牌,游戏开始时进行计时。
第一次使用鼠标点击棋盘中的游戏牌,该游戏牌此时为“被选中”,以特殊方式显示;再次以鼠标点击其他游戏牌,若该游戏牌与被选中的游戏牌两者花色相同,且把第一张游戏牌到第二张游戏牌连起来,中间的线段不超过3条,则消掉这一对游戏牌,否则第一张游戏牌恢复成未被选中状态,而第二张游戏牌变成被选中状态。
每消去一对游戏牌,应检查棋盘内是否至少还存在一对能消去的游戏牌,如果不存在,则在原有位置对游戏牌进行重排,直到至少存在一对能够消去的游戏牌。
如果在指定的时间内消去了所有的游戏牌,游戏胜利;如果时间耗尽还未能消除全部的游戏牌,游戏失败。
要求每人独立完成,使用主流开发工具,尽可能采用面向对象方法,在设计判断两张相同花色游戏牌能否消去的算法时,尽可能使用状态空间搜索算法(如回溯法、分支限界法等),要求代码的具有一定的可读性、可维护性和可扩充性。
可以参考教师给定的程序或者其他程序,但程序和报告严禁全盘抄袭。
工作内容及时间进度安排第11周~第12周:对需要开发的软件进行需求分析和软件设计第13周:论证方案设计第14周~第16周:程序设计第17周:测试、修改程序,撰写报告第18周:验收答辩课程设计成果1.与设计内容对应的软件程序2.课程设计总结报告摘要近年来,Java作为一种新的编程语言,以其可移植性和平台无关性等优点,得到了广泛地应用,特别是Java与万维网的完美结合,使其成为网络编程和嵌入式编程领域的首选编程语言。
滨江学院实验报告||实验名称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 ImageIcon[54];URLClassLoader loader = (URLClassLoader)getClass().getClassLoader();for(int i=0;i<39;i++) {images[i] = new ImageIcon(getClass().getResource("images/"+i+".gif"));}images[39] = new ImageIcon(getClass().getResource("images/dots.gif"));images[40] = new ImageIcon(getClass().getResource("images/ico.gif"));images[41] = new ImageIcon(getClass().getResource("images/topbar.gif"));images[42] = new ImageIcon(getClass().getResource("images/splash.gif"));images[43] = new ImageIcon(getClass().getResource("images/sico.gif"));}public ImageIcon getImageicon(int i) {return images[i];}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) {eBomb();} 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 Vector[3];path[0] = new Vector();path[1] = new Vector();path[2] = 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 KyodaiGrid[yBound][xBound];int count = 0;int sub = levelInfo.getXBound()*levelInfo.getYBound()/4;KyodaiGrid temp[] = new KyodaiGrid[xBound*yBound];for(int y=0;y<yBound;y++) {for(int x=0;x<xBound;x++) {grid[y][x] = new KyodaiGrid(x, y);if(x==0||x==(xBound-1)||y==0||y==(yBound-1)) {grid[y][x].setIcon(ImageFactory.getInstance().getImageicon(39));grid[y][x].setVisible(false);} else {grid[y][x].setIcon(ImageFactory.getInstance().getImageicon(count%sub));grid[y][x].setBorder(opaqueBorder);grid[y][x].addMouseListener(gma);temp[count] = grid[y][x];count++;}gridPanel.add(grid[y][x]);}}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 KyodaiGrid[xBound*yBound];for(int y=1;y<yBound-1;y++) {for(int x=1;x<xBound-1;x++){grid[y][x].setIcon(ImageFactory.getInstance().getImageicon(count%sub));grid[y][x].setBorder(opaqueBorder);grid[y][x].setVisible(true);temp[count] =grid[y][x];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;i<count;i++) {j = (int)(Math.random()*count);k = (int)(Math.random()*count);temp = array[k].getIcon();array[k].setIcon(array[j].getIcon());array[j].setIcon(temp);}setVisible(true);} while(!findPair());}public void refresh() {if(wingame||progress.getValue()==0||refreshcount==0) return;KyodaiGrid temp[] = new KyodaiGrid[xBound*yBound];int count = 0;for(int y=1;y<yBound-1;y++) {for(int x=1;x<xBound-1;x++) {if(grid[y][x].isVisible()) {grid[y][x].setBorder(opaqueBorder);temp[count] = grid[y][x];count++;}}}if(count!=0) {refreshcount--;refreshLabel.setText(""+refreshcount);shuffle(temp,count);} else win();}private boolean xdirect(KyodaiGrid start, KyodaiGrid end,Vector path) { if(start.getYpos()!=end.getYpos()) return false;int direct = 1;if(start.getXpos()>end.getXpos()) {direct = -1;}path.removeAllElements();for(intx=start.getXpos()+direct;x!=end.getXpos()&&x<xBound&&x>=0;x+=direct) { if(grid[start.getYpos()][x].isVisible()) return false;path.add(grid[start.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(inty=start.getYpos()+direct;y!=end.getYpos()&&y<yBound&&y>=0;y+=direct) { if(grid[y][start.getXpos()].isVisible()) return false;path.add(grid[y][start.getXpos()]);}path.add(end);return true;}private int findPath(KyodaiGrid start, KyodaiGrid end) {//0 connerif(xdirect(start,end,path[0])) {return 1;}if(ydirect(start,end,path[0])) {return 1;}//1 connerKyodaiGrid xy = grid[start.getYpos()][end.getXpos()];if(!xy.isVisible()&&xdirect(start,xy,path[0])&&ydirect(xy,end,path[1])) {return 2;}KyodaiGrid yx = grid[end.getYpos()][start.getXpos()];if(!yx.isVisible()&&ydirect(start,yx,path[0])&&xdirect(yx,end,path[1])) {return 2;}//2 conner//uppath[0].removeAllElements();for(int y=start.getYpos()-1;y>=0;y--) {xy = grid[y][start.getXpos()];yx = grid[y][end.getXpos()];if(xy.isVisible()) break;path[0].add(xy);if(!yx.isVisible()&&xdirect(xy,yx,path[1])&&ydirect(yx,end,path[2])) {return 3;}}//downpath[0].removeAllElements();for(int y=start.getYpos()+1;y<yBound;y++) {xy = grid[y][start.getXpos()];yx = grid[y][end.getXpos()];if(xy.isVisible()) break;path[0].add(xy);if(!yx.isVisible()&&xdirect(xy,yx,path[1])&&ydirect(yx,end,path[2])) {return 3;}}//leftpath[0].removeAllElements();for(int x=start.getXpos()-1;x>=0;x--) {yx = grid[start.getYpos()][x];xy = grid[end.getYpos()][x];if(yx.isVisible()) break;path[0].add(yx);if(!xy.isVisible()&&ydirect(yx,xy,path[1])&&xdirect(xy,end,path[2])) {return 3;}}//rightpath[0].removeAllElements();for(int x=start.getXpos()+1;x<xBound;x++) {yx = grid[start.getYpos()][x];xy = grid[end.getYpos()][x];if(yx.isVisible()) break;path[0].add(yx);if(!xy.isVisible()&&ydirect(yx,xy,path[1])&&xdirect(xy,end,path[2])) {return 3;}}return 0;}/***在布局中消除配对*/private void deletePair(KyodaiGrid prev, KyodaiGrid current) {//尝试寻找路径//如果找到路径//animateVector temp = new Vector();temp.add(prev);for(int i=0;i<pcount;i++) {temp.addAll(path[i]);path[i].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);}}/***发现有连接路径的配对*@返回是否发现*/private boolean findPair() {nexts = null;nexte = null;for(int sy=1;sy<yBound-1;sy++) {for(int sx=1;sx<xBound-1;sx++) {if(!grid[sy][sx].isVisible()) continue;for(int ey=sy;ey<yBound-1;ey++) {for(int ex=1;ex<xBound-1;ex++) {if(!grid[ey][ex].isVisible()||(ey==sy&&ex==sx)) continue;if(grid[sy][sx].getIcon()==grid[ey][ex].getIcon()) {pcount = findPath(grid[sy][sx],grid[ey][ex]);if(pcount!=0) {nexts = grid[sy][sx];nexte = grid[ey][ex];return true;}}}}}}return false;}private class GridMouseAdapter extends MouseAdapter { private KyodaiGrid prev;public void mouseClicked(MouseEvent me) {if(prev == null) {prev = (KyodaiGrid)me.getSource();prev.setBorder(selectedBorder);} else {if(progress.getValue()==0) return;KyodaiGrid current = (KyodaiGrid)me.getSource();if(current == prev) return;if(current.getIcon()==prev.getIcon()) {pcount = findPath(prev,current);if(pcount!=0) {deletePair(prev,current);//setprev = null;return;}}prev.setBorder(opaqueBorder);prev = current;prev.setBorder(selectedBorder);if(!findPair()) refresh();}}}private class AnimateThread extends Thread {private Vector v;public AnimateThread(Vector temp) {v = temp;}public void run() {KyodaiGrid prev = null;KyodaiGrid current;int j = 0;while(j<v.size()) {prev = (KyodaiGrid)v.remove(0);prev.setVisible(true);v.add(prev);j++;try {sleep(20);} catch(InterruptedException ire) {System.err.println("sleep interrupted");}}current = prev;prev = (KyodaiGrid)v.remove(0);while(!v.isEmpty()) {((KyodaiGrid)v.remove(0)).setVisible(false);try {sleep(20);} catch(InterruptedException ire) {System.err.println("sleep interrupted");}}prev.setVisible(false);current.setVisible(false);current.setIcon(ImageFactory.getInstance().getImageicon(39));prev.setIcon(ImageFactory.getInstance().getImageicon(39));current.setBorder(opaqueBorder);prev.setBorder(opaqueBorder);if(!findPair()) refresh();}//end of method run}private class ProgressThread extends Thread {public ProgressThread() {}public void run() {while(true) {while(progress.getValue()>0) {progress.setValue(progress.getValue()-1);try {sleep(100);} catch(InterruptedException ire) {System.err.println("sleep interrupted");}}repaint();suspend();}}}}【总结自己的体会和收获】:通过这次课程设计我学到了不少东西,也发现了大量的问题,同时在设计的过程中也发现了自己的不足之处,对以前学过的知识理解的不够深刻,对安卓手机小游戏的程序编写有了一定的了解。
一、实验目的1. 熟悉并掌握使用Java语言进行图形界面编程的基本方法。
2. 理解并运用面向对象编程思想,提高编程能力。
3. 掌握连连看游戏的基本原理和实现方法。
4. 培养团队合作能力和创新意识。
二、实验内容1. 熟悉Java语言的基本语法和面向对象编程思想。
2. 使用Java Swing库开发图形界面。
3. 实现连连看游戏的基本功能,包括游戏界面、数据结构、逻辑算法等。
三、实验环境1. 操作系统:Windows 102. 开发工具:Eclipse3. 编程语言:Java四、实验步骤1. 创建Java项目,命名为“连连看游戏”。
2. 设计游戏界面,包括游戏区域、时间显示、分数显示等。
3. 定义数据结构,用于存储游戏中的图片和匹配关系。
4. 编写游戏逻辑算法,包括初始化游戏、随机生成图片、匹配图片等。
5. 实现游戏控制,包括鼠标点击事件、图片移动、匹配判断等。
6. 编写游戏结束判断和结果显示。
五、实验结果与分析1. 实验结果通过以上步骤,成功开发了一个连连看游戏。
游戏界面美观,功能完善,可以满足用户的基本需求。
2. 实验分析(1)游戏界面设计:采用Java Swing库中的JPanel、JLabel等组件,实现了游戏界面的布局。
通过设置背景图片、颜色等属性,使游戏界面更具吸引力。
(2)数据结构:使用二维数组存储游戏中的图片,每行每列代表一个位置。
通过遍历数组,判断相邻图片是否可以匹配。
(3)逻辑算法:在游戏开始时,随机生成图片并存储到数组中。
当用户点击一个图片时,判断其相邻图片是否可以匹配。
如果可以匹配,则将这两个图片移动到一起,并更新分数和时间。
(4)游戏控制:通过监听鼠标点击事件,实现图片的移动和匹配判断。
当图片移动到一起时,判断是否匹配,并更新游戏状态。
六、实验总结1. 通过本次实验,掌握了Java语言的基本语法和面向对象编程思想,提高了编程能力。
2. 熟悉了Java Swing库在图形界面编程中的应用,为以后开发类似项目奠定了基础。
Java程序课程设计任务书1、主要内容:本程序基本实现了小游戏连连看的功能,玩家找出游戏中2个相同图案的方块,如果它们之间的连接线不多于3根直线,则将其连接起来,就可以成功将图案相同的方块消除,否则不会消失,当游戏中已没有满足条件的图案时,点击重列,可重新排序,游戏结束会跳出所得分数,该游戏的特点是与自己竞争,超过自己之前所创纪录。
2、具体要求(包括技术要求等):a. 该游戏界面为方格类型,由纵6横7的直线平行垂直交叉组成,分别是6行5列方块拼接,共有30格小方块。
方块上随机分布一些数字,数字的要求是至少两两相同,位置随机打乱。
b.当将相同数字的方块连接,但要满足只能至少单边无阻碍呈直线趋势连接,否则无效,若连接一对成功就消失于界面,继续游戏,直到游戏结束,并能返回所得分数。
c. 重列按钮(帮助)的功能:游戏过程中,遇到困难难以寻找下一符合要求的一对数字,可按左下按钮重置重新排列方可继续游戏。
d. 退出按钮:击左下方的“退出游戏”按钮,即可结束游戏。
e.运用基于SWING的图形用户界面设计知识等。
3、学习并掌握以下技术:Java等4、熟练使用以下开发工具:Jcreate 等实现系统上述的功能。
三、计划进度12月28日-12月29 日:课程设计选题,查找参考资料12月30日-12月31日:完成需求分析、程序设计1月1日-1月3日:完成程序代码的编写1月4日-1月6日:系统测试与完善1月7日-1月8日:完成课程设计报告,准备答辩四、主要参考文献[1] (美)埃克尔著陈昊鹏,饶若楠等译. Java编程思想[J]. 机械工业出版社,2005[2](美)Gary J.Bronson著张珑刘雅文译. Java编程原理[J]. 清华大学出版社,2004[3](美)Michael Morrison著徐刚,于健,薛雷译. 游戏编程入门[J]. 人民邮电出版社,2005.9[4](美)Wendy Stahler著冯宝坤,曹英译. 游戏编程中的数理应用[J]. 红旗出版社,2005[5](美)克罗夫特(David Wallace Croft)著彭晖译. Java游戏高级编程[J]. 清华大学出版社,2005[6](美)David Brackeen著邱仲潘译. Java游戏编程[J]. 科学出版社,2004[7] 聂庆亮编著. Java应用开发指南[J]. 清华大学出版社,2010[8] 耿祥义,张跃平编著. Java面向对象程序设计[J]. 清华大学出版社,2010[9] 杨绍方编著. Java编程实用技术与案例[J]. 清华大学出版社,2000.11[10] 明日科技编著. Java编程全能词典[J]. 电子工业出版社,2010摘要随着Java语言的不断发展和壮大,现在的Java已经广泛的应用于各个领域,包括医药,汽车工业,手机行业,游戏,等等地方。
目录一、实验目的与要求 (2)二、实验方案 (2)三、实验结果和数据处理 (3)四、结论 (20)五、问题与讨论 (20)一、实验目的与要求(1)实验目的:通过在指定的窗口界面完成“连连看”小游戏程序的编写和调试,加深对面向对象程序设计的理解。
(2)实验要求:按照Java程序设计教程的要求完成第三章中“连连看”游戏程序的编写和调试。
二、实验方案定义了一个lianliankan类,实现了接口ActionListener:①main()方法:主函数;②actionPerformed()方法,用来实现重来一局按钮的响应事件;③go()方法:初始化界面,排列图形;④ex()方法:设置“退出游戏”窗体界面;⑤suiji()方法:产生随机数,来填充游戏界面对应的数组的各个位置;⑥chonglie()方法:当无符合条件的图形可消去时,需要重新排列图形;⑦ling()方法:将数组中为零的成员所对应的按钮消去(设为不可见);⑧wei()方法:判断并记录每次单击按钮的一些信息;⑨xiao()方法:判断两个按钮在对应数组元素的值相同时能不能消去。
三、实验结果和数据处理整个游戏最重要的方法,xiao()方法,用来判断两个按钮在对应数组元素的值相同时能不能消去:import javax.swing.*;import java.awt.*;import java.awt.event.*;public class lianliankan implements ActionListener{JFrame mainFrame; //主面板Container thisContainer;JPanel centerPanel,southPanel,northPanel; //子面板JButton diamondsButton[][] = new JButton[6][5];//游戏按钮数组JButton exitButton,resetButton,newlyButton; //退出,重列,重新开始按钮JLabel fractionLable=new JLabel("0"); //分数标签JButton firstButton,secondButton; //分别记录两次被选中的按钮int grid[][] = new int[8][7];//储存游戏按钮位置static boolean pressInformation=false; //判断是否有按钮被选中int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateL V; //游戏按钮的位置坐标int i,j,k,n;//消除方法控制public void init(){mainFrame=new JFrame("JKJ连连看");thisContainer = mainFrame.getContentPane();thisContainer.setLayout(new BorderLayout()); centerPanel=new JPanel();southPanel=new JPanel();northPanel=new JPanel();thisContainer.add(centerPanel,"Center"); thisContainer.add(southPanel,"South"); thisContainer.add(northPanel,"North"); centerPanel.setLayout(new GridLayout(6,5));for(int cols = 0;cols < 6;cols++){for(int rows = 0;rows < 5;rows++ ){ diamondsButton[cols][rows]=newJButton(String.valueOf(grid[cols+1][rows+1])); diamondsButton[cols][rows].addActionListener(this); centerPanel.add(diamondsButton[cols][rows]);}}exitButton=new JButton("退出");exitButton.addActionListener(this);resetButton=new JButton("重列");resetButton.addActionListener(this);newlyButton=new JButton("再来一局"); newlyButton.addActionListener(this); southPanel.add(exitButton);southPanel.add(resetButton);southPanel.add(newlyButton);fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText()))); northPanel.add(fractionLable);mainFrame.setBounds(280,100,500,450);mainFrame.setVisible(true);}public void randomBuild() {int randoms,cols,rows;for(int twins=1;twins<=15;twins++) {randoms=(int)(Math.random()*25+1);for(int alike=1;alike<=2;alike++) {cols=(int)(Math.random()*6+1);rows=(int)(Math.random()*5+1);while(grid[cols][rows]!=0) {cols=(int)(Math.random()*6+1);rows=(int)(Math.random()*5+1);}this.grid[cols][rows]=randoms;}}}public void fraction(){fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())+1 00));}public void reload() {int save[] = new int[30];int n=0,cols,rows;int grid[][]= new int[8][7];for(int i=0;i<=6;i++) {for(int j=0;j<=5;j++) {if(this.grid[i][j]!=0) {save[n]=this.grid[i][j];n++;}}}n=n-1;this.grid=grid;while(n>=0) {cols=(int)(Math.random()*6+1);rows=(int)(Math.random()*5+1);while(grid[cols][rows]!=0) {cols=(int)(Math.random()*6+1);rows=(int)(Math.random()*5+1);}this.grid[cols][rows]=save[n];n--;}mainFrame.setVisible(false);pressInformation=false; //这里一定要将按钮点击信息归为初始init();for(int i = 0;i < 6;i++){for(int j = 0;j < 5;j++ ){if(grid[i+1][j+1]==0)diamondsButton[i][j].setVisible(false);}}}public void estimateEven(int placeX,int placeY,JButton bz) {if(pressInformation==false) {x=placeX;y=placeY;secondMsg=grid[x][y];secondButton=bz;pressInformation=true;}else {x0=x;y0=y;fristMsg=secondMsg;firstButton=secondButton;x=placeX;y=placeY;secondMsg=grid[x][y];secondButton=bz;if(fristMsg==secondMsg && secondButton!=firstButton){xiao();}}}public void xiao() { //相同的情况下能不能消去。
仔细分析,不一条条注释if((x0==x &&(y0==y+1||y0==y-1)) || ((x0==x+1||x0==x-1)&&(y0==y))){ //判断是否相邻remove();}else{for (j=0;j<7;j++ ) {if (grid[x0][j]==0){ //判断第一个按钮同行哪个按钮为空if (y>j) { //如果第二个按钮的Y坐标大于空按钮的Y坐标说明第一按钮在第二按钮左边for (i=y-1;i>=j;i-- ){ //判断第二按钮左侧直到第一按钮中间有没有按钮if (grid[x][i]!=0) {k=0;break;}else{ k=1; } //K=1说明通过了第一次验证}if (k==1) {linePassOne();}}if (y<j){ //如果第二个按钮的Y坐标小于空按钮的Y坐标说明第一按钮在第二按钮右边for (i=y+1;i<=j ;i++ ){ //判断第二按钮左侧直到第一按钮中间有没有按钮if (grid[x][i]!=0){k=0;break;}else { k=1; }}if (k==1){linePassOne();}}if (y==j ) {linePassOne();}}if (k==2) {if (x0==x) {remove();}if (x0<x) {for (n=x0;n<=x-1;n++ ) {if (grid[n][j]!=0) {k=0;break;}if(grid[n][j]==0 && n==x-1) { remove();}}}if (x0>x) {for (n=x0;n>=x+1 ;n-- ) {if (grid[n][j]!=0) {k=0;break;}if(grid[n][j]==0 && n==x+1) { remove();}}}}}for (i=0;i<8;i++ ) { //列if (grid[i][y0]==0) {if (x>i) {for (j=x-1;j>=i ;j-- ) {if (grid[j][y]!=0) {k=0;break;}else { k=1; }}if (k==1) {rowPassOne();}}if (x<i) {for (j=x+1;j<=i;j++ ) { if (grid[j][y]!=0) {k=0;break;}else { k=1; }}if (k==1) { rowPassOne();}}if (x==i) { rowPassOne();}}if (k==2){if (y0==y) {remove();}if (y0<y) {for (n=y0;n<=y-1 ;n++ ) {if (grid[i][n]!=0) {k=0;break;}if(grid[i][n]==0 && n==y-1) { remove();}}}if (y0>y) {for (n=y0;n>=y+1 ;n--) {if (grid[i][n]!=0) {k=0;break;}if(grid[i][n]==0 && n==y+1) { remove();}}}}}}}public void linePassOne(){if (y0>j){ //第一按钮同行空按钮在左边for (i=y0-1;i>=j ;i-- ){ //判断第一按钮同左侧空按钮之间有没按钮if (grid[x0][i]!=0) {k=0;break;}else { k=2; } //K=2说明通过了第二次验证}}if (y0<j){ //第一按钮同行空按钮在与第二按钮之间for (i=y0+1;i<=j ;i++){if (grid[x0][i]!=0) {k=0;break;}else{ k=2; }}}}public void rowPassOne(){if (x0>i) {for (j=x0-1;j>=i ;j-- ) {if (grid[j][y0]!=0) {k=0;break;}else { k=2; }}}if (x0<i) {for (j=x0+1;j<=i ;j++ ) {if (grid[j][y0]!=0) {k=0;break;}else { k=2; }}}}public void remove(){ firstButton.setVisible(false); secondButton.setVisible(false); fraction();pressInformation=false;k=0;grid[x0][y0]=0;grid[x][y]=0;}public void actionPerformed(ActionEvent e) {if(e.getSource()==newlyButton){int grid[][] = new int[8][7];this.grid = grid;randomBuild();mainFrame.setVisible(false);pressInformation=false;init();}if(e.getSource()==exitButton)System.exit(0);if(e.getSource()==resetButton)reload();for(int cols = 0;cols < 6;cols++){for(int rows = 0;rows < 5;rows++ ){if(e.getSource()==diamondsButton[cols][rows]) estimateEven(cols+1,rows+1,diamondsButton[cols][rows]); }}}public static void main(String[] args) { lianliankan llk = new lianliankan(); llk.randomBuild();llk.init();}}运行后产生下面的界面:“重列”后的界面:完全消去后的界面:选择“再来一局”后出现的界面:四、结论本次实验,主要是关于“连连看”游戏的程序设计和调试,在这次试验中,先用主函数实现应用程序入口,再调用suiji()方法产生随机数,确定二维数组元素值,接着调用go()方法,排列游戏窗体布局,再调用wei()方法,记录单击按钮图形的信息,最后调用xiao()方法,消去符合条件的两个相同图形的按钮,若无相同图形可消,则调用chonglie()方法重新排列图形。