-
Notifications
You must be signed in to change notification settings - Fork 0
/
pong.js
133 lines (112 loc) · 3.83 KB
/
pong.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* Created by noam on 5/15/16.
*/
(function () {
window.onload=function () {
new Game();
}
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function( callback ){
window.setTimeout(callback, 1000 / 60);
};
}());
function Game() {
var i = 0;
console.log('oiioioiuip');
var canvas = initCanvas();
var ctx = canvas.getContext("2d");
var ball = new Ball();
var peddle = new Peddle(canvas.width - 20, 38, 40);
var peddle2 = new Peddle(20, 81, 65);
document.body.appendChild(canvas);
console.log(canvas);
requestAnimFrame(render);
function initCanvas() {
var width = 600;
var height = 400;
var canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
canvas.id = "canvas";
return canvas;
}
function drawBoard() {
ctx.fillStyle = "#ff00ff";
ctx.fillRect(0,0,canvas.width,canvas.height);
}
function clearCanvas() {
ctx.clearRect(0,0, canvas.width,canvas.height);
}
function render() {
clearCanvas();
drawBoard();
ball.drawBall();
peddle.drawPeddle();
peddle2.drawPeddle();
requestAnimFrame(render);
}
function Ball() {
var x = canvas.width/2;
var y = canvas.height/2; // starting point of ball
var speedX = 1;
var speedY = 1;
Ball.prototype.drawBall = function () {
if (x==peddle.x && y<peddle.y+peddle.height && y>peddle.y){
speedX = speedX*-1; //make the ball bounce off the peddle wall
}
if ( y > canvas.height || y < 0 ) {
//make the ball bounce off the upper and lower wall
speedY = speedY*-1;
}
// calculate new coordinates
x = x + speedX;
y = y + speedY;
// redraw ball in new coordinates
ctx.beginPath();
ctx.arc(x, y, 5, 0, Math.PI*2);
ctx.fillStyle = "#000000";
ctx.closePath();
ctx.fill();
}
}
function Peddle(startX,upKeyCode,downKeyCode) {
var self = this;
var keyUp = upKeyCode;
var keyDown= downKeyCode;
this.x = startX;
this.height = 60;
this.y = (canvas.height - this.height) / 2;
this.speedY = 2;
this.key = 0;
window.addEventListener("keydown", onDown);
window.addEventListener("keyup", onUp);
function onDown(event) {
if (event.keyCode==keyDown||event.keyCode==keyUp){
self.key = event.keyCode;
}
}
function onUp() {
if (event.keyCode==keyDown||event.keyCode==keyUp){
self.key = 0;
}
}
this.drawPeddle = function () {
self.speed = getMoveSpeed(self.key);
self.y = self.y + self.speed;
ctx.fillStyle = "black";
ctx.fillRect(self.x, self.y, 10, self.height);
}
function getMoveSpeed(keyCode){
if (keyCode==keyUp){ //handle up key
return self.speedY * -1;
} else if (keyCode==keyDown){ //handle down key
return self.speedY;
}
return 0;
}
}
}
}())