美文网首页
CocosLua3.10 贪吃蛇(主要游戏玩法,只移动)

CocosLua3.10 贪吃蛇(主要游戏玩法,只移动)

作者: 叫我29就好啦 | 来源:发表于2019-11-22 10:03 被阅读0次

参考文章:https://blog.csdn.net/qq_26437925/article/details/51842647

只做了蛇的移动,通过键盘事件来移动。并且使用+号来增加蛇的长度

local Move_Speed = 0.1
local InterVal = 50
local Snake_Size = cc.size(40, 40)
local Snake_Init_Length = 5

-- ZOrder
local ZOrder_Snake_Head = 5
local Zorder_Snake_Body = 1

-- 移动坐标位置
local Move_Direction = {
    'Left', 'Right', 'Up', 'Down',
}

local event = class('event', cc.Node)

function event:ctor()
    self:initValue()
    self:initNode()

    self:bindEvent()
    self:startScheduler()
end

function event:initValue()
    self._startScaheduler = nil
    self._snake = {}
    self._moveDirection = Move_Direction[1]
end

function event:initNode()
    self:initSnake()
end

function event:initSnake()
    for i = 1, Snake_Init_Length do
        self:snakeGrow(i == 1)
    end
end

function event:snakeGrow(isHead)
    local tailPosX, tailPosY = self:getSnakeTailPos()
    local snakeBody = self:createBody(cc.p(tailPosX, tailPosY), isHead)
    self:addChild(snakeBody)
    table.insert(self._snake, snakeBody)
end

-- 蛇头
function event:getSankeHeadPos()
    if #self._snake == 0 then
        return 0, 0
    end

    return self._snake[1].x, self._snake[1].y
end

-- 蛇尾
function event:getSnakeTailPos()
    if #self._snake == 0 then
        return 0, 0 
    end

    return self._snake[#self._snake].x, self._snake[#self._snake].y
end

-- 得到一个新位置
function event:getNewPos(x, y, dir)
    if dir == 'Left' then
        return x - 1, y
    elseif dir == 'Right' then
        return x + 1, y
    elseif dir == 'Up' then
        return x, y + 1
    elseif dir == 'Down' then
        return x, y -1 
    end

    print('getNewPos Error')
    return x, y
end

function event:updateSnake()
    if self._snake == 0 then
        return
    end

    -- 蛇头更新位置,非蛇头更新上一个位置
    for i = #self._snake, 1, -1 do
        local body = self._snake[i]
        if i == 1 then
            body.x, body.y = self:getNewPos(body.x, body.y, self._moveDirection)
        else
            local lastBody = self._snake[i - 1]
            body.x, body.y = lastBody.x, lastBody.y
        end

        self:updateBodyPos(body)
    end
end

function event:createBody(pos, isHead)
    local color = isHead and cc.c3b(150, 150, 150) or cc.c3b(50, 50, 50)
    local layout = ccui.Layout:create()
    layout:setAnchorPoint(cc.p(0.5, 0.5))
    layout:setContentSize(Snake_Size)
    layout:setBackGroundColorType(ccui.LayoutBackGroundColorType.solid) --设置颜色
    layout:setBackGroundColor(color)
    layout:setLocalZOrder(isHead and ZOrder_Snake_Head or Zorder_Snake_Body)
    layout.x = pos.x
    layout.y = pos.y

    self:updateBodyPos(layout)
    return layout
end

function event:updateBodyPos(node)
    if not node then
        return
    end

    node:setPositionX(node.x * InterVal + display.width / 2)
    node:setPositionY(node.y * InterVal + display.height /2)
end

function event:bindEvent()
    local function onKeyCallbackPressed(code, event)
        local temp = {
            'KEY_LEFT_ARROW', 'KEY_RIGHT_ARROW', 'KEY_UP_ARROW', 'KEY_DOWN_ARROW',
        }

        for i, v in ipairs(temp) do
            if code == cc.KeyCode[v] then
                self._moveDirection = Move_Direction[i]
                break
            end
        end

        if code == cc.KeyCode.KEY_EQUAL then
            self:snakeGrow(false)
        end
    end

    local function onKeyCallbackReleased(code, event)
    end

    local keyListener = cc.EventListenerKeyboard:create()
    keyListener:registerScriptHandler(onKeyCallbackPressed, cc.Handler.EVENT_KEYBOARD_PRESSED)
    keyListener:registerScriptHandler(onKeyCallbackReleased, cc.Handler.EVENT_KEYBOARD_RELEASED)
    local eventDispatcher = cc.Director:getInstance():getEventDispatcher()
    eventDispatcher:addEventListenerWithFixedPriority(keyListener, 1)
end

function event:startScheduler()
    local function callback()
        self:updateSnake()
    end

    self._startScaheduler = cc.Director:getInstance():getScheduler():scheduleScriptFunc(callback, Move_Speed, false)
end

return event
贪吃蛇,用的layer做蛇身体.png

ε≡٩(๑>₃<)۶ 请大家多多评论一起讨论讨论

相关文章

网友评论

      本文标题:CocosLua3.10 贪吃蛇(主要游戏玩法,只移动)

      本文链接:https://www.haomeiwen.com/subject/ezjfwctx.html