HTML5移动对象

时间:2017-01-04 18:03:10

标签: javascript html5 html5-canvas

我已经阅读了很多帖子,并经历过HTML5和画布上的几个教程,但到目前为止,我无法复制我的确切问题。如果已经有答案,请指出我正确的方向。

我的最终目标是制作简单的Pong游戏。我使用JavaScript绘制了基本对象,现在我试图让播放器(左)移动。我使用当前代码遇到的问题是,它不是移动桨,而是填充它前往的区域。通过各种试验和适应和尝试不同方法的错误我不认为桨是拉长的(向高度添加像素),但似乎正在创建一个新的桨对象而不是被移动的对象。

我一遍又一遍地看着它(你们不是第一次努力),但似乎无法弄清楚发生了什么。任何帮助将不胜感激。

// Requests a callback 60 times per second from browser
var animate = window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame    ||
        window.oRequestAnimationFrame      ||
        window.msRequestAnimationFrame     ||
    function(callback) { window.setTimeout(callback, 1000/60) };

// Get canvas and set context
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");

context.fillStyle = "white";

// Settle variables for canvas width and height
var canvas_width = 500;
var canvas_height = 400;

// Set varaibles for paddle width and height
var paddle_width = 15;
var paddle_height = 75;

// Initializes variables
var playerScore = 0;
var computerScore = 0;
var player = new Player();
var computer = new Computer();
var ball = new Ball((canvas_width/2),(canvas_height/2));

// Renders the pong table
var render = function() {
  player.render();
  computer.render();
  ball.render();
};

var update = function() {
  player.update();
};

// Callback for animate function
var step = function() {
  update();
  render();
  animate(step);
};

// Creates paddle object to build player and computer objects
function Paddle(x, y, width, height) {
  this.x = x;
  this.y = y; 
  this.width = width;
  this.height = height;
  this.x_speed = 0;
  this.y_speed = 0;
};

function Player() {
  this.paddle = new Paddle(1, ((canvas_height/2) - (paddle_height/2)), paddle_width, paddle_height);
};

function Computer() {
  this.paddle = new Paddle((canvas_width - paddle_width - 1), ((canvas_height/2) - (paddle_height/2)), paddle_width, paddle_height);
};

// Creates ball object
function Ball(x, y) {
  this.x = x;
  this.y = y;
  this.radius = 10;
};

// Adds render functions to objects allowing them to be drawn on canvas
Ball.prototype.render = function() {
  context.beginPath();
  context.arc(this.x, this.y, this.radius, Math.PI * 2, false);
  context.fillStyle = "white";
  context.fill();
  context.closePath();
};

Paddle.prototype.render = function() {
  context.fillStyle = "white";
  context.fillRect(this.x, this.y, this.width, this.height);
};

Player.prototype.render = function() {
  this.paddle.render();
};

// Appends a move method to Paddle prototype
Paddle.prototype.move = function(x, y) {
  this.y += y;
  this.y_speed = y;
};

// Updates the location of the player paddle
Player.prototype.update = function() {
  for(var key in keysDown) {
    var value = Number(key);
    if(value == 38) {
      this.paddle.move(0, -4);
    } else if (value == 40) {
      this.paddle.move(0, 4);
    } else {
      this.paddle.move(0, 0);
    }
  }
};

Computer.prototype.render = function() {
  this.paddle.render();
};

// Draws center diving line
context.strokeStyle = "white";
context.setLineDash([5, 3]);
context.beginPath();
context.moveTo((canvas_width/2), 0);
context.lineTo((canvas_width/2), canvas_height);
context.stroke();
context.closePath();

// Draws score on canvas
context.font = "40px Arial";
context.fillText('0', (canvas_width * .23), 50);
context.fillText('0', (canvas_width * .73), 50);

window.onload = function() {
  animate(step);
};

var keysDown = {};

window.addEventListener("keydown", function(event) {
  keysDown[event.keyCode] = true;
});

window.addEventListener("keyup", function(event) {
  delete keysDown[event.keyCode];
});

道歉:我剪切了html / css代码并打算粘贴它,但忘了。

pong.html:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Pong</title>
    <link rel="stylesheet" href="style.css">
  </head>
  <body>
    <canvas id="canvas" width="500" height="400"></canvas>

    <script src="main.js"></script>
  </body>
</html>

的style.css:

#canvas {
  background-color: black;
}

1 个答案:

答案 0 :(得分:2)

画布本身没有“对象”,它只是一个位图,你在它上面绘制的任何东西只会改变某些像素的颜色,使它看起来像是在那里已经存在的“顶部”,但它没有甚至那样做。它只是翻转像素颜色。

我没有看到任何代码为您的下一帧“重置”画布,所以您只是通过使用桨的颜色着色不同的像素而不使用原始像素重新着色来绘制不同高度值的相同球拍背景颜色。

此处最简单的解决方案是在context.clearRect(0, 0, canvas.width, canvas.height);开头添加render()

var render = function() {
  // clear the canvas so we can draw a new frame
  context.clearRect(0,0,canvas.width,canvas.height);
  // draw the frame content to the bitmap
  player.render();
  computer.render();
  ball.render();
};

请注意,这表明您还需要每帧绘制得分和中心线。要么是这样,要么你需要确保你的桨(和你的球)首先恢复旧位置的背景颜色,然后再将它们放在新的位置上,这需要更多的代码。