📄 wawatou.java
字号:
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.lcdui.*;
import java.util.Random;
public class wawatou extends MIDlet
{
Can can;
Display display;
public wawatou()
{
super();
display=Display.getDisplay(this);
can=new Can();
can.start();
// TODO Auto-generated constructor stub
}
protected void startApp() throws MIDletStateChangeException
{
display.setCurrent(can);
// TODO Auto-generated method stub
}
protected void pauseApp()
{
// TODO Auto-generated method stub
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException
{
// TODO Auto-generated method stub
}
}
class Can extends Canvas implements Runnable
{
int n=270;//蛇头嘴巴的起始角度
int x=0,y=0,s=45;//蛇头的位置
int x1=0,y1=0;//食物的位子
int x2=0,y2=0;//
int f=1;//在蛇到边界时,转向的随机变量
Random rand1,rand2,fx;//rand为食物的坐标,fx为蛇到边界时要走的方向;
boolean eat=true; //吃到食物没有
boolean widthl=false;//是否在界外(左 右 上 下)
boolean widthr=false;
boolean heightu=false;
boolean heightd=false;
protected void paint(Graphics g)
{
g.setColor(255,255,255);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(200,0, 0);
g.fillArc(x,y, 10, 10, s, n);
repaint();
// g.setColor(0,255,0);
// g.fillArc(0,5, 10,10,0,360);//无用的
//作为主角的出边碰撞检测 true为界外,false为界内
if(x>getWidth()-15)
{
widthr=true;
}
else
widthr=false;
if(x<5)
{
widthl=true;
}
else
widthl=false;
if(y>getHeight()-15)
{
heightd=true;
}
else
heightd=false;
if(y<5)
{
heightu=true;
}
else
heightu=false;
//随即产生一个点,作为食物
if(eat)
{
rand1=new Random();//当eat为真,就是食物被吃了时生成食物的坐标
rand2=new Random();
do
{
x1=rand1.nextInt(getWidth()-10);
y1=rand2.nextInt(getHeight()-10);//此句有问题。??
}while(x1<=0&&y1<=0&&x1%5!=0&y1%5!=0);
eat=false;
}
switch(s)//画尾巴
{
case 225://嘴巴向左
if(widthl)
{
if(f==1)
{
s=135;
}
else
{
s=315;
}
}
g.setColor(255,0,255);
g.fillArc(x2,y2, 10,10,0,360);
repaint();
break;
case 45://嘴巴向右
if(widthr)
{
if(f==1)
{
s=315;
}
else
{
s=135;
}
}
g.setColor(255,0,255);
g.fillArc(x2, y2, 10, 10, 0, 360);
repaint();
break;
case 135://嘴巴向上
if(heightu)
{
if(f==1)
{
s=45;
}
else
{
s=225;
}
}
g.setColor(255,0,255);
g.fillArc(x2, y2, 10, 10, 0, 360);
repaint();
break;
case 315://嘴巴向下
if(heightd)
{
if(f==1)
{
s=225;
}
else
{
s=45;
}
}
g.setColor(255,0,255);
g.fillArc(x2, y2, 10, 10, 0, 360);
repaint();
break;
}
if(eat==false)
{
g.setColor(0,255,0);
g.fillArc(x1,y1, 10,10,0,360);//用随机数画食物
fx=new Random();
f=fx.nextInt(2);
}
//判断是否吃到食物,做一个食物的生成开关 ,true是吃掉,false是存在
if(x1+10>x&&x1+1<x+10&&y1+10>y&&y1+1<y+10)
{
eat=true;
}
}
public void run()
{
while(true)
{
//System.out.print("aa");
try
{
Thread.sleep(200);
if(n==270)
{
n=360;
}
else
{
if(n==360)
{
n=270;
}
}
}catch(InterruptedException e)
{
e.printStackTrace();
}
try
{
Thread.sleep(100);
switch(s)
{
case 225:
x2=x;
y2=y;
x=x-10;
break;
case 45:
x2=x;
y2=y;
x=x+10;
break;
case 135:
x2=x;
y2=y;
y=y-10;
break;
case 315:
x2=x;
y2=y;
y=y+10;
break;
}
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
public void start()
{
Thread t=new Thread(this);
t.start();
}
protected void keyPressed(int keyCode)
{
int key=getGameAction(keyCode);
//是否对运动做反应,界外不反应
if(widthl==false&&key==LEFT)
{
s=225;
}
if(widthr==false&&key==RIGHT)
{
s=45;
}
if(heightu==false&&key==UP)
{
s=135;
}
if(heightd==false&&key==DOWN)
{
s=315;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -