PSD-Tutorials.de
Forum für Design, Fotografie & Bildbearbeitung
Tutkit
Agentur
Hilfe
Kontakt
Start
Forum
Aktuelles
Besonderer Inhalt
Foren durchsuchen
Tutorials
News
Anmelden
Kostenlos registrieren
Aktuelles
Suche
Suche
Nur Titel durchsuchen
Von:
Menü
Anmelden
Kostenlos registrieren
App installieren
Installieren
JavaScript ist deaktiviert. Für eine bessere Darstellung aktiviere bitte JavaScript in deinem Browser, bevor du fortfährst.
Du verwendest einen veralteten Browser. Es ist möglich, dass diese oder andere Websites nicht korrekt angezeigt werden.
Du solltest ein Upgrade durchführen oder einen
alternativen Browser
verwenden.
Antworten auf deine Fragen:
Neues Thema erstellen
Start
Forum
Sonstiges
Webdesign, Webentwicklung & Programmierung
Webdesign: HTML/CSS, Responsive Design, Sass...
Minesweeper
Beitrag
<blockquote data-quote="keul3" data-source="post: 2511226" data-attributes="member: 53523"><p>Hi,</p><p></p><p>ich habe jetzt endlich mein erstes JS geschrieben. Da ich mich erstmal mit der Sprache vertraut machen wollte, ist etwas kleineres geworden, eine Adaption von meiner <a href="https://www.psd-tutorials.de/forum/threads/keules-flash-ecke.134309/page-4#post-2489676" target="_blank"><u>FlashVersion</u></a></p><p></p><p>Es ist soweit spielbar, aber Luxusfunktionen wie Schwierigkeitsgrad oder ein HUD fehlen. Auch ist das ganze nicht wirklich fancy</p><p></p><p><a href="http://keul3.com/minesweeper.html" target="_blank"><u>zum Spiel</u></a></p><p></p><p>Da das für mich alles noch neu ist, hätte ich eine Bitte und zwar: könntet ihr bitte den Code einmal überfliegen und mir grundlegende Fehler (Peformancefresser) aufzeigen?<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite1" alt=":)" title="Smile :)" loading="lazy" data-shortname=":)" /></p><p></p><p>Auf anonyme Funktionen habe ich komplett verzichtet. Ich habe jede Variable innerhalb von Funktionen und Schleifen declariert, was afaik nicht unbedingt nötig ist, aber da ich mit einer rekursiven Funktion inkl. nicht deklarierter Variabel ziemlich aufs Maul geflogen bin, bin ich danach auf Nummer sicher gegangen<img src="/styles/default/xenforo/smilies/biggrin.gif" class="smilie" loading="lazy" alt=":D" title="Big Grin :D" data-shortname=":D" /></p><p>das JS hab ich erstmal als ganzes gelassen, würde es aber normalerweise aufteilen, so das z.B. alle drawFunktionen ausgelagert werden.</p><p></p><p>Danke schonmal für eure Hilfe<img src="/styles/default/xenforo/smilies/zwinker.gif" class="smilie" loading="lazy" alt=";)" title="Wink ;)" data-shortname=";)" /></p><p></p><p>[code=JavaScript]// JavaScript Document</p><p>//Minesweeper</p><p></p><p>var xCount = 30;</p><p>var yCount = 15;</p><p>var cellSize = 30;</p><p>var mineRatio = .2;</p><p>var canvasRect;</p><p>var canvas;</p><p>var cells;</p><p>var oldCell;</p><p>var markerKeyDown;</p><p>var gameOver;</p><p>var cellsToClear;</p><p></p><p>function init()</p><p>{</p><p> createCanvas();</p><p> window.addEventListener("keydown", keyDownHandler);</p><p> window.addEventListener("keyup", keyUpHandler);</p><p> window.addEventListener("resize", resizeHandler);</p><p> createField()</p><p>}</p><p></p><p></p><p>function createField()</p><p>{</p><p> markerKeyDown = false;</p><p> gameOver = false;</p><p></p><p> cells = createCells(xCount, yCount); //[x][y]</p><p></p><p> if (mineRatio > 1) mineRatio = 1;</p><p> if (mineRatio < 0) mineRatio = 0;</p><p> var cellCount = xCount * yCount;</p><p> var mineCount = Math.ceil(cellCount * mineRatio);</p><p> cellsToClear = cellCount - mineCount;</p><p> var mines = getMines(cellCount, mineCount);</p><p> assignMinesToCells(mines, cells);</p><p> countSurroundingMines(cells);</p><p></p><p> for (var i = 0; i < cells.length; i++)</p><p> {</p><p> for (var j = 0; j < cells[i].length; j++) drawCover(cells[i][j]);</p><p> }</p><p>}</p><p></p><p>function createCells(xC, yC)</p><p>{</p><p> var cells = new Array();</p><p></p><p> for (var i = 0; i < xCount; i++)</p><p> {</p><p> var temp = new Array();</p><p></p><p> for (j = 0; j < yCount; j++)</p><p> {</p><p> var cell = new Object();</p><p> cell.idX = i;</p><p> cell.idY = j;</p><p> cell.x = i * cellSize;</p><p> cell.y = j * cellSize;</p><p> cell.revealed = false;</p><p> cell.revealedSurroundings = false;</p><p> cell.marked = false;</p><p> </p><p> temp.push(cell);</p><p> }</p><p> cells.push(temp);</p><p> }</p><p></p><p> return cells;</p><p>}</p><p></p><p></p><p>function getMines(cellCount, mineCount)</p><p>{</p><p> var numbers = new Array();</p><p> var mines = new Array();</p><p> for (var i = 0; i < cellCount; i++)</p><p> {</p><p> numbers.push(i);</p><p> mines.push(false);</p><p> }</p><p> </p><p> for (var j = 0; j < mineCount; j++)</p><p> {</p><p> var rn = Math.floor( Math.random() * numbers.length ); //randomNumber</p><p> mines[ numbers[rn] ] = true;</p><p> numbers.splice(rn, 1);</p><p> }</p><p> </p><p> return mines;</p><p>}</p><p></p><p>function assignMinesToCells(m, c)</p><p>{</p><p> var a = 0;</p><p> </p><p> for (var i = 0; i < c.length; i++)</p><p> {</p><p> for (var j = 0; j < c[i].length; j++)</p><p> {</p><p> c[i][j].mine = m[a];</p><p> a++;</p><p> }</p><p> }</p><p>}</p><p></p><p>function countSurroundingMines(c)</p><p>{</p><p> for (var i = 0; i < c.length; i++)</p><p> {</p><p> for (var j = 0; j < c[i].length; j++)</p><p> {</p><p> var a = 0;</p><p> var sCells = [</p><p> [i - 1, j - 1], //top-left</p><p> [i , j - 1], //top</p><p> [i + 1, j - 1], //top-right</p><p> [i - 1, j ], //left</p><p> [i + 1, j ], //right</p><p> [i - 1, j + 1], //bottom-left</p><p> [i , j + 1], //bottom</p><p> [i + 1, j + 1] //bottom-right</p><p> ];</p><p> </p><p> for (var k = 0; k < sCells.length; k++)</p><p> {</p><p> if ( hasMine(sCells[k][0], sCells[k][1], c) ) a++;</p><p> }</p><p> c[i][j].minesAround = a;</p><p> }</p><p> }</p><p>}</p><p></p><p>function hasMine(x, y, c)</p><p>{</p><p> var b = false;</p><p> </p><p> if (x >= 0 && x < xCount && y >= 0 && y < yCount) b = c[x][y].mine;</p><p> </p><p> return b;</p><p>}</p><p></p><p></p><p>function createCanvas()</p><p>{</p><p> var w = xCount * cellSize;</p><p> var h = yCount * cellSize;</p><p></p><p> canvas = document.createElement("CANVAS");</p><p></p><p> var attWidth = document.createAttribute("width");</p><p> attWidth.value = w.toString();</p><p> canvas.setAttributeNode(attWidth);</p><p></p><p> var attHeight = document.createAttribute("height");</p><p> attHeight.value = h.toString();</p><p> canvas.setAttributeNode(attHeight);</p><p> document.body.appendChild(canvas);</p><p></p><p> canvas.addEventListener("click", clickHandler);</p><p> canvas.addEventListener("mousemove", mouseMoveHandler);</p><p></p><p> elementToCenterOfWindow(canvas);</p><p> canvasRect = canvas.getBoundingClientRect();</p><p>}</p><p></p><p>function elementToCenterOfWindow(element)</p><p>{</p><p> element.style.position = "absolute"</p><p> element.style.left = (window.innerWidth - element.width)/2 + "px";</p><p> element.style.top = (window.innerHeight - element.height)/2 + "px";</p><p>}</p><p></p><p></p><p>function drawCover(cell)</p><p>{</p><p> var lineWidth = 2;</p><p></p><p> var ctx = canvas.getContext("2d");</p><p> ctx.beginPath();</p><p> ctx.rect(cell.x + lineWidth/2, cell.y + lineWidth/2, cellSize - lineWidth, cellSize - lineWidth);</p><p> ctx.fillStyle = "#009900";</p><p> ctx.fill();</p><p> ctx.lineWidth = lineWidth;</p><p> ctx.strokeStyle = "#006600";</p><p> ctx.stroke();</p><p> ctx.closePath();</p><p>}</p><p></p><p>function drawCoverHighlight(cell)</p><p>{</p><p> var lineWidth = 2;</p><p></p><p> var ctx = canvas.getContext("2d");</p><p> ctx.beginPath();</p><p> ctx.rect(cell.x + lineWidth/2, cell.y + lineWidth/2, cellSize - lineWidth, cellSize - lineWidth);</p><p> ctx.fillStyle = "#00bb00";</p><p> ctx.fill();</p><p> ctx.lineWidth = lineWidth;</p><p> ctx.strokeStyle = "#009900";</p><p> ctx.stroke();</p><p> ctx.closePath();</p><p>}</p><p></p><p></p><p>function drawCell(cell)</p><p>{</p><p> var lineWidth = 2;</p><p></p><p> var ctx = canvas.getContext("2d");</p><p> ctx.beginPath();</p><p> ctx.rect(cell.x + lineWidth/2, cell.y + lineWidth/2, cellSize - lineWidth, cellSize - lineWidth);</p><p> ctx.fillStyle = "#ffff00";</p><p> ctx.fill();</p><p> ctx.lineWidth = lineWidth;</p><p> ctx.strokeStyle = "#aaaa00";</p><p> ctx.stroke();</p><p> ctx.closePath();</p><p></p><p> if (cell.mine)</p><p> {</p><p> var xCenter = cell.x + cellSize/2;</p><p> var yCenter = cell.y + cellSize/2;</p><p> var radius = cellSize/2 * .6;</p><p> ctx.beginPath();</p><p> ctx.arc(xCenter, yCenter, radius, 0, 2 * Math.PI, false);</p><p> ctx.fillStyle = "#000000";</p><p> ctx.fill();</p><p> ctx.closePath();</p><p> }</p><p> else if (cell.minesAround)</p><p> {</p><p> var fontSize = cellSize * .4;</p><p> var text = cell.minesAround.toString();</p><p> ctx.font = fontSize.toString() + "pt Calibri";</p><p> ctx.textAlign = "center";</p><p> ctx.fillStyle = "#000000";</p><p> textWidth = ctx.measureText(text).width;</p><p> var d = (cellSize - fontSize)/2;</p><p> var x = cell.x + cellSize/2;</p><p> var y = cell.y + cellSize - d;</p><p> ctx.fillText(text, x, y);</p><p> }</p><p>}</p><p></p><p></p><p>function drawMarker(cell)</p><p>{</p><p> var ctx = canvas.getContext("2d");</p><p> var xCenter = cell.x + cellSize/2;</p><p> var yCenter = cell.y + cellSize/2;</p><p> var radius = cellSize/2 * .8;</p><p> ctx.beginPath();</p><p> ctx.arc(xCenter, yCenter, radius, 0, 2 * Math.PI, false);</p><p> ctx.fillStyle = "#ff0000";</p><p> ctx.fill();</p><p> ctx.closePath();</p><p>}</p><p></p><p></p><p>function clickHandler(event)</p><p>{</p><p> if (!gameOver)</p><p> {</p><p> var cell = getCellByMouse(event);</p><p> if (!cell.revealed)</p><p> {</p><p> if (markerKeyDown) markCell(cell)</p><p> else revealCell(cell);</p><p> }</p><p> }</p><p> else restart();</p><p>}</p><p></p><p>function markCell(cell)</p><p>{</p><p> if (cell.marked)</p><p> {</p><p> drawCover(cell);</p><p> cell.marked = false;</p><p> }</p><p> else</p><p> {</p><p> drawCover(cell);</p><p> drawMarker(cell);</p><p> cell.marked = true;</p><p> }</p><p>}</p><p></p><p>function getCellByMouse(e)</p><p>{</p><p> var x = e.clientX - canvasRect.left;</p><p> var y = e.clientY - canvasRect.top;</p><p></p><p> x = Math.floor(x / cellSize);</p><p> y = Math.floor(y / cellSize);</p><p></p><p> var cell = cells[x][y];</p><p></p><p> return cell;</p><p>}</p><p></p><p>function revealCell(cell)</p><p>{</p><p> if (!cell.marked)</p><p> {</p><p> cell.revealed = true;</p><p> drawCell(cell);</p><p> cellsToClear --;</p><p> if (!cellsToClear) won();</p><p> if (!cell.minesAround) revealZeroSurroundings( cell );</p><p> if (cell.mine) boom();</p><p> }</p><p></p><p>}</p><p></p><p>function revealZeroSurroundings(c)</p><p>{</p><p> var sCells = [</p><p> [c.idX - 1, c.idY - 1], //top-left</p><p> [c.idX , c.idY - 1], //top</p><p> [c.idX + 1, c.idY - 1], //top-right</p><p> [c.idX - 1, c.idY ], //left</p><p> [c.idX + 1, c.idY ], //right</p><p> [c.idX - 1, c.idY + 1], //bottom-left</p><p> [c.idX , c.idY + 1], //bottom</p><p> [c.idX + 1, c.idY + 1] //bottom-right</p><p> ];</p><p></p><p></p><p> for (var i = 0; i < sCells.length; i++)</p><p> {</p><p> var x = sCells[i][0];</p><p> var y = sCells[i][1];</p><p> </p><p> if (x >= 0 && x < xCount && y >= 0 && y < yCount)</p><p> {</p><p> if (!cells[x][y].revealed) revealCell(cells[x][y])</p><p> </p><p> if (!cells[x][y].minesAround && !cells[x][y].revealedSurroundings)</p><p> {</p><p> cells[x][y].revealedSurroundings = true;//um einen Stapelüberlauf zu verhindern</p><p> revealZeroSurroundings( cells[x][y] )</p><p> }</p><p> }</p><p> }</p><p>}</p><p></p><p>function mouseMoveHandler(event)</p><p>{</p><p> if (!gameOver)</p><p> {</p><p> var cell = getCellByMouse(event);</p><p> if (cell)</p><p> {</p><p> if (!cell.revealed)</p><p> {</p><p> if (cell != oldCell)</p><p> {</p><p> if (oldCell)//am Anfang ist oldCell null</p><p> {</p><p> if (!oldCell.revealed && !oldCell.marked) drawCover(oldCell);</p><p> }</p><p> if (!cell.marked) drawCoverHighlight(cell);</p><p> oldCell = cell;</p><p> }</p><p> }</p><p> }</p><p> }</p><p>}</p><p></p><p>function boom()</p><p>{</p><p> gameOver = true;</p><p> drawBanner("BOOOOOOOM");</p><p>}</p><p></p><p>function won()</p><p>{</p><p> gameOver = true;</p><p> drawBanner("TOTAL CLEARANCE");</p><p>}</p><p></p><p></p><p>function drawBanner(txt)</p><p>{</p><p> var ctx = canvas.getContext("2d");</p><p></p><p> var bannerWidth = .9;</p><p> var bannerHeight = .6;</p><p> var w = canvas.width * bannerWidth;</p><p> var h = canvas.height * bannerHeight;</p><p></p><p> ctx.beginPath();</p><p> ctx.rect( (canvas.width - w)/2, (canvas.height - h)/2, w, h );</p><p> ctx.fillStyle = "#ffffff";</p><p> ctx.fill();</p><p> ctx.lineWidth = 2;</p><p> ctx.strokeStyle = "#550000";</p><p> ctx.stroke();</p><p> ctx.closePath();</p><p></p><p> var fontSize = h * .2;</p><p></p><p> var text = txt;</p><p> ctx.font = fontSize.toString() + "pt Calibri";</p><p> ctx.textAlign = "center";</p><p> var textWidth = ctx.measureText(text).width;</p><p> if (textWidth > w) //textwidth reduzieren auf bannerwidth</p><p> {</p><p> var ratio = w/textWidth;</p><p> fontSize *= ratio;</p><p> ctx.font = fontSize.toString() + "pt Calibri";</p><p> }</p><p> var textHeight = fontSize;</p><p> ctx.fillStyle = "#550000";</p><p> var d = (cellSize - fontSize)/2;</p><p> var x = canvas.width/2;</p><p> var y = canvas.height/2 + fontSize/2;</p><p> ctx.fillText(text, x, y);</p><p></p><p> fontSize *= .5;</p><p> text = "click on canvas to restart";</p><p> ctx.font = fontSize.toString() + "pt Calibri";</p><p> ctx.textAlign = "center";</p><p> ctx.fillStyle = "#550000";</p><p> x = canvas.width/2;</p><p> y = canvas.height/2 + fontSize/2 + textHeight;</p><p> ctx.fillText(text, x, y);</p><p>}</p><p></p><p>function restart()</p><p>{</p><p> canvas.getContext("2d").clearRect(0, 0, canvas.width, canvas.height);</p><p> createField();</p><p>}</p><p></p><p>function keyDownHandler(event) { if (event.ctrlKey) markerKeyDown = true; }</p><p>function keyUpHandler(event) { if (!event.ctrlKey && markerKeyDown) markerKeyDown = false; }</p><p>function resizeHandler(event) { elementToCenterOfWindow(canvas); canvasRect = canvas.getBoundingClientRect();}[/code]</p></blockquote><p></p>
[QUOTE="keul3, post: 2511226, member: 53523"] Hi, ich habe jetzt endlich mein erstes JS geschrieben. Da ich mich erstmal mit der Sprache vertraut machen wollte, ist etwas kleineres geworden, eine Adaption von meiner [URL='https://www.psd-tutorials.de/forum/threads/keules-flash-ecke.134309/page-4#post-2489676'][U]FlashVersion[/U][/URL] Es ist soweit spielbar, aber Luxusfunktionen wie Schwierigkeitsgrad oder ein HUD fehlen. Auch ist das ganze nicht wirklich fancy [URL='http://keul3.com/minesweeper.html'][U]zum Spiel[/U][/URL] Da das für mich alles noch neu ist, hätte ich eine Bitte und zwar: könntet ihr bitte den Code einmal überfliegen und mir grundlegende Fehler (Peformancefresser) aufzeigen?:) Auf anonyme Funktionen habe ich komplett verzichtet. Ich habe jede Variable innerhalb von Funktionen und Schleifen declariert, was afaik nicht unbedingt nötig ist, aber da ich mit einer rekursiven Funktion inkl. nicht deklarierter Variabel ziemlich aufs Maul geflogen bin, bin ich danach auf Nummer sicher gegangen:D das JS hab ich erstmal als ganzes gelassen, würde es aber normalerweise aufteilen, so das z.B. alle drawFunktionen ausgelagert werden. Danke schonmal für eure Hilfe;) [code=JavaScript]// JavaScript Document //Minesweeper var xCount = 30; var yCount = 15; var cellSize = 30; var mineRatio = .2; var canvasRect; var canvas; var cells; var oldCell; var markerKeyDown; var gameOver; var cellsToClear; function init() { createCanvas(); window.addEventListener("keydown", keyDownHandler); window.addEventListener("keyup", keyUpHandler); window.addEventListener("resize", resizeHandler); createField() } function createField() { markerKeyDown = false; gameOver = false; cells = createCells(xCount, yCount); //[x][y] if (mineRatio > 1) mineRatio = 1; if (mineRatio < 0) mineRatio = 0; var cellCount = xCount * yCount; var mineCount = Math.ceil(cellCount * mineRatio); cellsToClear = cellCount - mineCount; var mines = getMines(cellCount, mineCount); assignMinesToCells(mines, cells); countSurroundingMines(cells); for (var i = 0; i < cells.length; i++) { for (var j = 0; j < cells[i].length; j++) drawCover(cells[i][j]); } } function createCells(xC, yC) { var cells = new Array(); for (var i = 0; i < xCount; i++) { var temp = new Array(); for (j = 0; j < yCount; j++) { var cell = new Object(); cell.idX = i; cell.idY = j; cell.x = i * cellSize; cell.y = j * cellSize; cell.revealed = false; cell.revealedSurroundings = false; cell.marked = false; temp.push(cell); } cells.push(temp); } return cells; } function getMines(cellCount, mineCount) { var numbers = new Array(); var mines = new Array(); for (var i = 0; i < cellCount; i++) { numbers.push(i); mines.push(false); } for (var j = 0; j < mineCount; j++) { var rn = Math.floor( Math.random() * numbers.length ); //randomNumber mines[ numbers[rn] ] = true; numbers.splice(rn, 1); } return mines; } function assignMinesToCells(m, c) { var a = 0; for (var i = 0; i < c.length; i++) { for (var j = 0; j < c[i].length; j++) { c[i][j].mine = m[a]; a++; } } } function countSurroundingMines(c) { for (var i = 0; i < c.length; i++) { for (var j = 0; j < c[i].length; j++) { var a = 0; var sCells = [ [i - 1, j - 1], //top-left [i , j - 1], //top [i + 1, j - 1], //top-right [i - 1, j ], //left [i + 1, j ], //right [i - 1, j + 1], //bottom-left [i , j + 1], //bottom [i + 1, j + 1] //bottom-right ]; for (var k = 0; k < sCells.length; k++) { if ( hasMine(sCells[k][0], sCells[k][1], c) ) a++; } c[i][j].minesAround = a; } } } function hasMine(x, y, c) { var b = false; if (x >= 0 && x < xCount && y >= 0 && y < yCount) b = c[x][y].mine; return b; } function createCanvas() { var w = xCount * cellSize; var h = yCount * cellSize; canvas = document.createElement("CANVAS"); var attWidth = document.createAttribute("width"); attWidth.value = w.toString(); canvas.setAttributeNode(attWidth); var attHeight = document.createAttribute("height"); attHeight.value = h.toString(); canvas.setAttributeNode(attHeight); document.body.appendChild(canvas); canvas.addEventListener("click", clickHandler); canvas.addEventListener("mousemove", mouseMoveHandler); elementToCenterOfWindow(canvas); canvasRect = canvas.getBoundingClientRect(); } function elementToCenterOfWindow(element) { element.style.position = "absolute" element.style.left = (window.innerWidth - element.width)/2 + "px"; element.style.top = (window.innerHeight - element.height)/2 + "px"; } function drawCover(cell) { var lineWidth = 2; var ctx = canvas.getContext("2d"); ctx.beginPath(); ctx.rect(cell.x + lineWidth/2, cell.y + lineWidth/2, cellSize - lineWidth, cellSize - lineWidth); ctx.fillStyle = "#009900"; ctx.fill(); ctx.lineWidth = lineWidth; ctx.strokeStyle = "#006600"; ctx.stroke(); ctx.closePath(); } function drawCoverHighlight(cell) { var lineWidth = 2; var ctx = canvas.getContext("2d"); ctx.beginPath(); ctx.rect(cell.x + lineWidth/2, cell.y + lineWidth/2, cellSize - lineWidth, cellSize - lineWidth); ctx.fillStyle = "#00bb00"; ctx.fill(); ctx.lineWidth = lineWidth; ctx.strokeStyle = "#009900"; ctx.stroke(); ctx.closePath(); } function drawCell(cell) { var lineWidth = 2; var ctx = canvas.getContext("2d"); ctx.beginPath(); ctx.rect(cell.x + lineWidth/2, cell.y + lineWidth/2, cellSize - lineWidth, cellSize - lineWidth); ctx.fillStyle = "#ffff00"; ctx.fill(); ctx.lineWidth = lineWidth; ctx.strokeStyle = "#aaaa00"; ctx.stroke(); ctx.closePath(); if (cell.mine) { var xCenter = cell.x + cellSize/2; var yCenter = cell.y + cellSize/2; var radius = cellSize/2 * .6; ctx.beginPath(); ctx.arc(xCenter, yCenter, radius, 0, 2 * Math.PI, false); ctx.fillStyle = "#000000"; ctx.fill(); ctx.closePath(); } else if (cell.minesAround) { var fontSize = cellSize * .4; var text = cell.minesAround.toString(); ctx.font = fontSize.toString() + "pt Calibri"; ctx.textAlign = "center"; ctx.fillStyle = "#000000"; textWidth = ctx.measureText(text).width; var d = (cellSize - fontSize)/2; var x = cell.x + cellSize/2; var y = cell.y + cellSize - d; ctx.fillText(text, x, y); } } function drawMarker(cell) { var ctx = canvas.getContext("2d"); var xCenter = cell.x + cellSize/2; var yCenter = cell.y + cellSize/2; var radius = cellSize/2 * .8; ctx.beginPath(); ctx.arc(xCenter, yCenter, radius, 0, 2 * Math.PI, false); ctx.fillStyle = "#ff0000"; ctx.fill(); ctx.closePath(); } function clickHandler(event) { if (!gameOver) { var cell = getCellByMouse(event); if (!cell.revealed) { if (markerKeyDown) markCell(cell) else revealCell(cell); } } else restart(); } function markCell(cell) { if (cell.marked) { drawCover(cell); cell.marked = false; } else { drawCover(cell); drawMarker(cell); cell.marked = true; } } function getCellByMouse(e) { var x = e.clientX - canvasRect.left; var y = e.clientY - canvasRect.top; x = Math.floor(x / cellSize); y = Math.floor(y / cellSize); var cell = cells[x][y]; return cell; } function revealCell(cell) { if (!cell.marked) { cell.revealed = true; drawCell(cell); cellsToClear --; if (!cellsToClear) won(); if (!cell.minesAround) revealZeroSurroundings( cell ); if (cell.mine) boom(); } } function revealZeroSurroundings(c) { var sCells = [ [c.idX - 1, c.idY - 1], //top-left [c.idX , c.idY - 1], //top [c.idX + 1, c.idY - 1], //top-right [c.idX - 1, c.idY ], //left [c.idX + 1, c.idY ], //right [c.idX - 1, c.idY + 1], //bottom-left [c.idX , c.idY + 1], //bottom [c.idX + 1, c.idY + 1] //bottom-right ]; for (var i = 0; i < sCells.length; i++) { var x = sCells[i][0]; var y = sCells[i][1]; if (x >= 0 && x < xCount && y >= 0 && y < yCount) { if (!cells[x][y].revealed) revealCell(cells[x][y]) if (!cells[x][y].minesAround && !cells[x][y].revealedSurroundings) { cells[x][y].revealedSurroundings = true;//um einen Stapelüberlauf zu verhindern revealZeroSurroundings( cells[x][y] ) } } } } function mouseMoveHandler(event) { if (!gameOver) { var cell = getCellByMouse(event); if (cell) { if (!cell.revealed) { if (cell != oldCell) { if (oldCell)//am Anfang ist oldCell null { if (!oldCell.revealed && !oldCell.marked) drawCover(oldCell); } if (!cell.marked) drawCoverHighlight(cell); oldCell = cell; } } } } } function boom() { gameOver = true; drawBanner("BOOOOOOOM"); } function won() { gameOver = true; drawBanner("TOTAL CLEARANCE"); } function drawBanner(txt) { var ctx = canvas.getContext("2d"); var bannerWidth = .9; var bannerHeight = .6; var w = canvas.width * bannerWidth; var h = canvas.height * bannerHeight; ctx.beginPath(); ctx.rect( (canvas.width - w)/2, (canvas.height - h)/2, w, h ); ctx.fillStyle = "#ffffff"; ctx.fill(); ctx.lineWidth = 2; ctx.strokeStyle = "#550000"; ctx.stroke(); ctx.closePath(); var fontSize = h * .2; var text = txt; ctx.font = fontSize.toString() + "pt Calibri"; ctx.textAlign = "center"; var textWidth = ctx.measureText(text).width; if (textWidth > w) //textwidth reduzieren auf bannerwidth { var ratio = w/textWidth; fontSize *= ratio; ctx.font = fontSize.toString() + "pt Calibri"; } var textHeight = fontSize; ctx.fillStyle = "#550000"; var d = (cellSize - fontSize)/2; var x = canvas.width/2; var y = canvas.height/2 + fontSize/2; ctx.fillText(text, x, y); fontSize *= .5; text = "click on canvas to restart"; ctx.font = fontSize.toString() + "pt Calibri"; ctx.textAlign = "center"; ctx.fillStyle = "#550000"; x = canvas.width/2; y = canvas.height/2 + fontSize/2 + textHeight; ctx.fillText(text, x, y); } function restart() { canvas.getContext("2d").clearRect(0, 0, canvas.width, canvas.height); createField(); } function keyDownHandler(event) { if (event.ctrlKey) markerKeyDown = true; } function keyUpHandler(event) { if (!event.ctrlKey && markerKeyDown) markerKeyDown = false; } function resizeHandler(event) { elementToCenterOfWindow(canvas); canvasRect = canvas.getBoundingClientRect();}[/code] [/QUOTE]
Bilder bitte
hier hochladen
und danach über das Bild-Icon (Direktlink vorher kopieren) platzieren.
Zitate einfügen…
Authentifizierung
Der grüne Frosch hüpft über die Hügel an den Bäumen vorbei in die Höhle. Bitte nenne das zweite Wort!
Antworten
Start
Forum
Sonstiges
Webdesign, Webentwicklung & Programmierung
Webdesign: HTML/CSS, Responsive Design, Sass...
Minesweeper
Oben