JAVA小程序贪吃蛇源代码Word格式文档下载.docx
- 文档编号:13515247
- 上传时间:2022-10-11
- 格式:DOCX
- 页数:17
- 大小:17.36KB
JAVA小程序贪吃蛇源代码Word格式文档下载.docx
《JAVA小程序贪吃蛇源代码Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《JAVA小程序贪吃蛇源代码Word格式文档下载.docx(17页珍藏版)》请在冰豆网上搜索。
publicintsnakeDirection=2;
publicintsnakeReDirection=4;
publicSnake()
this.add(newPoint(3,3));
this.add(newPoint(4,3));
this.add(newPoint(5,3));
this.add(newPoint(6,3));
this.add(newPoint(7,3));
this.add(newPoint(8,3));
this.add(newPoint(9,3));
this.add(newPoint(10,3));
publicvoidchangeDirection(Pointtemp,intdirection)
this.snakeDirection=direction;
switch(direction)
case1:
//up
this.snakeReDirection=3;
this.add(newPoint(temp.x,temp.y-1));
break;
case2:
//right
this.snakeReDirection=4;
this.add(newPoint(temp.x+1,temp.y));
case3:
//down
this.snakeReDirection=1;
this.add(newPoint(temp.x,temp.y+1));
case4:
//left
this.snakeReDirection=2;
this.add(newPoint(temp.x-1,temp.y));
publicbooleancheckBeanIn(Pointbean)
Pointtemp=(Point)this.getLast();
if(temp.equals(bean))
returntrue;
returnfalse;
publicvoidremoveTail()
this.remove(0);
publicvoiddrawSnake(Graphicsg,intsingleWidthX,intsingleHeightY,intcooPos)
g.setColor(ColorGroup.COLOR_SNAKE);
IteratorsnakeSq=this.iterator();
while(snakeSq.hasNext())
PointtempPoint=(Point)snakeSq.next();
this.drawSnakePiece(g,tempPoint.x,tempPoint.y,
singleWidthX,singleHeightY,cooPos);
publicvoiddrawSnakePiece(Graphicsg,inttemp1,inttemp2,
intsingleWidthX,intsingleHeightY,intcooPos)
g.fillRoundRect(singleWidthX*temp1+1,
singleHeightY*temp2+1,
singleWidthX-2,
singleHeightY-2,
cooPos,
cooPos);
publicvoidclearEndSnakePiece(Graphicsg,inttemp1,inttemp2,
g.setColor(ColorGroup.COLOR_BACK);
GameFrame.java
importjava.awt.event.*;
importjava.awt.geom.*;
classGameFrameextendsJFrame
privateToolkittempKit;
privateinthorizontalGrid,verticalGrid;
privateintsingleWidthX,singleHeightY;
privateintcooPos;
privateSnakemainSnake;
privateLinkedListeatedBean;
eatedBean=newLinkedList();
privateIteratorsnakeSq;
publicjavax.swing.TimersnakeTimer;
privateintdirection=2;
privateintscore;
privateStringinfo;
privatePointbean,eatBean;
bean=newPoint();
privatebooleanflag;
privateJMenuBarinfoMenu;
privateJMenu[]tempMenu;
privateJMenuItem[]tempMenuItem;
privateJRadioButtonMenuItem[]levelMenuItem,versionMenuItem;
privateJLabelscoreLabel;
scoreLabel=newJLabel();
privateGraphics2Dg;
privateImageIconsnakeHead;
snakeHead=newImageIcon("
LOGO.gif"
);
privateConfigMenuconfigMenu;
privatebooleanhasStoped=true;
publicGameFrame()
this.tempKit=this.getToolkit();
this.setSize(tempKit.getScreenSize());
this.setGrid(60,40,5);
this.getContentPane().setBackground(ColorGroup.COLOR_BACK);
this.setUndecorated(true);
this.setResizable(false);
this.addKeyListener(newKeyHandler());
GameFrame.this.snakeTimer=newjavax.swing.Timer(80,newTimerHandler());
this.getContentPane().add(scoreLabel,BorderLayout.SOUTH);
this.scoreLabel.setFont(newFont("
Fixedsys"
Font.BOLD,15));
this.scoreLabel.setText("
Pause[SPACE]-Exit[ESC]"
this.configMenu=newConfigMenu(this);
this.setVisible(true);
publicvoidsetGrid(inttemp1,inttemp2,inttemp3)
this.horizontalGrid=temp1;
this.verticalGrid=temp2;
this.singleWidthX=this.getWidth()/temp1;
this.singleHeightY=this.getHeight()/temp2;
this.cooPos=temp3;
privateclassKeyHandlerextendsKeyAdapter
publicvoidkeyPressed(KeyEvente)
if(e.getKeyCode()==27)
snakeTimer.stop();
if(JOptionPane.showConfirmDialog(null,"
Areyousuretoexit?
"
)==0)
System.exit(0);
snakeTimer.start();
elseif(e.getKeyCode()==37&
&
mainSnake.snakeDirection!
=2)//left
direction=4;
elseif(e.getKeyCode()==39&
=4)//right
direction=2;
elseif(e.getKeyCode()==38&
=3)//up
direction=1;
elseif(e.getKeyCode()==40&
=1)//down
direction=3;
elseif(e.getKeyCode()==32)
if(!
hasStoped)
flag)
configMenu.setVisible(true);
configMenu.setMenuEnable(false);
flag=true;
else
snak
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- JAVA 程序 贪吃 源代码