J2me中贪吃蛇怎样实现吃了食物加速移动?
是这样我想实现的是蛇吃了特定的一种食物那就可以加速移动5秒,吃了特定的食物蛇酒可以缓慢的移动5秒?同时非常感谢你的回答,如果你还知道请你帮个忙,急用呀...
是这样我想实现的是蛇吃了特定的一种食物那就可以加速移动5秒,吃了特定的食物蛇酒可以缓慢的移动5秒?同时非常感谢你的回答,如果你还知道请你帮个忙,急用呀
展开
2个回答
展开全部
import java.util.Random;
import java.util.Vector;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
public class SnakeCanvas extends Canvas implements Runnable {
/**
* java.util.Vector类
* 实现List接口,Vector类允许内部有重复的元素存在,且内部元素有特定的顺序。
* Vector 类 提 供 了 实 现 可 增 长 数 组 的 功 能
*/
//实现蛇身的增长,存储蛇身的point
Vector snake = new Vector();
//每一块snake单元的宽度
private static final int tileSize = 5;
//snake的运动方向
private int direction;
//存储Apple的point
private Point apple;
//线程休眠时间,控制snake的速度
private int sleeptime = 500;
//循环标志
boolean tab = true;
/**
* SankCanvas构造函数
* 作用:绘制初始化snake
*/
public SnakeCanvas(){
super();
snake.addElement(new Point(2,5));
snake.addElement(new Point(3,5));
snake.addElement(new Point(4,5));
snake.addElement(new Point(5,5));
snake.addElement(new Point(6,5));
snake.addElement(new Point(6,6));
direction = Canvas.RIGHT;
//调用方法绘制apple
this.apple = new Point().createPoint();
//启用线程
new Thread(this).start();
}
protected void keyPressed(int keyCode){
super.keyPressed(keyCode);
int action = this.getGameAction(keyCode);
//防止snake倒退
switch(direction){
case Canvas.UP:
action = (action == Canvas.DOWN)?Canvas.UP:action;
break;
case Canvas.DOWN:
action = (action == Canvas.UP)?Canvas.DOWN:action;
break;
case Canvas.LEFT:
action = (action == Canvas.RIGHT)?Canvas.LEFT:action;
break;
case Canvas.RIGHT:
action = (action == Canvas.LEFT)?Canvas.RIGHT:action;
break;
}
switch(action){
case Canvas.UP:
direction = Canvas.UP;
break;
case Canvas.DOWN:
direction = Canvas.DOWN;
break;
case Canvas.LEFT:
direction = Canvas.LEFT;
break;
case Canvas.RIGHT:
direction = Canvas.RIGHT;
break;
}
}
public void run() {
while(tab){
Point head = (Point)snake.elementAt(snake.size()-1);
//把第二块的坐标改为第一块的坐标
for(int i = 0;i < snake.size()-1;i++){
((Point)snake.elementAt(i)).setPoint((Point)snake.elementAt(i+1));
}
switch(direction){
case Canvas.UP:
head.setY(head.getY()-1);
break;
case Canvas.DOWN:
head.setY(head.getY()+1);
break;
case Canvas.LEFT:
head.setX(head.getX()-1);
break;
case Canvas.RIGHT:
head.setX(head.getX()+1);
break;
}
if(head.getX() < 0){
System.out.println("蛇头碰壁,游戏结束");
tab = false;
}else if(head.getY() < 0){
System.out.println("蛇头碰壁,游戏结束");
tab = false;
}else if(head.getX() > this.getWidth()){
System.out.println("蛇头碰壁,游戏结束");
tab = false;
}else if(head.getY() > this.getHeight()){
System.out.println("蛇头碰壁,游戏结束");
tab = false;
}
for(int i = 0;i < snake.size()-1;i++){
if(head.equals((Point)snake.elementAt(i))){
System.out.println("触碰蛇身,游戏结束");
tab = false;
}
}
Point tail=(Point) snake.elementAt(0);
Point retail = null;
if(head.equals(apple)){
switch(direction){
case Canvas.UP:
retail = new Point(tail.getX(),tail.getY()-1);
break;
case Canvas.DOWN:
retail = new Point(tail.getX(),tail.getY()+1);
break;
case Canvas.LEFT:
retail = new Point(tail.getX()-1,tail.getY());
break;
case Canvas.RIGHT:
retail = new Point (tail.getX()+1,tail.getY());
break;
}
snake.insertElementAt(retail,0);
sleeptime *= 0.99;
this.apple = new Point().createPoint();
}
this.repaint();
try {
Thread.sleep(sleeptime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void paint(Graphics g) {
g.setColor(255,255,255);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(0, 0, 0);
//绘制snake
for(int i = 0;i < snake.size();i++){
Point point = (Point)snake.elementAt(i);
g.fillRect(point.getX()*tileSize, point.getY()*tileSize, tileSize, tileSize);
}
//绘制apple
g.fillRect(apple.getX()*tileSize, apple.getY()*tileSize, tileSize, tileSize);
}
/**
* Point类
*
* 作用:方便坐标点的设置
*
* @author Administrator
*
*/
class Point{
private int x;
private int y;
public Point(){
super();
}
public Point(int x,int y){
super();
this.x = x;
this.y = y;
}
public Point(Point point){
super();
this.x = point.getX();
this.y = point.getY();
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public void setPoint(Point point){
this.x = point.getX();
this.y = point.getY();
}
/**
* createPoint()
* 作用:创造一个新的Point用于随机生成apple
* @return Point
*/
public Point createPoint(){
/**
* java.util.Random类
* 创建一个新的随机数生成器。
* 它是以当前时间的毫秒数,为种子。
* 可以通过System.currentTimeMillis() 来获得当前时间的毫秒数
*/
Random random = new Random();
/**
* java.util.Random.nextInt(int n);
* 返回一个伪随机数。
* 它是从此随机数生成器的序列中取出在 0(包括)和指定值(不包括)之间均匀分布的 int值。
*/
return new Point(random.nextInt(48),random.nextInt(60));
}
/**
* equeals()
* 作用:判断两个Point对象是否相等
* @param point
* @return boolean
*/
public boolean equals(Point point){
return this.x == point.getX() && this.y == point.getY();
}
}
}
import java.util.Vector;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
public class SnakeCanvas extends Canvas implements Runnable {
/**
* java.util.Vector类
* 实现List接口,Vector类允许内部有重复的元素存在,且内部元素有特定的顺序。
* Vector 类 提 供 了 实 现 可 增 长 数 组 的 功 能
*/
//实现蛇身的增长,存储蛇身的point
Vector snake = new Vector();
//每一块snake单元的宽度
private static final int tileSize = 5;
//snake的运动方向
private int direction;
//存储Apple的point
private Point apple;
//线程休眠时间,控制snake的速度
private int sleeptime = 500;
//循环标志
boolean tab = true;
/**
* SankCanvas构造函数
* 作用:绘制初始化snake
*/
public SnakeCanvas(){
super();
snake.addElement(new Point(2,5));
snake.addElement(new Point(3,5));
snake.addElement(new Point(4,5));
snake.addElement(new Point(5,5));
snake.addElement(new Point(6,5));
snake.addElement(new Point(6,6));
direction = Canvas.RIGHT;
//调用方法绘制apple
this.apple = new Point().createPoint();
//启用线程
new Thread(this).start();
}
protected void keyPressed(int keyCode){
super.keyPressed(keyCode);
int action = this.getGameAction(keyCode);
//防止snake倒退
switch(direction){
case Canvas.UP:
action = (action == Canvas.DOWN)?Canvas.UP:action;
break;
case Canvas.DOWN:
action = (action == Canvas.UP)?Canvas.DOWN:action;
break;
case Canvas.LEFT:
action = (action == Canvas.RIGHT)?Canvas.LEFT:action;
break;
case Canvas.RIGHT:
action = (action == Canvas.LEFT)?Canvas.RIGHT:action;
break;
}
switch(action){
case Canvas.UP:
direction = Canvas.UP;
break;
case Canvas.DOWN:
direction = Canvas.DOWN;
break;
case Canvas.LEFT:
direction = Canvas.LEFT;
break;
case Canvas.RIGHT:
direction = Canvas.RIGHT;
break;
}
}
public void run() {
while(tab){
Point head = (Point)snake.elementAt(snake.size()-1);
//把第二块的坐标改为第一块的坐标
for(int i = 0;i < snake.size()-1;i++){
((Point)snake.elementAt(i)).setPoint((Point)snake.elementAt(i+1));
}
switch(direction){
case Canvas.UP:
head.setY(head.getY()-1);
break;
case Canvas.DOWN:
head.setY(head.getY()+1);
break;
case Canvas.LEFT:
head.setX(head.getX()-1);
break;
case Canvas.RIGHT:
head.setX(head.getX()+1);
break;
}
if(head.getX() < 0){
System.out.println("蛇头碰壁,游戏结束");
tab = false;
}else if(head.getY() < 0){
System.out.println("蛇头碰壁,游戏结束");
tab = false;
}else if(head.getX() > this.getWidth()){
System.out.println("蛇头碰壁,游戏结束");
tab = false;
}else if(head.getY() > this.getHeight()){
System.out.println("蛇头碰壁,游戏结束");
tab = false;
}
for(int i = 0;i < snake.size()-1;i++){
if(head.equals((Point)snake.elementAt(i))){
System.out.println("触碰蛇身,游戏结束");
tab = false;
}
}
Point tail=(Point) snake.elementAt(0);
Point retail = null;
if(head.equals(apple)){
switch(direction){
case Canvas.UP:
retail = new Point(tail.getX(),tail.getY()-1);
break;
case Canvas.DOWN:
retail = new Point(tail.getX(),tail.getY()+1);
break;
case Canvas.LEFT:
retail = new Point(tail.getX()-1,tail.getY());
break;
case Canvas.RIGHT:
retail = new Point (tail.getX()+1,tail.getY());
break;
}
snake.insertElementAt(retail,0);
sleeptime *= 0.99;
this.apple = new Point().createPoint();
}
this.repaint();
try {
Thread.sleep(sleeptime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void paint(Graphics g) {
g.setColor(255,255,255);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(0, 0, 0);
//绘制snake
for(int i = 0;i < snake.size();i++){
Point point = (Point)snake.elementAt(i);
g.fillRect(point.getX()*tileSize, point.getY()*tileSize, tileSize, tileSize);
}
//绘制apple
g.fillRect(apple.getX()*tileSize, apple.getY()*tileSize, tileSize, tileSize);
}
/**
* Point类
*
* 作用:方便坐标点的设置
*
* @author Administrator
*
*/
class Point{
private int x;
private int y;
public Point(){
super();
}
public Point(int x,int y){
super();
this.x = x;
this.y = y;
}
public Point(Point point){
super();
this.x = point.getX();
this.y = point.getY();
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public void setPoint(Point point){
this.x = point.getX();
this.y = point.getY();
}
/**
* createPoint()
* 作用:创造一个新的Point用于随机生成apple
* @return Point
*/
public Point createPoint(){
/**
* java.util.Random类
* 创建一个新的随机数生成器。
* 它是以当前时间的毫秒数,为种子。
* 可以通过System.currentTimeMillis() 来获得当前时间的毫秒数
*/
Random random = new Random();
/**
* java.util.Random.nextInt(int n);
* 返回一个伪随机数。
* 它是从此随机数生成器的序列中取出在 0(包括)和指定值(不包括)之间均匀分布的 int值。
*/
return new Point(random.nextInt(48),random.nextInt(60));
}
/**
* equeals()
* 作用:判断两个Point对象是否相等
* @param point
* @return boolean
*/
public boolean equals(Point point){
return this.x == point.getX() && this.y == point.getY();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |