-
Notifications
You must be signed in to change notification settings - Fork 0
/
tennis.html
73 lines (47 loc) · 1.25 KB
/
tennis.html
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
<html>
<canvas id = "gameCanvas" width ="800" height ="600"></canvas>
<script>
var canvas;
var canvasContext;
var ballX = 50;
var ballSpeedX = 10;
window.onload = function() {
canvas = document.getElementById('gameCanvas');
canvasContext = canvas.getContext('2d');
var framesPerSecond = 30;
setInterval(callBoth, 1000/framesPerSecond);
drawEverything();
drawEverything();
drawEverything();
drawEverything();
}
function callBoth(){
moveEverything();
drawEverything();
}
function moveEverything(){
ballX = ballX + ballSpeedX;
if(ballX >= canvas.width){
ballSpeedX = -ballSpeedX;
}
if(ballX <= 0){
ballSpeedX = -ballSpeedX;
}
}
function drawEverything() {
//\next linr blanks the screen with black
colorRect(0,0,canvas.width,canvas.height, 'black');
//this is the left player's paddle
colorRect(0,210,10,100, 'white');
//This line draws the ball
canvasContext.fillStyle = 'white';
canvasContext.beginPath();
canvasContext.arc(ballX, 100, 10, 0,Math.PI*2, true);
canvasContext.fill();
}
function colorRect(leftX,topY,width,height, drawColor){
canvasContext.fillStyle = drawColor;
canvasContext.fillRect(leftX,topY,width,height)
}
</script>
</html>