⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gameframe.java

📁 该程序源码是我大二暑假时用Java语言写的一个贪食蛇游戏
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        pp.setProperty(inputName,String.valueOf(score));
        pp.store(new FileOutputStream("miracle.ini"),null);
      }
      while(enumt.hasMoreElements())
      {
        String name = (String)enumt.nextElement();
        int score_temp  = Integer.parseInt(pp.getProperty(name));
        //pp.remove(name);
        if(score > score_temp)
        {
          String inputName = JOptionPane.showInputDialog("Please Input your Name");
          pp.remove(name);
          pp.setProperty(inputName,String.valueOf(score));
          pp.store(new FileOutputStream("miracle.ini"),null);
        }
        //System.out.println(name+"="+score_temp);
      }
    }catch(Exception e)
    {
      System.out.println("Cannot find the file");
      System.out.println(e.getMessage());
    }
  }

  public void changePausedState()               // 改变暂停状态
  {
    paused = !paused;
  }

  public void stopGame()
  {
    this.snakeTimer.stop();
    Graphics2D g = (Graphics2D)GameFrame.this.getGraphics();
    g.setColor(Global.COLOR_BACK);
    super.paint(g);
    this.configMenu.setVisible(true);
  }

  public void resetGame()                        // 重新开始新的游戏。。一些初始化的工作
  {
    Graphics2D g = (Graphics2D)GameFrame.this.getGraphics();
    g.setColor(Global.COLOR_BACK);
    super.paint(g);
    this.mainSnake = new Snake();
    this.createBean(bean);
    this.eatedBean.clear();
    this.paused = false;

    mainSnake.drawSnake(g,singleWidthX,singleHeightY,cooPos);
    this.snakeTimer.start();
    int temp = Integer.parseInt(this.delay.get(this.Level).toString());
    System.out.println(temp);
    this.snakeTimer.setDelay(temp);
    this.direction = 2;
    this.score = 0;
  }

  public void drawBackGround(Graphics g)                       //画出背景
  {
    g.setColor(Global.COLOR_BACK);
    g.fillRect(0,0,GameFrame.this.getWidth(),(GameFrame.this.getHeight())-1);

  }

  public void moveSnake(int direction)                    // 移动蛇。。
  {
    if(mainSnake.checkBeanIn(this.bean))
    {
      this.score += 100;
      /*
      if(score == 100)
      {
        this.Level = 1;
        //this.resetGame();

        for(int i=30; i<55;i++)
        {
          this.wallBean.add(new Point(i,19));
          this.wallBean.add(new Point(i,41));
        }

        for (int j=21;j<40;j++)
        {
          this.wallBean.add(new Point(28,j));
          this.wallBean.add(new Point(56,j));
        }

      }else if(score == 300)
      {
        this.Level = 2;
        this.wallBean.clear();

        for (int i=20;i<30;i++)
        {
          this.wallBean.add(new Point(i,30));
        }

      }else if(score == 400)
      {
        this.Level = 3;
      }else if(score == 500)
      {
        this.Level = 4;
      }else if(score == 600)
      {
        this.Level = 5;
      }*/
      this.scoreLabel.setText("Current Score:"+this.score);
      this.eatedBean.add(new Point(this.bean));
      this.createBean(this.bean);
    }
    mainSnake.changeDirection((Point)mainSnake.getLast(),direction);
    Point temp = (Point)mainSnake.getFirst();
    if(eatedBean.size() != 0)
    {
      if(eatedBean.getFirst().equals(temp))
      {
        eatedBean.remove(0);
      }
      else
      {
        mainSnake.clearEndSnakePiece(getGraphics(),temp.x,temp.y,singleWidthX,singleHeightY,cooPos);
        mainSnake.removeTail();
      }
    }else
    {
      mainSnake.clearEndSnakePiece(getGraphics(),temp.x,temp.y,singleWidthX,singleHeightY,cooPos);
      mainSnake.removeTail();
    }
  }

  //private void drawWall(Graphics g,int level)
  /*
  private void drawWall(Graphics g)
  {
    g.setColor(ColorGroup.COLOR_WALL);
    iterator = wallBean.iterator();
    while(iterator.hasNext())
    {
      Point tempPoint = (Point)iterator.next();
      //System.out.println(tempPoint.x+":"+tempPoint.y);
      this.drawPiece(g,tempPoint.x,tempPoint.y);



      if(level ==0)
      {


      }else if(level == 1)
      {
        for (int i=40;i<50;i++)
        {
          this.drawPiece(g,i,6);
        }
        g.setColor(ColorGroup.COLOR_WALL);
        int x = 20;
        for (int j= 30; j<60;j++)
        {
          this.drawPiece(g,j,x);
        }
      }else if(level == 2)
      {

      }else if(level == 3)
      {

      }else if(level == 4)
      {

      }else if(level == 5)
      {

      }
    }
  }*/


  private void drawBeanAndEBean(Graphics g)
  {
    g.setColor(Global.COLOR_BEAN);
    System.out.println("test---"+this.bean.x+":"+this.bean.y);
    this.drawPiece(g,this.bean.x,this.bean.y);
    g.setColor(Global.COLOR_EATEDBEAN);
    iterator = eatedBean.iterator();
    while(iterator.hasNext())
    {
      Point tempPoint = (Point)iterator.next();
      this.drawPiece(g,tempPoint.x,tempPoint.y);
    }
  }
  public void drawPiece(Graphics g,int x,int y)
  {
    g.fillRoundRect(this.singleWidthX*x+1,
        this.singleWidthX*y+1,
        this.singleWidthX-2,
        this.singleHeightY-2,
        this.cooPos,
        this.cooPos);
  }
  public void createBean(Point temp)  // 随机生成Bean算法
                                      // 当随机生成的Bean在 当前的蛇身上的,
                                      // 重新生成一个Bean,

                                      // 这个算法写得不怎么精炼
                                      // 不过已经经过测试。。。正确。。
  {
    boolean flag = true;
    for(;flag == true;)
    {
      temp.x = (int)(Math.random()*this.horizontalGrid);
      temp.y = (int)(Math.random()*this.verticalGrid);
      iterator = mainSnake.iterator();
      while(iterator.hasNext())
      {
        if(iterator.next().equals(new Point(temp.x,temp.y)))
        {
         flag = false;
        }
      }
      if(flag = false)
      {
        flag = true;
      }
      else
      {
        flag = false;
      }
    }
    System.out.println("Bean:"+temp.x+"--"+temp.y);
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -