javascript:labirintas
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| javascript:labirintas [2016/12/05 16:05] – created aistis | javascript:labirintas [2016/12/06 00:10] (current) – aistis | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| Labirintą taip pat galima padaryti su JavaScript. Čia pateikiu kodą kuris paišo labirintą ir judina kursorių, dar reikia pridėti tikrinima kad neleistu eiti jeigu lieti juodą sieną. | Labirintą taip pat galima padaryti su JavaScript. Čia pateikiu kodą kuris paišo labirintą ir judina kursorių, dar reikia pridėti tikrinima kad neleistu eiti jeigu lieti juodą sieną. | ||
| + | |||
| + | <code html> | ||
| + | <canvas id=" | ||
| + | Your browser does not support the HTML5 canvas tag.</ | ||
| + | </ | ||
| <code javascript> | <code javascript> | ||
| var c = document.getElementById(" | var c = document.getElementById(" | ||
| var ctx = c.getContext(" | var ctx = c.getContext(" | ||
| - | // | ||
| - | // | ||
| - | // | ||
| - | var x = 50; | + | var x = 50, y = 50, step = 5; |
| - | var y = 50; | + | |
| - | var step = 5; | + | |
| var img = new Image; | var img = new Image; | ||
| - | img.src = " | + | img.src = " |
| - | ctx.drawImage(img, | + | media=javascript: |
| + | ctx.drawImage(img, | ||
| document.onkeydown = checkKey; | document.onkeydown = checkKey; | ||
| + | |||
| + | function refresh(){ | ||
| + | ctx.clearRect(0, | ||
| + | ctx.drawImage(img, | ||
| + | ctx.beginPath(); | ||
| + | ctx.arc(x, | ||
| + | ctx.stroke(); | ||
| + | } | ||
| function checkKey(e) { | function checkKey(e) { | ||
| - | | + | e = e || window.event; |
| - | | + | if (e.keyCode == ' |
| - | ctx.clearRect(0, | + | else if (e.keyCode == ' |
| - | ctx.drawImage(img, | + | else if (e.keyCode == ' |
| - | y = y - step; | + | else if (e.keyCode == ' |
| - | | + | |
| - | ctx.arc(x, | + | |
| - | ctx.stroke(); | + | |
| - | } | + | |
| - | | + | |
| - | ctx.clearRect(0, | + | |
| - | ctx.drawImage(img, | + | |
| - | y = y + step; | + | |
| - | | + | |
| - | ctx.arc(x, | + | |
| - | ctx.stroke(); | + | |
| - | } | + | |
| - | | + | |
| - | ctx.clearRect(0, | + | |
| - | ctx.drawImage(img, | + | |
| - | x = x - step; | + | |
| - | | + | |
| - | ctx.arc(x, | + | |
| - | ctx.stroke(); | + | |
| - | } | + | |
| - | | + | |
| - | ctx.clearRect(0, | + | |
| - | ctx.drawImage(img, | + | |
| - | x = x + step; | + | |
| - | ctx.beginPath(); | + | |
| - | ctx.arc(x, | + | |
| - | ctx.stroke(); | + | |
| - | } | + | |
| - | } | + | if (x < 0) x = 0; |
| + | if (y < 0) y = 0; | ||
| + | if (x > 600) x = 600; | ||
| + | if (y > 600) y = 600; | ||
| + | refresh(); | ||
| + | }</ | ||
| - | </ | + | |
| + | Labirintas: | ||
| + | |||
| + | {{: | ||
javascript/labirintas.1480946754.txt.gz · Last modified: by aistis
