cocos2D-X LUA 非常简单的一个贪吃蛇案例

--[[
    贪吃蛇
]]

local RetroSnaker = class("RetroSnaker", function()
    return cc.Layer:create();
end )
local Obj = require("cocos.Packaging_Kernel")

local KEY_UP = 28
local KEY_DOWN = 29
local KEY_LEFT = 26
local KEY_RIGHT = 27

local Snake =
{
    ["UP"] = { },
    ["DOWN"] = { },
    ["LEFT"] = { },
    ["RIGHT"] = { }
}
local SnakePoint = { }

local struct = { }
struct.SnakePosition = { }

function struct.COOR()
    local Coor = { }
    Coor = cc.p(0, 0);
    return Coor
end
function struct.SnakeDirName()
    local SnakeDir =
    {
        UP = "UP",
        DOWN = "DOWN",
        LEFT = "LEFT",
        RIGHT = "RIGHT"
    }
    return SnakeDir;
end
function struct.SnakeDirCode()
    local SnakeDirCode =
    {
        UP = 28,
        DOWN = 29,
        LEFT = 26,
        RIGHT = 27
    }
    return SnakeDirCode;
end
function struct.FOOD()
    local Food = { }
    Food.COOR = struct.COOR()
    Food.Flag = 0;
    return Food;
end
function struct.SNAKE()
    struct.Len = 30;
    struct.MaxLen = 100;
    struct.Dir = struct.SnakeDirCode().RIGHT
    for i = 0, struct.MaxLen - 1 do
        struct.SnakePosition[i + 1] = { }
        struct.SnakePosition[i + 1] = struct.COOR()
    end
    return struct;
end
function struct.SetSnakePosition(Position)
    struct.SnakePosition[0] = Position;
end



local Switch = { }
Switch.Value = { }
function Switch.switch(Value)
    Switch.Value = Value;
end
function Switch.case(Value, Function)
    if (Switch.Value == Value) then Function() end
end
local if_s = { }
if_s.bool = { }
function if_s.ifs(bool)
    if_s.bool = bool
    return bool
end
function if_s.elseifs(bool)
    if_s.bool = bool
    return bool
end
function if_s.Call(Function)
    if (if_s.bool == true) then
        Function()
    end
end
function if_s.elses(Function)
    if (if_s.bool == false) then
        Function()
    end
end

function RetroSnaker:ctor()
    -- AudioEngine.playMusic(cc.FileUtils:getInstance():fullPathForFilename("RetroSnaker/BackgroundMusic.mp3"), true);
    self:InitData()
end

function RetroSnaker:InitData()

    self:CreateKeyEvent()
    -- self:CreateFrameCall()
    self:CreateTimeFrameCall(0.2)

    Snake["UP"] = cc.Sprite:create("RetroSnaker/SnakeUp.png")
    Snake["UP"]:setPosition(display.center)
    self:addChild(Snake["UP"])
    Snake["DOWN"] = cc.Sprite:create("RetroSnaker/SnakeDown.png")
    Snake["DOWN"]:setPosition(display.center)
    self:addChild(Snake["DOWN"])
    Snake["LEFT"] = cc.Sprite:create("RetroSnaker/SnakeLeft.png")
    Snake["LEFT"]:setPosition(display.center)
    self:addChild(Snake["LEFT"])
    Snake["RIGHT"] = cc.Sprite:create("RetroSnaker/SnakeRight.png")
    Snake["RIGHT"]:setPosition(display.center)
    self:addChild(Snake["RIGHT"])


    for Key, Value in pairs(Snake) do
        print("call")
        print(Key)
        Value:setVisible(false)
    end
    Snake["RIGHT"]:setVisible(true)

    self.CurSnakeHead = Snake["RIGHT"]
    self.CurSnakeHead:setTag(struct.SnakeDirCode().RIGHT)


    struct.SNAKE();


    for i = 0, struct.Len - 1 do
        struct.SnakePosition[i + 1] = cc.p(display.cx - i * 50,display.cy)
    end

end

function RetroSnaker:SnakeRefresh()
    local i = struct.Len;
    while(i > 1)do
        struct.SnakePosition[i].x = struct.SnakePosition[i - 1].x;
        struct.SnakePosition[i].y = struct.SnakePosition[i - 1].y;
        i = i - 1
    end
end
function RetroSnaker:SnakeBody()
    local i = struct.Len;
    while(i > 1)do
        if(self:getChildByName("SnakeBody"..i) == nil)then
            local SnakeBody = cc.Sprite:create("RetroSnaker/SnakeBody.png")
            --防止与蛇头的Tag的冲突
            SnakeBody:setName("SnakeBody"..i)
            SnakeBody:setPosition(struct.SnakePosition[i].x,struct.SnakePosition[i].y)
            self:addChild(SnakeBody)
        else
            self:getChildByName("SnakeBody"..i):setPosition(struct.SnakePosition[i].x,struct.SnakePosition[i].y)
        end

        i = i - 1
    end
end


function RetroSnaker:SnakeMove()
    print("SnakeMove")
    self:SnakeRefresh()
    --上面这个函数虽然设置了坐标,但蛇头的坐标未改变
    Switch.switch(struct.Dir)
    Switch.case(struct.SnakeDirCode().UP,
    function()
        struct.SnakePosition[1].y = struct.SnakePosition[1].y + 50;
        if (self.CurSnakeHead:getTag() ~= struct.SnakeDirCode().UP) then
            self.CurSnakeHead:setVisible(false)
            self.CurSnakeHead = Snake["UP"]
            self.CurSnakeHead:setVisible(true)
            self.CurSnakeHead:setTag(struct.SnakeDirCode().UP)
        end
        self.CurSnakeHead:setPosition(struct.SnakePosition[1])
    end )
    Switch.case(struct.SnakeDirCode().DOWN,
    function()
        struct.SnakePosition[1].y = struct.SnakePosition[1].y - 50;
        if (self.CurSnakeHead:getTag() ~= struct.SnakeDirCode().DOWN) then
            self.CurSnakeHead:setVisible(false)
            self.CurSnakeHead = Snake["DOWN"]
            self.CurSnakeHead:setVisible(true)
            self.CurSnakeHead:setTag(struct.SnakeDirCode().DOWN)
        end
        self.CurSnakeHead:setPosition(struct.SnakePosition[1])
    end )
    Switch.case(struct.SnakeDirCode().LEFT,
    function()
        struct.SnakePosition[1].x = struct.SnakePosition[1].x - 50;
        if (self.CurSnakeHead:getTag() ~= struct.SnakeDirCode().LEFT) then
            self.CurSnakeHead:setVisible(false)
            self.CurSnakeHead = Snake["LEFT"]
            self.CurSnakeHead:setVisible(true)
            self.CurSnakeHead:setTag(struct.SnakeDirCode().LEFT)
        end
        self.CurSnakeHead:setPosition(struct.SnakePosition[1])
    end )
    Switch.case(struct.SnakeDirCode().RIGHT,
    function()
        struct.SnakePosition[1].x = struct.SnakePosition[1].x + 50;
        if (self.CurSnakeHead:getTag() ~= struct.SnakeDirCode().RIGHT) then
            self.CurSnakeHead:setVisible(false)
            self.CurSnakeHead = Snake["RIGHT"]
            self.CurSnakeHead:setVisible(true)
            self.CurSnakeHead:setTag(struct.SnakeDirCode().RIGHT)
        end
        self.CurSnakeHead:setPosition(struct.SnakePosition[1])
    end )
    --头先移动再试身体移动
    self:SnakeBody()
end

function RetroSnaker:Updata(interval)
    print(interval)
    print("Update Call")

end
function RetroSnaker:UpdateTimer(Time)
    print(Time)
    print("UpdateTimer Call")
    print("Snake Dir " .. struct.Dir)
    self:SnakeMove()
end
-- 按下
function RetroSnaker:OnKeyPressed(KeyCode, Event)
    print(" OnKeyPressed " .. KeyCode)
end
-- 抬起
function RetroSnaker:OnKeyReleased(KeyCode, Event)
    print(" OnKeyReleased " .. KeyCode)

    Switch.switch(KeyCode)
    Switch.case(struct.SnakeDirCode().UP,
    function()
        print("UP")
        struct.Dir = struct.SnakeDirCode().UP;
    end )
    Switch.case(struct.SnakeDirCode().DOWN,
    function()
        print("DOWN")
        struct.Dir = struct.SnakeDirCode().DOWN;
    end )
    Switch.case(struct.SnakeDirCode().LEFT,
    function()
        print("LEFT")
        struct.Dir = struct.SnakeDirCode().LEFT;
    end )
    Switch.case(struct.SnakeDirCode().RIGHT,
    function()
        print("RIGHT")
        struct.Dir = struct.SnakeDirCode().RIGHT;
    end )

end

































function RetroSnaker:CreateFrameCall()
    -- 每一帧调用
    local function handler(interval)
        self:Updata(interval);
    end
    self:scheduleUpdateWithPriorityLua(handler, 0);
end

function RetroSnaker:CreateTimeFrameCall(Second)
    local scheduler = cc.Director:getInstance():getScheduler()

    -- 根据设置每几秒调用这个函数
    local function onTimerCallback(dt)
        self:UpdateTimer(dt)
    end

    self.schedulerID = scheduler:scheduleScriptFunc(
    function(dt)
        onTimerCallback(dt)
    end , Second, false)
end


function RetroSnaker:CreateKeyEvent()

    local function OnKeyPressed(KeyCode, Event)
        self:OnKeyPressed(KeyCode, Event)
    end
    local function OnKeyReleased(KeyCode, Event)
        self:OnKeyReleased(KeyCode, Event)
    end
    local Listener = cc.EventListenerKeyboard:create();
    Listener:registerScriptHandler(OnKeyPressed, cc.Handler.EVENT_KEYBOARD_PRESSED);
    Listener:registerScriptHandler(OnKeyReleased, cc.Handler.EVENT_KEYBOARD_RELEASED);
    cc.Director:getInstance():getEventDispatcher():addEventListenerWithSceneGraphPriority(Listener, self);
end

return RetroSnaker;

 

posted @ 2019-11-04 19:52  YZFHKMS-X  阅读(719)  评论(0编辑  收藏  举报