JAVA程序源代码贪吃蛇.docx
《JAVA程序源代码贪吃蛇.docx》由会员分享,可在线阅读,更多相关《JAVA程序源代码贪吃蛇.docx(18页珍藏版)》请在冰豆网上搜索。
JAVA程序源代码贪吃蛇
贪吃蛇源代码
将Location、LocationRO、SnakeFrame、SnakeModel、SnakePanel放到命名为snake的文件夹里,主函数MainApp放到外面运行主函数即可实现。
主函数
packagesnake;
importjavax.swing.*;
importsnake.*;
publicclassMainApp{
publicstaticvoidmain(String[]args){
JFrame.setDefaultLookAndFeelDecorated(true);
SnakeFrameframe=newSnakeFrame();
frame.setSize(350,350);
frame.setResizable(false);
frame.setLocation(330,220);
frame.setTitle("贪吃蛇");
frame.setVisible(true);
}
}
packagesnake;
publicclassLocation{
privateintx;
privateinty;
Location(intx,inty){
this.x=x;
this.y=y;
}
intgetX(){
returnx;
}
intgetY(){
returny;
}
voidsetX(intx){
this.x=x;
}
voidsetY(inty){
this.y=y;
}
publicbooleanequalOrRev(Locatione){
return((e.getX()==getX())&&(e.getY()==getY()))
||((e.getX()==getX())&&(e.getY()==-1*getY()))
||((e.getX()==-1*getX())&&(e.getY()==getY()));
}
publicbooleanequals(Locatione){
return(e.getX()==getX())&&(e.getY()==getY());
}
publicbooleanreverse(Locatione){
return((e.getX()==getX())&&(e.getY()==-1*getY()))
||((e.getX()==-1*getX())&&(e.getY()==getY()));
}
}
packagesnake;
publicclassLocationRO{
privateintx;
privateinty;
LocationRO(intx,inty){
this.x=x;
this.y=y;
}
intgetX(){
returnx;
}
intgetY(){
returny;
}
publicbooleanequalOrRev(LocationROe){
return((e.getX()==getX())&&(e.getY()==getY()))
||((e.getX()==getX())&&(e.getY()==-1*getY()))
||((e.getX()==-1*getX())&&(e.getY()==getY()));
}
publicbooleanequals(LocationROe){
return(e.getX()==getX())&&(e.getY()==getY());
}
publicbooleanreverse(LocationROe){
return((e.getX()==getX())&&(e.getY()==-1*getY()))
||((e.getX()==-1*getX())&&(e.getY()==getY()));
}
}
packagesnake;
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
classSnakeFrameextendsJFrameimplementsActionListener{
finalSnakePanelp=newSnakePanel(this);
JMenuBarmenubar=newJMenuBar();
JMenufileMenu=newJMenu("文件");
JMenuItemnewgameitem=newJMenuItem("开始");
JMenuItemstopitem=newJMenuItem("暂停");
JMenuItemrunitem=newJMenuItem("继续");
JMenuItemexititem=newJMenuItem("退出");
//"设置"菜单
JMenuoptionMenu=newJMenu("设置");
//等级选项
ButtonGroupgroupDegree=newButtonGroup();
JRadioButtonMenuItemoneItem=newJRadioButtonMenuItem("初级");
JRadioButtonMenuItemtwoItem=newJRadioButtonMenuItem("中级");
JRadioButtonMenuItemthreeItem=newJRadioButtonMenuItem("高级");
JMenudegreeMenu=newJMenu("等级");
JMenuhelpMenu=newJMenu("帮助");
JMenuItemhelpitem=newJMenuItem("操作指南");
finalJCheckBoxMenuItemshowGridItem=newJCheckBoxMenuItem("显示网格");
JLabelscorelabel;
publicJTextFieldscoreField;
privatelongspeedtime=200;
privateStringhelpstr="游戏说明:
\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);
Containercontentpane=getContentPane();
contentpane.setLayout(newFlowLayout());
contentpane.add(p);
scorelabel=newJLabel("得分:
");
scoreField=newJTextField("0",15);
scoreField.setEnabled(false);
scoreField.setHorizontalAlignment(0);
JPaneltoolPanel=newJPanel();
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);
}
publicvoidactionPerformed(ActionEvente){
try{
if(e.getSource()==helpitem){
JOptionPane.showConfirmDialog(p,helpstr,"操纵说明",JOptionPane.PLAIN_MESSAGE);
}
elseif(e.getSource()==exititem)System.exit(0);
elseif(e.getSource()==newgameitem)p.newGame(speedtime);
elseif(e.getSource()==stopitem)p.stopGame();
elseif(e.getSource()==runitem)p.returnGame();
elseif(e.getSource()==showGridItem){
if(!
showGridItem.isSelected()){
p.setBackground(Color.lightGray);
}else{
p.setBackground(Color.darkGray);
}
}
elseif(e.getSource()==oneItem)speedtime=200;
elseif(e.getSource()==twoItem)speedtime=100;
elseif(e.getSource()==threeItem)speedtime=50;
}catch(Exceptionee){ee.printStackTrace();}
}
}
packagesnake;
importjava.util.*;
importjavax.swing.JOptionPane;
publicclassSnakeModel{
privateintrows,cols;//行列数
privateLocationsnakeHead,runingDiriction;//运行方向
privateLocationRO[][]locRO;//LocationRO类数组
privateLinkedListsnake,playBlocks;//蛇及其它区域块
privateLocationROsnakeFood;//目标食物
privateintgameScore=0;//分数
privatebooleanAddScore=false;//加分
//获得蛇头
publicLocationROgetSnakeHead(){
return(LocationRO)(snake.getLast());
}
//蛇尾
publicLocationROgetSnakeTail(){
return(LocationRO)(snake.getFirst());
}
//运行路线
publicLocationgetRuningDiriction(){
returnruningDiriction;
}
//获得蛇实体区域
publicLinkedListgetSnake(){
returnsnake;
}
//其他区域
publicLinkedListgetOthers(){
returnplayBlocks;
}
//获得总分
publicintgetScore(){
returngameScore;
}
//获得增加分数
publicbooleangetAddScore(){
returnAddScore;
}
//设置蛇头方向
privatevoidsetSnakeHead(LocationsnakeHead){
this.snakeHead=snakeHead;
}
//获得目标食物
publicLocationROgetSnakeFood(){
returnsnakeFood;
}
//随机设置目标食物
privatevoidsetSnakeFood(){
snakeFood=(LocationRO)(playBlocks.get((int)(Math.random()*playBlocks.size())));
}
//移动
privatevoidmoveTo(Objecta,LinkedListfromlist,LinkedListtolist){
fromlist.remove(a);
tolist.add(a);
}
//初始设置
publicvoidinit(){
playBlocks.clear();
snake.clear();
gameScore=0;
for(inti=0;ifor(intj=0;jplayBlocks.add(locRO[i][j]);
}
}
//初始化蛇的形状
for(inti=4;i<7;i++){
moveTo(locRO[4][i],playBlocks,snake);
}
//蛇头位置
snakeHead=newLocation(4,6);
//设置随机块
snakeFood=newLocationRO(0,0);
setSnakeFood();
//初始化运动方向
runingDiriction=newLocation(0,1);
}
//Snake构造器
publicSnakeModel(introws1,intcols1){
rows=rows1;
cols=cols1;
locRO=newLocationRO[rows][cols];
snake=newLinkedList();
playBlocks=newLinkedList();
for(inti=0;ifor(intj=0;jlocRO[i][j]=newLocationRO(i,j);
}
}
init();
}
/**定义布尔型move方法,如果运行成功则返回true,否则返回false
*参数direction是Location类型,
*direction的值:
(-1,0)表示向上;(1,0)表示向下;
*(0,-1)表示向左;(0,1)表示向右;
**/
publicbooleanmove(Locationdirection){
//判断设定的方向跟运行方向是不是相反
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());
}
//如果蛇吃到了目标食物
try{
if((snakeHead.getX()==snakeFood.getX())
&&(snakeHead.getY()==snakeFood.getY()))
{
moveTo(locRO[snakeHead.getX()][snakeHead.getY()],playBlocks,snake);
setSnakeFood();
gameScore+=10;
AddScore=true;
}else{
AddScore=false;
//是否出界
if((snakeHead.getX()=0&&(snakeHead.getY()>=0))){
//如果不出界,判断是否与自身相交
if(snake.contains(locRO[snakeHead.getX()][snakeHead.getY()])){
//如果相交,结束游戏
JOptionPane.showMessageDialog(null,"GameOver!
","游戏结束",JOptionPane.INFORMATION_MESSAGE);
returnfalse;
}else{
//如果不相交,就把snakeHead加到snake里面,并且把尾巴移出
moveTo(locRO[snakeHead.getX()][snakeHead.getY()],playBlocks,snake);
moveTo(snake.getFirst(),snake,playBlocks);
}
}else{
//出界,游戏结束
JOptionPane.showMessageDialog(null,"GameOver!
","游戏结束",JOptionPane.INFORMATION_MESSAGE);
returnfalse;
}
}
}catch(ArrayIndexOutOfBoundsExceptione){
returnfalse;
}
//设置运行方向
if(!
(direction.reverse(runingDiriction)
||direction.equals(runingDiriction))){
runingDiriction.setX(direction.getX());
runingDiriction.setY(direction.getY());
}
returntrue;
}
}
packagesnake;
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
importjava.util.*;
publicclassSnakePanelextendsJPanelimplementsRunnable,KeyListener{
JFrameparent=newJFrame();
privateintrow=20;//网格行数
privateintcol=30;//列数
privateJPanel[][]gridsPanel;//面板网格
privateLocationdirection;//方向定位
privateSnakeModelsnake;//贪吃蛇
privateLinkedListsnakeBody;//蛇的身体
privateLinkedListotherBlocks;//其他区域
privateLocationROsnakeHead;//蛇的头部
privateLocationROsnakeFood;//目标食物
privateColorbodyColor=Color.orange;//蛇的身体颜色
privateColorheadColor=Color.black;//蛇的头部颜色
privateColorfoodColor=Color.red;//目标食物颜色
privateColorothersColor=Color.lightGray;//其他区域颜色
privateintgameScore=0;//总分
privatelongspeed;//速度(难度设置)
privat