网站导航网学 原创论文 原创专题 网站设计 最新系统 原创论文 论文降重 发表论文 论文发表 UI设计定制 论文答辩PPT格式排版 期刊发表 论文专题
返回网学首页
网学原创论文
最新论文 推荐专题 热门论文 论文专题
当前位置: 网学 > 交易代码 > Java精品代码 > 正文

java小游戏-贪吃蛇-JSP教程,Java技巧及代码

论文降重修改服务、格式排版等 获取论文 论文降重及排版 论文发表 相关服务
);

  this.getcontentpane().setbackground( colorgroup.color_back );

  this.setundecorated( true );

  this.setresizable( false );

  this.addkeylistener( new keyhandler() );

  gameframe.this.snaketimer = new javax.swing.timer( 80, new timerhandler() );

  this.getcontentpane().add( scorelabel, borderlayout.south );

  this.scorelabel.setfont( new font( "fixedsys", font.bold, 15 ) );

  this.scorelabel.settext( "pause[space] - exit[esc]" );

  this.configmenu = new configmenu( this );

  this.setvisible( true );

  }

  public void setgrid( int temp1, int temp2, int temp3 )

  {

  this.horizontalgrid = temp1;

  this.verticalgrid = temp2;

  this.singlewidthx = this.getwidth() / temp1;

  this.singleheighty = this.getheight() / temp2;

  this.coopos = temp3;

  }

  private class keyhandler extends keyadapter

  {

  public void keypressed( keyevent e )

  {

  if( e.getkeycode() == 27 )

  {

  snaketimer.stop();

  if( joptionpane.showconfirmdialog( null, "are you sure to exit?" ) == 0 )

  {

  system.exit( 0 );

  }

  snaketimer.start();

  }

  else if( e.getkeycode() == 37 && mainsnake.snakedirection != 2 )//left

  {

  direction = 4;

  }

  else if( e.getkeycode() == 39 && mainsnake.snakedirection != 4 )//right

  {

  direction = 2;

  }

  else if( e.getkeycode() == 38 && mainsnake.snakedirection != 3 )//up

  {

  direction = 1;

  }

  else if( e.getkeycode() == 40 && mainsnake.snakedirection != 1 )//down

  {

  direction = 3;

  }

  else if( e.getkeycode() == 32 )

  {

  if( !hasstoped )

  {

  if( !flag )

  {

  snaketimer.stop();

  configmenu.setvisible( true );

  configmenu.setmenuenable( false );

  flag = true;

  }

  else

  {

  snaketimer.start();

  configmenu.setvisible( false );

  configmenu.setmenuenable( true );

  flag = false;

  }

  }

  }

  }

  }

  private class timerhandler implements actionlistener

  {

  public synchronized void actionperformed( actionevent e )

  {

  point temp = (point) mainsnake.getlast();

  snakesq = mainsnake.iterator();

  while ( snakesq.hasnext() )

  {

  point temppoint = (point)snakesq.next();

  if( temp.equals( temppoint ) && snakesq.hasnext() != false )

  {

  snaketimer.stop();

  stopgame();

  joptionpane.showmessagedialog( null,

  "your score is " + score + "\n\nyou loss!" );

  }

  }

  system.out.println( temp.x + " " + temp.y );

  if( (temp.x == 0 && direction == 4) ||

  (temp.x == horizontalgrid-1 && direction == 2) ||

  (temp.y == 0 && direction == 1) ||

  (temp.y == verticalgrid-1 && direction == 3) )

  {

  snaketimer.stop();

  stopgame();

  joptionpane.showmessagedialog( null,

  "your score is " + score + "\n\nyou loss!" );

  }

  if( direction != mainsnake.snakeredirection )

  {

  movesnake( direction );

  }

  mainsnake.drawsnake( getgraphics(), singlewidthx, singleheighty, coopos );

  drawbeanandebean( getgraphics() );

  }

  }

  public void stopgame()

  {

  this.hasstoped = true;

  this.snaketimer.stop();

  graphics2d g = (graphics2d) gameframe.this.getgraphics();

  g.setcolor( colorgroup.color_back );

  super.paint( g );

  configmenu.setvisible( true );

  }

  public void resetgame()

  {

  system.gc();

  this.hasstoped = false;

  graphics2d g = (graphics2d) gameframe.this.getgraphics();

  g.setcolor( colorgroup.color_back );

  super.paint( g );

  this.mainsnake = new snake();

  this.createbean( bean );

  this.eatedbean.clear();

  mainsnake.drawsnake( getgraphics(), singlewidthx, singleheighty, coopos );

  this.snaketimer.start();

  this.direction = 2;

  this.score = 0;

  this.scorela

  • 上一篇资讯: 记事本的JAVA源代码
  • 设为首页 | 加入收藏 | 网学首页 | 原创论文 | 计算机原创
    版权所有 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
    Copyright 2008-2020 myeducs.Cn www.myeducs.Cn All Rights Reserved 湘ICP备09003080号 常年法律顾问:王律师