-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcolorGame.html
More file actions
196 lines (183 loc) · 5.37 KB
/
colorGame.html
File metadata and controls
196 lines (183 loc) · 5.37 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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Piksel</title>
<script type="text/javascript">
window.addEventListener('load', eventWindowLoaded, false);
function eventWindowLoaded() {
canvasApp();
}
function canvasSupport () {
return true;
}
var screenWidth = 20; //number of blocks in a row
var screenHeight = 20; //number of block in a column
var map = new map();
var screenSize = screenWidth * screenHeight;
function canvasApp(){
if (!canvasSupport()) {
return;
}else{
restart();
}
}
function box(color, x ,y){
this.color = color;
this.width = 20;
this.height = 20;
this.x = x;
this.y = y;
this.active = false;
this.draw = function(context){
if(this.color == 1)
context.fillStyle = '#ff0000';
else if(this.color == 2)
context.fillStyle = '#00ff00';
else if(this.color == 3)
context.fillStyle = '#0000ff';
else if(this.color == 4)
context.fillStyle = '#ff00ff';
else if(this.color == 5)
context.fillStyle = '#ffff00';
else if(this.color == 6)
context.fillStyle = '#00ffff';
context.fillRect(x*this.width, y*this.height, this.width, this.height);
if(this.active){
context.fillStyle = '#000000';
context.fillRect(x*this.width+8, y*this.height+8, 2, 2);
}
}
}
function map(){
this.step = 0;
this.mapArray = new Array(screenWidth);
this.isGameOn = true;
this.initialize = function(){
this.step = 0;
this.isGameOn = true;
for(var i = 0; i < screenWidth; i++){
this.mapArray[i] = new Array(screenHeight);
for(var j = 0; j < screenHeight; j++){
this.mapArray[i][j] = new box(Math.floor(Math.random()*6)+1, i, j);
}
}
this.mapArray[0][0].active = true;
this.checkNeighbours(this.mapArray[0][0].color, 0, 0);
}
this.draw = function(){
console.log('drawing...');
var theCanvas = document.getElementById("canvas");
var context = theCanvas.getContext("2d");
context.clearRect(0, 0, 500, 600);
for(var i = 0; i < screenWidth; i++){
for(var j = 0; j < screenHeight; j++){
var box = this.mapArray[i][j];
box.draw(context);
}
}
var stepDiv = document.getElementById("step");
stepDiv.innerHTML = 'Step = ' + this.step;
}
this.changeActiveColor = function(color){
if(this.isGameOn){
var activeBoxCount = 0;
for(var i = 0; i < screenWidth; i++){
for(var j = 0; j < screenHeight; j++){
var box = this.mapArray[i][j];
if(box.active){
this.checkNeighbours(color, i, j);
}
}
}
for(var i = 0; i < screenWidth; i++){
for(var j = 0; j < screenHeight; j++){
var box = this.mapArray[i][j];
if(box.active){
box.color = color;
activeBoxCount++;
}
}
}
this.step++;
this.draw();
if(activeBoxCount == screenSize){
this.endGame();
}
}
}
this.endGame = function(){
var statusDiv = document.getElementById("gameStatus");
statusDiv.innerHTML = 'Game Over';
this.isGameOn = false;
}
this.checkNeighbours = function(color, x, y){
if(x > 0){
if(this.mapArray[x-1][y].color == color){
if(!this.mapArray[x-1][y].active){
this.mapArray[x-1][y].active = true;
this.checkNeighbours(color, x-1, y);
}
}
}
if(x < screenWidth-1){
if(this.mapArray[x+1][y].color == color){
if(!this.mapArray[x+1][y].active){
this.mapArray[x+1][y].active = true;
this.checkNeighbours(color, x+1, y);
}
}
}
if(y > 0){
if(this.mapArray[x][y-1].color == color){
if(!this.mapArray[x][y-1].active){
this.mapArray[x][y-1].active = true;
this.checkNeighbours(color, x, y-1);
}
}
}
if(y < screenHeight-1){
if(this.mapArray[x][y+1].color == color){
if(!this.mapArray[x][y+1].active){
this.mapArray[x][y+1].active = true;
this.checkNeighbours(color, x, y+1);
}
}
}
}
}
function clickColor(colorCode){
map.changeActiveColor(colorCode);
}
function restart(){
var statusDiv = document.getElementById("gameStatus");
statusDiv.innerHTML = '';
map.initialize();
map.draw();
}
</script>
</head>
<body>
<div>
<input type="button" value="New Game" onclick="javascript:restart()" />
</div>
<div id="gameStatus" style="color:#ff2222; font-size:16px; font-family:comic-sans;">
</div>
<div id="step">
Step = 0
</div>
<div style="top: 50px; left: 50px;">
<canvas id="canvas" width="500" height="400">
Your browser does not support HTML5 Canvas.
</canvas>
</div>
<div>
<input type="button" onclick="javascript:clickColor(1)" style="background-color:#ff0000; width:60px; height:60px" />
<input type="button" onclick="javascript:clickColor(2)" style="background-color:#00ff00; width:60px; height:60px" />
<input type="button" onclick="javascript:clickColor(3)" style="background-color:#0000ff; width:60px; height:60px" />
<input type="button" onclick="javascript:clickColor(4)" style="background-color:#ff00ff; width:60px; height:60px" />
<input type="button" onclick="javascript:clickColor(5)" style="background-color:#ffff00; width:60px; height:60px" />
<input type="button" onclick="javascript:clickColor(6)" style="background-color:#00ffff; width:60px; height:60px" />
</div>
</body>
</html>