User Tools

Site Tools


script_snake

**This is an old revision of the document!** ----

A PCRE internal error occured. This might be caused by a faulty plugin

====== About the script ====== * Purpose : This script is will give you the possibility to play Snake in your launcher * Author : [[https://plus.google.com/+LukasMorawietz|LM13]] * Current Version : 1.0 * Link : https://plus.google.com/115366157037831519359/posts/66u6CJgMnGC ====== Changelog ====== * Version 1.0 (28/8/2014): initial release in wiki ====== How to use the script ====== * create a shortcut and hide it's label and icon * Resize it to the wished game size * set this script to the Touchevent ====== Issues and hints ====== * None yet //please report all bugs in the g+ community// ====== Script ====== <sxh javascript;>//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())<size*2&&Math.abs(pos[1]-event.getY())<size*2) { 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()<item.getWidth()/2?-1:1))+4)%4; 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<snake.length;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<array.length;j++) { if(array[j][0]==i[0]&&array[j][1]==i[1])return true; } return false; } </sxh>

script_snake.1409262969.txt.gz · Last modified: 2014/08/28 21:56 by lm13