201442085 发表于 2024-3-28 12:36:09

帮我写一个HUD


带吃喝和速度表的

吃喝显示屏幕中间下面

ʚ 七时吉祥 ɞ 12:35:51
速度表显示在右下角


ChatGPT 发表于 2024-3-28 12:36:26

当然,我可以帮你编写一个HUD。请将以下代码放在客户端的脚本文件中:

-- 创建一个全局变量用于保存当前的饥渴度和喝水量
local hunger = 0
local thirst = 0

-- 创建一个函数用于显示吃喝信息
function DrawHungerAndThirst()
    -- 获取屏幕宽度和高度
    local screenWidth, screenHeight = GetScreenResolution()

    -- 设置文本的位置和大小
    local textX = screenWidth / 2
    local textY = screenHeight - 50
    local textSize = 0.3

    -- 绘制饥渴度和喝水量文本
    SetTextFont(4)
    SetTextProportional(1)
    SetTextScale(textSize, textSize)
    SetTextColour(255, 255, 255, 255)
    SetTextDropShadow(0, 0, 0, 0, 255)
    SetTextEdge(2, 0, 0, 0, 255)
    SetTextCentre(true)

    -- 格式化饥渴度和喝水量文本
    local hungerText = string.format("饥渴度: %.1f%%", hunger)
    local thirstText = string.format("喝水量: %.1f%%", thirst)

    -- 绘制文本
    BeginTextCommandDisplayText("STRING")
    AddTextComponentSubstringPlayerName(hungerText)
    EndTextCommandDisplayText(textX, textY)

    textY = textY + 30

    BeginTextCommandDisplayText("STRING")
    AddTextComponentSubstringPlayerName(thirstText)
    EndTextCommandDisplayText(textX, textY)
end

-- 创建一个函数用于显示速度表
function DrawSpeedometer(speed)
    -- 获取屏幕宽度和高度
    local screenWidth, screenHeight = GetScreenResolution()

    -- 设置文本的位置和大小
    local textX = screenWidth - 100
    local textY = screenHeight - 50
    local textSize = 0.3

    -- 绘制速度文本
    SetTextFont(4)
    SetTextProportional(1)
    SetTextScale(textSize, textSize)
    SetTextColour(255, 255, 255, 255)
    SetTextDropShadow(0, 0, 0, 0, 255)
    SetTextEdge(2, 0, 0, 0, 255)
    SetTextCentre(true)

    local speedText = string.format("速度: %d km/h", speed)

    -- 绘制文本
    BeginTextCommandDisplayText("STRING")
    AddTextComponentSubstringPlayerName(speedText)
    EndTextCommandDisplayText(textX, textY)
end

-- 创建一个循环函数用于更新HUD
Citizen.CreateThread(function()
    while true do
      -- 获取玩家的坐标和车辆
      local playerPed = PlayerPedId()
      local vehicle = GetVehiclePedIsIn(playerPed, false)

      -- 如果玩家在车内
      if IsPedInAnyVehicle(playerPed, false) then
            -- 获取车辆的速度
            local speed = GetEntitySpeed(vehicle) * 3.6

            -- 调用显示速度表的函数
            DrawSpeedometer(speed)
      end

      -- 调用显示吃喝信息的函数
      DrawHungerAndThirst()

      -- 暂停一秒钟
      Citizen.Wait(1000)
    end
end)

-- 创建一个事件监听函数用于更新饥渴度和喝水量
RegisterNetEvent('hud:updateHungerAndThirst')
AddEventHandler('hud:updateHungerAndThirst', function(newHunger, newThirst)
    hunger = newHunger
    thirst = newThirst
end)

请确保你在服务器的资源文件中注册并触发`hud:updateHungerAndThirst`事件,将新的`hunger`和`thirst`值传递到客户端以更新饥渴度和喝水量信息。

希望这个HUD满足你的需求!
页: [1]
查看完整版本: 帮我写一个HUD