本文共 1890 字,大约阅读时间需要 6 分钟。
public void showsnake(graphics g){//globeinfo.snake为vector类型 int num=globeinfo.snake.size(); int left=(w-grid*globeinfo.gridxnum)/2; int top=h-grid*globeinfo.gridynum-space*2; g.setcolor(0,0,255); for(int i=0;i<num;i++){ point point=(point)globeinfo.snake.elementat(i); if(point.x>=0&&point.y>=0){ int l=left+point.x*grid; int t=top+point.y*grid; g.fillrect(l, t, grid, grid); } } } public void movesnake(){//point 为一个格式点的数据结构(x,y) point pointtail=(point)globeinfo.snake.elementat(0); point pointhead=(point)globeinfo.snake.elementat(globeinfo.snake.size()-1); point point=new point(-1, -1); if(direct.right){ point.x=pointhead.x+1; point.y=pointhead.y; if(point.x>=globeinfo.gridxnum) end=true; } else if(direct.left){ point.x=pointhead.x-1; point.y=pointhead.y; if(point.x<0) end=true; } else if(direct.up){ point.x=pointhead.x; point.y=pointhead.y-1; if(point.y<0) end=true; } else if(direct.down){ point.x=pointhead.x; point.y=pointhead.y+1; if(point.y>=globeinfo.gridynum) end=true; } for(int i=0;i<globeinfo.snake.size();i++){ point pointother=(point)globeinfo.snake.elementat(i); if(pointother.x==point.x&&pointother.y==point.y){ end=true; break; } } if(end==false&&globeinfo.snake.size()>0){ if(point.x==eatx && point.y==eaty){ eat=false; show=true; globeinfo.snake.addelement(point); globeinfo.money+=100; num++; if(num>=globeinfo.levelnum) end=true; } else{ globeinfo.snake.removeelementat(0); globeinfo.snake.addelement(point); } } else{ timer.cancel(); } servicerepaints(); } public void randomeat(){ if(eat==false){ random rand=new random(); boolean product=false; while(product==false){ int x=rand.nextint(globeinfo.gridxnum); int y=rand.nextint(globeinfo.gridynum); int i; for(i=0;i<globeinfo.snake.size();i++){ point point=(point)globeinfo.snake.elementat(i); if(point.x==x&&point.y==y) break; } if(i==globeinfo.snake.size()){ eatx=x; eaty=y; eat=true; product=true; } } } }
转载地址:http://zdedi.baihongyu.com/