Lua · Examples · HUD

Velocity and keystrokes

Speed under the crosshair with last-jump takeoff, and WASD boxes that light up.

Uses: velocity.state, keystrokes.state

velocity_and_keystrokes.lua
-- name: Movement HUD
-- author: Adolfjon
-- version: 1.0
-- description: Speed, takeoff and keystrokes.

ui.checkbox("keys", "Show keys", true)

local white = color(240, 242, 246)
local dim = color(240, 242, 246, 90)
local box = color(12, 14, 18, 190)
local on = color(240, 242, 246, 220)
local dark = color(10, 10, 12)
local last_speed = 0

local function key(x, y, w, label, down)
    draw.rect_filled(x, y, w, 26, down and on or box, 5)
    local tw, th = draw.text_size(label, 12)
    draw.text(x + (w - tw) / 2, y + (26 - th) / 2, label, down and dark or white, 12)
end

function on_frame()
    local v = velocity.state()
    if not v.valid then return end
    local w, h = draw.screen()
    local speed = math.floor(v.speed + 0.5)
    local col = speed >= last_speed and white or color(255, 140, 120)
    last_speed = speed
    local text = tostring(speed)
    local tw = draw.text_size(text, 26)
    draw.text_outline(w / 2 - tw / 2, h * 0.72, text, col, 26)
    if v.takeoff > 0 then
        local t = "(" .. math.floor(v.takeoff) .. ")"
        local t2 = draw.text_size(t, 14)
        draw.text_outline(w / 2 - t2 / 2, h * 0.72 + 30, t, dim, 14)
    end

    if not ui.get("keys") then return end
    local k = keystrokes.state()
    local x, y = w / 2 - 44, h * 0.80
    key(x + 30, y, 28, "W", k.w)
    key(x, y + 30, 28, "A", k.a)
    key(x + 30, y + 30, 28, "S", k.s)
    key(x + 60, y + 30, 28, "D", k.d)
    key(x, y + 60, 88, "JUMP", k.space)
end

Install it

  1. Press Copy on the code above.
  2. In the menu, open Lua and press From Clipboard. The file is named from its -- name: line.
  3. Turn the script on in the list. Its settings show on its own tab.

Change anything you like and press Save (Ctrl+S) in the editor; the script reloads at once. See Getting started for the other ways to add scripts.