====== Differences ====== This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
script_snake [2014/08/28 21:53] lm13 created |
script_snake [2016/06/28 12:31] (current) f43nd1r |
||
---|---|---|---|
Line 1: | Line 1: | ||
====== About the script ====== | ====== About the script ====== | ||
* Purpose : This script is will give you the possibility to play Snake in your launcher | * Purpose : This script is will give you the possibility to play Snake in your launcher | ||
- | * Author : [[https://plus.google.com/+LukasMorawietz|LM13]] | + | * Author : F43nd1r |
* Current Version : 1.0 | * Current Version : 1.0 | ||
* Link : https://plus.google.com/115366157037831519359/posts/66u6CJgMnGC | * Link : https://plus.google.com/115366157037831519359/posts/66u6CJgMnGC | ||
+ | * Download available! (Check repository) | ||
====== Changelog ====== | ====== Changelog ====== | ||
Line 12: | Line 13: | ||
* Resize it to the wished game size | * Resize it to the wished game size | ||
* set this script to the Touchevent | * set this script to the Touchevent | ||
+ | * Note: For this manual way you won't need the setup script | ||
====== Issues and hints ====== | ====== Issues and hints ====== | ||
- | * None yet | + | * The Setup script won't work outside of the APK |
//please report all bugs in the g+ community// | //please report all bugs in the g+ community// | ||
- | ====== Script ====== | + | ====== Setup Script ====== |
- | <sxh javascript;> | + | <sxh javascript;>var MY_PKG="net.pierrox.lightning_launcher.llscript.snake"; |
+ | // install (or update) a script given its id in the package, and its clear name in the launcher data | ||
+ | function installScript(id,name){ | ||
+ | // load the script (if any) among the existing ones | ||
+ | var script=LL.getScriptByName(name); | ||
+ | |||
+ | var script_text=LL.loadRawResource(MY_PKG,id); | ||
+ | |||
+ | if(script==null){ | ||
+ | // script not found: install it | ||
+ | script=LL.createScript(name,script_text,0); | ||
+ | }else{ | ||
+ | // the script already exists: update its text | ||
+ | script.setText(script_text); | ||
+ | } | ||
+ | |||
+ | return script; | ||
+ | } | ||
+ | |||
+ | if(!confirm("Do you want to load Snake in this Container? It should be empty to avoid problems."))return; | ||
+ | var c=LL.getEvent().getContainer(); | ||
+ | var i=c.getItemByName("SnakeField")||c.addShortcut("SnakeField",new Intent(),0,0); | ||
+ | i.setName("SnakeField"); | ||
+ | var script=installScript("snake","Snake"); | ||
+ | var editor=i.getProperties().edit(); | ||
+ | editor.setEventHandler("i.touch",EventHandler.RUN_SCRIPT,script.getId()); | ||
+ | editor.setBoolean("s.labelVisibility",false); | ||
+ | editor.setBoolean("s.iconVisibility",false); | ||
+ | editor.setBoolean("i.onGrid",false); | ||
+ | editor.commit(); | ||
+ | i.setSize(c.getWidth(),c.getHeight()); | ||
+ | i.setBoxBackground(LL.createImage(MY_PKG,"start_screen"),"nsf"); | ||
+ | </sxh> | ||
+ | |||
+ | ====== Snake 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> | </sxh> |