E-mail Comment Del.icio.us Digg Reddit Technorati Furl

一个一百多行代码的开关灯游戏

试着关闭所有的灯:

今天在flashandmath.com上闲逛,发现了这个小游戏,前前后后只有一百多行代码.蛮有意思.

用flash cs3创建一个fla文件,然后在场景上放个按钮,起名:btnNewGame

然后贴入下面的代码:

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
function setupGame():void {
	tbDoneText.text = "";
	addGameBoard();
	drawBoxes();
}
 
function newGame(evt:MouseEvent):void {
	removeGameBoard();
	setupGame();
}
 
// add a listener for the "New Game" button
btnNewGame.addEventListener(MouseEvent.CLICK,newGame);
 
var boxHeight:int = 40;
var boxWidth:int = 40;
var boxXSpace:int = 10;
var boxYSpace:int = 10;
 
function addGameBoard():void {
	// create a board to display all of the square buttons on
	var gb:Sprite = new Sprite();
	gb.graphics.beginFill(0xFFFFFF);
 
	// since the entire grid is at most 6x6, make enough room
	gb.graphics.drawRect(0,0,6*boxWidth+7*boxXSpace,6*boxHeight+7*boxYSpace);
	gb.graphics.endFill();
 
	gb.name = "gameBoard"
	gb.x = 20;
	gb.y = 20;
 
	this.addChild(gb);
}
 
function removeGameBoard():void {
 
	// remove the game board and all of the squares on it
	var gb:Sprite=Sprite(this.getChildByName("gameBoard"));
	var thisClip:Sprite;
 
	while(gb.numChildren>0){
		thisClip=Sprite(gb.getChildAt(0));
		thisClip.graphics.clear();
		gb.removeChild(thisClip);
	}
	gb.graphics.clear();
	this.removeChild(gb);
}
 
var onColor = 0xFF3333;
var offColor = 0x333333;
var numberOfRows:int;
var numberOfColumns:int;
var states:Array;
 
function drawBoxes():void {
	// create the squares on the game board
	var gb:Sprite = Sprite(this.getChildByName("gameBoard"));
	var square:Sprite;
 
	// create 3 to 6 rows and 3 to 6 columns
	numberOfRows = Math.floor(Math.random()*4+3);
	numberOfColumns = Math.floor(Math.random()*4+3);
 
	//create the states array, which has the same number of
	//"rows" and "columns" as our grid of squares
	states = new Array(numberOfRows);
 
	for (var i:int = 0; i < numberOfRows; i++) {
	// i = row number
 
		states[i] = new Array(numberOfColumns);
 
		for (var j:int = 0; j < numberOfColumns; j++) {
		// j = column number
 
			// create a square, which is by default 'off'
			square = new Sprite();
			square.graphics.lineStyle(2, 0x000000);
			square.graphics.beginFill(offColor);
			square.graphics.drawRect(0,0,boxWidth,boxHeight);
			square.graphics.endFill();
			states[i][j] = false;
			gb.addChild(square);
 
			// position the square in the correct location
			square.x = (j+1)*boxXSpace + j*boxWidth;
			square.y = (i+1)*boxYSpace + i*boxHeight;
			square.name = "square" + String(i) + String(j);
 
			// add a dynamically created listener for this square
			square.addEventListener(MouseEvent.CLICK,createListener(i,j));
		}
	}
	randomizeGameBoard();
}
 
function createListener(a:int, b:int):Function {
	// dynamically create a listener for the square in position (a,b)
	// that switches the box at (a,b) and the adjacent boxes, and
	// then checks to see if you are done
	var foo:Function = function (evt:MouseEvent):void {
		switchAt(a,b);
		if (checkDone()) {
			tbDoneText.text = "Good job!";
		}
	}
	return foo;
}
 
function invertState(row:int, col:int):void {
	// lookup the square located at (row, col)
	var gb:Sprite = Sprite(this.getChildByName("gameBoard"));
	var thisClip:Sprite = Sprite(gb.getChildByName(("square" + row) + col));
 
	// if the square is off, turn it on, and vice versa
 
	if (states[row][col]) {
		thisClip.graphics.beginFill(offColor);
		thisClip.graphics.drawRect(0,0,boxWidth,boxHeight);
		thisClip.graphics.endFill();
	}
	else {
		thisClip.graphics.beginFill(onColor);
		thisClip.graphics.drawRect(0,0,boxWidth,boxHeight);
		thisClip.graphics.endFill();
	}
	// reverse the state of the square
	states[row][col] = !states[row][col];
}
 
function switchAt(row:int, col:int):void {
	// switch the square that was clicked on
	invertState(row,col);
 
	// switch the squares adjacent to the clicked square
	if (row > 0) {
		invertState(row-1,col);
	}
	if (row < numberOfRows - 1) {
		invertState(row+1,col);
	}
	if (col > 0) {
		invertState(row,col-1);
	}
	if (col < numberOfColumns - 1) {
		invertState(row,col+1);
	}
}
 
function checkDone():Boolean {
	// check to see if all the lights are off
	var bool:Boolean = true;
	var i,j:int;
 
	for (i = 0; i < numberOfRows; i++) {
		for (j = 0; j < numberOfColumns; j++) {
			bool = bool && !states[i][j];
		}
	}
	return(bool);
}
 
function randomizeGameBoard():void {
	// randomly click the game board a random number of times
	// this ensures the game is solvable
	var r:int = Math.floor(Math.random()*5+5);
	var row:int, col:int, i:int;
 
	for (i = 0; i < r; i++) {
		row = Math.floor(Math.random() * numberOfRows);
		col = Math.floor(Math.random() * numberOfColumns);
		switchAt(row,col);
	}
}
 
setupGame();

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">