快速业务通道

DOJA开发贪吃蛇的代码 - 编程入门网

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-06-21
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)

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号