Rotation Game Tutorial
โœ‚๐Ÿƒ๐Ÿพโ€โ™€๏ธ๐Ÿƒ๐Ÿผโ€โ™‚๏ธ
๐Ÿ“–

Rotation Game Tutorial

Rotation Game Tutorial

Can you guess what todayโ€™s tutorial is about if we say โ€ฆ

โ€ฆ
โ€ฆ
โ€ฆ
โ€ฆ
โ€ฆ
โ€ฆ
โ€ฆ
โ€ฆ
โ€ฆ
โ€ฆ
โ€ฆ
โ€ฆ
โ€ฆ
โ€ฆ
โ€ฆ
โ€ฆ

Okay, Tad Low, youโ€™ve had your hand up for ten minutes now!

โ€ฆ
โ€ฆ
โ€ฆ
โ€ฆ
โ€ฆ
โ€ฆ
โ€ฆ
โ€ฆ
โ€ฆ
โ€ฆ
โ€ฆ
โ€ฆ
โ€ฆ
โ€ฆ
โ€ฆ
โ€ฆ

Oh. Well, itโ€™s around the corner, and remember to wash your hands after. You may be excused. Anyone else, anyone?

โ€ฆ
โ€ฆ
โ€ฆ
โ€ฆ
โ€ฆ
โ€ฆ
โ€ฆ
โ€ฆ
โ€ฆ
โ€ฆ
โ€ฆ
โ€ฆ
โ€ฆ
โ€ฆ
โ€ฆ
โ€ฆ ?

Yes, Kyungjin Lee, am after โ€œSpin the BottleMobile Device Gameโ€ โ€ฆ yayyyyyyy!

Yes, be careful how you set it up so that your mobile device will not fall and/or hit anything bad โ€ฆ carpet (with lots of room) is good โ€ฆ but โ€ฆ

  1. Tap Deviceโœ‚Orientation live run link โ€ฆ
  2. Tap any โ€œStartโ€ and/or โ€œAllowโ€ button(s) presented at the top left (and answer any permissions questions, pending) โ€ฆ
  3. Tap the new โ€œSpin the Deviceโ€ button โ€ฆ
  4. Select number of players โ€ฆ from 1 to 6 โ€ฆ
  5. Players take it in turns, starting with a score of 36000 each, to be given a nominated clockwise angle in degrees to spin, that player โ€ฆ
    • Taps their yellow โ€œNot Readyโ€ button โ€ฆ to effectively say, โ€œam braced, ready to spin my deviceโ€ โ€ฆ
    • Player then โ€œspins that deviceโ€ so as to closely match that nominated number of clockwise degrees โ€ฆ then taps their (now) green button for the web applicationโ€™s verdict โ€ฆ
    • And the score reduces by any difference between the nominal number of (clockwise) degrees and the playerโ€™s spin number of (clockwise) degrees rotation detected (by good olโ€™ window.DeviceOrientationEvent event logic)
  6. Next playerโ€™s turnspin!

The new static HTML table element for the โ€œSpin the Deviceโ€ game, positioned just below any permissions related buttons up the top of the webpage, initialized to โ€ฆ



<table id=mytable>

</table>

โ€ฆ supported by the new relevant Javascript โ€œSpin the Deviceโ€ game functions and global variables โ€ฆ



var nump=0;

var spinscores=[];

var spingoes=[];

var spinnext=0;

var degtospin=0;



function amready(whoin) {

var who=eval(eval(whoin) + 0);

//alert('Who=' + who);

if (document.getElementById('b' + who).innerHTML == 'Not Ready') {

document.getElementById('b' + who).style.backgroundColor='green';

document.getElementById('b' + who).innerHTML='Spin close to <font size=4 color=red>' + degtospin + '&#176; clockwise &#8753;</font>, then tap me again';

initial_yaw=eval('0' + document.getElementById("doBearing").innerHTML);

if (document.getElementById('th0').innerHTML.indexOf('tap your ') != -1) {

document.getElementById('th0').innerHTML=document.getElementById('th0').innerHTML.split('tap your ')[0] + 'spin now. Good luck!';

}

} else if (document.getElementById('b' + who).innerHTML != 'Ready') {

//alert('WHO=' + who);

spingoes[eval(-1 + eval(who))]++;

//alert('' + Math.abs(eval(degtospin - eval(eval(360 + eval('0' + document.getElementById('doBearing').innerHTML) - initial_yaw) % 360))) + ' via initial_yaw=' + initial_yaw + ' and curbrg=' + document.getElementById('doBearing').innerHTML);

spinscores[eval(-1 + eval(who))]-=Math.abs(eval(degtospin - eval(eval(360 + eval('0' + document.getElementById('doBearing').innerHTML) - initial_yaw) % 360)));

document.getElementById('goes' + who).innerHTML='' + spingoes[eval(-1 + eval(who))];

document.getElementById('score' + who).innerHTML='' + spinscores[eval(-1 + eval(who))];

document.getElementById('b' + who).style.backgroundColor='white';

document.getElementById('b' + who).innerHTML='Not Ready';

spinnext++;

if (spinnext == nump) { spinnext=0; }

gospin(spinnext);

}

}



function playspin(cnump) {

var mytih='<thead><tr></tr></thead><tfoot><tr></tr></tfoot>';

if (cnump.trim() != '') {

nump=eval(cnump);

mytih=mytih.replace('<thead>', '<thead><tr><th style="cursor:pointer;" onclick="alert(this.title);" title="Careful how you play so as not to drop your device nor have it hit something. On carpet with lots of room is ideal here." id=th0 colspan=' + nump + '>Welcome to the <font title="Careful how you play so as not to drop your device nor have it hit something. On carpet with lots of room is ideal here." color=purple size=5>Spin the Device</font> game for ' + nump + '<span id=s0></span></th></tr>');

for (var ii=1; ii<=nump; ii++) {

spinscores.push(36000);

spingoes.push(0);

mytih=mytih.replace('</tr></thead>', '<th id=th' + ii + '><div id=div' + ii + ' contenteditable=true>Player ' + ii + '</div></th></tr></thead>');

mytih=mytih.replace('</tr></tfoot>', '<td id=td' + ii + '><span id=score' + ii + '>' + spinscores[eval(-1 + ii)] + '</span><span> / </span><span id=goes' + ii + '>0</span><br><br><button title="Click when ready" id=b' + ii + ' onclick=amready(' + ii + ');>Not Ready</button></th></tr></tfoot>');

}

document.getElementById('mytable').innerHTML=mytih;

document.getElementById('mytable').border='20';

}

gospin(spinnext);

}



function gospin(whoin) {

var who=eval(eval(whoin) + 1);

//alert('who=' + who);

document.getElementById('b' + who).style.backgroundColor='yellow';

degtospin=eval(Math.floor(Math.random() * 358) + 1);

document.getElementById('s0').innerHTML=' ... <font color=blue size=4>' + document.getElementById('div' + who).innerHTML + '</font> ... try to spin close to <font color=red size=4>' + degtospin + '&#176; clockwise &#8753;</font> & tap your yellow Not Ready button to start';

}



function spingame() {

document.getElementById('dspin').innerHTML="<select onchange='playspin(this.value);'><option value=''>Spin Game number of players below ...</option><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option><option value='5'>5</option><option value='6'>6</option></select>";

}



Previous relevant HTML5 Device Orientation Events Game Tutorial is shown below.

HTML5 Device Orientation Events Game Tutorial

HTML5 Device Orientation Events Game Tutorial

Today weโ€™ve written a web application suiting mobile devices, but probably not many laptops. That is because we are harnessing the power of the Device Orientation (today) and Device Motion (at a later date) events new to HTML5, and very well explained at this tremendous link, thanks.

If you use a mobile device regularly, youโ€™ll probably have seen mobile and web applications making use of the fact that the mobile device being used is moving itself, and has its screen orientation an option, rather than an awkwardly controlled hardware setting. These dynamisms of what we are talking about harnessing today, and we closely follow the leads of the code supplied in the link above, and build a โ€œDevice Orientationโ€ game based on these HTML5 and Javascript based smarts.

The bottom line of this is that with this functionality we can glean the 3 rotations of the mobile device relative to the world around it, termed as โ€ฆ

  • โ€œAlphaโ€ โ€ฆ or Direction, or Bearing, or in shipping โ€œmotionโ€ terms, โ€œyawโ€
  • โ€œBetaโ€ โ€ฆ or Tilt Front/Back, or in shipping โ€œmotionโ€ terms, โ€œpitchโ€
  • โ€œGammaโ€ โ€ฆ or Tilt Left/Right, or in shipping โ€œmotionโ€ terms, โ€œrollโ€

Defining those, our game can take you into the cockpit of a plane, perhaps, to set you tasks you try to achieve as accurately as possible, in a โ€œseconds survivedโ€ game of skill and perseverance.

The HTML and Javascript programming source code you could call yaw_etcโšซhtml does not have to be played as a game in its liveโœ‚run form, and in that โ€œnon-gameโ€ mode of use, just gets you used to the way the three rotations above happen for mobile devices where the Javascript โ€ฆ



<script type='text/javascript'>

if (window.DeviceOrientationEvent) {

// You're in business with HTML5 Device Orientation Events

}

</script>

โ€ฆ which, happily, is the case for the iPad screenshot of todayโ€™s tutorial picture.

Image helping websites weโ€™d like to thank are โ€ฆ

If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.

This entry was posted in eLearning, Event-Driven Programming, Tutorials and tagged , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *