快速业务通道

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

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-06-21
;  }  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);  } } } //蛇对象类 package greedSnake; import java.util.Random; import java.util.Vector; import java.util.Enumeration; import com.nttdocomo.ui.Graphics; public class Snake { public final static int UP = 1; public final static int DOWN  = 3; public final static int LEFT  = 2; public final static int RIGHT = 4; private int direction; //  private Grid          grid; private Node node; private Vector snakeData = null; private static Random rnd= new Random(); /** Private internal constructor. */ public Snake() {  snakeData = new Vector();  Node node = new Node(4,0);  snakeData.addElement(node);  node = new Node(3,0);  snakeData.addElement(node);  node = new Node(2,0);  snakeData.addElement(node);  node = new Node(1,0);  snakeData.addElement(node);  node = new Node(0,0);  snakeData.addElement(node);  setDirection(Snake.DOWN); } public int getDirection() {  return direction; } public void setDirection(int dir) {  if(direction%2!=dir%2){ direction = dir;  } } public Node getHead() {  return (Node) snakeData.elementAt(0); } public Node getNextHead() {  Node node = (Node) snakeData.elementAt(0);  node = new Node(node.x,node.y);  switch(this.direction) { case Snake.UP:   node.y--;   break; case Snake.DOWN:    node.y++;    break;    case Snake.LEFT:    node.x--;    break;    case Snake.RIGHT:    node.x++;    break;   }   return node;   }   public void move() {  Node node= (Node)snakeData.elementAt(0);  Node newNode = this.getNextHead();   for (int i =snakeData.size()-1 ; i>0; i--) { Node node1 = (Node) snakeData.elementAt(i); Node node2 = (Node) snakeData.elementAt(i-1);    node1.x = node2.x;    node1.y = node2.y;   }   node.x = newNode.x;   node.y = newNode.y;   } public void addNode() {  Node node = (Node) snakeData.lastElement();  Node newNode = new Node(node.x,node.y);  snakeData.addElement(newNode); }   public void paint(Graphics g) {   int SnakeColor= 0xff0000;   int gn = GreedSnake.NODE_SIZE;  Enumeration e = snakeData.elements() ;  while(e.hasMoreElements()){ Node node = (Node)e.nextElement() ; g.setColor(SnakeColor); g.fillRect(node.x * gn, node.y * gn, gn, gn);   }   } public void eatFood(Food food) {  this.addNode(); } public boolean ptInSnake(Node node) {  for (int i = 1; i<snakeData.size()

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站: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号