-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
146 lines (118 loc) · 4.87 KB
/
index.html
File metadata and controls
146 lines (118 loc) · 4.87 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<html>
<h1>Pong</h1>
<canvas id='game' width="800" height="600"></canvas>
<script>
function drawEverything(){
canvasContext.fillStyle = 'black' ;
canvasContext.fillRect(0,0,canvas.width , canvas.height);
// PADDLE
var paddleSize = 100 ;
canvasContext.fillStyle = 'white' ;
canvasContext.fillRect(1 ,paddle1.position , 9 , paddle1.size);
canvasContext.fillRect(canvas.width - 10 , paddle2.position, 9 , paddle2.size);
// BALL
canvasContext.fillStyle = 'red';
canvasContext.beginPath();
canvasContext.arc(ball.x , ball.y , ball.size , 0 , Math.PI*2 , true)
canvasContext.fill();
// SCORE
canvasContext.fillStyle = 'white' ;
canvasContext.font = "30px Arial";
canvasContext.fillText(paddle1.score , 100 , 100);
canvasContext.fillText(paddle2.score , canvas.width - 100 , 100);
}
function calculateMousePosition(evt){
var rect = canvas.getBoundingClientRect() ;
var root = document.documentElement;
var mouseY = evt.clientY - rect.top - root.scrollTop ;
var mouseX = evt.clientX- rect.left - root.scrollLeft ;
return {
x:mouseX,
y:mouseY
};
}
var Ball = function(size , speedX , speedY){
this.size = size ;
this.x = canvas.width / 2 ;
this.y = canvas.height / 2 ;
this.speedX = speedX ;
this.speedY = speedY ;
this.move = function(){
if(this.x < 0){
if(this.y > paddle1.position && this.y < paddle1.position + paddle1.size){
this.speedX += 0.25
this.speedX = -this.speedX ;
var delta = ball.y -(paddle1.position + (paddle1.size / 2) ) ;
this.speedY = delta * 0.15 ;
}else{
paddle2.score += 1 ;
this.reset(true);
}
}
if(this.x > canvas.width){
if(this.y > paddle2.position && this.y < paddle2.position + paddle1.size){
this.speedX += 0.25
this.speedX = -this.speedX ;
var delta = ball.y -(paddle2.position + (paddle2.size / 2) ) ;
this.speedY = delta * 0.15
}else{
paddle1.score += 1 ;
this.reset(false);
}
}
if(this.y > canvas.height || this.y < 0){
this.speedY = -this.speedY ;
}
this.y += this.speedY ;
this.x += this.speedX ;
}
this.reset = function(dir){
this.x = canvas.width / 2 ;
this.y = canvas.height / 2 ;
if(dir){
this.speedX = 2 ;
}else{
this.speedX = -2 ;
}
this.speedY = Math.floor(Math.random() * 10) - 5 ;
}
}
var Paddle = function( size ){
this.position = (canvas.height /2) - (size / 2) ;
this.size = size ;
this.score = 0 ;
this.play = function(){
var center = this.position + this.size /2 ;
if(center < ball.y + this.size /3 ){
this.position += 3 ;
}
if(center > ball.y - this.size /3){
this.position -= 3 ;
}
}
}
function frame(){
ball.move();
paddle2.play();
// paddle1.play();
drawEverything();
}
var canvas ;
var canvasContext ;
var ball ;
window.onload = function(){
canvas = document.getElementById("game") ;
canvasContext = canvas.getContext('2d');
ball = new Ball(10 , 5 , 2.5);
paddle1 = new Paddle(100);
paddle2 = new Paddle(100);
var framePerSecond = 120 ;
drawEverything();
setInterval(frame , 1000/framePerSecond);
canvas.addEventListener('mousemove' , function(evt) {
var mousePos = calculateMousePosition(evt);
paddle1.position = mousePos.y - paddle1.size / 2;
} )
}
</script>
</html>