|
- <div class="blockcode"><blockquote>-- 创建主窗口(横屏模式:1020x600)
- local win = GUI:Win_Create("Win_1", 0, 0, 1020, 600)
- -- 黑屏界面
- local blackScreen = GUI:Layout_Create(win, "BlackScreen", 0, 0, 1020, 600, true)
- GUI:Layout_setBackGroundColorType(blackScreen, 1)
- GUI:Layout_setBackGroundColor(blackScreen, "#000000") -- 黑色背景
- -- 开始游戏按钮
- local btnStart = GUI:Button_Create(blackScreen, "BtnStart", 410, 350, "res/public/btn_normal_11.png")
- GUI:Button_setTitleText(btnStart, "开始游戏")
- -- 关闭游戏按钮
- local btnClose = GUI:Button_Create(blackScreen, "BtnClose", 410, 250, "res/public/btn_fubenan_01_1.png")
- GUI:Button_setTitleText(btnClose, "关闭游戏")
- -- 游戏区域大小
- local GRID_WIDTH = 10 -- 横向格子数
- local GRID_HEIGHT = 20 -- 纵向格子数
- local GRID_SIZE = 30 -- 每个格子的大小
- -- 游戏区域二维数组
- local gameGrid = {}
- for y = 1, GRID_HEIGHT do
- gameGrid[y] = {}
- for x = 1, GRID_WIDTH do
- gameGrid[y][x] = 0 -- 0 表示空格子
- end
- end
- -- 渲染游戏区域
- local gameArea = GUI:Layout_Create(win, "GameArea", 360, 30, GRID_WIDTH * GRID_SIZE, GRID_HEIGHT * GRID_SIZE, true)
- GUI:Layout_setBackGroundColorType(gameArea, 1)
- GUI:Layout_setBackGroundColor(gameArea, "#000000") -- 黑色背景
- GUI:setVisible(gameArea, false) -- 初始隐藏游戏区域
- -- 7 种方块形状
- local SHAPES = {
- { {1, 1, 1, 1} }, -- I
- { {1, 1}, {1, 1} }, -- O
- { {0, 1, 0}, {1, 1, 1} }, -- T
- { {1, 0, 0}, {1, 1, 1} }, -- L
- { {0, 0, 1}, {1, 1, 1} }, -- J
- { {0, 1, 1}, {1, 1, 0} }, -- S
- { {1, 1, 0}, {0, 1, 1} } -- Z
- }
- -- 当前方块
- local currentShape = SHAPES[math.random(1, #SHAPES)]
- local currentX = GRID_WIDTH / 2 - 1
- local currentY = GRID_HEIGHT - 1
- -- 渲染当前方块
- local function renderShape()
- -- 清除之前的方块
- for y = 1, GRID_HEIGHT do
- for x = 1, GRID_WIDTH do
- local cell = GUI:getChildByName(gameArea, "ShapeCell_" .. x .. "_" .. y)
- if cell then
- GUI:removeFromParent(cell)
- end
- end
- end
- -- 渲染当前方块
- for y = 1, #currentShape do
- for x = 1, #currentShape[y] do
- if currentShape[y][x] == 1 then
- local cell = GUI:Image_Create(gameArea, "ShapeCell_" .. x .. "_" .. y, (currentX + x - 1) * GRID_SIZE, (currentY + y - 1) * GRID_SIZE, "res/public/1900000550.png")
- -- GUI:Image_setScale(cell, GRID_SIZE / 32) -- 调整图片大小
- GUI:setContentSize(cell, 25, 25)
- end
- end
- end
- end
- -- 检查方块是否可以移动或旋转
- local function canMove(newShape, newX, newY)
- for y = 1, #newShape do
- for x = 1, #newShape[y] do
- if newShape[y][x] == 1 then
- local gridX = newX + x - 1
- local gridY = newY + y - 1
- if gridX < 1 or gridX > GRID_WIDTH or gridY < 1 or (gameGrid[gridY] and gameGrid[gridY][gridX] == 1) then
- return false
- end
- end
- end
- end
- return true
- end
- -- 移动方块
- local function moveShape(dx, dy)
- if canMove(currentShape, currentX + dx, currentY + dy) then
- currentX = currentX + dx
- currentY = currentY + dy
- renderShape()
- else
- -- 方块落地,固定到游戏区域
- for y = 1, #currentShape do
- for x = 1, #currentShape[y] do
- if currentShape[y][x] == 1 then
- gameGrid[currentY + y - 1][currentX + x - 1] = 1
- end
- end
- end
- -- 生成新方块
- currentShape = SHAPES[math.random(1, #SHAPES)]
- currentX = GRID_WIDTH / 2 - 1
- currentY = GRID_HEIGHT - 1
- end
- end
- -- 旋转方块
- local function rotateShape()
- local newShape = {}
- local width = #currentShape[1]
- local height = #currentShape
- -- 旋转 90 度
- for x = 1, width do
- newShape[x] = {}
- for y = 1, height do
- newShape[x][y] = currentShape[height - y + 1][x]
- end
- end
- -- 检查旋转后是否可以放置
- if canMove(newShape, currentX, currentY) then
- currentShape = newShape
- renderShape()
- end
- end
- -- 消除满行
- local function clearLines()
- for y = GRID_HEIGHT, 1, -1 do
- local full = true
- for x = 1, GRID_WIDTH do
- if gameGrid[y][x] == 0 then
- full = false
- break
- end
- end
- if full then
- -- 消除该行
- for x = 1, GRID_WIDTH do
- gameGrid[y][x] = 0
- end
- -- 上方行下移
- for yy = y, GRID_HEIGHT - 1 do
- for x = 1, GRID_WIDTH do
- gameGrid[yy][x] = gameGrid[yy + 1][x]
- end
- end
- end
- end
- end
- -- 游戏循环
- local function gameLoop()
- moveShape(0, -1) -- 方块下落
- clearLines()
- renderShape()
- end
- -- 启动定时器
- local gameTimer = nil
- -- 开始游戏
- local function startGame()
- GUI:setVisible(blackScreen, false) -- 隐藏黑屏界面
- GUI:setVisible(gameArea, true) -- 显示游戏区域
- gameTimer = GUI:schedule(win, gameLoop, 1) -- 启动游戏循环
- end
- -- 关闭游戏
- local function closeGame()
- GUI:setVisible(blackScreen, true) -- 显示黑屏界面
- GUI:setVisible(gameArea, false) -- 隐藏游戏区域
- if gameTimer then
- GUI:unSchedule(gameTimer) -- 停止游戏循环
- gameTimer = nil
- end
- end
- -- 设置按钮点击事件
- GUI:addOnClickEvent(btnStart, startGame)
- GUI:addOnClickEvent(btnClose, closeGame)
- -- 键盘控制
- -- 左键:左移
- GUI:addKeyboardEvent("KEY_LEFT_ARROW", function()
- moveShape(-1, 0)
- end)
- -- 右键:右移
- GUI:addKeyboardEvent("KEY_RIGHT_ARROW", function()
- moveShape(1, 0)
- end)
- -- 下键:加速下落
- GUI:addKeyboardEvent("KEY_DOWN_ARROW", function()
- moveShape(0, -1)
- end)
- -- 上键:旋转方块
- GUI:addKeyboardEvent("KEY_UP_ARROW", function()
- rotateShape()
- end)
复制代码
|
|