-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeedybox.html
123 lines (104 loc) · 2.75 KB
/
speedybox.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
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
<html>
<head>
<title>Speedy Box</title>
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" />
<script async src="https://www.googletagmanager.com/gtag/js?id=G-Y55LGP63JX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-Y55LGP63JX');
</script>
</head>
<script type="text/javascript">
// 0 is not started or game over, 1 is active
var state = 0;
var score = 0;
// Boundary points
var leftBound = 0;
var rightBound = 0;
// 0 is left, 1 is right
var cursorDirection = 0;
// X coordinate of cursor position
var cursorPosition = 1;
// Variable speed of cursor
var cursorSpeed = 0;
function Tap()
{
if (state === 0)
{
ClearScreen();
state = 1;
leftBound = 10;
rightBound = 250;
cursorPosition = 20;
cursorDirection = 1;
cursorSpeed = 0;
score = 0;
UpdateScreen();
}
else
{
// update score counter and move boundary
cursorDirection = cursorDirection ^ 1;
score = score + 1;
cursorSpeed = cursorSpeed + 3;
}
}
function ClearScreen()
{
var canvas = document.getElementById("gameCanvas");
var ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, 300, 100);
}
// Update position of left bound, right bound, and cursor with direction
function UpdateScreen()
{
var canvas = document.getElementById("gameCanvas");
var ctx = canvas.getContext("2d");
// check if collision occurred, if so, end game
if (cursorPosition <= leftBound || cursorPosition >= rightBound)
{
ctx.clearRect(0, 0, 300, 100);
state = 0;
ctx.fillText('game over', 20, 20);
return;
}
ctx.clearRect(0, 0, 400, 100);
// if game is still going, update cursor location
if (cursorDirection === 0)
{
cursorPosition = cursorPosition - 5 - cursorSpeed;
}
else
{
cursorPosition = cursorPosition + 5 + cursorSpeed;
}
// draw our cursor
ctx.beginPath();
ctx.rect(cursorPosition, 20, 50, 30);
ctx.stroke();
// update left and right boundaries
ctx.rect(leftBound, 10, 10, 50);
ctx.rect(rightBound, 10, 10, 50);
ctx.stroke();
// draw score counter
ctx.fillText(score, 300, 20);
// continue our ticks by re-running setTimeout calls
setTimeout(UpdateScreen, 50);
}
function ShowStart()
{
var canvas = document.getElementById("gameCanvas");
var ctx = canvas.getContext("2d");
ctx.font = "18px Arial Bold";
ctx.fillText('tap to start', 50, 50);
}
</script>
<body onload="ShowStart()">
<p>
<canvas id="gameCanvas" width="350" height="100" style="border: 1px #000000 solid;" onclick="Tap()">
</canvas>
</p>
</body>
</html>