主要源码如下:
package com.topyunp.snake
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import mx.controls.Alert;
import mx.core.Application;
import mx.core.FlexGlobals;
public class SnakeGame extends Sprite
{
private const CELL_SIZE:int = 10;
private var snake:Array;
private var direction:String;
private var food:Sprite;
private var gameWidth:int = 400;
private var gameHeight:int = 400;
private var frameCounter:int = 0;
private var speed:int = 5; // 控制蛇速度的变量,数值越大速度越慢
public function SnakeGame()
{
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage(event:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
init();
}
private function init():void
{
snake = [];
direction = "right";
createSnake();
createFood();
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function createSnake():void
{
for (var i:int = 0; i < 5; i++)
{
var segment:Sprite = new Sprite();
segment.graphics.beginFill(0x006400);
segment.graphics.drawRect(0, 0, CELL_SIZE, CELL_SIZE);
segment.graphics.endFill();
segment.x = i * CELL_SIZE;
segment.y = 0;
snake.push(segment);
addChild(segment);
}
}
private function createFood():void
{
food = new Sprite();
food.graphics.beginFill(0xFFA500);
food.graphics.drawRect(0, 0, CELL_SIZE, CELL_SIZE);
food.graphics.endFill();
positionFood();
addChild(food);
}
private function positionFood():void
{
food.x = Math.floor(Math.random() * (gameWidth / CELL_SIZE)) * CELL_SIZE;
food.y = Math.floor(Math.random() * (gameHeight / CELL_SIZE)) * CELL_SIZE;
}
private function onKeyDown(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.LEFT:
if (direction != "right")
direction = "left";
break;
case Keyboard.RIGHT:
if (direction != "left")
direction = "right";
break;
case Keyboard.UP:
if (direction != "down")
direction = "up";
break;
case Keyboard.DOWN:
if (direction != "up")
direction = "down";
break;
}
}
private function onEnterFrame(event:Event):void
{
frameCounter++;
if (frameCounter >= speed)
{
frameCounter = 0;
updateSnake();
checkCollision();
}
}
private function updateSnake():void
{
var head:Sprite = snake[snake.length - 1];
var newHead:Sprite = new Sprite();
newHead.graphics.beginFill(0x006400);
newHead.graphics.drawRect(0, 0, CELL_SIZE, CELL_SIZE);
newHead.graphics.endFill();
switch (direction)
{
case "left":
newHead.x = head.x - CELL_SIZE;
newHead.y = head.y;
break;
case "right":
newHead.x = head.x + CELL_SIZE;
newHead.y = head.y;
break;
case "up":
newHead.x = head.x;
newHead.y = head.y - CELL_SIZE;
break;
case "down":
newHead.x = head.x;
newHead.y = head.y + CELL_SIZE;
break;
}
snake.push(newHead);
addChild(newHead);
if (newHead.x == food.x && newHead.y == food.y)
{
positionFood();
}
else
{
var tail:Sprite = snake.shift();
removeChild(tail);
}
}
private function checkCollision():void
{
var head:Sprite = snake[snake.length - 1];
if (head.x < 0 || head.x >= gameWidth || head.y < 0 || head.y >= gameHeight)
{
endGame();
}
for (var i:int = 0; i < snake.length - 1; i++)
{
if (head.x == snake[i].x && head.y == snake[i].y)
{
endGame();
}
}
}
private function endGame():void
{
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
Alert.show("Click [OK] to restart game", "Game over", 4, null, restartGame);
}
private function restartGame(e:*):void
{
// 清空蛇和食物
while (snake.length > 0)
{
var segment:Sprite = snake.pop();
removeChild(segment);
}
removeChild(food);
// 重新初始化游戏
init();
var app:Application = FlexGlobals.topLevelApplication as Application;
app.setFocus();
}
}
}