资源预览内容
第1页 / 共10页
第2页 / 共10页
第3页 / 共10页
第4页 / 共10页
第5页 / 共10页
第6页 / 共10页
第7页 / 共10页
第8页 / 共10页
第9页 / 共10页
第10页 / 共10页
亲,该文档总共10页全部预览完了,如果喜欢就下载吧!
资源描述
贪吃蛇作业说明1 游戏说明一个贪吃蛇游戏软件,主要实现游戏的开始、暂停、退出功能,分为: 游戏主界面模块、游戏控制模块以二个模块。性能:方便、实用,在性能方面效率高,不易出错等。游戏主界面模块:主要包括游戏图形区域界面、游戏开始按钮、暂停游戏按钮、关闭按钮。游戏控制模块:主要完成控制游戏的开始、暂停、退出等功能游戏控制模块是游戏的中心环节。2. 系统开发环境(1) 软件环境 Eclipse3. 程序流程图单击“开始游戏”按钮, 游戏开始,蛇开始随机。键盘操作: 向上左移右移向下没有吃到食物且蛇碰到边界单击“退出游戏”退出游戏游戏中途欲中断,稍后继续游戏单击“暂停游戏”游戏暂停进入游戏画面再次单击开始游戏,游戏恢复游戏流程图4.程序中自定义类说明SnakeWin类该类包含main方法,为应用程序的主类。该类用来创建游戏的用户界面,整个程序从该类的main方法开始执行。SnakeFrame类用来设计游戏的用户界面。PaintComponent函数设置游戏运行状态,设置蛇头颜色,背景颜色,随机块颜色,移动以及分数更新,实现上下左右移动的方法接口。Move函数设置初始化蛇的形状和运行方向,设置随机块的位置,判断游戏是否出界,以及出界后的处理,设置方向的更新。Eat函数判断是否吃到食物Speed 类用来控制游戏的速度5.程序中主要方法说明Main方法功能:创建了SnakeFrame窗口类的一个实例,应用程序从main方法开始执行。备注:应用程序的入口方法。SnakeFrame方法功能:加载整个用户界面,注册子菜单的ActionEvent事件。备注:构造函数。actionPerformed方法功能:ActionEvent事件处理方法。该方法处理各按钮的动作事件。把pause的值赋给speedtime,处理速度事件。备注:接口方法。Speed方法功能:用来控制游戏的速度。备注:构造函数。getX方法功能:得到x坐标。备注:自定义方法。getY方法功能:得到y坐标。备注:自定义方法。setX方法功能:设置x坐标。备注:自定义方法。setY方法功能:设置Y坐标。备注:自定义方法。SnakePanel方法功能:设置游戏界面的属性及注册组件的键盘事件。备注:构造函数。newGame方法功能:对重新游戏的设置以及初始化。stopGame方法功能:结束当前游戏的线程。resumeGame方法功能:暂停游戏。Move方法功能:判断运行方向,判断是否吃了随机块,判断游戏是否出界 ,以及运行方向的更新。6.源代码第一部分:package game;import javax.swing.*;public class snack extends JFrame public snack() snackWin win=new snackWin(); add(win); setTitle(贪吃蛇-王亮制作); setSize(435,390); setLocationRelativeTo(null); setVisible(true); public static void main(String args) new snack(); 第二部分:package game;import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.util.*;import java.util.List;public class snackWin extends JPanel implements ActionListener,KeyListener,RunnableJButton newGame,stopGame; int fenshu=0,Speed=1; boolean start=false; Random r=new Random(); int rx=0,ry=0; List list=new ArrayList(); int temp=0; JDialog dialog=new JDialog(); JLabel label=new JLabel(你挂了你的分数是+fenshu); JButton ok=new JButton(好吧=.=); Thread nThread; public snackWin() newGame = new JButton(开始); stopGame = new JButton(退出); this.setLayout(new FlowLayout(FlowLayout.LEFT); newGame.addActionListener(this); stopGame.addActionListener(this); this.addKeyListener(this); this.add(newGame); this.add(stopGame); ok.addActionListener(this); dialog.setLayout(new FlowLayout(); dialog.add(label); dialog.add(ok); dialog.setSize(200,100); /设置窗体大小 dialog.setLocation(200,200); dialog.setVisible(false); public void paintComponent(Graphics g)/画图 super.paintComponent(g); g.drawRect(10, 40, 400, 300); g.drawString(分数:+fenshu, 150, 16); g.drawString(速度:+Speed, 150, 34); g.setColor(new Color(0,255,0); if(start) g.fillRect(10+rx*10, 40+ry*10, 10, 10); g.setColor(new Color(255,0,0); for(int i=0;iSpeed*50) if(Speed9) Speed+; public void otherMove() snackAct tempAct=new snackAct(); for(int i=0;i1) tempAct=list.get(i-1); list.set(i-1,list.get(i); list.set(i, tempAct); public void move(int x,int y) /运动方向 if(minYes(x,y) otherMove(); list.get(0).setX(list.get(0).getX()+x); list.get(0).setY(list.get(0).getY()+y); eat(); repaint(); else/死亡方法 nThread=n
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号