-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
183 lines (160 loc) · 4.82 KB
/
index.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<!DOCTYPE HTML>
<head>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/coffee-script/1.4.0/coffee-script.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.0/css/materialize.min.css">
<title>Avoid blue!</title>
<style type="text/css">
body {
font-family: Arial, sans-serif;
color: white;
background-color: black;
overflow: hidden;
}
form {position: absolute;}
.player, .enemy{
width: 10px;
height: 10px;
position: absolute;
}
.player {background-color: #f44336;}
.enemy {background-color: #2196f3;}
</style>
<script type="text/javascript">
function KeyboardController(keys, repeat) {
var timers= {};
document.onkeydown= function(event) {
var key= (event || window.event).keyCode;
if (!(key in keys))
return true;
if (!(key in timers)) {
timers[key]= null;
keys[key]();
if (repeat!==0)
timers[key]= setInterval(keys[key], repeat);
}
return false;
};
document.onkeyup= function(event) {
var key= (event || window.event).keyCode;
if (key in timers) {
if (timers[key]!==null)
clearInterval(timers[key]);
delete timers[key];
}
};
window.onblur= function() {
for (key in timers)
if (timers[key]!==null)
clearInterval(timers[key]);
timers= {};
};
};
</script>
<script type="text/coffeescript">
makeEntity = (x, y, speed, player) ->
entity =
x: x
y: y
speed: speed
winHeight = window.innerHeight
winWidth = window.innerWidth
score = 0
enemySpawnRate = 50
player = makeEntity winWidth*0.5, winHeight*0.5, 5
enemies = []
run = false
spawnEnemy = (enemies) ->
newEnemy = makeEntity 0, 0, score
enemies[enemies.length] = setRandomEnemyStart newEnemy
setRandomEnemyStart = (enemy) ->
side = Math.floor(Math.random()*4)
switch side
when 0
enemy.x = -100
enemy.y = Math.random()*winHeight
when 1
enemy.x = Math.random()*winWidth
enemy.y = -100
when 2
enemy.x = 100 + winWidth
enemy.y = Math.random()*winHeight
when 3
enemy.x = Math.random()*winWidth
enemy.y = 100 + winHeight
return enemy
movePlayer = (keyCode) ->
switch keyCode
when 37 then moveEntity player, -player.speed, 0
when 38 then moveEntity player, 0, -player.speed
when 39 then moveEntity player, player.speed, 0
when 40 then moveEntity player, 0, player.speed
moveEnemies = (enemies) ->
for enemy in enemies
moveEntity enemy, (player.x-enemy.x)*0.07, (player.y-enemy.y)*0.07
moveEntity = (entity, diffX, diffY) ->
entity.x += diffX
entity.y += diffY
initialize = (enemies) ->
spawnEnemy enemies, winWidth, winHeight
run = true
drawScreen = (board) ->
do board.children().remove
for enemy in enemies
board.append "<div class=\"enemy\" style=\"left:"+enemy.x+"px; top:"+enemy.y+"px;\">"
board.append "<div class=\"player\" style=\"left:"+player.x+"px; top:"+player.y+"px;\">"
drawHighscore = (board) ->
board.append "<form action=\"highscoreRegister.jsp\" method=\"post\" style=\"left:"+(winWidth*0.5-150)+"px; top:"+(winHeight*0.5-100)+"px;\">
<p><h2>Game over!</h2></p>
<p><h4>Score: "+score+"</h4></p>
<p><h3><a href=\"index.html\">Play again?</a></h3></p>
</form>"
update = (enemies) ->
#moveEnemies enemies
KeyboardController {
37: (-> movePlayer 37),
38: (-> movePlayer 38),
39: (-> movePlayer 39),
40: (-> movePlayer 40) }, 24
hasCollided = (enemy, player) ->
diffX = (player.x-enemy.x)
diffY = (player.y-enemy.y)
hasNotCollidedX = diffX*diffX <= 110
hasNotCollidedY = diffY*diffY <= 110
hasNotCollidedX and hasNotCollidedY
playerHitWall = (player) ->
player.x <= 0 or player.y <= 0 or player.x >= winWidth or player.y >= winHeight
checkCollisions = (enemies, player) ->
if playerHitWall player
run = false
for enemy in enemies
if hasCollided player, enemy
run = false
for otherEnemy in enemies
if (hasCollided enemy, otherEnemy) and (enemy.speed isnt otherEnemy.speed)
enemy = setRandomEnemyStart enemy
otherEnemy = setRandomEnemyStart otherEnemy
gameLoop = (board) ->
$("#score").text score += 1
#update enemies
if score >= 300
enemySpawnRate = 200
if (score % enemySpawnRate) is 0
spawnEnemy enemies, winWidth, winHeight
checkCollisions enemies, player
moveEnemies enemies
drawScreen board
if run
setTimeout (-> gameLoop board), 24
else
drawHighscore board
$ ->
initialize enemies, run
update enemies
gameLoop $("#board")
</script>
</head>
<body>
<h4 id="score"></h4>
<div id="board"></div>
</body>