簡體   English   中英

HTML 編碼幫助插入圖片

[英]HTML Coding help to insert a picture

我正在修改預先編寫的代碼以完成我的項目。 這是一個蛇吃蘋果並長大的游戲。 我需要用圖像替換游戲中的蘋果紅色斑點。 由於無法上傳整個代碼,我已將代碼包含到需要繪制蘋果的部分。 如果您需要完整代碼,請告訴我。

 <html> <head> <title>IT105 Presentation</title> <style> html, body { height: 100%; margin: 0; } body { background: white; display: flex; align-items: center; justify-content: center; } canvas { background-image: url(field.jpg); border: 1px solid black; } </style> </head> <body> <canvas width="400" height="400" id="game"></canvas> <script> var canvas = document.getElementById('game'); var context = canvas.getContext('2d'); var grid = 16; var count = 0; var snake = { x: 160, y: 160, // snake velocity. moves one grid length every frame in either the x or y direction dx: grid, dy: 0, // keep track of all grids the snake body occupies cells: [], // length of the snake. grows when eating an apple maxCells: 4 }; var apple = { x: 320, y: 320 }; // get random whole numbers in a specific range // @see https://stackoverflow.com/a/1527820/2124254 function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min)) + min; } // game loop function loop() { requestAnimationFrame(loop); // slow game loop to 15 fps instead of 60 (60/15 = 4) if (++count < 4) { return; } count = 0; context.clearRect(0, 0, canvas.width, canvas.height); // move snake by it's velocity snake.x += snake.dx; snake.y += snake.dy; // wrap snake position horizontally on edge of screen if (snake.x < 0) { snake.x = canvas.width - grid; } else if (snake.x >= canvas.width) { snake.x = 0; } // wrap snake position vertically on edge of screen if (snake.y < 0) { snake.y = canvas.height - grid; } else if (snake.y >= canvas.height) { snake.y = 0; } // keep track of where snake has been. front of the array is always the head snake.cells.unshift({ x: snake.x, y: snake.y }); // remove cells as we move away from them if (snake.cells.length > snake.maxCells) { snake.cells.pop(); } // draw apple context.fillStyle = 'red'; context.fillRect(apple.x, apple.y, grid - 1, grid - 1); // draw snake one cell at a time context.fillStyle = 'gold'; snake.cells.forEach(function(cell, index) { // drawing 1 px smaller than the grid creates a grid effect in the snake body so you can see how long it is context.fillRect(cell.x, cell.y, grid - 1, grid - 1); // snake ate apple if (cell.x === apple.x && cell.y === apple.y) { snake.maxCells++; // canvas is 400x400 which is 25x25 grids apple.x = getRandomInt(0, 25) * grid; apple.y = getRandomInt(0, 25) * grid; } // check collision with all cells after this one (modified bubble sort) for (var i = index + 1; i < snake.cells.length; i++) { // snake occupies same space as a body part. reset game if (cell.x === snake.cells[i].x && cell.y === snake.cells[i].y) { snake.x = 160; snake.y = 160; snake.cells = []; snake.maxCells = 4; snake.dx = grid; snake.dy = 0; apple.x = getRandomInt(0, 25) * grid; apple.y = getRandomInt(0, 25) * grid; } } }); } // listen to keyboard events to move the snake document.addEventListener('keydown', function(e) { // prevent snake from backtracking on itself by checking that it's // not already moving on the same axis (pressing left while moving // left won't do anything, and pressing right while moving left // shouldn't let you collide with your own body) // left arrow key if (e.which === 37 && snake.dx === 0) { snake.dx = -grid; snake.dy = 0; } // up arrow key else if (e.which === 38 && snake.dy === 0) { snake.dy = -grid; snake.dx = 0; } // right arrow key else if (e.which === 39 && snake.dx === 0) { snake.dx = grid; snake.dy = 0; } // down arrow key else if (e.which === 40 && snake.dy === 0) { snake.dy = grid; snake.dx = 0; } }); // start the game requestAnimationFrame(loop); </script> </body> </html>

我相信您正在尋找的是 JS 函數drawImage()

當前,您正在實現表單context.fillRect(x,y,width,height); fillRect( context.fillRect(x,y,width,height); 繪制一個“蘋果”,然后將填充顏色設置為“紅色”。

使用您已經擁有的x , y , width , height變量,您可以根據需要使圖像與“Apple”的大小相同,對應於公式context.fillRect(x,y,width,height); 是:

  • x : 蘋果.x
  • y : 蘋果.y
  • width :grid-1
  • height :grid-1

本質上,實現這種功能drawImage形式的void ctx.drawImage(image, dx, dy, dWidth, dHeight); 根據CanvasRenderingContext2D.drawImage()

所以你的Draw Apple部分將從此改變:

**// draw apple**
  context.fillStyle='red';
  context.fillRect(apple.x, apple.y, grid-1, grid-1);

對此:

**// draw apple -- or use image in place of apple**
  const img = new Image();
  img.src = 'http://writingexercises.co.uk/images2/randomimage/restaurant-view.jpg';
  context.drawImage(img, apple.x, apple.y, grid-1, grid-1);

我很難測試此解決方案,因為我沒有完整代碼或您可能引用的任何導入文件,但這應該為您提供有關如何進行的指南。

另請參閱我的參考資料:

CanvasRenderingContext2D.drawImage()

我將如何用圖像填充矩形?

HTML canvas fillRect() 方法

 <html> <head> <title>IT105 Presentation</title> <style> html, body { height: 100%; margin: 0; } body { background: white; display: flex; align-items: center; justify-content: center; } canvas { background-image: url(field.jpg); border: 1px solid black; } </style> </head> <body> <canvas width="400" height="400" id="game"></canvas> <script> var canvas = document.getElementById('game'); var context = canvas.getContext('2d'); var grid = 16; var count = 0; var snake = { x: 160, y: 160, **// snake velocity. moves one grid length every frame in either the x or y direction** dx: grid, dy: 0, **// keep track of all grids the snake body occupies** cells: [], **// length of the snake. grows when eating an apple** maxCells: 4 }; var apple = { x: 320, y: 320 }; **// get random whole numbers in a specific range** function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min)) + min; } **// game loop** function loop() { requestAnimationFrame(loop); **// slow game loop to 15 fps instead of 60 (60/15 = 4)** if (++count < 4) { return; } count = 0; context.clearRect(0,0,canvas.width,canvas.height); **// move snake by it's velocity** snake.x += snake.dx; snake.y += snake.dy; **// wrap snake position horizontally on edge of screen** if (snake.x < 0) { snake.x = canvas.width - grid; } else if (snake.x >= canvas.width) { snake.x = 0; } **// wrap snake position vertically on edge of screen** if (snake.y < 0) { snake.y = canvas.height - grid; } else if (snake.y >= canvas.height) { snake.y = 0; } **// keep track of where snake has been. front of the array is always the head** snake.cells.unshift({x: snake.x, y: snake.y}); **// remove cells as we move away from them** if (snake.cells.length > snake.maxCells) { snake.cells.pop(); } **// draw apple -- or use image in place of apple** const img = new Image(); img.src = 'http://writingexercises.co.uk/images2/randomimage/restaurant-view.jpg'; context.drawImage(img, apple.x, apple.y, grid-1, grid-1); **// draw snake one cell at a time** context.fillStyle = 'gold'; snake.cells.forEach(function(cell, index) {

讓我知道你是怎么辦的!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM