not as cool as TrianguloY ‘s snake with icons, but I had a funny hour scripting it.
not as cool as TrianguloY ‘s snake with icons, but I had a funny hour scripting it.
Setup is easy: create a shortcut, hide label and icon and set the script to the Touch event.
Steering is also easy: tap left to turn left, tap right to turn right, tap long to start.
http://www.pierrox.net/android/applications/lightning_launcher/wiki/doku.php?id=script_snake
]]>
< ![CDATA[
//config
var bgColor=0xffffffff;//has to be a solid color;
var headColor=0xff00ff00;
var tailColor=0xff008000;
var foodColor=0xff000090;
var borderColor=0xffff0000;
var tick=130;
var fieldSize=30;//the width of the field
//endconfig
var size=Math.min(item.getWidth(),item.getHeight())/fieldSize;
if(event.getAction()==MotionEvent.ACTION_DOWN)
{
pos=[event.getX(),event.getY()];
}
else if(event.getAction()==MotionEvent.ACTION_UP&&Math.abs(pos[0]-event.getX())
{
if(typeof alive===’undefined’||!alive)
{
if(event.eventTime-event.downTime<300)
{
Android.makeNewToast(“To Start a new game long press and then release anywhere”,true).show();
return;
}
else
{
paint=new Paint();
image=item.getBoxBackground(“n”);
if(image.getWidth()!=item.getWidth()||image.getHeight()!=item.getHeight())image=LL.createImage(item.getWidth(),item.getHeight());
canvas=image.draw();
item.setBoxBackground(image,”n”);
alive=true;
snake=[[rand(3,item.getWidth()/size-3),rand(3,item.getHeight()/size-3)]];
dir=rand(0,4);
dirChanged=false;
snake.push([snake[0][0]+(dir==0||dir==2?0:dir==1?-1:1),snake[0][1]+(dir==1||dir==3?0:dir==2?-1:1)]);
snake.push([snake[0][0]+(dir==0||dir==2?0:dir==1?-2:2),snake[0][1]+(dir==1||dir==3?0:dir==2?-2:2)]);
newFood();
score=0;
draw();
var countDown=3;
setTimeout(start,tick);
}
}
else if(!dirChanged)
{
dir=((dir+(event.getX()-
dirChanged=true;
}
}
function start()
{
if(countDown==0)
{
Android.makeNewToast(“Start!”,true).show();
setTimeout(move,tick);
}
else
{
Android.makeNewToast(countDown+”…”,true).show();
countDown-=1;
setTimeout(start,2000);
}
}
function rand(min,max)//random Integer out of [min,max)
{
return Math.floor(Math.random()*(max-min))+min;
}
function move()
{
var next=[snake[0][0]+(dir==0||dir==2?0:dir==1?1:-1),snake[0][1]+(dir==1||dir==3?0:dir==2?1:-1)];
if(next[0]<1||next[0]>Math.floor(item.getWidth()/size)-2||next[1]<1||next[1]>Math.floor(item.getHeight()/size)-2||contains(snake,next))
{
alive=false;
Android.makeNewToast(“Lost! Score: “+score,false).show();
}
else
{
snake.unshift(next);
if(next[0]==food[0]&&next[1]==food[1])
{
score+=1;
newFood();
}
else snake.pop();
dirChanged=false;
draw();
setTimeout(move,tick);
}
}
function draw()
{
canvas.drawColor(bgColor);
paint.setColor(headColor);
canvas.drawRect(snake[0][0]*size,snake[0][1]*size,snake[0][0]*size+size,snake[0][1]*size+size,paint);
paint.setColor(tailColor);
for(var a=1;a
{
canvas.drawRect(snake[a][0]*size,snake[a][1]*size,snake[a][0]*size+size,snake[a][1]*size+size,paint);
}
paint.setColor(foodColor);
canvas.drawRect(food[0]*size,food[1]*size,food[0]*size+size,food[1]*size+size,paint);
paint.setColor(borderColor);
canvas.drawRect(0,0,size,item.getHeight(),paint);
canvas.drawRect(0,0,item.getWidth(),size,paint);
canvas.drawRect((Math.floor(item.getWidth()/size)-1)*size,0,item.getWidth(),item.getHeight(),paint);
canvas.drawRect(0,(Math.floor(item.getHeight()/size)-1)*size,item.getWidth(),item.getHeight(),paint);
image.update();
item.setBoxBackground(image,”n”);
}
function newFood()
{
do
{
food=[rand(1,Math.floor(item.getWidth()/size)-2),rand(1,Math.floor(item.getHeight()/size)-2)];
}
while(contains(snake,food));
}
function contains(array,i)
{
for(j=0;j
{
if(array[j][0]==i[0]&&array[j][1]==i[1])return true;
}
return false;
}
]]>
< ![CDATA[
lol good job.
😀
]]>
< ![CDATA[
Btw: my highscore so far was 40. Who can beat me? 😉
]]>