问题描述
为什么这个蛇有的时候会一下走两格?一卡一卡的找了好久都没有找出错误....才这么点程序不会是电脑卡造成的吧,求大神帮忙看看[code=publicclassGameTest{publicstaticvoidmain(String[]args){Yardy=newYard(newSnake());y.gameBegin();}}importjava.awt.Color;importjava.awt.Font;importjava.awt.Frame;importjava.awt.Graphics;importjava.awt.Image;importjava.awt.event.KeyAdapter;importjava.awt.event.KeyEvent;importjava.awt.event.WindowAdapter;importjava.awt.event.WindowEvent;publicclassYardextendsFrame{privateSnakesnake;//privateEggegg;//privateWallwall;publicYard(Snakesnake){super("贪食蛇");this.snake=snake;//this.egg=egg;//this.wall=wall;this.setBounds(0,0,util.COLS*util.SIZE,util.ROWS*util.SIZE);this.setBackground(Color.GRAY);this.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});this.setVisible(true);this.addKeyListener(newKeyMoniter());}privateImageibuffer;privateGraphicsgbuffer;publicvoidupdate(Graphicsg){if(ibuffer==null){ibuffer=this.createImage(this.getSize().width,this.getSize().width);gbuffer=ibuffer.getGraphics();}gbuffer.setColor(getBackground());gbuffer.fillRect(0,0,this.getSize().width,this.getSize().height);paint(gbuffer);g.drawImage(ibuffer,0,0,null);}Fontfont=newFont("宋体",Font.BOLD,20);publicvoidpaint(Graphicsg){System.out.println("画出游戏界面和格子不包括窗口");g.setColor(Color.BLACK);//画出横线for(inti=0;i<util.ROWS;i++){g.drawLine(0,util.SIZE*i,util.SIZE*util.COLS,util.SIZE*i);}//画出竖线for(inti=0;i<util.COLS;i++){g.drawLine(util.SIZE*i,0,util.SIZE*i,util.SIZE*util.ROWS);}g.setFont(font);g.setColor(Color.red);g.drawString("score:",1*util.SIZE,3*util.SIZE);g.drawString(util.getScore(),6*util.SIZE,3*util.SIZE);//wall.drawMe();snake.drawMe(g);//egg.drawMe();}privateclasssnakeThreadimplementsRunnable{//蛇不断移动publicvoidrun(){while(true){repaint();snake.move();try{Thread.sleep(util.TEMPO);}catch(InterruptedExceptione){e.printStackTrace();}}}}publicvoidstart(){newThread(newsnakeThread()).start();}publicvoidgameBegin(){System.out.println("开始游戏");start();}publicclassKeyMoniterextendsKeyAdapter{publicvoidkeyPressed(KeyEvente){switch(e.getKeyCode()){caseKeyEvent.VK_UP:snake.changeDir(util.UP);break;caseKeyEvent.VK_DOWN:snake.changeDir(util.DOWN);break;caseKeyEvent.VK_LEFT:snake.changeDir(util.LEFT);break;caseKeyEvent.VK_RIGHT:snake.changeDir(util.RIGHT);break;}}}}importjava.awt.Color;importjava.awt.Graphics;importjava.util.LinkedList;publicclassSnake{privateintdir;//蛇方向privateYardyard;publicLinkedList<body>bodys=newLinkedList<body>();publicSnake(){init();}privatevoidinit(){//初始化方法for(inti=0;i<util.INIT;i++){bodys.addLast(newbody(util.C_ROW,util.C_COL+i));}dir=util.LEFT;}publicvoidmove(){System.out.println("蛇移动");//去尾bodys.removeLast();//加头bodyb=bodys.getFirst();introw=b.ROW;intcol=b.COL;switch(this.dir){caseutil.UP:if(row<2){row=util.ROWS-2;}else{row--;}break;caseutil.DOWN:if(row>util.ROWS-2){row=2;}else{row++;}break;caseutil.LEFT:if(col<2){col=util.COLS-2;}else{col--;}break;caseutil.RIGHT:if(col>util.COLS-2){col=2;}else{col++;}break;}bodys.addFirst(newbody(row,col));}publicvoidchangeMove(){System.out.println("改变蛇移动速度");}publicvoidchangeDir(intdir){System.out.println("蛇改变方向");this.dir=dir;}publicvoideatEgg(){System.out.println("蛇吃蛋");}publicbooleanisEatBody(){System.out.println("判断蛇是否吃到身体");returnfalse;}publicvoiddrawMe(Graphicsg){System.out.println("画出蛇");g.setColor(Color.BLUE);for(bodyb:bodys){g.fill3DRect(b.COL*util.SIZE,b.ROW*util.SIZE,util.SIZE,util.SIZE,true);}}publicvoiddie(){System.out.println("蛇死了");}privatestaticclassbody{//蛇身体类privateintROW;privateintCOL;publicbody(intrOW,intcOL){ROW=rOW;COL=cOL;}publicintgetROW(){returnROW;}publicvoidsetROW(intrOW){ROW=rOW;}publicintgetCOL(){returnCOL;}publicvoidsetCOL(intcOL){COL=cOL;}}}//参数类publicclassutil{//游戏界面参数publicstaticfinalintROWS=30;//行publicstaticfinalintCOLS=30;//列publicstaticfinalintSIZE=15;//蛇参数publicstaticfinalintINIT=10;//初始时蛇身长度//蛇移动速度参数publicstaticfinalintTEMPO=1000;publicstaticfinalintSLOW=1000;publicstaticfinalintCENTER=600;publicstaticfinalintFAST=300;publicstaticfinalintMIN=100;//蛇初始位置publicstaticfinalintC_COL=COLS/2;publicstaticfinalintC_ROW=ROWS/2;//方向参数publicstaticfinalintUP=-1;publicstaticfinalintDOWN=1;publicstaticfinalintLEFT=-2;publicstaticfinalintRIGHT=2;//分数publicstaticIntegerscore=0;publicvoidsetScore(intscore){this.score=score;}publicstaticStringgetScore(){//TODOAuto-generatedmethodstubreturnscore.toString();}}[/code]
解决方案
本帖最后由 u010795682 于 2014-06-27 23:38:16 编辑