CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Java >  GUI 设计

高分悬赏,在线等,请指教

楼主lining771()2006-10-04 13:39:57 在 Java / GUI 设计 提问

这是游戏贪食蛇的游戏代码,我不知道为什么运行后只显示一个JFRAME的框,画布还有蛇什么的都没有显示,代码语法上没错,不知道逻辑上哪错了,请指点,在线等,解决就给分  
   
  import   java.awt.*;  
  import   java.awt.event.*;  
   
  import   javax.swing.*;  
   
  import   java.util.*;  
  public   class   EatSnake   implements   KeyListener  
  {  
  JFrame   f;  
  Canvas   c;  
  SnakeModel   snakeModel=   null;  
  public   static   final   int   cw=450;  
  public   static   final   int   ch=400;  
  public   static   final   int   nw=10;  
  public   static   final   int   nh=10;  
  public   EatSnake()  
  {  
  f=   new   JFrame("she");  
  Container   cp   =   f.getContentPane();  
  c=   new   Canvas();  
  c.setSize(cw,ch);  
  c.addKeyListener(this);  
  cp.add(c,   BorderLayout.CENTER);  
  f.addKeyListener(this);  
  f.pack();  
  f.setResizable(false);  
  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  f.setVisible(true);  
  begin();  
  }  
  public   void   keyPressed(KeyEvent   e)  
  {  
  int   KeyCode=e.getKeyCode();  
  if(snakeModel.running)  
  switch(KeyCode)  
  {  
  case   KeyEvent.VK_UP:  
  snakeModel.changeDirection(snakeModel.UP);  
  break;  
  case   KeyEvent.VK_DOWN:  
  snakeModel.changeDirection(snakeModel.DOWN);  
  break;  
  case   KeyEvent.VK_LEFT:  
  snakeModel.changeDirection(snakeModel.LEFT);  
  break;  
  case   KeyEvent.VK_RIGHT:  
  snakeModel.changeDirection(snakeModel.RIGHT);  
  break;  
  default:  
          }  
  if(KeyCode==KeyEvent.VK_R)  
  snakeModel.running=false;  
  begin();  
  }  
   
  public   void   keyReleased(KeyEvent   e)  
  {  
   
  }  
  public   void   keyTyped(KeyEvent   e)  
  {  
   
  }  
  void   repaint()  
  {  
  Graphics   g=c.getGraphics();  
  g.setColor(Color.WHITE);  
  g.fillRect(10,0,cw,ch);  
  g.setColor(Color.BLACK);  
  LinkedList   l=snakeModel.nodeArray;  
  Iterator   it=l.iterator();  
  while(it.hasNext())  
  {  
  Node   n=(Node)it.next();  
  drawnode(g,n);  
  }  
  g.setColor(Color.RED);  
  Node   n   =   snakeModel.food;  
  drawnode(g,n);  
  }  
  private   void   drawnode(Graphics   g,Node   n)  
  {  
  g.fillRect(n.x*nw,n.y*nh,nw-1,nh-1);  
  }  
  void   begin()  
  {  
  if   (snakeModel   ==   null   ||   !snakeModel.running)  
  {  
  snakeModel   =   new   SnakeModel(this,cw/nw,ch/nh);  
  (new   Thread(snakeModel)).start();  
  }  
  }  
  class   SnakeModel   implements   Runnable  
  {  
  EatSnake   es;  
  boolean[][]   matrix;  
  LinkedList   nodeArray   =   new   LinkedList();  
  Node   food;  
  int   maxX;  
  int   maxY;  
  int   direction   =   2;  
  int   timeInterval   =   200;  
  boolean   running   =   false;  
  public   static   final   int   UP   =   2;  
  public   static   final   int   DOWN   =   4;  
  public   static   final   int   LEFT   =   1;  
  public   static   final   int   RIGHT   =   3;  
  public   SnakeModel(EatSnake   es,int   maxX,int   maxY)  
  {  
          this.es   =   es;  
                  this.maxX   =   maxX;  
                  this.maxY   =   maxY;  
          matrix=   new   boolean[maxX][];  
                  for(int   i=0;i<maxX;++i)  
                  {  
              matrix[i]=new   boolean   [maxY];  
              Arrays.fill(matrix[i],false);  
   
                  }  
                  int   intarraylenth=maxX/2;  
                  for(int   i=0;i<intarraylenth;i++)  
                  {  
                  int     x=maxX/2+i;  
                  int     y=maxY/2;  
                  nodeArray.addLast(new   Node(x,y));  
                  matrix[x][y]=true;  
          }  
                  food   =   createfood();  
          matrix[food.x][food.y]   =   true;  
  }  
  public   void   changeDirection(int   newDirection)  
  {  
          if   (direction   %   2   !=   newDirection   %   2)  
          {  
              direction   =   newDirection;  
          }  
  }  
  public   boolean   moveOn()  
  {  
          Node   n   =   (Node)nodeArray.getFirst();  
          int   x   =   n.x;  
          int   y   =   n.y;  
   
          switch(direction)  
          {  
              case   UP:  
                  y--;  
                  break;  
              case   DOWN:  
                  y++;    
                  break;  
              case   LEFT:  
                  x--;  
                  break;  
              case   RIGHT:  
                  x++;  
                  break;  
                          }  
          if((0   <=   x   &&   x   <   maxX)   &&   (0   <=   y   &&   y   <   maxY))  
          {  
          if(matrix[x][y]=true)  
          {  
           
          if(x==food.x&&y==food.y)  
           
          {  
                  nodeArray.addFirst(food);  
                  food=createfood();  
                  matrix[food.x][food.y]=true;  
                  return   true;  
          }  
          else   return   false;  
                   
          }  
          else  
          {  
          nodeArray.addFirst(new   Node(x,y));  
          matrix[x][y]=true;  
          n=(Node)nodeArray.removeLast();  
          matrix[n.x][n.y]=true;  
          return   true;  
          }  
          }  
          else   return   false;  
          }  
  public   void   run()  
  {  
  running=true;  
  while   (running)  
  {  
              try{  
                  Thread.sleep(timeInterval);  
              }  
              catch(Exception   e)  
              {  
                  break;  
              }  
  }  
  if   (moveOn())  
  {  
                      es.repaint();  
          }  
          else  
          {  
                      JOptionPane.showMessageDialog(  
                              null,  
                              "you   failed",  
                              "Game   Over",  
                              JOptionPane.INFORMATION_MESSAGE);  
            }  
  running=false;  
   
  }  
  public   Node   createfood()  
  {  
  int   x=0;  
  int   y=0;  
  do  
  {Random   r=   new   Random();  
  x=r.nextInt(maxX);  
  y=r.nextInt(maxY);  
  return   new   Node(x,y);}  
  while(matrix[x][y]);  
  }  
   
        }  
  class   Node  
  {  
  int   x;  
  int   y;  
  Node(int   x,int   y)  
  {  
  this.x=x;  
  this.y=y;  
  }  
   
  }  
  public   static   void   main(String   args[])  
  {  
  EatSnake   es=new   EatSnake();  
  }  
   
  } 问题点数:100、回复次数:6Top

1 楼lbfhappy(千里冰封)回复于 2006-10-04 16:44:11 得分 0

你这个写得太复杂了,并且也没有计算好  
  import   java.awt.*;  
  import   java.awt.event.*;  
  import   javax.swing.*;  
  import   java.util.*;  
  import   static   java.lang.Math.*;//静态导入  
  /*  
  *   此类是贪吃蛇的简单实现方法  
  *   自己可以加入在开始时的设置,比如  
  *   选关,初始的蛇的长度等等  
  *   作者:千里冰封  
  *   日期:2006年6月25日15:47  
  */  
  public   class   Snake   extends   JPanel{  
  private   Direction   dir;//要走的方向  
  private   int   blockWidth=10;//块大小  
  private   int   blockSpace=2;//块之间的间隔  
  private   long   sleepTime;//重画的进间间隔  
  private   MySnake   my;  
  private   int   total;//代表蛇的长度  
  private   Rectangle   food;//代表蛇的食物  
  private   volatile   boolean   go;  
  private   int   round;//表示第几关  
  public   Snake(JFrame   jf){  
      initOther();  
      //为顶级窗口类JFrame添加事件处理函数  
      jf.addKeyListener(new   KeyAdapter(){  
        public   void   keyReleased(KeyEvent   ke){  
          int   code=ke.getKeyCode();  
          if(code==KeyEvent.VK_RIGHT){  
            if(dir!=Direction.WEST)  
              dir=Direction.EAST;  
          }  
             
          else   if(code==KeyEvent.VK_LEFT){  
            if(dir!=Direction.EAST)  
              dir=Direction.WEST;  
          }  
             
          else   if(code==KeyEvent.VK_UP){  
            if(dir!=Direction.SOUTH)  
              dir=Direction.NORTH;  
          }  
             
          else   if(code==KeyEvent.VK_DOWN){  
            if(dir!=Direction.NORTH)  
              dir=Direction.SOUTH;  
          }  
          else   if(code==KeyEvent.VK_ENTER){  
            if(!go)  
            initOther();  
          }  
        }  
      });  
      this.setBounds(300,300,400,400);  
      this.setVisible(true);  
  }  
  //随机生成一个食物的位置  
  private   void   makeFood(){  
      int   x=40+(int)(random()*30)*12;  
      int   y=10+(int)(random()*30)*12;  
      food=new   Rectangle(x,y,10,10);  
  }  
  //做一些初始化的工作  
  private   void   initOther(){  
      dir=Direction.EAST;  
      sleepTime=500;  
      my=new   MySnake();  
      makeFood();  
      total=3;  
      round=1;  
      new   Thread(new   Runnable(){  
        public   void   run(){  
          go=true;  
          while(go){  
            try{  
              Thread.sleep(sleepTime);  
              repaint();  
            }  
            catch(Exception   exe){  
              exe.printStackTrace();  
            }  
          }  
        }  
      }).start();  
  }  
  //处理多少关的函数  
  private   void   handleRound(){  
      if(total==6){  
        round=2;  
        sleepTime=300;  
      }  
      else   if(total==10){  
        round=3;  
        sleepTime=200;  
      }  
      else   if(total==15){  
        round=4;  
        sleepTime=100;  
      }  
      else   if(total==18){  
        round=5;  
        sleepTime=50;  
      }  
      else   if(total==20){  
        round=6;  
        sleepTime=20;  
      }  
      else   if(total>21){  
        round=7;  
        sleepTime=15;  
      }  
  }  
  //把自己的组件全部画出来  
  public   void   paintComponent(Graphics   g){  
        g.setColor(Color.PINK);  
        g.fillRect(0,0,this.getWidth(),this.getHeight());  
        g.setColor(Color.BLACK);  
        g.drawRect(40,10,358,360);  
        if(go){  
          my.move();  
          my.draw(g);  
          g.setFont(new   Font("黑体",Font.BOLD,20));  
          g.drawString("您的得分:"+(total*10)+"             第"+round+"关",40,400);  
        }  
        else{  
          g.setFont(new   Font("黑体",Font.BOLD,20));  
          g.drawString("游戏结束,按回车(ENTER)键重玩!",40,440);  
        }  
        g.setColor(Color.RED);  
        g.fillRect(food.x,food.y,food.width,food.height);  
  }  
  private   class   MySnake{  
      private   ArrayList<Rectangle>   list;  
      public   MySnake(){  
        list=new   ArrayList<Rectangle>();  
        list.add(new   Rectangle(160+24,130,10,10));  
        list.add(new   Rectangle(160+12,130,10,10));  
        list.add(new   Rectangle(160,130,10,10));  
      }  
      //蛇移动的方法  
      public   void   move(){  
        if(isDead()){  
          go=false;  
          return;  
        }  
        if(dir==Direction.EAST){  
          Rectangle   rec=list.get(0);  
          Rectangle   rec1=new   Rectangle(rec.x+(blockWidth+blockSpace),rec.y,rec.width,rec.height);  
          list.add(0,rec1);  
        }  
        else   if(dir==Direction.WEST){  
          Rectangle   rec=list.get(0);  
          Rectangle   rec1=new   Rectangle(rec.x-(blockWidth+blockSpace),rec.y,rec.width,rec.height);  
          list.add(0,rec1);  
        }  
        else   if(dir==Direction.NORTH){  
          Rectangle   rec=list.get(0);  
          Rectangle   rec1=new   Rectangle(rec.x,rec.y-(blockWidth+blockSpace),rec.width,rec.height);  
          list.add(0,rec1);  
        }  
        else   if(dir==Direction.SOUTH){  
          Rectangle   rec=list.get(0);  
          Rectangle   rec1=new   Rectangle(rec.x,rec.y+(blockWidth+blockSpace),rec.width,rec.height);  
          list.add(0,rec1);  
        }  
        if(isEat()){  
          handleRound();  
          makeFood();  
        }  
        else{  
          list.remove(list.size()-1);  
        }  
         
      }  
      //判断是否吃到了食物  
      private   boolean   isEat(){  
        if(list.get(0).contains(food)){  
          total++;  
          return   true;  
        }  
        else    
          return   false;  
      }  
      //判断是否死了,如果碰壁或者自己吃到自己都算死了  
      private   boolean   isDead(){  
        Rectangle   temp=list.get(0);  
        if(dir==Direction.EAST){  
          if(temp.x==388)  
            return   true;  
          else{  
            Rectangle   comp=new   Rectangle(temp.x+12,temp.y,10,10);  
            for(Rectangle   rec:list){  
              if(rec.contains(comp))  
                return   true;  
            }  
          }  
            return   false;  
        }  
        else   if(dir==Direction.WEST){  
          if(temp.x==40)  
            return   true;  
          else   {  
            Rectangle   comp=new   Rectangle(temp.x-12,temp.y,10,10);  
            for(Rectangle   rec:list){  
              if(rec.contains(comp))  
                return   true;  
            }  
          }  
            return   false;  
        }  
        else   if(dir==Direction.NORTH){  
          if(temp.y==10)  
            return   true;  
          else{  
            Rectangle   comp=new   Rectangle(temp.x,temp.y-12,10,10);  
            for(Rectangle   rec:list){  
              if(rec.contains(comp))  
                return   true;  
            }  
          }  
            return   false;  
        }  
        else   if(dir==Direction.SOUTH){  
          if(temp.y==358)  
            return   true;  
          else{  
            Rectangle   comp=new   Rectangle(temp.x,temp.y+12,10,10);  
            for(Rectangle   rec:list){  
              if(rec.contains(comp))  
                return   true;  
            }  
          }  
            return   false;  
        }  
        else{  
          return   false;  
        }  
      }  
      //把自己画出来  
      public   void   draw(Graphics   g){  
        for(Rectangle   rec:list){  
          g.fillRect(rec.x,rec.y,rec.width,rec.height);  
        }  
      }  
       
  }  
  public   static   void   main(String   arsg[]){  
      JFrame   jf=new   JFrame("贪吃蛇");  
      Snake   s=new   Snake(jf);  
      jf.getContentPane().add(s,BorderLayout.CENTER);  
      jf.setBounds(300,300,500,500);  
      jf.setVisible(true);  
      jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  }  
  }  
  //定义一个枚举,在此也可以用接口或者常量值代替  
  enum   Direction{  
  EAST,  
  SOUTH,  
  WEST,  
  NORTH;  
  }Top

2 楼lbfhappy(千里冰封)回复于 2006-10-04 16:44:28 得分 0

你可以运行一下试试看Top

3 楼lining771()回复于 2006-10-04 19:04:09 得分 0

你这个我会仔细看看,不过这是我辛苦写的算法呀,不知道哪错了,不白写了嘛,我想知道哪不对,跪求了,剩这100都给你们了Top

4 楼yybjroam05(月蒙影)回复于 2006-10-04 22:13:52 得分 0

帮你顶一下!慢慢分析!如果有地方是你不敢肯定正确的地方容易出错!Top

5 楼gtlang78()回复于 2006-10-05 16:28:25 得分 0

run()   方法里的循环  
   
  while   (running)  
  {  
        ...  
  }  
   
  没有把   moveOn()   和   es.repaint()包进来.Top

6 楼lining771()回复于 2006-10-05 21:35:03 得分 0

我已经改了,可是改完之后怎么一运行就出现你已经失败的的对话框呢,请高手再给指导一下行么,因为我没分了,所以一起把这个问题问完吧Top

相关问题

关键词

得分解答快速导航

  • 帖主:lining771

相关链接

  • CSDN Java频道
  • Java类图书
  • Java类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo