templeos-info/public/Wb/Demo/Games/CircleTrace.HC.HTML

160 lines
15 KiB
HTML
Executable File

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="generator" content="TempleOS V5.03">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="/style/templeos.css">
<script src="/script/templeos.js"></script>
<style type="text/css">
.cF0{color:#000000;background-color:#ffffff;}
.cF1{color:#0000aa;background-color:#ffffff;}
.cF2{color:#00aa00;background-color:#ffffff;}
.cF3{color:#00aaaa;background-color:#ffffff;}
.cF4{color:#aa0000;background-color:#ffffff;}
.cF5{color:#aa00aa;background-color:#ffffff;}
.cF6{color:#aa5500;background-color:#ffffff;}
.cF7{color:#aaaaaa;background-color:#ffffff;}
.cF8{color:#555555;background-color:#ffffff;}
.cF9{color:#5555ff;background-color:#ffffff;}
.cFA{color:#55ff55;background-color:#ffffff;}
.cFB{color:#55ffff;background-color:#ffffff;}
.cFC{color:#ff5555;background-color:#ffffff;}
.cFD{color:#ff55ff;background-color:#ffffff;}
.cFE{color:#ffff55;background-color:#ffffff;}
.cFF{color:#ffffff;background-color:#ffffff;}
</style>
</head>
<body>
<pre id="content">
<a name="l1"></a><span class=cF2>//Practice tracing circles</span><span class=cF0>
<a name="l2"></a>
<a name="l3"></a></span><span class=cF2>//Giotto, a famous artist, drew a freehand circle to get a job.</span><span class=cF0>
<a name="l4"></a>
<a name="l5"></a></span><span class=cF5>RegDft</span><span class=cF0>(</span><span class=cF6>&quot;TempleOS/CircleTrace&quot;</span><span class=cF0>,</span><span class=cF6>&quot;F64 best_score=999;\n&quot;</span><span class=cF0>);
<a name="l6"></a></span><span class=cF5>RegExe</span><span class=cF0>(</span><span class=cF6>&quot;TempleOS/CircleTrace&quot;</span><span class=cF0>);
<a name="l7"></a>
<a name="l8"></a></span><span class=cF9>I64</span><span class=cF0> cx,cy;
<a name="l9"></a></span><span class=cF1>F64</span><span class=cF0> avg_error=0,elapsed_time=0,total_error=0,score=999;
<a name="l10"></a>
<a name="l11"></a></span><span class=cF1>U0</span><span class=cF0> SongTask(</span><span class=cF9>I64</span><span class=cF0>)
<a name="l12"></a>{
<a name="l13"></a> </span><span class=cF5>Fs</span><span class=cF0>-&gt;task_end_cb=&amp;</span><span class=cF5>SndTaskEndCB</span><span class=cF0>;
<a name="l14"></a> </span><span class=cF5>MusicSettingsRst</span><span class=cF0>;
<a name="l15"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF3>TRUE</span><span class=cF0>)
<a name="l16"></a> </span><span class=cF5>Play</span><span class=cF0>(</span><span class=cF6>&quot;5hEDC4A5RCDECR&quot;</span><span class=cF0>);
<a name="l17"></a>}
<a name="l18"></a>
<a name="l19"></a></span><span class=cF1>U0</span><span class=cF0> </span><span class=cF5>DrawIt</span><span class=cF0>(</span><span class=cF9>CTask</span><span class=cF0> *task,</span><span class=cF9>CDC</span><span class=cF0> *dc)
<a name="l20"></a>{
<a name="l21"></a> </span><span class=cF5>GrPrint</span><span class=cF0>(dc,0,0,</span><span class=cF6>&quot;Error:%6.3f Time:%6.3f Score:%6.3f Best:%8.3f&quot;</span><span class=cF0>,
<a name="l22"></a> avg_error,elapsed_time,score,best_score);
<a name="l23"></a>}
<a name="l24"></a>
<a name="l25"></a>#</span><span class=cF1>define</span><span class=cF0> CIRCLE_RADIUS 100
<a name="l26"></a>#</span><span class=cF1>define</span><span class=cF0> CIRCUMFERENCE (2*</span><span class=cF3>pi</span><span class=cF0>*CIRCLE_RADIUS)
<a name="l27"></a>
<a name="l28"></a></span><span class=cF1>Bool</span><span class=cF0> CTPlot(</span><span class=cF9>CDC</span><span class=cF0> *dc,</span><span class=cF9>I64</span><span class=cF0> x,</span><span class=cF9>I64</span><span class=cF0> y,</span><span class=cF9>I64</span><span class=cF0>)
<a name="l29"></a>{
<a name="l30"></a> </span><span class=cF1>F64</span><span class=cF0> rad=</span><span class=cF5>Sqrt</span><span class=cF0>(</span><span class=cF5>Sqr</span><span class=cF7>(</span><span class=cF0>x-cx</span><span class=cF7>)</span><span class=cF0>+</span><span class=cF5>Sqr</span><span class=cF7>(</span><span class=cF0>y-cy</span><span class=cF7>)</span><span class=cF0>);
<a name="l31"></a> </span><span class=cF5>GrPlot</span><span class=cF0>(dc,x,y);
<a name="l32"></a> total_error+=</span><span class=cF5>Abs</span><span class=cF0>(rad-CIRCLE_RADIUS);
<a name="l33"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>TRUE</span><span class=cF0>;
<a name="l34"></a>}
<a name="l35"></a>
<a name="l36"></a></span><span class=cF1>U0</span><span class=cF0> CircleTrace()
<a name="l37"></a>{
<a name="l38"></a> </span><span class=cF9>I64</span><span class=cF0> msg_code,arg1,arg2,x1,y1;
<a name="l39"></a> </span><span class=cF1>F64</span><span class=cF0> rad,t0,total_distance;
<a name="l40"></a> </span><span class=cF9>CDC</span><span class=cF0> *dc=</span><span class=cF5>DCAlias</span><span class=cF0>;
<a name="l41"></a>
<a name="l42"></a> </span><span class=cF5>SettingsPush</span><span class=cF0>; </span><span class=cF2>//See </span><a href="/Wb/Adam/TaskSettings.HC.HTML#l3"><span class=cF4>SettingsPush</span></a><span class=cF0>
<a name="l43"></a> </span><span class=cF5>Fs</span><span class=cF0>-&gt;song_task=</span><span class=cF5>Spawn</span><span class=cF0>(&amp;SongTask,</span><span class=cF3>NULL</span><span class=cF0>,</span><span class=cF6>&quot;Song&quot;</span><span class=cF0>,,</span><span class=cF5>Fs</span><span class=cF0>);
<a name="l44"></a> </span><span class=cF5>AutoComplete</span><span class=cF0>;
<a name="l45"></a> </span><span class=cF5>WinBorder</span><span class=cF0>;
<a name="l46"></a> </span><span class=cF5>WinMax</span><span class=cF0>;
<a name="l47"></a> </span><span class=cF5>DocCursor</span><span class=cF0>;
<a name="l48"></a> </span><span class=cF5>DocClear</span><span class=cF0>;
<a name="l49"></a>
<a name="l50"></a> cx=</span><span class=cF5>Fs</span><span class=cF0>-&gt;pix_width/2;
<a name="l51"></a> cy=</span><span class=cF5>Fs</span><span class=cF0>-&gt;pix_height/2;
<a name="l52"></a>
<a name="l53"></a> </span><span class=cF5>Fs</span><span class=cF0>-&gt;win_inhibit=</span><span class=cF3>WIG_TASK_DFT</span><span class=cF0>-</span><span class=cF3>WIF_SELF_FOCUS</span><span class=cF0>-</span><span class=cF3>WIF_SELF_BORDER</span><span class=cF0>;
<a name="l54"></a> </span><span class=cF5>Fs</span><span class=cF0>-&gt;draw_it=&amp;</span><span class=cF5>DrawIt</span><span class=cF0>;
<a name="l55"></a>
<a name="l56"></a> </span><span class=cF5>DCFill</span><span class=cF0>;
<a name="l57"></a> dc-&gt;color=</span><span class=cF3>ROP_XOR</span><span class=cF0>+</span><span class=cF3>BLACK</span><span class=cF0>^</span><span class=cF3>TRANSPARENT</span><span class=cF0>;
<a name="l58"></a> </span><span class=cF5>GrCircle</span><span class=cF0>(dc,cx,cy,CIRCLE_RADIUS);
<a name="l59"></a> </span><span class=cF1>do</span><span class=cF0> </span><span class=cF7>{</span><span class=cF0>
<a name="l60"></a> msg_code=</span><span class=cF5>GetMsg</span><span class=cF0>(&amp;arg1,&amp;arg2,
<a name="l61"></a> 1&lt;&lt;</span><span class=cF3>MSG_KEY_DOWN</span><span class=cF0>+1&lt;&lt;</span><span class=cF3>MSG_MS_L_DOWN</span><span class=cF0>);
<a name="l62"></a> </span><span class=cF1>switch</span><span class=cF0> (msg_code) {
<a name="l63"></a> </span><span class=cF1>case</span><span class=cF0> </span><span class=cF3>MSG_KEY_UP</span><span class=cF0>:
<a name="l64"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l65"></a> </span><span class=cF1>case</span><span class=cF0> </span><span class=cF3>MSG_MS_L_DOWN</span><span class=cF0>:
<a name="l66"></a> </span><span class=cF5>DCFill</span><span class=cF0>;
<a name="l67"></a> dc-&gt;color=</span><span class=cF3>ROP_XOR</span><span class=cF0>+</span><span class=cF3>BLACK</span><span class=cF0>^</span><span class=cF3>TRANSPARENT</span><span class=cF0>;
<a name="l68"></a> </span><span class=cF5>GrCircle</span><span class=cF0>(dc,cx,cy,CIRCLE_RADIUS);
<a name="l69"></a> dc-&gt;color=</span><span class=cF3>ROP_XOR</span><span class=cF0>+</span><span class=cF3>RED</span><span class=cF0>^</span><span class=cF3>TRANSPARENT</span><span class=cF0>;
<a name="l70"></a> t0=</span><span class=cF5>tS</span><span class=cF0>;
<a name="l71"></a> total_distance=0.001;
<a name="l72"></a> total_error=0;
<a name="l73"></a> x1=arg1; y1=arg2;
<a name="l74"></a> </span><span class=cF1>do</span><span class=cF0> </span><span class=cF7>{</span><span class=cF0>
<a name="l75"></a> msg_code=</span><span class=cF5>ScanMsg</span><span class=cF0>(&amp;arg1,&amp;arg2,
<a name="l76"></a> 1&lt;&lt;</span><span class=cF3>MSG_MS_L_UP</span><span class=cF0>+1&lt;&lt;</span><span class=cF3>MSG_MS_MOVE</span><span class=cF0>);
<a name="l77"></a> </span><span class=cF1>switch</span><span class=cF0> (msg_code) {
<a name="l78"></a> </span><span class=cF1>case</span><span class=cF0> </span><span class=cF3>MSG_MS_L_UP</span><span class=cF0>:
<a name="l79"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l80"></a> </span><span class=cF1>case</span><span class=cF0> </span><span class=cF3>MSG_MS_MOVE</span><span class=cF0>:
<a name="l81"></a> </span><span class=cF1>if</span><span class=cF0> (x1!=arg1 || y1!=arg2) </span><span class=cF7>{</span><span class=cF0>
<a name="l82"></a> total_distance+=</span><span class=cF5>Sqrt</span><span class=cF0>(</span><span class=cF5>Sqr</span><span class=cF7>(</span><span class=cF0>x1-arg1</span><span class=cF7>)</span><span class=cF0>+</span><span class=cF5>Sqr</span><span class=cF7>(</span><span class=cF0>y1-arg2</span><span class=cF7>)</span><span class=cF0>);
<a name="l83"></a> </span><span class=cF5>Line</span><span class=cF0>(dc,x1,y1,0,arg1,arg2,0,&amp;CTPlot);
<a name="l84"></a> x1=arg1;y1=arg2;
<a name="l85"></a></span><span class=cF2>//undo overlapping pixs on segments</span><span class=cF0>
<a name="l86"></a> </span><span class=cF5>GrPlot</span><span class=cF0>(dc,x1,y1);
<a name="l87"></a> rad=</span><span class=cF5>Sqrt</span><span class=cF0>(</span><span class=cF5>Sqr</span><span class=cF7>(</span><span class=cF0>x1-cx</span><span class=cF7>)</span><span class=cF0>+</span><span class=cF5>Sqr</span><span class=cF7>(</span><span class=cF0>y1-cy</span><span class=cF7>)</span><span class=cF0>);
<a name="l88"></a> total_error-=</span><span class=cF5>Abs</span><span class=cF0>(rad-CIRCLE_RADIUS);
<a name="l89"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l90"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l91"></a> }
<a name="l92"></a> elapsed_time=</span><span class=cF5>tS</span><span class=cF0>-t0;
<a name="l93"></a> avg_error=total_error/total_distance;
<a name="l94"></a> score=elapsed_time*avg_error;
<a name="l95"></a>
<a name="l96"></a> </span><span class=cF2>//</span><a href="/Wb/Kernel/KMisc.HC.HTML#l155"><span class=cF4>Sleep</span></a><span class=cF2>()s until the next time the</span><span class=cF0>
<a name="l97"></a> </span><span class=cF2>//window mgr task runs. The</span><span class=cF0>
<a name="l98"></a> </span><span class=cF2>//window mgr calls the UpdateWin() routine</span><span class=cF0>
<a name="l99"></a> </span><span class=cF2>//and places msgs in the que, so there's</span><span class=cF0>
<a name="l100"></a> </span><span class=cF2>//no need to do anything until the window mgr runs.</span><span class=cF0>
<a name="l101"></a> </span><span class=cF5>Refresh</span><span class=cF0>;
<a name="l102"></a>
<a name="l103"></a> </span><span class=cF7>}</span><span class=cF0> </span><span class=cF1>while</span><span class=cF0> (msg_code!=</span><span class=cF3>MSG_MS_L_UP</span><span class=cF0>);
<a name="l104"></a> </span><span class=cFB>music</span><span class=cF0>.mute=</span><span class=cF3>TRUE</span><span class=cF0>; </span><span class=cF5>Sleep</span><span class=cF0>(200);
<a name="l105"></a> </span><span class=cF1>if</span><span class=cF0> (total_distance&gt;0.95*CIRCUMFERENCE) </span><span class=cF7>{</span><span class=cF0>
<a name="l106"></a> </span><span class=cF1>if</span><span class=cF0> (score&lt;best_score) {
<a name="l107"></a> </span><span class=cF5>Snd</span><span class=cF0>(86);</span><span class=cF5>Sleep</span><span class=cF0>(50);</span><span class=cF5>Snd</span><span class=cF0>;</span><span class=cF5>Sleep</span><span class=cF0>(50);
<a name="l108"></a> </span><span class=cF5>Snd</span><span class=cF0>(86);</span><span class=cF5>Sleep</span><span class=cF0>(50);</span><span class=cF5>Snd</span><span class=cF0>;</span><span class=cF5>Sleep</span><span class=cF0>(50);
<a name="l109"></a> </span><span class=cF5>Snd</span><span class=cF0>(86);</span><span class=cF5>Sleep</span><span class=cF0>(50);
<a name="l110"></a> best_score=score;
<a name="l111"></a> } </span><span class=cF1>else</span><span class=cF0> {
<a name="l112"></a> </span><span class=cF5>Snd</span><span class=cF0>(62);</span><span class=cF5>Sleep</span><span class=cF0>(50);
<a name="l113"></a> }
<a name="l114"></a> </span><span class=cF7>}</span><span class=cF0> </span><span class=cF1>else</span><span class=cF0> </span><span class=cF7>{</span><span class=cF0>
<a name="l115"></a> </span><span class=cF5>Snd</span><span class=cF0>(34);</span><span class=cF5>Sleep</span><span class=cF0>(1000);
<a name="l116"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l117"></a> </span><span class=cF5>Snd</span><span class=cF0>; </span><span class=cF5>Sleep</span><span class=cF0>(200); </span><span class=cFB>music</span><span class=cF0>.mute=</span><span class=cF3>FALSE</span><span class=cF0>;
<a name="l118"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l119"></a> }
<a name="l120"></a> </span><span class=cF7>}</span><span class=cF0> </span><span class=cF1>while</span><span class=cF0> (msg_code!=</span><span class=cF3>MSG_KEY_DOWN</span><span class=cF0> || arg1!=</span><span class=cF3>CH_SHIFT_ESC</span><span class=cF0> &amp;&amp; arg1!=</span><span class=cF3>CH_ESC</span><span class=cF0>);
<a name="l121"></a> </span><span class=cF5>GetMsg</span><span class=cF0>(,,1&lt;&lt;</span><span class=cF3>MSG_KEY_UP</span><span class=cF0>);
<a name="l122"></a> </span><span class=cF5>SettingsPop</span><span class=cF0>;
<a name="l123"></a> </span><span class=cF5>DCFill</span><span class=cF0>;
<a name="l124"></a> </span><span class=cF5>DCDel</span><span class=cF0>(dc);
<a name="l125"></a> </span><span class=cF5>RegWrite</span><span class=cF0>(</span><span class=cF6>&quot;TempleOS/CircleTrace&quot;</span><span class=cF0>,</span><span class=cF6>&quot;F64 best_score=%5.4f;\n&quot;</span><span class=cF0>,best_score);
<a name="l126"></a>}
<a name="l127"></a>
<a name="l128"></a>CircleTrace; </span><span class=cF2>//Execute when #included</span><span class=cF0>
</span></pre></body>
</html>