Java me j2me,为简单的蛇游戏生成食物的随机位置
这是我的蛇游戏代码,我需要做的是为蛇食生成位置,一旦蛇接触到蛇食,它将重新生成食物的位置~请,我真的需要你们的帮助!!!请看一看Java me j2me,为简单的蛇游戏生成食物的随机位置,java-me,Java Me,这是我的蛇游戏代码,我需要做的是为蛇食生成位置,一旦蛇接触到蛇食,它将重新生成食物的位置~请,我真的需要你们的帮助!!!请看一看 package snake; import java.util.Random; import java.util.Timer; import java.util.TimerTask; import javax.microedition.lcdui.Canvas; import javax.microedition.lcdui.Graphics; public c
package snake;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
public class SnakeCanvas extends Canvas{
private int currentX =getWidth()/2;
private int currentY =getHeight()/2;
private boolean condition = false;
private boolean position = false;
Random rand = new Random();
int randomX=rand.nextInt(180) + 20;
int randomY =rand.nextInt(250) + 20;
private final Midlet midlet;
private int score = 0;
Timer t;
public SnakeCanvas(Midlet midlet)
{
this.midlet = midlet;
t = new Timer();
t.schedule(new TimerTask() {
public void run() {
if((condition==false)&&(position==false))
{
currentY -= 5 ;
}
if((condition==false)&&(position==true))
{
currentY += 5 ;
}
if((condition==true)&&(position==false))
{
currentX -= 5 ;
}
if((condition==true)&&(position==true))
{
currentX += 5 ;
}
}
}, 50, 50);
}
public void paint(Graphics g) {
boolean touch=false;
//drawing Background
g.setColor(0x000000);
g.fillRect(0, 0, getWidth(), getHeight());
//drawing snake
g.setColor(0xffffff);
g.fillRoundRect(currentX, currentY, 20, 20, 5, 5);
//snake food
g.setColor(234,41,42);
g.fillRect(randomX,randomY,10,10);
if((currentX-9<=randomX+4&¤tX+9>=randomX-4)&&(currentY-9<=randomY+4&¤tY+9>=randomY-4)){
addScore();
}
//drawing block
g.setColor(82,133,190);
g.fillRect(0, 0, getWidth(), 5);
g.fillRect(0, getHeight()-5, getWidth(), 5);
g.fillRect(0, 0, 5, getHeight());
g.fillRect(getWidth()-5, 0, 5, getHeight());
if((currentX>=getWidth()-10)||(currentX<5)||(currentY>=getHeight()-5)||(currentY<5))
midlet.GameOver();
g.setColor(142,255,17);
g.drawString("Score : "+score, 8, 7, Graphics.TOP|Graphics.LEFT);
repaint();
}
protected void keyPressed(int keyCode) {
System.out.println(keyCode);
switch(keyCode)
{
case -1:
condition = false;
position = false;
break;
case -2:
condition = false;
position = true;
break;
case -3:
condition = true;
position = false;
break;
case -4:
condition = true;
position= true;
break;
}
repaint();
}
private void addScore() {
score=score+1;**strong text**
}
private void food(){
Random rand = new Random();
int randomX=rand.nextInt(150) + 20;
int randomY=rand.nextInt(250) + 20;
}
}
包装蛇;
导入java.util.Random;
导入java.util.Timer;
导入java.util.TimerTask;
导入javax.microedition.lcdui.Canvas;
导入javax.microedition.lcdui.Graphics;
公共类SnakeCanvas扩展画布{
private int currentX=getWidth()/2;
private int currentY=getHeight()/2;
私有布尔条件=false;
私有布尔位置=false;
Random rand=新的Random();
int randomX=rand.nextInt(180)+20;
int randomY=rand.nextInt(250)+20;
私人决赛中段;
私人智力得分=0;
定时器t;
公共蛇画布(小型小型小型)
{
this.midlet=midlet;
t=新定时器();
t、 附表(新TimerTask(){
公开募捐{
如果((条件==假)&&(位置==假))
{
电流y-=5;
}
如果((条件==假)和&(位置==真))
{
电流y+=5;
}
如果((条件==真)和&(位置==假))
{
电流x-=5;
}
如果((条件==true)和&(位置==true))
{
电流x+=5;
}
}
}, 50, 50);
}
公共空间涂料(图g){
布尔触摸=假;
//绘图背景
g、 setColor(0x000000);
g、 fillRect(0,0,getWidth(),getHeight());
//画蛇
g、 设置颜色(0xffffff);
g、 fillRoundRect(currentX、currentY、20、20、5、5);
//蛇食
g、 setColor(234,41,42);
g、 fillRect(randomX,randomY,10,10);
如果((当前X-9=随机X-4)和&(当前Y-9=随机Y-4)){
addScore();
}
//绘图块
g、 setColor(82133190);
g、 fillRect(0,0,getWidth(),5);
g、 fillRect(0,getHeight()-5,getWidth(),5);
g、 fillRect(0,0,5,getHeight());
g、 fillRect(getWidth()-5,0,5,getHeight());
如果((currentX>=getWidth()-10)| |(currentX=getHeight()-5)| |(currentY好的。我不知道您是否解决了问题,但您的代码似乎有一些问题
此代码块开头的第一个:
Private int currentX =getWidth()/2;
private int currentY =getHeight()/2;
private boolean condition = false;
private boolean position = false;
Random rand = new Random();
int randomX=rand.nextInt(180) + 20;
int randomY =rand.nextInt(250) + 20;
^我确信这不会编译,在声明头变量时不能调用函数
第二
if((condition==false)&&(position==false))
{
currentY -= 5 ;
}
if((condition==false)&&(position==true))
{
currentY += 5 ;
}
if((condition==true)&&(position==false))
{
currentX -= 5 ;
}
if((condition==true)&&(position==true))
{
currentX += 5 ;
}
}
}, 50, 50);
}
你的块编码风格很糟糕,也很难阅读。有一个很好的指导来掌握它,读吧!
这也适用于您的switch/case语句
最后,请回答你的问题:
private void food(){
Random rand = new Random();
int randomX=rand.nextInt(150) + 20;
int randomY=rand.nextInt(250) + 20;
}
^只是生成随机数,但对它们没有任何作用。我猜你的意思是:
private void food(){
Random rand = new Random();
randomX=rand.nextInt(150) + 20;
randomY=rand.nextInt(250) + 20;
}
它将类全局randomX和randomY设置为新值
但这并没有什么帮助,因为您实际上并没有在这个类的任何地方调用Food()函数!而且考虑到它是“私有的”,只能由这个类使用,这肯定是一个错误
tl;博士,你需要进一步学习编程,但这是一项很好的工作。如果你愿意,我会在评论中为你提供更多帮助。如果我的答案有帮助,请接受
Fenix你的问题是什么?在哪里?另外,请删除所有不相关的代码,并发布一个只包含相关代码的短代码。我是这里的新手,我的问题实际上是关于公共空间油漆的//snakefood(图g),当蛇接触蛇食时,如何重新生成蛇食的随机位置~*随机rand=new random();int randomX=rand.nextInt(180)+20;int randomY=rand.nextInt(250)+20;