如果你是flash编程师,你改用哪些代码做一个2048
时间: 2024-06-09 17:06:59 浏览: 141
C++语言99个常见编程错误 pdf 绝对高清
4星 · 用户满意度95%
如果我是 Flash 编程师,我会使用 ActionScript 3.0 编写一个 2048 游戏。以下是可能的代码实现:
首先,定义一个 `Tile` 类来表示游戏面板上的每一个方块:
```
package {
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFormat;
public class Tile extends Sprite {
private var value:int;
private var label:TextField;
public function Tile() {
value = 0;
draw();
}
public function setValue(v:int):void {
value = v;
draw();
}
private function draw():void {
graphics.clear();
graphics.beginFill(getColor(value));
graphics.drawRect(0, 0, 100, 100);
graphics.endFill();
if (value > 0) {
if (!label) {
label = new TextField();
label.width = 100;
label.height = 100;
label.defaultTextFormat = new TextFormat("Arial", 36, 0xFFFFFF, true, null, null, null, null, "center");
addChild(label);
}
label.text = value.toString();
} else if (label) {
removeChild(label);
label = null;
}
}
private function getColor(v:int):uint {
switch (v) {
case 2: return 0xEEEEEE;
case 4: return 0xDDDDDD;
case 8: return 0xCCCCCC;
case 16: return 0xBBBBBB;
case 32: return 0xAAAAAA;
case 64: return 0x999999;
case 128: return 0x888888;
case 256: return 0x777777;
case 512: return 0x666666;
case 1024: return 0x555555;
case 2048: return 0x444444;
default: return 0xDDDDDD;
}
}
}
}
```
然后,定义一个 `Board` 类来管理游戏面板:
```
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Board extends Sprite {
private var tiles:Array;
private var gameOver:Boolean;
private var score:int;
private var scoreText:TextField;
private var restartText:TextField;
public function Board() {
tiles = [];
gameOver = false;
score = 0;
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
addEventListener(Event.REMOVED_FROM_STAGE, destroy);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
for (var i:int = 0; i < 4; i++) {
tiles[i] = [];
for (var j:int = 0; j < 4; j++) {
var tile:Tile = new Tile();
tile.x = j * 100;
tile.y = i * 100;
addChild(tile);
tiles[i][j] = tile;
}
}
spawnTile();
spawnTile();
scoreText = new TextField();
scoreText.x = 400;
scoreText.y = 20;
scoreText.width = 200;
scoreText.height = 50;
scoreText.defaultTextFormat = new TextFormat("Arial", 24, 0x000000, true, null, null, null, null, "right");
addChild(scoreText);
updateScore();
restartText = new TextField();
restartText.x = 250;
restartText.y = 300;
restartText.width = 300;
restartText.height = 50;
restartText.defaultTextFormat = new TextFormat("Arial", 36, 0xFF0000, true, null, null, null, null, "center");
restartText.text = "Press R to restart";
addChild(restartText);
}
private function destroy(e:Event):void {
removeEventListener(Event.REMOVED_FROM_STAGE, destroy);
stage.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
}
private function onKeyDown(e:KeyboardEvent):void {
if (gameOver) {
if (e.keyCode == Keyboard.R) {
restart();
}
return;
}
var moved:Boolean = false;
switch (e.keyCode) {
case Keyboard.LEFT:
moved = moveLeft();
break;
case Keyboard.RIGHT:
moved = moveRight();
break;
case Keyboard.UP:
moved = moveUp();
break;
case Keyboard.DOWN:
moved = moveDown();
break;
}
if (moved) {
spawnTile();
checkGameOver();
updateScore();
}
}
private function moveLeft():Boolean {
var moved:Boolean = false;
for (var i:int = 0; i < 4; i++) {
for (var j:int = 1; j < 4; j++) {
var tile:Tile = tiles[i][j];
if (tile.value > 0) {
var k:int = j;
while (k > 0 && tiles[i][k - 1].value == 0) {
k--;
}
if (k > 0 && tiles[i][k - 1].value == tile.value) {
tiles[i][k - 1].setValue(tile.value * 2);
tiles[i][j].setValue(0);
score += tile.value * 2;
moved = true;
} else if (k < j) {
tiles[i][k].setValue(tile.value);
tiles[i][j].setValue(0);
moved = true;
}
}
}
}
return moved;
}
private function moveRight():Boolean {
var moved:Boolean = false;
for (var i:int = 0; i < 4; i++) {
for (var j:int = 2; j >= 0; j--) {
var tile:Tile = tiles[i][j];
if (tile.value > 0) {
var k:int = j;
while (k < 3 && tiles[i][k + 1].value == 0) {
k++;
}
if (k < 3 && tiles[i][k + 1].value == tile.value) {
tiles[i][k + 1].setValue(tile.value * 2);
tiles[i][j].setValue(0);
score += tile.value * 2;
moved = true;
} else if (k > j) {
tiles[i][k].setValue(tile.value);
tiles[i][j].setValue(0);
moved = true;
}
}
}
}
return moved;
}
private function moveUp():Boolean {
var moved:Boolean = false;
for (var j:int = 0; j < 4; j++) {
for (var i:int = 1; i < 4; i++) {
var tile:Tile = tiles[i][j];
if (tile.value > 0) {
var k:int = i;
while (k > 0 && tiles[k - 1][j].value == 0) {
k--;
}
if (k > 0 && tiles[k - 1][j].value == tile.value) {
tiles[k - 1][j].setValue(tile.value * 2);
tiles[i][j].setValue(0);
score += tile.value * 2;
moved = true;
} else if (k < i) {
tiles[k][j].setValue(tile.value);
tiles[i][j].setValue(0);
moved = true;
}
}
}
}
return moved;
}
private function moveDown():Boolean {
var moved:Boolean = false;
for (var j:int = 0; j < 4; j++) {
for (var i:int = 2; i >= 0; i--) {
var tile:Tile = tiles[i][j];
if (tile.value > 0) {
var k:int = i;
while (k < 3 && tiles[k + 1][j].value == 0) {
k++;
}
if (k < 3 && tiles[k + 1][j].value == tile.value) {
tiles[k + 1][j].setValue(tile.value * 2);
tiles[i][j].setValue(0);
score += tile.value * 2;
moved = true;
} else if (k > i) {
tiles[k][j].setValue(tile.value);
tiles[i][j].setValue(0);
moved = true;
}
}
}
}
return moved;
}
private function spawnTile():void {
var emptyTiles:Array = [];
for (var i:int = 0; i < 4; i++) {
for (var j:int = 0; j < 4; j++) {
if (tiles[i][j].value == 0) {
emptyTiles.push({x: i, y: j});
}
}
}
if (emptyTiles.length == 0) {
return;
}
var index:int = Math.floor(Math.random() * emptyTiles.length);
var value:int = Math.random() < 0.9 ? 2 : 4;
tiles[emptyTiles[index].x][emptyTiles[index].y].setValue(value);
}
private function checkGameOver():void {
gameOver = true;
for (var i:int = 0; i < 4; i++) {
for (var j:int = 0; j < 4; j++) {
if (tiles[i][j].value == 0) {
gameOver = false;
return;
}
if (i > 0 && tiles[i - 1][j].value == tiles[i][j].value) {
gameOver = false;
return;
}
if (i < 3 && tiles[i + 1][j].value == tiles[i][j].value) {
gameOver = false;
return;
}
if (j > 0 && tiles[i][j - 1].value == tiles[i][j].value) {
gameOver = false;
return;
}
if (j < 3 && tiles[i][j + 1].value == tiles[i][j].value) {
gameOver = false;
return;
}
}
}
}
private function updateScore():void {
scoreText.text = "Score: " + score.toString();
}
private function restart():void {
gameOver = false;
score = 0;
updateScore();
for (var i:int = 0; i < 4; i++) {
for (var j:int = 0; j < 4; j++) {
tiles[i][j].setValue(0);
}
}
spawnTile();
spawnTile();
removeChild(restartText);
}
}
}
```
最后,在主场景中创建一个 `Board` 对象:
```
var board:Board = new Board();
addChild(board);
```
这样就完成了一个简单的 2048 游戏!
阅读全文