欢迎来到淘文阁 - 分享文档赚钱的网站! | 帮助中心 好文档才是您的得力助手!
淘文阁 - 分享文档赚钱的网站
全部分类
  • 研究报告>
  • 管理文献>
  • 标准材料>
  • 技术资料>
  • 教育专区>
  • 应用文书>
  • 生活休闲>
  • 考试试题>
  • pptx模板>
  • 工商注册>
  • 期刊短文>
  • 图片设计>
  • ImageVerifierCode 换一换

    JAVA程序源代码(贪吃蛇)(共13页).doc

    • 资源ID:13644923       资源大小:46.50KB        全文页数:13页
    • 资源格式: DOC        下载积分:20金币
    快捷下载 游客一键下载
    会员登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录   QQ登录  
    二维码
    微信扫一扫登录
    下载资源需要20金币
    邮箱/手机:
    温馨提示:
    快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如填写123,账号就是123,密码也是123。
    支付方式: 支付宝    微信支付   
    验证码:   换一换

     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    JAVA程序源代码(贪吃蛇)(共13页).doc

    精选优质文档-倾情为你奉上贪吃蛇源代码将Location、LocationRO、SnakeFrame、SnakeModel、SnakePanel放到命名为snake的文件夹里,主函数MainApp放到外面运行主函数即可实现。主函数package snake;import javax.swing.*;import snake.*;public class MainApp public static void main(String args) JFrame.setDefaultLookAndFeelDecorated(true);SnakeFrame frame=new SnakeFrame();frame.setSize(350,350);frame.setResizable(false);frame.setLocation(330,220);frame.setTitle("贪吃蛇");frame.setVisible(true);package snake;public class Location private int x;private int y;Location(int x,int y)this.x=x;this.y=y;int getX()return x; int getY()return y;void setX(int x)this.x=x;void setY(int y)this.y=y;public boolean equalOrRev(Location e)return (e.getX()=getX()&&(e.getY()=getY()|(e.getX()=getX()&&(e.getY()=-1*getY()|(e.getX()=-1*getX()&&(e.getY()=getY();public boolean equals(Location e)return(e.getX()=getX()&&(e.getY()=getY();public boolean reverse(Location e)return (e.getX()=getX()&&(e.getY()=-1*getY()|(e.getX()=-1*getX()&&(e.getY()=getY();package snake;public class LocationRO private int x;private int y;LocationRO(int x,int y)this.x=x;this.y=y;int getX()return x; int getY()return y;public boolean equalOrRev(LocationRO e)return (e.getX()=getX()&&(e.getY()=getY()|(e.getX()=getX()&&(e.getY()=-1*getY()|(e.getX()=-1*getX()&&(e.getY()=getY();public boolean equals(LocationRO e)return(e.getX()=getX()&&(e.getY()=getY();public boolean reverse(LocationRO e)return (e.getX()=getX()&&(e.getY()=-1*getY()|(e.getX()=-1*getX()&&(e.getY()=getY();package snake;import java.awt.*;import java.awt.event.*;import javax.swing.*;class SnakeFrame extends JFrame implements ActionListenerfinal SnakePanel p=new SnakePanel(this);JMenuBar menubar=new JMenuBar();JMenu fileMenu=new JMenu("文件");JMenuItem newgameitem=new JMenuItem("开始");JMenuItem stopitem=new JMenuItem("暂停");JMenuItem runitem=new JMenuItem("继续");JMenuItem exititem=new JMenuItem("退出"); /"设置"菜单JMenu optionMenu=new JMenu("设置"); /等级选项 ButtonGroup groupDegree = new ButtonGroup(); JRadioButtonMenuItem oneItem= new JRadioButtonMenuItem("初级"); JRadioButtonMenuItem twoItem= new JRadioButtonMenuItem("中级"); JRadioButtonMenuItem threeItem= new JRadioButtonMenuItem("高级"); JMenu degreeMenu=new JMenu("等级"); JMenu helpMenu=new JMenu("帮助"); JMenuItem helpitem=new JMenuItem("操作指南"); final JCheckBoxMenuItem showGridItem = new JCheckBoxMenuItem("显示网格"); JLabel scorelabel; public JTextField scoreField; private long speedtime=200; private String helpstr = "游戏说明:n1 :方向键控制蛇移动的方向."+ "n2 :单击菜单'文件->开始'开始游戏."+ "n3 :单击菜单'文件->暂停'或者单击键盘空格键暂停游戏."+ "n4 :单击菜单'文件->继续'继续游戏."+ "n5 :单击菜单'设置->等级'可以设置难度等级."+ "n6 :单击菜单'设置->显示网格'可以设置是否显示网格."+ "n7 :红色为食物,吃一个得10分同时蛇身加长."+ "n8 :蛇不可以出界或自身相交,否则结束游戏." SnakeFrame() setJMenuBar(menubar); fileMenu.add(newgameitem); fileMenu.add(stopitem); fileMenu.add(runitem); fileMenu.add(exititem); menubar.add(fileMenu); oneItem.setSelected(true); groupDegree.add(oneItem); groupDegree.add(twoItem); groupDegree.add(threeItem); degreeMenu.add(oneItem); degreeMenu.add(twoItem); degreeMenu.add(threeItem); optionMenu.add(degreeMenu); / 风格选项 showGridItem.setSelected(true); optionMenu.add(showGridItem); menubar.add(optionMenu); helpMenu.add(helpitem); menubar.add(helpMenu);Container contentpane=getContentPane(); contentpane.setLayout(new FlowLayout();contentpane.add(p);scorelabel=new JLabel("得 分: ");scoreField=new JTextField("0",15);scoreField.setEnabled(false);scoreField.setHorizontalAlignment(0);JPanel toolPanel=new JPanel();toolPanel.add(scorelabel);toolPanel.add(scoreField);contentpane.add(toolPanel);oneItem.addActionListener(this); twoItem.addActionListener(this); threeItem.addActionListener(this); newgameitem.addActionListener(this); stopitem.addActionListener(this); runitem.addActionListener(this); exititem.addActionListener(this); helpitem.addActionListener(this); showGridItem.addActionListener(this);public void actionPerformed(ActionEvent e) try if(e.getSource()=helpitem) JOptionPane.showConfirmDialog(p,helpstr,"操纵说明",JOptionPane.PLAIN_MESSAGE); else if(e.getSource()=exititem)System.exit(0); else if(e.getSource()=newgameitem)p.newGame(speedtime); else if(e.getSource()=stopitem)p.stopGame(); else if(e.getSource()=runitem)p.returnGame(); else if(e.getSource()=showGridItem)if(!showGridItem.isSelected()p.setBackground(Color.lightGray);elsep.setBackground(Color.darkGray); else if(e.getSource()=oneItem) speedtime=200; else if(e.getSource()=twoItem) speedtime=100; else if(e.getSource()=threeItem) speedtime=50; catch(Exception ee)ee.printStackTrace(); package snake;import java.util.*;import javax.swing.JOptionPane;public class SnakeModel private int rows,cols;/行列数private Location snakeHead,runingDiriction;/运行方向private LocationRO locRO;/LocationRO类数组private LinkedList snake,playBlocks;/蛇及其它区域块private LocationRO snakeFood;/目标食物private int gameScore=0; /分数private boolean AddScore=false;/加分 /获得蛇头public LocationRO getSnakeHead() return (LocationRO)(snake.getLast();/蛇尾public LocationRO getSnakeTail()return (LocationRO)(snake.getFirst();/运行路线public Location getRuningDiriction()return runingDiriction;/获得蛇实体区域public LinkedList getSnake()return snake;/其他区域public LinkedList getOthers()return playBlocks;/获得总分public int getScore()return gameScore;/获得增加分数public boolean getAddScore()return AddScore;/设置蛇头方向private void setSnakeHead(Location snakeHead)this.snakeHead=snakeHead; /获得目标食物public LocationRO getSnakeFood()return snakeFood;/随机设置目标食物private void setSnakeFood()snakeFood=(LocationRO)(playBlocks.get(int)(Math.random()*playBlocks.size();/移动private void moveTo(Object a,LinkedList fromlist,LinkedList tolist)fromlist.remove(a);tolist.add(a);/初始设置public void init()playBlocks.clear();snake.clear();gameScore=0; for(int i=0;i<rows;i+) for(int j=0;j<cols;j+) playBlocks.add(locROij); /初始化蛇的形状 for(int i=4;i<7;i+) moveTo(locRO4i,playBlocks,snake); /蛇头位置 snakeHead=new Location(4,6); /设置随机块 snakeFood=new LocationRO(0,0); setSnakeFood(); /初始化运动方向 runingDiriction=new Location(0,1); /Snake构造器public SnakeModel(int rows1,int cols1) rows=rows1; cols=cols1; locRO=new LocationROrowscols; snake=new LinkedList(); playBlocks=new LinkedList(); for(int i=0;i<rows;i+) for(int j=0;j<cols;j+) locROij=new LocationRO(i,j); init(); /*定义布尔型move方法,如果运行成功则返回true,否则返回false*参数direction是Location类型,*direction 的值:(-1,0)表示向上;(1,0)表示向下;*(0,-1)表示向左;(0,1)表示向右;*/public boolean move(Location direction)/判断设定的方向跟运行方向是不是相反if (direction.reverse(runingDiriction)snakeHead.setX(snakeHead.getX()+runingDiriction.getX();snakeHead.setY(snakeHead.getY()+runingDiriction.getY();elsesnakeHead.setX(snakeHead.getX()+direction.getX();snakeHead.setY(snakeHead.getY()+direction.getY();/如果蛇吃到了目标食物tryif (snakeHead.getX()=snakeFood.getX()&&(snakeHead.getY()=snakeFood.getY()moveTo(locROsnakeHead.getX()snakeHead.getY(),playBlocks,snake);setSnakeFood();gameScore+=10;AddScore=true;elseAddScore=false;/是否出界if(snakeHead.getX()<rows)&&(snakeHead.getY()<cols)&&(snakeHead.getX()>=0&&(snakeHead.getY()>=0)/如果不出界,判断是否与自身相交if(snake.contains(locROsnakeHead.getX()snakeHead.getY()/如果相交,结束游戏JOptionPane.showMessageDialog(null, "Game Over!", "游戏结束", JOptionPane.INFORMATION_MESSAGE);return false;else/如果不相交,就把snakeHead加到snake里面,并且把尾巴移出moveTo(locROsnakeHead.getX()snakeHead.getY(),playBlocks,snake);moveTo(snake.getFirst(),snake,playBlocks);else/出界,游戏结束JOptionPane.showMessageDialog(null, "Game Over!", "游戏结束", JOptionPane.INFORMATION_MESSAGE);return false;catch(ArrayIndexOutOfBoundsException e)return false;/设置运行方向if (!(direction.reverse(runingDiriction)|direction.equals(runingDiriction)runingDiriction.setX(direction.getX();runingDiriction.setY(direction.getY();return true;package snake;import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.util.*;public class SnakePanel extends JPanel implements Runnable,KeyListenerJFrame parent=new JFrame();private int row=20; /网格行数private int col=30; /列数private JPanel gridsPanel; /面板网格private Location direction;/方向定位private SnakeModel snake; /贪吃蛇private LinkedList snakeBody; /蛇的身体private LinkedList otherBlocks; /其他区域private LocationRO snakeHead; /蛇的头部private LocationRO snakeFood; /目标食物private Color bodyColor=Color.orange;/蛇的身体颜色private Color headColor=Color.black; /蛇的头部颜色private Color foodColor=Color.red; /目标食物颜色private Color othersColor=Color.lightGray;/其他区域颜色private int gameScore=0; /总分private long speed; /速度(难度设置)private boolean AddScore;/加分private Thread t; /线程private boolean isEnd; /暂停private static boolean notExit; /构造器,初始化操作public SnakePanel(SnakeFrame parent)this.parent=parent;gridsPanel=new JPanelrowcol;otherBlocks=new LinkedList();snakeBody=new LinkedList();snakeHead=new LocationRO(0,0);snakeFood=new LocationRO(0,0);direction=new Location(0,1); / 布局setLayout(new GridLayout(row,col,1,1);for(int i=0;i<row;i+)for(int j=0;j<col;j+)gridsPanelij=new JPanel();gridsPanelij.setBackground(othersColor);add(gridsPanelij);addKeyListener(this); /开始游戏public void newGame(long speed)this.speed=speed;if (notExit) snake.init();elsesnake=new SnakeModel(row,col);notExit=true;t=new Thread(this);t.start();requestFocus();direction.setX(0);direction.setY(1);gameScore=0;updateTextFiled(""+gameScore);isEnd=false; /暂停游戏public void stopGame()requestFocus();isEnd=true; /继续public void returnGame()requestFocus();isEnd=false; /获得总分public int getGameScore()return gameScore;/更新总分private void updateTextFiled(String str)(SnakeFrame)parent).scoreField.setText(str);/更新各相关单元颜色private void updateColors() / 设定蛇身颜色snakeBody=snake.getSnake();Iterator i =snakeBody.iterator();while(i.hasNext()LocationRO t=(LocationRO)(i.next();gridsPanelt.getX()t.getY().setBackground(bodyColor);/设定蛇头颜色snakeHead=snake.getSnakeHead();gridsPanelsnakeHead.getX()snakeHead.getY().setBackground(headColor);/设定背景颜色otherBlocks=snake.getOthers();i =otherBlocks.iterator();while(i.hasNext()LocationRO t=(LocationRO)(i.next();gridsPanelt.getX()t.getY().setBackground(othersColor);/设定临时块的颜色snakeFood=snake.getSnakeFood();gridsPanelsnakeFood.getX()snakeFood.getY().setBackground(foodColor);public boolean isFocusTraversable() return true; /实现Runnable接口public void run() while(true)tryThread.sleep(speed);catch (InterruptedException e) if(!isEnd)isEnd=!snake.move(direction);updateColors(); if(snake.getAddScore()gameScore+=10;updateTextFiled(""+gameScore); /实现KeyListener接口public void keyPressed(KeyEvent event) int keyCode = event.getKeyCode(); if(notExit) if (keyCode = KeyEvent.VK_LEFT) /向左 direction.setX(0); direction.setY(-1); else if (keyCode = KeyEvent.VK_RIGHT) /向右 direction.setX(0); direction.setY(1); else if (keyCode = KeyEvent.VK_UP) /向上 direction.setX(-1); direction.setY(0); else if (keyCode = KeyEvent.VK_DOWN) /向下 direction.setX(1); direction.setY(0); else if (keyCode = KeyEvent.VK_SPACE) /空格键 isEnd=!isEnd; public void keyReleased(KeyEvent event)public void keyTyped(KeyEvent event)专心-专注-专业

    注意事项

    本文(JAVA程序源代码(贪吃蛇)(共13页).doc)为本站会员(飞****2)主动上传,淘文阁 - 分享文档赚钱的网站仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知淘文阁 - 分享文档赚钱的网站(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    关于淘文阁 - 版权申诉 - 用户使用规则 - 积分规则 - 联系我们

    本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

    工信部备案号:黑ICP备15003705号 © 2020-2023 www.taowenge.com 淘文阁 

    收起
    展开