-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameControl.js
138 lines (132 loc) · 2.83 KB
/
gameControl.js
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
/* global initGame, nextGeneration, randomInitGame, $ */
var gameStatus = 0;
//0: before the game is running
//1: the game is running now
//2: the game is paused
var gameBlockEdge = 10;
var gameMap;
var gameWidth = 100;
var gameHeight = 50;
var gameCellNumber = Math.floor(gameHeight * gameWidth / 3);
var gameUpdateRate = 100;
var gameGeneration = 0;
function chooseBlock(i, j)
{
return $("#b" + i + "-" + j);
}
function showMap()
{
for(var i = 0; i < gameMap.length; i++)
{
for(var j = 0; j < gameMap[i].length; j++)
{
var block = chooseBlock(i, j);
if(gameMap[i][j] == 0 && block.hasClass("black"))
{
block.removeClass("black");
block.addClass("white");
}
else if(gameMap[i][j] == 1 && block.hasClass("white"))
{
block.removeClass("white");
block.addClass("black");
}
}
}
}
function startTimer()
{
if(gameStatus == 1)
{
setTimeout("startTimer()", gameUpdateRate);
nextGeneration(gameMap);
showMap();
gameGeneration++;
$("#generation").text("Generation: " + gameGeneration);
}
}
$(document).ready(function()
{
$("#generation").text("Generation: " + gameGeneration);
$("#init").text("随机初始化");
$("#start").text("开始繁殖");
gameMap = initGame(gameWidth, gameHeight);
var area = "";
for(var j = 0; j < gameMap.length; j++)
{
for(var i = 0; i < gameMap[j].length; i++)
{
area += "<div class = 'white' id = 'b" + j + "-" + i +
"' style = 'left: " + i * gameBlockEdge +
"px; top: " + j * gameBlockEdge + "px'></div>";
}
}
$("#gamemain")
.html(area)
.width(gameMap[0].length * gameBlockEdge)
.height(gameMap.length * gameBlockEdge)
.show();
});
$(function()
{
$("#gamemain").mousedown(function(e)
{
if(gameStatus == 1) return;
var clickBlock = $(e.target);
var id = clickBlock.attr("id");
var x = parseInt(id.substring(1, id.indexOf("-")));
var y = parseInt(id.substring(id.indexOf("-") + 1));
var block = chooseBlock(x, y);
if(gameMap[x][y] == 0)
{
gameMap[x][y] = 1;
block.removeClass("white");
block.addClass("black");
}
else
{
gameMap[x][y] = 0;
block.removeClass("black");
block.addClass("white");
}
});
$("#init").click(function()
{
if(gameStatus == 0)
{
gameMap = randomInitGame(gameWidth, gameHeight, gameCellNumber);
showMap();
}
else if(gameStatus == 2)
{
$("#init").text("暂停");
gameStatus = 1;
startTimer();
}
else
{
$("#init").text("继续");
gameStatus = 2;
}
});
$("#start").click(function()
{
if(gameStatus == 0)
{
$("#start").text("重新开始");
$("#init").text("暂停");
gameStatus = 1;
startTimer();
}
else
{
$("#start").text("开始繁殖");
$("#init").text("随机初始化");
gameStatus = 0;
gameGeneration = 0;
$("#generation").text("Generation: " + gameGeneration);
gameMap = initGame(gameWidth, gameHeight);
showMap();
}
});
});