0

how do I make a point system in javascript?

ive made a ping pong game and i want to make it so when it hits the top/bottom of the screen you get a point and it shows your points but i cant manage to do that. (this is my code btw) var animate = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function(callback) { window.setTimeout(callback, 1000/60) }; var canvas = document.createElement('canvas'); var width = 400; var height = 600; canvas.width = width; canvas.height = height; var context = canvas.getContext('2d'); window.onload = function() { document.body.appendChild(canvas); animate(step); }; var step = function() { update(); render(); animate(step); }; var update = function() { }; 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; } Paddle.prototype.render = function() { context.fillStyle = "#000FFF"; context.fillRect(this.x, this.y, this.width, this.height); }; function Player() { this.paddle = new Paddle(175, 580, 50, 10); } function Computer() { this.paddle = new Paddle(175, 10, 50, 10); } Player.prototype.render = function() { this.paddle.render(); }; Computer.prototype.render = function() { this.paddle.render(); }; function Ball(x, y) { this.x = x; this.y = y; this.x_speed = 0; this.y_speed = 3; this.radius = 5; } Ball.prototype.render = function() { context.beginPath(); context.arc(this.x, this.y, this.radius, 2 * Math.PI, false); context.fillStyle = "#00ffd8"; context.fill(); }; var player = new Player(); var computer = new Computer(); var ball = new Ball(200, 300); var render = function() { context.fillStyle = "#ff7700"; context.fillRect(0, 0, width, height); player.render(); computer.render(); ball.render(); }; var update = function() { ball.update(); }; Ball.prototype.update = function() { this.x += this.x_speed; this.y += this.y_spe

30th Apr 2019, 8:09 PM
George
George - avatar
2 Respuestas
+ 1
youll need to create an ongoing loop that adds ++ to a variable everytime an if occurs.
30th Apr 2019, 10:21 PM
Jason Kennedy
0
Lol thx
1st May 2019, 12:32 AM
George
George - avatar