|
- -- 创建主窗口(横屏模式: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 gameArea = GUI:Layout_Create(win, "GameArea", 0, 0, 1020, 600, true)
- GUI:Layout_setBackGroundColorType(gameArea, 1)
- GUI:Layout_setBackGroundColor(gameArea, "#000000") -- 黑色背景
- GUI:setVisible(gameArea, false) -- 初始隐藏游戏区域
- -- 赛道背景
- local track = GUI:Image_Create(gameArea, "Track", 0, 0, "res/public/lu.png")
- GUI:setContentSize(track, 1020, 600) -- 设置背景大小
- -- 玩家赛车
- local car = GUI:Image_Create(gameArea, "Car", 480, 100, "res/public/car.png")
- GUI:setContentSize(car, 50, 50) -- 设置赛车大小
- -- 障碍物
- local obstacles = {}
- local OBSTACLE_SPEED = 5 -- 障碍物下落速度
- local CAR_SPEED = 10 -- 赛车移动速度
- -- 得分
- local score = 0
- local scoreText = GUI:Text_Create(gameArea, "ScoreText", 500, 550, 24, "#ffffff", "得分: 0") -- 修正文本创建
- -- 游戏状态
- local isGameOver = false
- -- 生成障碍物
- local function createObstacle()
- local x = math.random(100, 920) -- 随机生成障碍物的 X 坐标
- local obstacle = GUI:Image_Create(gameArea, "Obstacle_" .. #obstacles + 1, x, 700, "res/public/zhangai.png")
- GUI:setContentSize(obstacle, 50, 50) -- 设置障碍物大小
- SL:print("障碍物", GUI:getName(obstacle))
- table.insert(obstacles, obstacle)
- end
- -- 更新障碍物位置
- local function updateObstacles()
- for i = #obstacles, 1, -1 do
- -- SL:print(#obstacles,GUI:getName(obstacles[i]))
- local obstacle = obstacles[i]
-
- local pos = GUI:getPosition(obstacle)
-
-
- x,y = pos.x , pos.y
- GUI:setPosition(obstacle, x, y-30)
- -- 如果障碍物超出屏幕,移除
- if y < -50 then
- GUI:removeFromParent(obstacle)
- table.remove(obstacles, i)
- score = score + 1
- GUI:Text_setString(scoreText, "得分: " .. score)
- end
- -- 碰撞检测
- local carpos = GUI:getPosition(car)
- carX,carY = carpos.x , carpos.y
- if math.abs(x - carX) < 50 and math.abs(y - carY) < 50 then
- isGameOver = true
- end
- end
- end
- -- 游戏循环
- local function gameLoop()
- if isGameOver then
- GUI:Text_setString(scoreText, "游戏结束! 得分: " .. score)
- return
- end
- -- 更新障碍物
- updateObstacles()
- -- 生成新障碍物
- if math.random(1, 20) == 1 then
-
- createObstacle()
- end
- end
- -- 启动定时器
- local gameTimer = nil
- -- 开始游戏
- local function startGame()
- GUI:setVisible(blackScreen, false) -- 隐藏黑屏界面
- GUI:setVisible(gameArea, true) -- 显示游戏区域
- isGameOver = false
- score = 0
- GUI:Text_setString(scoreText, "得分: 0")
- -- 清空障碍物
- for i = #obstacles, 1, -1 do
- GUI:removeFromParent(obstacles[i])
- table.remove(obstacles, i)
- end
- -- 启动游戏循环
- gameTimer = GUI:schedule(win, gameLoop, 0.05) -- 每 0.05 秒更新一次
- 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()
- local pos = GUI:getPosition(car)
-
- -- GUI:setPosition(obstacle, x, y)
- x,y = pos.x , pos.y
- x = x - CAR_SPEED
- if x < 100 then x = 100 end -- 限制左边界
- GUI:setPosition(car, x, y)
- end)
- -- 右键:右移
- GUI:addKeyboardEvent("KEY_RIGHT_ARROW", function()
- local pos = GUI:getPosition(car)
-
- -- GUI:setPosition(obstacle, x, y)
- x,y = pos.x , pos.y
- x = x + CAR_SPEED
- if x > 920 then x = 920 end -- 限制右边界
- GUI:setPosition(car, x, y)
- end)
复制代码
|
|