SHIP Hra Ship je psána pomocí příkazů Javascriptu a standardních příkazů HTML. Hráč hádá slovo, jehož počet písmen se zobrazuje pomocí prázdných znaků reprezentovaných podtržítkem. Hráč kliká myší na písmena abecedy, které jsou pod hádaným slovem. Pokud uhodne, správné písmeno se zobrazí. Pokud ne. Zobrazená loď se o kousek potopí. Po sedmi pokusech, jejichž zbývající počet se ve hře aktuálně zobrazuje (Number of attemps), se loď potopí na maximum a hráči se zobrazí hlášení o neúspěchu. Pokud je hráč úspěšný, zobrazí se tabulka s textemí o úspěšnosti hry a čase, ve kterém bylo slovo uhodnuto. Nabízí také možnost hru hrát znovu. Hráči může pomoci nápověda, (Tlačítko Help) která popisuje hádané slovo. Ve hře je použita databáze slov používaných v počítačové technice. Tlačítko Teachers Guide obsahuje odkaz na stručný popis hry a její účel v projektu Ingot. Stažení hry Hru je možné zkopírovat a upravit dle vlastních znalostí: Pravým tlačítkem myši se zobrazí menu, ze kterého je nutné zvolit Zobrazit zdrojový kód (Text se může mírně lišit podle používaného prohlížeče. Předchozí text platí pro Internet Explorer. Pro Mozzilla Firefox je to text Zobrazit zdrojový kód stránky. ) Pomocí standardních příkazů OS Windows se text označí (př. kombinace kláves CTRL+A) a zkopíruje do schránky. (př. kombinace kláves CTRL+C) V libovolném textovém editoru, který umí pracovat s prostým textem (např. Notepad, který je součástí OS Windows)se otevře nový soubor a text se do něj ze schránky vloží.(př. kombinace kláves CTRL+V) Název souboru je libovolný, ale musí se uložit s příponou html nebo htm (standardní přípona souboru pro web) Další nutný soubor je wordlist.js, ve kterém je uložena databáze slov a procedure s texty nápovědy. Tento soubor se dá stáhnout, tak, že v adresním řádku prohlížeče se smaže ship.htm a nahradí wordlist.js ( př. www.ingot.org/javascript/ship/ship.htm na www.ingot.org/javascript/ship/wordlist.js ) Po odklepnutí klávesou Enter se zobrazí výzva k uložení souboru. Soubor je nutno uložit pod stejným názvem i příponou do stejné složky s předchozím souborem. Poslední nutnou součástí hry jsou obrázky. Seznam je následující: ship0.png, ship0.png, ship1.png, ship2.png, ship3.png, ship4.png, ship5.png, ship6.png, ship7.png obrázky jednotlivých fází potápějící se lodě.
pozadi.jpg vzor pro pozadí. Obrázky se stáhnou obdobným způsobem jako soubor wordlist.js. Jsou ale uloženy v podsložce images. ( př. www.ingot.org/javascript/ship/ship.htm na www.ingot.org/javascript/ship/images/ship0.png ) Obrázek je také nutné uložit do podsložky images, kterou je nutné vytvořit. Správnou funkci všech stažených komponent hry lze ověřit přímým spuštěním souboru ship.htm Editace databáze slov Soubor wordlist.js obsahuje již dříve zmíněnou databázi slov, které hráč hádá. Je uložen v proměnné Wordlist. Slova lze libovolně ubírat nebo přidávat za dodržení těchto podmínek: - Slova musí být oddělena mezerou - Celý seznam slov musí být oddělený úvozovkami, viz. příklad (př. WordList=" antivirus audiocity ) Výměna obrázků Obrázky ve hře lze nahradit vlastními. Lze nahradit jak pozadí tak i jednotlivé části potápějící se lodi za těchto podmínek. - obrázky musí mít stejný název a typ (Zkušenější programátoři mohou použít jiný typ obrázku za předpokladu, že názvy obrázků upraví ve zdrojovém kódu, bude popsáno níže.) - obrázky ship0.png ship7.png musí mít průhledné (transparentní) pozadí (popis této vlastnosti je mimo rozsah tohoto návodu a je možné jej najít na internetu) - pozadí hry je pouze vzorek, který skládá pozadí ze svých opakujících se částí - obrázky musí mít stejnou délku a šířku v pixelech (popis této vlastnosti je opět mimo rozsah tohoto návodu a je možné jej najít na internetu)
Použití jiných názvů obrázku, změna názvu složky, změna počtu pokusů pro hádání slova Pokud je nutné použít jiné názvy obrázků nebo podsložku, kde jsou uloženy je nutné je patřičně upravit v souboru ship.htm. Tato činnost je určena zkušenějším programátrům. Název obrázku pozadí je na řádku 8 součástí HTML příkazu: <BODY bgcolor=#00ffff background="images/pozadi.jpg"> Název obrázků lodi je na řádku 38 součástí cyklu v javascriptu, který načítá obrázky do pole: Pic = new Array(8); for (n=0; n<8; n++) { Pic[n] = new Image(); Pic[n].src = "images/ship"+n+".png"; Zkušenější programátoři mohou měnit počet pokusů pro hádání slova, tak že zvýší nebo sníží konečnou hodnotu cyklu (Současná je 8) Zároveň s tím musí, ale ubrat nebo přidat počet odpovídající obrázků lodi. Stručný popis funkcí hry v jazyce Javascript Hra je, jak bylo již uvedeno výše napsána pomocí Javascriptu a příkazů HTML. Přímo v kódu jsou uvedeny v komentářových řádcíchstručné nápovědy jednotlivých funkcí a prvků a popisy proměnných. K pochopení funkce je nutné zvládnout základy Javascriptu a HTML. (př. /* wordlist - variable in file wordlist.js containing list of words */ ) V souboru ship.htm, je na řádku 9 HTML příkaz: <script language="javascript" src="wordlist.js" type="text/javascript"></script> V něm jsou připojeny příkazy a procedury souboru wordlist.js k spouštěcímu souboru ship.htm. Zkušenější programátoři mohu použít pak jiný název souboru a zde ho změnit. Následuje zachycení události stisku klávesy. <script for=document event="onkeydown()" language="jscript"> //Event Keydown return keycode if (window.event) KeyDown(window.event.keyCode); </script>
Další procedura zajišťuje rozpoznání a odklik správného písmene v kódové tabulce ASCII // detection pressed key function KeyDown(whichkey) { if (IsOver) return; if ((whichkey>64)&&(whichkey<91)) Click(String.fromCharCode(whichkey)); if ((whichkey>96)&&(whichkey<123)) Click(String.fromCharCode(whichkey-32)); Následuje seznam proměnných a cyklus, který načte obrázky do pole. /* n - start initial variable for cycle m - end initial variable for cycle (number of attempts is <8) IsOver - true if game is over Moves - actual attempt Letters - guessed letters Word - word which is displayed StartTime - start time EndTime - end time */ var n, m, IsOver=true, Moves, Letters, Word, StartTime, EndTime; // Pic - field containing images Pic = new Array(8); for (n=0; n<8; n++) { Pic[n] = new Image(); Pic[n].src = "images/ship"+n+".png"; Funkce Init nastaví počáteční proměnné,zinicializuje hru a náhodným způsobem vybere hádané slovo. function Init() /* ii - start initial variable for cycle jj - variable displaying letters in cycle (A-Z is charcode between 65-90) wordlist - variable in file wordlist.js containing list of words */ { var ii, jj; if (!IsOver) { if (! confirm("do you want to give up the current game?")) return; document.options.attempt.value=7; IsOver=false; Moves=0; Letters=""; for (ii=65; ii<91; ii++) { jj=string.fromcharcode(ii); document.options[jj].value=jj;
do { do ii=math.round(math.random()*wordlist.length); while (WordList.charAt(ii)==" "); while (WordList.charAt(ii)!=" ") ii--; jj=++ii; while (WordList.charAt(jj)!=" ") jj++; Word=WordList.substring(ii, jj).touppercase(); while ((Word.length<5) (Word.length>9)) var ii, ss=" "; for (ii=0; ii<word.length; ii++) ss+="_ "; document.options.tword.value=ss; document.images[0].src=pic[moves].src; Now = new Date(); StartTime = Now.getTime() / 1000; document.options.tword.focus(); document.options.tword.blur(); Funkce Click zajišťuje rozpoznání stisknutého písmene, zobrazení pokud ho hádané slovo obsahuje a v případě uhádnutí slova nebi vyčerpání počtu pokusů potřebný výsledek. //action after click on any button function Click(cc) /* ii - start initial variable for cycle ss - variable displaying actual result in button Tword of guess word */ { var ii, ss=" "; document.options.tword.focus(); document.options.tword.blur(); if (IsOver) return; if (Letters.indexOf(cc)>=0) return; document.options[cc].value=" "; Letters+=cc; if (Word.indexOf(cc)>=0) { for (ii=0; ii<word.length; ii++) { if (Letters.indexOf(Word.charAt(ii))>=0) ss+=word.charat(ii)+" "; else ss+="_ "; document.options.tword.value=ss; if (ss.indexof("_")<0) { IsOver=true; Now = new Date(); EndTime = Now.getTime() / 1000; ii=math.floor(endtime - StartTime); if (window.opener) { if (window.opener.sethighscores)
window.opener.sethighscores("hangman","",ii,-1); if (confirm("super, you solved this game in "+ii+" seconds!\nplay again?")) Init(); else { document.options.attempt.value--; Moves++; document.images[0].src=pic[moves].src; if (Moves==7) { IsOver=true; alert("you lost!!!\nthe word was "+Word+"."); Níže je HTML kód zobrazující vlastní prvky hry a grafiku a nastavující události při kliku myší. </head> <BODY bgcolor=#00ffff background="images/pozadi.jpg"> <form name="options"> <p class="uvodni"> Number of attempts is: <!- attempt - name of button displaying number off attempts -> <input type=button value="7" width=15 style="width:15;background-color:#ffffff" name="attempt"> <!- napoveda - name button diplaying link on help -> <input type=button value="help" OnClick="Javascript:DispHelp(Word)" width=35 style="width:35;backgroundcolor:#ffffff" name=napoveda"> <a href="teachers_guide.htm" target="_blank">teachers Guide</a> </p> <p ALIGN=center> <table noborder width=100% cellpadding=0 cellspacing=0> <tr> <td align=left valign=top> <table noborder width=102 height=50 cellpadding=0 cellspacing=0> <tr><td align=center valign=middle> <!- Tword - name of button displaying guessed word -> <input type=button value=" _" width=125 style="width:180;background-color:#ffffff;fontsize:20" name="tword"></td></tr> </table> </td> <td align=center rowspan=2> <table noborder cellpadding=0 cellspacing=0> <tr><td><img src="images/ship0.gif" border=0></td></tr> </table> </td>
</tr> <tr><td align valign=top> <table noborder width=192 cellpadding=0 cellspacing=0> <tr><td align=center valign=top><table border cellpadding=0 cellspacing=4><tr><td> <!- Buttons displaying letters of alphabet -> <input type="button" name="a" value="a" onclick="click('a')" width=20 style="width:20"></td><td> <input type="button" name="b" value="b" onclick="click('b')" width=20 style="width:20"></td><td> <input type="button" name="c" value="c" onclick="click('c')" width=20 style="width:20"></td><td> <input type="button" name="d" value="d" onclick="click('d')" width=20 style="width:20"></td><td> <input type="button" name="e" value="e" onclick="click('e')" width=20 style="width:20"></td><td> <input type="button" name="f" value="f" onclick="click('f')" width=20 style="width:20"></td><td> <input type="button" name="g" value="g" onclick="click('g')" width=20 style="width:20"></td></tr><tr><td> <input type="button" name="h" value="h" onclick="click('h')" width=20 style="width:20"></td><td> <input type="button" name="i" value="i" onclick="click('i')" width=20 style="width:20"></td><td> <input type="button" name="j" value="j" onclick="click('j')" width=20 style="width:20"></td><td> <input type="button" name="k" value="k" onclick="click('k')" width=20 style="width:20"></td><td> <input type="button" name="l" value="l" onclick="click('l')" width=20 style="width:20"></td><td> <input type="button" name="m" value="m" onclick="click('m')" width=20 style="width:20"></td><td> <input type="button" name="n" value="n" onclick="click('n')" width=20 style="width:20"></td></tr><tr><td> <input type="button" name="o" value="o" onclick="click('o')" width=20 style="width:20"></td><td> <input type="button" name="p" value="p" onclick="click('p')" width=20 style="width:20"></td><td> <input type="button" name="q" value="q" onclick="click('q')" width=20 style="width:20"></td><td> <input type="button" name="r" value="r" onclick="click('r')" width=20 style="width:20"></td><td> <input type="button" name="s" value="s" onclick="click('s')" width=20 style="width:20"></td><td> <input type="button" name="t" value="t" onclick="click('t')" width=20 style="width:20"></td><td> <input type="button" name="u" value="u" onclick="click('u')" width=20 style="width:20"></td></tr><tr><td> <input type="button" name="v" value="v" onclick="click('v')" width=20 style="width:20"></td><td> <input type="button" name="w" value="w" onclick="click('w')" width=20 style="width:20"></td><td> <input type="button" name="x" value="x" onclick="click('x')" width=20 style="width:20"></td><td> <input type="button" name="y" value="y" onclick="click('y')" width=20 style="width:20"></td><td> <input type="button" name="z" value="z" onclick="click('z')" width=20 style="width:20"></td><td colspan=2> <input type="button" value="new" onclick="init()" width=48 style="width:48"></td></tr></table> </td></tr> </form> Kód který volá inicializační funkci hry. <script language="javascript"> Init(); </script> </BODY> </HTML>
Proměnná obsahující seznam hádaných slov. //list of used words in game WordList=" antivirus audacity computer copyright display drive file firefox flash hardware inkscape internet keyboard license linux malware memory monitor mouse openoffice opensource owner password platform printer processor safety scanner security smartphone software standard storage touchpad trackball virus windows"; Funkce zobrazující okno nápovědy k hádanému slovu. //display window with help text function DispHelp(sel_word) { odkud="\n\nsource: From Wikipedia, the free encyclopedia"; //sel_word - displayed word switch (sel_word) { case "ANTIVIRUS": alert("this software is used to prevent, detect, and remove malware, including computer viruses, worms, and trojan horses. Such programs may also prevent and remove adware, spyware, and other forms of malware."+odkud); case "AUDACITY": alert("there is a digital audio editor and recording application. The application is free, open source and cross-platform, available for Windows, Mac OS X, Linux and BSD."+odkud); case "COMPUTER": alert("a machine that manipulates data according to a set of instructions."+odkud); case "COPYRIGHT": alert("there is a form of intellectual property that gives the author of an original work exclusive right for a certain time period in relation to that work, including its publication, distribution and adaptation, after which time the work is said to enter the public domain."+odkud); case "DISPLAY": alert("the device is an output device for presentation of information for visual, tactile or auditive reception, acquired, stored, or transmitted in various forms. When the input information is supplied as an electrical signal, the display is called as electronic."+odkud); case "DRIVE": alert("there is a peripheral device used to record and retrieve information. Main implementations are hard disks, floppy disks and optical discs."+odkud); case "FILE": alert("there is a block of arbitrary information, or resource for storing information, which is available to a computer program and is usually based on some kind of durable storage."+odkud); case "FIREFOX":
alert("there is a free and open source web browser descended from the Mozilla Application Suite and managed by Mozilla Corporation."+odkud); case "FLASH": alert("there is a multimedia platform originally acquired by Macromedia and currently developed and distributed by Adobe Systems. Since its introduction in 1996, there has become a popular method for adding animation and interactivity to web pages."+odkud); case "HARDWARE": alert("there is a general term for the physical artifacts of a technology. It may also mean the physical components of a computer system."+odkud); case "INKSCAPE": alert("there is a vector graphics editor application. It is distributed under a free software license, the GNU GPL."+odkud); case "INTERNET": alert("there is a global system of interconnected computer networks that use TCP/IP to serve billions of users worldwide."+odkud); case "KEYBOARD": alert("there is an input device, which uses an arrangement of buttons or keys, to act as mechanical levers or electronic switches."+odkud); case "LICENSE": alert("it means to give permission. There may be granted by a party ('licensor') to another party ('licensee') as an element of an agreement between those parties."+odkud); case "LINUX": alert("there is a generic term referring to Unix-like computer operating systems based on the Linux kernel. Their development is one of the most prominent examples of free and open source software collaboration; typically all the underlying source code can be used, freely modified, and redistributed, both commercially and non-commercially, by anyone under licenses such as the GNU GPL."+odkud); case "MALWARE": alert("short for malicious software, is software designed to infiltrate or damage a computer system without the owner's informed consent."+odkud); case "MEMORY": alert("there is an ability to store, retain, and recall information."+odkud); case "MONITOR": alert("there is a piece of electrical equipment which displays images generated by devices such as computers, without producing a permanent record."+odkud); case "MOUSE": alert("there is a pointing device that functions by detecting two-dimensional motion relative to its supporting surface."+odkud); case "OPENOFFICE":
alert("there is an open source software application suite available for a number of different computer operating systems. It is distributed as free software and written using its own GUI toolkit."+odkud); case "OPENSOURCE": alert("there is computer software for which the source code and certain other rights normally reserved for copyright holders are provided under a software license that meets the Definition or that is in the public domain."+odkud); case "OWNER": alert("he/she is related to ownership, the state or fact of exclusive rights and control over property, which may be an object, land/real estate or intellectual property."+odkud); case "PASSWORD": alert("there is a secret word or string of characters that is used for authentication, to prove identity or gain access to a resource."+odkud); case "PLATFORM": alert("that describes some sort of hardware architecture or software framework (including application frameworks), that allows software to run."+odkud); case "PRINTER": alert("peripheral which produces a hard copy (permanent readable text and/or graphics) of documents stored in electronic form, usually on physical print media such as paper or transparencies."+odkud); case "PROCESSOR": alert("there is a system which processes data which has been captured and encoded in a format recognizable by the data processing system or has been created and stored by another unit of an information processing system."+odkud); case "SAFETY": alert("there is the state of being 'safe', the condition of being protected against physical, social, spiritual, financial, political, emotional, occupational, psychological, educational or other types or consequences of failure, damage, error, accidents, harm or any other event which could be considered non-desirable."+odkud); case "SCANNER": alert("there is a device that optically scans images, printed text, handwriting, or an object, and converts it to a digital image."+odkud); case "SECURITY": alert("there is the degree of protection against danger, loss, and criminals."+odkud); case "SMARTPHONE": alert("there is a mobile phone offering advanced capabilities, often with PC-like functionality."+odkud); case "SOFTWARE": alert("there is a general term used to describe the role that computer programs, procedures and documentation play in a computer system."+odkud); case "STANDARD":
alert("there is an established norm or requirement. It is usually a formal document that establishes uniform engineering or technical criteria, methods, processes and practices."+odkud); case "STORAGE": alert("there is a device for recording (storing) information (data)."+odkud); case "TOUCHPAD": alert("there is a pointing device consisting of specialized surface that can translate the motion and position of a user's fingers to a relative position on screen."+odkud); case "TRACKBALL": alert("there is a pointing device consisting of a ball held by a socket containing sensors to detect a rotation of the ball about two axes-like an upside-down mouse with an exposed protruding ball."+odkud); case "VIRUS": alert("there is a computer program that can copy itself and infect a computer."+odkud); case "WINDOWS": alert("there is a series of software operating systems and graphical user interfaces produced by Microsoft."+odkud);