1、JAVA程序源代码贪吃蛇贪吃蛇源代码 将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(
2、); 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
3、=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) ret
4、urn (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
5、.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 sna
6、ke;import java.awt.*;import java.awt.event.*;import javax.swing.*;class SnakeFrame extends JFrame implements ActionListener final SnakePanel p=new SnakePanel(this); JMenuBar menubar=new JMenuBar(); JMenu fileMenu=new JMenu(文件); JMenuItem newgameitem=new JMenuItem(开始); JMenuItem stopitem=new JMenuIte
7、m(暂停); JMenuItem runitem=new JMenuItem(继续); JMenuItem exititem=new JMenuItem(退出); /设置菜单 JMenu optionMenu=new JMenu(设置); /等级选项 ButtonGroup groupDegree = new ButtonGroup(); JRadioButtonMenuItem oneItem= new JRadioButtonMenuItem(初级); JRadioButtonMenuItem twoItem= new JRadioButtonMenuItem(中级); JRadioBut
8、tonMenuItem 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;
9、private String helpstr = 游戏说明:n1 :方向键控制蛇移动的方向.+ n2 :单击菜单文件-开始开始游戏.+ n3 :单击菜单文件-暂停或者单击键盘空格键暂停游戏.+ n4 :单击菜单文件-继续继续游戏.+ n5 :单击菜单设置-等级可以设置难度等级.+ n6 :单击菜单设置-显示网格可以设置是否显示网格.+ n7 :红色为食物,吃一个得10分同时蛇身加长.+ n8 :蛇不可以出界或自身相交,否则结束游戏.; SnakeFrame() setJMenuBar(menubar); fileMenu.add(newgameitem); fileMenu.add(stopi
10、tem); 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); / 风格选项 showG
11、ridItem.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); scoreFie
12、ld.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.addActionListe
13、ner(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,操纵
14、说明,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.setBack
15、ground(Color.lightGray); else p.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.JOpti
16、onPane;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;/加分 / 获得蛇头 pu
17、blic 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() re
18、turn 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() snakeF
19、ood=(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;irows;i+) for(int j=0;jcols;j+) playBl
20、ocks.add(locROij); /初始化蛇的形状 for(int i=4;i7;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 Loc
21、ationROrowscols; snake=new LinkedList(); playBlocks=new LinkedList(); for(int i=0;irows;i+) for(int j=0;jcols;j+) locROij=new LocationRO(i,j); init(); /*定义布尔型move方法,如果运行成功则返回true,否则返回false *参数direction是Location类型, *direction 的值:(-1,0)表示向上;(1,0)表示向下; *(0,-1)表示向左;(0,1)表示向右; */ public boolean move(Loca
22、tion direction) /判断设定的方向跟运行方向是不是相反 if (direction.reverse(runingDiriction) snakeHead.setX(snakeHead.getX()+runingDiriction.getX(); snakeHead.setY(snakeHead.getY()+runingDiriction.getY(); else snakeHead.setX(snakeHead.getX()+direction.getX(); snakeHead.setY(snakeHead.getY()+direction.getY(); /如果蛇吃到了目标
23、食物 try if (snakeHead.getX()=snakeFood.getX() &(snakeHead.getY()=snakeFood.getY() moveTo(locROsnakeHead.getX()snakeHead.getY(),playBlocks,snake); setSnakeFood(); gameScore+=10; AddScore=true; else AddScore=false; /是否出界 if(snakeHead.getX()rows)&(snakeHead.getY()=0&(snakeHead.getY()=0) /如果不出界,判断是否与自身相交
24、 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(),s
25、nake,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(directi
26、on.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,KeyListener JFrame parent=new JFrame(); private int row=20; /网格行数 private int col=
27、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; /加分
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1