This shows you the differences between two versions of the page.
sample_code:dice [2016/08/29 06:20] titus |
sample_code:dice [2016/09/03 03:50] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | **Dice** | ||
- | You play by throwing a dice after which if the number on your side coincides with the number at the center, you earn points. The one who earns 10 point earlier than the other is the winner. | ||
- | |||
- | <code JavaScript dice.js> | ||
- | //Called when application is started. | ||
- | var a = 0 | ||
- | var b =0 | ||
- | var chk = true | ||
- | function OnStart() | ||
- | { | ||
- | //Create a layout with objects vertically centered. | ||
- | lay = app.CreateLayout( " | ||
- | |||
- | //Create Layout | ||
- | layhor= app.CreateLayout( " | ||
- | | ||
- | //Create a text label and add it to layout. | ||
- | txt = app.CreateText( " | ||
- | txt.SetTextSize( 25 ); | ||
- | layhor.AddChild( txt ); | ||
- | lay.AddChild( layhor ); | ||
- | //Create Text | ||
- | txts = app.CreateText(0) | ||
- | layhor.AddChild(txts ); | ||
- | // | ||
- | btn1 = app.CreateButton(" | ||
- | btn1.SetOnTouch( clc ); | ||
- | layhor.AddChild(btn1 ); | ||
- | // Create text | ||
- | txtsc = app.CreateText(0) | ||
- | layhor.AddChild(txtsc ); | ||
- | //Create layout | ||
- | laycent= app.CreateLayout( " | ||
- | laycent.SetSize(.1, | ||
- | //Create Text | ||
- | txtdice= app.CreateText( " | ||
- | txtdice.SetTextSize(35 ); | ||
- | laycent.AddChild( txtdice ); | ||
- | lay.AddChild(laycent ); | ||
- | |||
- | //Create Layout | ||
- | layhor2 = app.CreateLayout( " | ||
- | layhor2.SetSize(0.45, | ||
- | txt2 = app.CreateText( " | ||
- | txt2.SetTextSize( 25 ); | ||
- | layhor2.AddChild( txt2 ); | ||
- | // Create Text | ||
- | texts = app.CreateText(0) | ||
- | layhor2.AddChild(texts ); | ||
- | |||
- | //Create Button | ||
- | btn2 = app.CreateButton(" | ||
- | btn2.SetOnTouch( clc2 ); | ||
- | layhor2.AddChild(btn2 ); | ||
- | // Create Text | ||
- | textsc = app.CreateText(0) | ||
- | layhor2.AddChild(textsc ); | ||
- | lay.AddChild( layhor2 ); | ||
- | |||
- | //Add layout to app. | ||
- | app.AddLayout( lay ); | ||
- | } | ||
- | function clc() { | ||
- | txts.SetTextColor( "# | ||
- | if(chk){ | ||
- | texts.SetTextColor( "# | ||
- | txtdice.SetTextColor( "# | ||
- | var num = Math.floor(Math.random()*10) | ||
- | var num2= Math.floor(Math.random()*10) | ||
- | txtdice.SetText(num ); | ||
- | txts.SetText(num2 ); | ||
- | if(num==num2){ | ||
- | a++ | ||
- | txtsc.SetText(a); | ||
- | txts.SetTextColor( "# | ||
- | txtdice.SetTextColor( "# | ||
- | over() | ||
- | } | ||
- | chk= false | ||
- | app.Wait(1.5) | ||
- | if(a< | ||
- | app.SimulateTouch(btn2 ); | ||
- | } | ||
- | } | ||
- | else{app.ShowPopup(" | ||
- | } | ||
- | |||
- | function clc2() { | ||
- | texts.SetTextColor( "# | ||
- | if(!chk){ | ||
- | txts.SetTextColor( "# | ||
- | txtdice.SetTextColor( "# | ||
- | var num = Math.floor(Math.random()*10) | ||
- | var num2= Math.floor(Math.random()*10) | ||
- | txtdice.SetText(num ); | ||
- | texts.SetText(num2 ); | ||
- | if(num==num2){ | ||
- | b++ | ||
- | textsc.SetText(b); | ||
- | txtdice.SetTextColor( "# | ||
- | texts.SetTextColor( "# | ||
- | over() | ||
- | } | ||
- | chk = true | ||
- | } | ||
- | else{ app.ShowPopup(" | ||
- | |||
- | } | ||
- | |||
- | function over(){ | ||
- | if(a>9){ | ||
- | alert( txt.GetText()+" | ||
- | replay() | ||
- | } | ||
- | if(b>9){ | ||
- | alert(" | ||
- | replay() | ||
- | } | ||
- | } | ||
- | function replay(){ | ||
- | if(confirm(" | ||
- | a=0 | ||
- | b=0 | ||
- | txtsc.SetText(0); | ||
- | textsc.SetText(0 ); | ||
- | } | ||
- | else{app.Exit()} | ||
- | } | ||
- | |||
- | </ | ||
- | |||
- | [[: |