<Array>
<Array>
<Array>
<Array>
<Array>
<Array>
<Array>
<Array><Array>
<Array>
(function() {
var requestAnimationFrame = window.requestAnimationFrame ||
window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame
|| window.msRequestAnimationFrame;
window.requestAnimationFrame = requestAnimationFrame;
})();
var canvas = document.getElementById("canvas"),
ctx = canvas.getContext("2d"),
width = 600,
height = 400,
score1 = 0,
score2 = 0,
player1 = {
radius : 20,
x : width,
y : height/2,
speed: 100,
accel : 2,
mass :25,
velX: 0,
velY: 0,
friction : 0.9,
up : 38,
down : 40,
right : 39,
left : 37,
}
player2 = {
radius : 20,
x : 0,
y : height/2,
speed: 100,
accel : 2,
mass : 25,
velX: 0,
velY: 0,
friction : 0.9,
up : 87,
down : 83,
right : 68,
left : 65,
}
player3 = {
radius : 20,
x : width-100,
y : height/2,
speed: 100,
accel : 1,
mass :20,
velX: 0,
velY: 0,
friction : 0.9,
up : 104,
down : 101,
right : 102,
left : 100,
}
player4 = {
radius : 20,
x : 100,
y : height/2,
speed: 100,
accel : 1,
mass :20,
velX: 0,
velY: 0,
friction : 0.9,
up : 73,
down : 75,
right : 76,
left : 74,
}
ball = {
radius : 15,
x : width/2,
y : height/2,
mass : 20,
speed : 100,
accel : 1,
velX: 0,
velY: 0,
friction : 0.999,
}
player1Goal ={
x : width,
y : height/2,
radius: 40,
}
player2Goal ={
x : 0,
y : height/2,
radius: 40
}
keys = [];
//set canvas
canvas.width = width;
canvas.height = height;
// load sprites
var playerImg = document.getElementById("player_right");
var pokeBall = document.getElementById("pokeBall");
//create functions for the game
//rotate function
function rotate(x, y, sin, cos, reverse) {
return {
x: (reverse) ? (x * cos + y * sin) : (x * cos - y * sin),
y: (reverse) ? (y * cos - x * sin) : (y * cos + x * sin)
};
}
//other collision check
function checkCollision2 (ball0, ball1) {
var dx = (ball1.x - ball0.x),
dy = (ball1.y - ball0.y),
dist = Math.sqrt(dx * dx + dy * dy);
if (dist <Array>= width-ball.radius) {
ball.x = width-ball.radius;
ball.velX *= -1;
} else if (ball.x-ball.radius <Array>= height-ball.radius){
ball.y = height-ball.radius;
ball.velY *= -1;
} else if(ball.y-ball.radius <Array> -player1.speed) {
player1.velY-= player1.accel ;
}
}
if (keys[down]) {//down
if (player1.velY <Array> -player1.speed) {
player1.velX-=player1.accel ;
}
}
}
function update(){
// check keys
keysCheck(player1,player1.up,player1.down,player1.right,player1.left);
keysCheck(player2,player2.up,player2.down,player2.right,player2.left);
//keysCheck(player3,player3.up,player3.down,player3.right,player3.left);
//keysCheck(player4,player4.up,player4.down,player4.right,player4.left);
//check collision
checkCollision(player1,player2);
//checkCollision(player1,player3);
//checkCollision(player1,player4);
//checkCollision(player2,player3);
//checkCollision(player2,player4);
//checkCollision(player3,player4);
checkCollision(ball,player2);
checkCollision(player1,ball);
//checkCollision(player3,ball);
//checkCollision(player4,ball);
if (checkCollision2(ball,player1Goal)){
ball.x = width/2;
ball.y = height/2;
ball.velX = 0;
ball.velY = 0;
score2 += 1;
player1.x = width;
player1.y = height/2;
player2.x = 0;
player2.y = height/2;
}
if (checkCollision2(ball,player2Goal)){
ball.x = width/2;
ball.y = height/2;
ball.velX = 0;
ball.velY = 0;
score1 += 1;
player1.x = width;
player1.y = height/2;
player2.x = 0;
player2.y = height/2;
}
//move balls
move(player1);
move(player2);
//move(player3);
//move(player4);
move(ball);
//make sure balls stay in canvas
inBounds(player2);
inBounds(player1);
//inBounds(player3);
//inBounds(player4);
inBounds(ball);
//clear all stuff
ctx.clearRect(0,0,width,height);
//draw background
//draw score
ctx.fillStyle="black";
ctx.fill();
ctx.font = "40px Arial";
ctx.fillText(score2+":"+score1,(width/2-25),30);
//draw goals
ctx.beginPath();
ctx.arc(player1Goal.x,player1Goal.y,player1Goal.radius,0*Math.PI,2*Math.PI);
ctx.stroke();
ctx.fillStyle="black";
ctx.fill();
ctx.beginPath();
ctx.arc(player2Goal.x,player2Goal.y,player2Goal.radius,0*Math.PI,2*Math.PI);
ctx.stroke();
ctx.fillStyle="black";
ctx.fill();
//draw player1
ctx.beginPath();
ctx.arc(player1.x,player1.y,player1.radius,0*Math.PI,2*Math.PI);
ctx.stroke();
ctx.fillStyle="blue";
ctx.fill();
//draw player2
ctx.beginPath();
ctx.arc(player2.x,player2.y,player2.radius,0*Math.PI,2*Math.PI);
ctx.stroke();
ctx.fillStyle="red";
ctx.fill();
//draw player3
// ctx.beginPath();
// ctx.arc(player3.x,player3.y,player3.radius,0*Math.PI,2*Math.PI);
// ctx.stroke();
// ctx.fillStyle="red";
// ctx.fill();
//draw player4
// ctx.beginPath();
// ctx.arc(player4.x,player4.y,player4.radius,0*Math.PI,2*Math.PI);
// ctx.stroke();
// ctx.fillStyle="blue";
// ctx.fill();
//draw ball
ctx.beginPath();
ctx.arc(ball.x,ball.y,ball.radius,0*Math.PI,2*Math.PI);
ctx.stroke();
ctx.fillStyle="green";
ctx.fill();
requestAnimationFrame(update);
}
document.body.addEventListener("keydown", function(e) {
keys[e.keyCode] = true;
});
document.body.addEventListener("keyup", function(e) {
keys[e.keyCode] = false;
});
window.addEventListener("load",function(){
update();
});
<Array>
<Array>
<Array>