最近学ActionScript3.0 以下为自己写的一个小游戏。尽量以最少的代码,实现功能
游戏原理:看代码注释
游戏规则:类似于【劲舞团】游戏,玩家可以按UP,DOWN,LEFT,RIGHT键来操控游戏。
打掉相应的箭头则得分,否则失手。
箭头落到底线没有被打掉,则为失手。
失手10次,游戏结束。
得分过30,升一级。难度加大。
先建一个箭头的类Baby.as 。提供四种不同造型的箭头
package
{
import flash.display.Sprite;
import flash.filters.ConvolutionFilter;
/**
* ...
* @author kkkloveyou
*/
public class Baby extends Sprite
{
private var color:uint = 0xFFFFFF;
private var direction:uint;
private const LEFT:uint = 37;
private const RIGHT:uint = 39;
private const UP:uint = 38;
private const DOWN:uint = 40;
public var flag:int = 0;
public function Baby(direction:uint):void
{
this.direction = direction;
switch (direction) {
case LEFT:
drawLeft();
break;
case RIGHT:
drawRight();
break;
case UP:
drawUp();
break;
case DOWN:
drawDown();
break;
default:
break;
}
}
public function drawRight():void {
graphics.lineStyle(3,0xff0000);//左面
graphics.moveTo(-60,-30);
graphics.lineTo(-60,30);
graphics.lineTo(30, 30);
graphics.lineTo(30,40);
graphics.lineTo(60, 0);
graphics.lineTo(30,-40);
graphics.lineTo(30, -30);
graphics.lineTo( -60, -30);
flag = 39;
}
public function drawLeft():void {
graphics.lineStyle(3,0x00ff00);//右面
graphics.moveTo(60,-30);
graphics.lineTo(60,30);
graphics.lineTo(-30, 30);
graphics.lineTo(-30,40);
graphics.lineTo(-60, 0);
graphics.lineTo(-30,-40);
graphics.lineTo(-30, -30);
graphics.lineTo(60, -30);
flag = 37;
}
public function drawUp():void {
graphics.lineStyle(3,0x00ffff);//上面
graphics.moveTo(-30,60);
graphics.lineTo(30,60);
graphics.lineTo(30, -30);
graphics.lineTo(40,-30);
graphics.lineTo(0, -60);
graphics.lineTo(-40,-30);
graphics.lineTo(-30, -30);
graphics.lineTo( -30, 60);
flag = 38;
}
public function drawDown():void {
graphics.lineStyle(3,0x001177);//下面
graphics.moveTo(-30,-60);
graphics.lineTo(30,-60);
graphics.lineTo(30, 30);
graphics.lineTo(40,30);
graphics.lineTo(0, 60);
graphics.lineTo(-40,30);
graphics.lineTo(-30, 30);
graphics.lineTo( -30, -60);
flag = 40;
}
}
}
再建一个控制类Main.as 用来控制游戏
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.ui.Keyboard;
public class Main extends Sprite
{
public var arr:Array;//储存下落箭头的数组
public var time:uint;
public var errorTxt:TextField;//错误显示文本
public var trueTxt:TextField;//正确显示文本
public var levelTxt:TextField;//等级文本
public var meTxt:TextField;//作者文本
public var errorCount:int = 0;//错误总计变量
public var trueCount:int = 0;//正确总计变量
public var levelCount:int = 1;//当前关卡等级
public var speed:uint = 5; //箭头移动速度
public function Main(){
init();
}
public function init():void{
this.addEventListener(Event.ENTER_FRAME,loopHandler);
stage.addEventListener(KeyboardEvent.KEY_DOWN,downHandler);
arr = new Array();
time = 0;
errorTxt = new TextField();
trueTxt = new TextField();
levelTxt = new TextField();
meTxt = new TextField();
errorTxt.text = "失手: "+errorCount;
trueTxt.text = "得分: " + trueCount;
levelTxt.text = "等级: " + levelCount;
meTxt.text = "卫哥作品";
levelTxt.x = 50;
levelTxt.y = 50;
errorTxt.x = 50;
errorTxt.y = 150;
trueTxt.x = 50;
trueTxt.y = 250;
meTxt.x = 700;
meTxt.y = 550;
this.addChild(errorTxt);
this.addChild(trueTxt);
this.addChild(levelTxt);
this.addChild(meTxt);
}
public function downHandler(event:KeyboardEvent):void{
errorCount++;
if (errorCount >= 10) {
stage.removeEventListener(KeyboardEvent.KEY_DOWN, downHandler);
this.removeEventListener(Event.ENTER_FRAME,loopHandler);
}
for(var i:Number = arr.length-1;i>=0;i--){
//判断键盘按下的键值是否和场景里的字母符合.如果符合则消去
var baby:Baby = arr[i] as Baby;
if ( baby ){
if(baby.flag == event.keyCode){
this.removeChild(arr[i]);
arr.splice(i,1);
trueCount++;
errorCount--;
break;
}
}
}
if (trueCount >= 30) {
levelCount++; //级数升级,得手清零
speed += 3; //级数升级,速度增大,难度增大
trueCount = 0;
}
errorTxt.text = "失手: "+errorCount;
trueTxt.text = "得分: " + trueCount;
levelTxt.text = "等级: " + levelCount
}
public function loopHandler(event:Event):void{
time++;
if(time>=10){
time = 0;
var baby:Baby = new Baby(int(Math.random() * 4) + 37); //随机产生箭头类型
baby.x = stage.stageWidth / 2;
baby.y = 30;
this.addChild(baby);
arr.unshift(baby);
}
for(var i:Number = arr.length-1;i>=0;i--){
arr[i].y+=speed; //箭头速度
if(arr[i].y>=stage.stageHeight){
this.removeChild(arr[i]);
arr.splice(i,1);
errorCount++;
errorTxt.text = "失手: "+errorCount;
break;
}
}
if (errorCount >= 10) {
stage.removeEventListener(KeyboardEvent.KEY_DOWN, downHandler);
this.removeEventListener(Event.ENTER_FRAME,loopHandler);
}
}
}
}
项目演示地址:http://up.qqhello.com/qzone/73e11069e924b6d6df5198f9440a2403.swf
标签:lineTo,30,60,源码,百行,graphics,var,劲舞团,public From: https://blog.51cto.com/u_9427273/6410342