您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 管理学资料 > 贪吃蛇JAVA源代码完整版
本文已或作者授权,转载请注明出处ALLRIGHTSRESERVED1游戏贪吃蛇的JAVA源代码一.文档说明a)本代码主要功能为实现贪吃蛇游戏,GUI界面做到尽量简洁和原游戏相仿。目前版本包含计分,统计最高分,长度自动缩短计时功能。b)本代码受计算机系大神指点,经许可后发布如下,向Java_online网致敬c)运行时请把.java文件放入defaultpackage即可运行。二.运行截图a)文件位置b)进入游戏本文已或作者授权,转载请注明出处ALLRIGHTSRESERVED2c)游戏进行中三.JAVA代码本文已或作者授权,转载请注明出处ALLRIGHTSRESERVED3importjava.awt.*;importjava.awt.event.*;importstaticjava.lang.String.format;importjava.util.*;importjava.util.List;importjavax.swing.*;publicclassSnakeextendsJPanelimplementsRunnable{enumDir{up(0,-1),right(1,0),down(0,1),left(-1,0);Dir(intx,inty){this.x=x;this.y=y;}finalintx,y;}staticfinalRandomrand=newRandom();staticfinalintWALL=-1;staticfinalintMAX_ENERGY=1500;volatilebooleangameOver=true;ThreadgameThread;intscore,hiScore;intnRows=44;intnCols=64;Dirdir;intenergy;int[][]grid;ListPointsnake,treats;FontsmallFont;publicSnake(){setPreferredSize(newDimension(640,440));setBackground(Color.white);setFont(newFont(SansSerif,Font.BOLD,48));setFocusable(true);smallFont=getFont().deriveFont(Font.BOLD,18);initGrid();本文已或作者授权,转载请注明出处ALLRIGHTSRESERVED4addMouseListener(newMouseAdapter(){@OverridepublicvoidmousePressed(MouseEvente){if(gameOver){startNewGame();repaint();}}});addKeyListener(newKeyAdapter(){@OverridepublicvoidkeyPressed(KeyEvente){switch(e.getKeyCode()){caseKeyEvent.VK_UP:if(dir!=Dir.down)dir=Dir.up;break;caseKeyEvent.VK_LEFT:if(dir!=Dir.right)dir=Dir.left;break;caseKeyEvent.VK_RIGHT:if(dir!=Dir.left)dir=Dir.right;break;caseKeyEvent.VK_DOWN:if(dir!=Dir.up)dir=Dir.down;break;}repaint();}});}voidstartNewGame(){gameOver=false;本文已或作者授权,转载请注明出处ALLRIGHTSRESERVED5stop();initGrid();treats=newLinkedList();dir=Dir.left;energy=MAX_ENERGY;if(scorehiScore)hiScore=score;score=0;snake=newArrayList();for(intx=0;x7;x++)snake.add(newPoint(nCols/2+x,nRows/2));doaddTreat();while(treats.isEmpty());(gameThread=newThread(this)).start();}voidstop(){if(gameThread!=null){Threadtmp=gameThread;gameThread=null;tmp.interrupt();}}voidinitGrid(){grid=newint[nRows][nCols];for(intr=0;rnRows;r++){for(intc=0;cnCols;c++){if(c==0||c==nCols-1||r==0||r==nRows-1)grid[r][c]=WALL;}}}@Overridepublicvoidrun(){本文已或作者授权,转载请注明出处ALLRIGHTSRESERVED6while(Thread.currentThread()==gameThread){try{Thread.sleep(Math.max(75-score,25));}catch(InterruptedExceptione){return;}if(energyUsed()||hitsWall()||hitsSnake()){gameOver();}else{if(eatsTreat()){score++;energy=MAX_ENERGY;growSnake();}moveSnake();addTreat();}repaint();}}booleanenergyUsed(){energy-=10;returnenergy=0;}booleanhitsWall(){Pointhead=snake.get(0);intnextCol=head.x+dir.x;intnextRow=head.y+dir.y;returngrid[nextRow][nextCol]==WALL;}booleanhitsSnake(){Pointhead=snake.get(0);intnextCol=head.x+dir.x;intnextRow=head.y+dir.y;for(Pointp:snake)if(p.x==nextCol&&p.y==nextRow)returntrue;returnfalse;本文已或作者授权,转载请注明出处ALLRIGHTSRESERVED7}booleaneatsTreat(){Pointhead=snake.get(0);intnextCol=head.x+dir.x;intnextRow=head.y+dir.y;for(Pointp:treats)if(p.x==nextCol&&p.y==nextRow){returntreats.remove(p);}returnfalse;}voidgameOver(){gameOver=true;stop();}voidmoveSnake(){for(inti=snake.size()-1;i0;i--){Pointp1=snake.get(i-1);Pointp2=snake.get(i);p2.x=p1.x;p2.y=p1.y;}Pointhead=snake.get(0);head.x+=dir.x;head.y+=dir.y;}voidgrowSnake(){Pointtail=snake.get(snake.size()-1);intx=tail.x+dir.x;inty=tail.y+dir.y;snake.add(newPoint(x,y));}voidaddTreat(){if(treats.size()3){if(rand.nextInt(10)==0){//1in10if(rand.nextInt(4)!=0){//3in4intx,y;本文已或作者授权,转载请注明出处ALLRIGHTSRESERVED8while(true){x=rand.nextInt(nCols);y=rand.nextInt(nRows);if(grid[y][x]!=0)continue;Pointp=newPoint(x,y);if(snake.contains(p)||treats.contains(p))continue;treats.add(p);break;}}elseif(treats.size()1)treats.remove(0);}}}voiddrawGrid(Graphics2Dg){g.setColor(Color.lightGray);for(intr=0;rnRows;r++){for(intc=0;cnCols;c++){if(grid[r][c]==WALL)g.fillRect(c*10,r*10,10,10);}}}voiddrawSnake(Graphics2Dg){g.setColor(Color.blue);for(Pointp:snake)g.fillRect(p.x*10,p.y*10,10,10);g.setColor(energy500?Color.red:Color.orange);Pointhead=snake.get(0);g.fillRect(head.x*10,head.y*10,10,10);}voiddrawTreats(Graphics2Dg){g.setColor(Color.green);for(Pointp:treats)g.fillRect(p.x*10,p.y*10,10,10);本文已或作者授权,转载请注明出处ALLRIGHTSRESERVED9}voiddrawStartScreen(Graphics2Dg){g.setColor(Color.blue);g.setFont(getFont());g.drawString(Snake,240,190);g.setColor(Color.orange);g.setFont(smallFont);g.drawString((clicktostart),250,240);}voiddrawScore(Graphics2Dg){inth=getHeight();g.setFont(smallFont);g.setColor(getForeground());Strings=format(hiscore%dscore%d,hiScore,score);g.drawString(s,30,h-30);g.drawString(format(energy%d,energy),getWidth()-150,h-30);}@OverridepublicvoidpaintComponent(Graphicsgg){super.paintComponent(gg);Graphics2Dg=(Graphics2D)gg;g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);drawGrid(g);if(gameOver){drawStartScreen(g);}else{drawSnake(g);drawTreats(g);drawScore(g);}}publicstaticvoidmain(String[]args){SwingUtilities.invokeLater(()-{JFramef=newJFrame();f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);f.setTitle(Snake);本文已或作者授权,转载请注明出处ALLRIGHTSRESERVED10f.setResizabl
本文标题:贪吃蛇JAVA源代码完整版
链接地址:https://www.777doc.com/doc-1988278 .html