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

📄 grid.java

📁 doja平台下的贪吃蛇游戏
💻 JAVA
字号:
package greedSnake;



import com.nttdocomo.ui.Canvas;
import com.nttdocomo.ui.Display;
import com.nttdocomo.ui.Font;
import com.nttdocomo.ui.Frame;
import com.nttdocomo.ui.Graphics;



//---------------------
/**
 * This class represents the grid box that catches
 * the pieces dropping from above. If a dropped piece
 * fills up one or more horizontal lines in the grid,
 * these lines will be removed from it.
 */
public class Grid extends Canvas {
 private int        width;
 private int        height;
 private GreedSnake    listener;
 private Snake           snake;
 private Food   food;
 private String     pad = "0000";
 private Font       score_font;
 private Font       game_over_font;
 private boolean    blDisplay;
 private boolean    game_over;
 static final int   SOFT_LEFT  = Frame.SOFT_KEY_1;
 static final int   SOFT_RIGHT = Frame.SOFT_KEY_2;
 
 /**
  * Create a new instance of this class.
  * @param witdth the width (in tiles) of this grid
  * @param height the height (in tiles) of this grid
  * @param score the score object to keep track of the score
  * @param listener a reference to the owning tetris object
  */
 public Grid(int width, int height, GreedSnake listener) {



  this.width    = width;
  this.height   = height;
  this.listener = listener;
  reset();
  setSoftLabel(SOFT_LEFT, "New");
  setSoftLabel(SOFT_RIGHT, "Quit");
  score_font    = Font.getFont(Font.FACE_MONOSPACE | Font.SIZE_SMALL);
  game_over_font = Font.getFont(Font.FACE_PROPORTIONAL | Font.SIZE_LARGE | Font.STYLE_BOLD);
 }




 /** Remove all pieces from this instance */
 private void reset() {
  
  synchronized (this) {
   food = null;
   snake= null;
  }
  game_over = false;
  blDisplay = false;
 }
 
 public void setGameOver() {
  snake = null;
  food = null;
  game_over = true;
 }



 /** Get the width (in tiles) of this instance. */
 public int getGridWidth() {
  return width;
 }



 /** Get the height (in tiles) of this instance. */
 public int getGridHeight() {
  return height;
 }
 
 /** Add the specified piece to this instance. */
 public Food addFood() {  
  blDisplay= false;
  int gs = GreedSnake.GRID_WIDTH;  
  if(snake == null){   
   this.food =Food.createRandomFood(gs,gs);   
  }else{
   for(;;){    
    this.food =Food.createRandomFood(gs,gs);    
    Node newNode = new Node(food.getX(),food.getY());
    if(snake.ptInSnake(newNode)){
     this.food = null;  
     continue;
    }else{
     break; 
    }
   }     
  }  
  return this.food; 
 }
 
 public void addSnake(Snake snake) { 
  
  this.snake=snake;
  Node head=(Node)snake.getHead();
 }
 /**
  * Returns <code>true</code> if the specified
  * piece overlaps with any tile in this instance,
  * <code>false</code> otherwise.
  */
 public boolean overlapsWith(Node node) {
  if ((node.x<0) || (node.x>getGridWidth()-1) 
    || (node.y<0) || (node.y>getGridHeight()-1)) {
          
   return true;
  }
  return false;  
 }
 
 public void reduceFood(Food food) {
  blDisplay = true;
 }
  
 private String format(int n) {



  String raw = "" + n;



  return pad.substring(0, 4 - raw.length()) + raw;
 }



 /** Paint this instance onto the specified graphics context. */
 public synchronized void paint(Graphics g) {
  g.lock();
  g.setColor(Graphics.getColorOfName(Graphics.BLACK));
  g.fillRect(0, 0, getWidth(), getHeight());
  g.setColor(Graphics.getColorOfName(Graphics.WHITE));
  g.drawLine(0, 0, 0, getHeight());
  g.drawLine(201, 0, 201, getHeight());
  g.drawLine(0, getHeight(), 201, getHeight());



  if ((food != null)&& (!blDisplay)) {
   food.paint(g);
  }
  
  if (snake != null) {  
   snake.paint(g);      
  }
   
  g.setColor(Graphics.getColorOfName(Graphics.SILVER));
  g.setFont(score_font); 
  g.drawString("LEVEL", 203, 40); 
  g.drawString(String.valueOf(listener.getLevel()), 220, 70);
  g.drawString("SCORE", 202, 100);
  g.drawString(format(listener.getScore()), 208, 130);
  
  if (game_over) { 
   g.setColor(0x00ffff);
   g.setFont(game_over_font);
   g.drawString("GAME", 70, 110);
   g.drawString("OVER", 70, 140);
  } 
  g.unlock(true);
 }



 /** Process key events. */
 public void processEvent(int type, int param) {



  if (type == Display.KEY_PRESSED_EVENT) {
   listener.keyPressed(param);
  }
 }
}

⌨️ 快捷键说明

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