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

📄 snakemodel.java

📁 本人经过一周废寝忘食编写的贪食蛇游戏改进版
💻 JAVA
字号:
package gameproject;

import java.util.*;
import javax.swing.*;
import java.awt.*;
import com.borland.jbcl.layout.XYConstraints;

public class SnakeModel implements Runnable {
  GreedSnakeFrame gs;
  boolean[][] matrix;
  LinkedList nodeArray=new LinkedList();
  Node food;
  int countWrong=0;    //when the shake hit wrong once,also can continue
  int maxX;          //the number of nodes
  int maxY;
  int direction=3;    //the default direction:right
  boolean running=false;   //is it be paused
  boolean paused=false;
  int score=0;
  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 boolean levelUpgrade=false;    //indicate if the level is upgrade
  public String level;
  public boolean recordUpgrade=false;    //indicate if the record is upgrade
  public int record;


  public SnakeModel(GreedSnakeFrame gs,int maxX,int maxY) {
    this.gs=gs;
    this.maxX=maxX;
    this.maxY=maxY;
    //initial matrix
    matrix = new boolean[maxX][];
    for(int i=0; i<maxX; ++i){
      matrix[i] = new boolean[maxY];
      Arrays.fill(matrix[i],false);
    }
    // initial the snake
    int initArrayLength = maxX/2-1;
    for(int i = 0; i < initArrayLength; ++i){
      int x = initArrayLength-i-1;  //x is from 0
      int y = maxY-1;               //y is from 0
      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]){
        //success to eat food
        if(x == food.x && y == food.y){
          food.orient=direction;
          nodeArray.addFirst(food);
          score+=Integer.valueOf(gs.speed.getText()).intValue();
          if(gs.jCheckBoxAudioVoice.isSelected()){
          Sound.playEat();
          }
          food = createFood();
          matrix[food.x][food.y] = true;
          countWrong=0;
          return true;
        }
        //hit itself
        else{
          countWrong++;
          if(countWrong>=2) return false;
          else return true;
        }
      }
      //not eat the food
      else{
        nodeArray.addFirst(new Node(x,y,direction));
        matrix[x][y] = true;
        n = (Node)nodeArray.removeLast();
        matrix[n.x][n.y] = false;
        return true;
      }
    }
    //hit the wall
    else{
          countWrong++;
          if(countWrong>=2) return false;
          else return true;
        }
  }


  public Node createFood(){
    int x = 0;
    int y = 0;
    do{
      Random r = new Random();
      x = r.nextInt(maxX);  //[0,maxX)
      y = r.nextInt(maxY);
    }while(matrix[x][y]);
    return new Node(x,y);
  }

  public void run(){
     running = true;
     while (running){
       try{
         Thread.sleep(gs.timeInterval);
       }
       catch(Exception e){
         break;
       }
       if(!paused){
         if (moveOn()){
           gs.repaint();
         }
         else{
           //update
           if(Integer.valueOf(gs.score.getText()).intValue()>
              Integer.valueOf(gs.record.getText()).intValue()){
             recordUpgrade=true;
             record=Integer.valueOf(gs.score.getText()).intValue();
             if(record>1400) level="皇帝";
                 else if (record>1300) level="王爷";
                   else if (record>1200) level="宰相";
                     else if (record>1100) level="一品大学士";
                       else if (record>1000) level="二品巡抚";
                         else if (record>900) level="三品按察使";
                           else if (record>800) level="四品知府";
                             else if (record>700) level="五品知州";
                               else if (record>600) level="六品通判";
                                 else if (record>500) level="七品知县";
                                   else if (record>400) level="八品县丞";
                                     else if (record>300) level="九品芝麻官";
                                       else if (record>200) level="举人";
                                         else if (record>100) level="秀才";
                                             else level="庶民";
             gs.record.setText(gs.score.getText());
             if(!gs.level.getText().equals(level)){
               levelUpgrade = true;
             }
             gs.level.setText(level);
             //write to file
             String[] msg=new String[2];
             msg[0]=level;
             msg[1]=gs.score.getText();
             try{
               Message.writeMsg(msg);
             }catch(Exception e){
               System.err.println("file error!");
             }
           }
           if(gs.jCheckBoxAudioVoice.isSelected()){
           Sound.playOver();
           }



     GreedSnakeFrame_GameOver dlg = new GreedSnakeFrame_GameOver(gs);
     Dimension dlgSize = dlg.getPreferredSize();
     Dimension frmSize = gs.getSize();
     Point loc = gs.getLocation();
     dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
     dlg.setModal(true);
     dlg.pack();
     dlg.show();
     //draw the picture
     gs.contentPane.add(gs.canvas,  new XYConstraints(0, 60, 405, 225));
     break;
         }
       }
     }
     running = false;
   }

   public void changePauseState(){
       paused = !paused;
       if(gs.jMenuGamePause.getText()=="暂停"){
      gs.jMenuGamePause.setText("继续");
    }
    else{
      gs.jMenuGamePause.setText("暂停");
    }

       if(gs.jCheckBoxAudioVoice.isSelected()){
         Sound.playPause();
      }
     }

}

⌨️ 快捷键说明

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