How to make a pixel-art RPG menu UI in Godot 4
Pack for this guide: Crisp Pixel (free sampler on itch.io). Also fits: Crisp Pixel Icons, Crisp Quest.
Short answer: Lay the UI out at a small base resolution such as 320 x 180 and let Godot scale it up: set the stretch mode to viewport and the scale mode to integer, so every art pixel becomes a whole square of screen pixels, and set the default texture filter to Nearest. Build frames with NinePatchRect using each piece's 9-slice borders, import the pixel font with antialiasing, hinting and subpixel positioning turned off and use it at its design size, then add a dialogue box and a GridContainer of 20 px slots with 16 px icons.
1. Set up pixel-perfect scaling
project.godot (the same settings are in Project Settings; turn on Advanced Settings)
[display]
window/size/viewport_width=320
window/size/viewport_height=180
window/size/window_width_override=1280
window/size/window_height_override=720
window/stretch/mode="viewport"
window/stretch/aspect="keep"
window/stretch/scale_mode="integer"
[rendering]
textures/canvas_textures/default_texture_filter=0
2d/snap/snap_2d_transforms_to_pixel=true
2d/snap/snap_2d_vertices_to_pixel=true
- Viewport width and height are the size you design for. The window overrides open the game at 4x on a desktop.
- Stretch mode viewport renders the whole game at 320 x 180 and scales the finished image up, so nothing can be drawn between two art pixels. The other mode,
canvas_items, draws at the window's resolution: movement and rotation look smoother, but sprites can land between art pixels. - Scale mode integer only scales by whole numbers and fills the rest with black bars: a 1920 x 1080 screen shows the game at exactly 6x, and a 1366 x 768 laptop at 4x, instead of a blurry 4.27x.
- Default texture filter 0 is Nearest, so textures are never smoothed.
- Snap 2D transforms and vertices draw sprites that move by fractions of a pixel on whole pixels.
2. Import the pixel art
With the viewport stretch mode, use the 1x PNGs: Godot does the enlarging. The 2x, 3x and 4x files of a pixel kit are for layouts drawn at a larger base size, or for the canvas_items mode. Godot's default 2D import settings (lossless compression, no mipmaps) are right for pixel art. If one node still looks soft, check that its Texture > Filter is Inherit or Nearest.
The code below expects the free Crisp Pixel sampler's Oak & Brass pieces in res://ui/crisp_pixel/wood/ and the free Crisp Pixel Icons in res://icons/ (the 1x PNGs, out of their category folders).
3. Make frames with NinePatchRect
pixel_frame.gd
class_name PixelFrame
## Makes a NinePatchRect from a 9-slice piece. The four numbers are the piece's
## slice_1x borders from the Crisp Pixel sheets.json: those edges never stretch.
static func make(path: String, left: int, top: int, right: int, bottom: int, tile := false) -> NinePatchRect:
var n := NinePatchRect.new()
n.texture = load(path)
n.patch_margin_left = left
n.patch_margin_top = top
n.patch_margin_right = right
n.patch_margin_bottom = bottom
if tile: # repeat a patterned edge instead of stretching it
n.axis_stretch_horizontal = NinePatchRect.AXIS_STRETCH_MODE_TILE_FIT
n.axis_stretch_vertical = NinePatchRect.AXIS_STRETCH_MODE_TILE_FIT
return n
The numbers are the slice_1x borders from the sampler's sheets.json: the 96 x 36 dialogue box and the 32 x 32 panel have 12 px borders, the 80 x 64 window has 12, 24, 12 and 12 (its title bar is built in), and a 20 px slot has 4. Corners and ornaments sit inside those borders and the middles are flat colour, so stretching never bends the detail. Pass tile = true for a piece with a patterned edge.
For buttons, which need normal, hover and pressed looks, put the same numbers into StyleBoxTexture styles in a Theme instead; the cozy UI theme guide shows how.
4. Use a pixel font at its design size
Select the .ttf file in the FileSystem dock and set, in the Import dock:
- Antialiasing: None
- Hinting: None
- Subpixel Positioning: Disabled
- Generate Mipmaps and Multichannel Signed Distance Field: off
then click Reimport. Use the font at the size it was drawn for, or a whole multiple of it. The Crisp Pixel font is drawn at 8 px per em: at size 8 each line is 9 px tall (7 above the baseline, 2 below). Labels add 3 px between lines by default; the dialogue box below sets 1 so that three lines fit. Crisp Pixel includes this font and a 3 x 5 tiny one for stack counts; any pixel font with its design size works the same way.
5. Build the dialogue box
dialogue_box.gd
class_name DialogueBox
extends Control
## A dialogue box for a 320 x 180 game: a 9-slice frame along the bottom,
## a name plate on its top-left corner and typewriter text.
signal finished
const UI := "res://ui/crisp_pixel/wood/"
@export var font: Font
@export var font_size := 8
@export var chars_per_second := 40.0
var frame := PixelFrame.make(UI + "dialog_box.png", 12, 12, 12, 12)
var plate := PixelFrame.make(UI + "dialog_name_plate.png", 4, 0, 4, 0)
var speaker := Label.new()
var body := Label.new()
var _tween: Tween
func _ready() -> void:
position = Vector2(8, 128)
size = Vector2(304, 46)
frame.size = size
add_child(frame)
body.position = Vector2(8, 7) # dialog_box content padding: 8 px sides, 7 px top and bottom
body.size = size - Vector2(16, 14) # room for 3 lines of 10 px
body.add_theme_constant_override("line_spacing", 1)
body.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
body.visible_characters_behavior = TextServer.VC_CHARS_AFTER_SHAPING
add_child(body)
plate.position = Vector2(6, -8) # overlaps the frame's top-left corner
add_child(plate)
speaker.position = Vector2(5, 3) # the plate's content padding
plate.add_child(speaker)
for label: Label in [speaker, body]:
if font:
label.add_theme_font_override("font", font)
label.add_theme_font_size_override("font_size", font_size)
func say(who: String, text: String) -> void:
speaker.text = who
plate.size = Vector2(ceilf(speaker.get_minimum_size().x) + 10, 13)
body.text = text
body.visible_ratio = 0.0
if _tween:
_tween.kill()
_tween = create_tween()
_tween.tween_property(body, "visible_ratio", 1.0, text.length() / chars_per_second)
func _unhandled_input(event: InputEvent) -> void:
if not visible or not event.is_action_pressed("ui_accept"):
return
get_viewport().set_input_as_handled()
if body.visible_ratio < 1.0:
_tween.kill()
body.visible_ratio = 1.0 # first press shows the whole line
else:
finished.emit() # the next press moves on
- The frame is 304 x 46 at the bottom of the 320 x 180 screen, 8 px from the edges, with room for three lines of text inside the box's content padding.
- The name plate overlaps the frame's top-left corner and is as wide as the name plus its 5 px padding each side. It is a fixed-height piece (13 px), so only its sides are borders.
- The typewriter tweens
visible_ratiofrom 0 to 1.VC_CHARS_AFTER_SHAPINGlays out the whole line first, so words do not jump to the next line while they are typed. - The first
ui_acceptpress (Enter or Space by default) shows the whole line; the next emitsfinished.
6. Build the inventory grid
inventory_grid.gd
class_name InventoryGrid
extends GridContainer
## A grid of 20 px slots with a 16 px icon in each, for a 320 x 180 game.
signal slot_pressed(index: int)
const UI := "res://ui/crisp_pixel/wood/"
@export var slot_count := 12
func _ready() -> void:
columns = 6
add_theme_constant_override("h_separation", 1)
add_theme_constant_override("v_separation", 1)
for i in slot_count:
var slot := TextureButton.new()
slot.texture_normal = load(UI + "slot.png")
slot.texture_hover = load(UI + "slot_hover.png")
slot.texture_focused = load(UI + "slot_selected.png") # keyboard / gamepad cursor
var icon := TextureRect.new()
icon.name = "Icon"
icon.position = Vector2(2, 2) # the 16 px icon area inside the slot
icon.mouse_filter = Control.MOUSE_FILTER_IGNORE
slot.add_child(icon)
slot.pressed.connect(slot_pressed.emit.bind(i))
add_child(slot)
func set_item(index: int, icon: Texture2D) -> void:
(get_child(index).get_node("Icon") as TextureRect).texture = icon
- Each slot is a
TextureButtonwith the slot pieces for its normal and hover looks; the selected slot piece is drawn over it when the slot has focus, which is the cursor for keyboard and gamepad players. The arrow keys move focus between slots by themselves. - The slot is 20 px with a 16 px icon area, so the icon sits at +2, +2. Crisp Pixel Icons are drawn to fit these slots, with the same palette, 1 px outline and light direction.
- With 1 px between slots, 6 columns by 2 rows come to 125 x 41 px.
7. Put the menu together
rpg_menu.gd (the main scene's root Control)
extends Control
## Main scene: an inventory window and a dialogue box on a 320 x 180 screen.
const ICONS := "res://icons/"
const ITEMS := ["sword", "potion_red", "apple", "key_gold"]
@export var font: Font = preload("res://fonts/CrispPixel.ttf")
var window := PixelFrame.make("res://ui/crisp_pixel/wood/window.png", 12, 24, 12, 12)
var grid := InventoryGrid.new()
var dialogue := DialogueBox.new()
func _ready() -> void:
window.position = Vector2(90, 8)
window.size = Vector2(139, 68) # grid + window padding (7 left and right, 20 top, 7 bottom)
add_child(window)
var title := Label.new()
title.text = "Bag"
title.position = Vector2(7, 5) # in the 17 px title bar
title.add_theme_font_override("font", font)
title.add_theme_font_size_override("font_size", 8)
window.add_child(title)
grid.position = Vector2(7, 20)
window.add_child(grid)
for i in ITEMS.size():
grid.set_item(i, load(ICONS + ITEMS[i] + ".png"))
grid.slot_pressed.connect(_on_slot_pressed)
dialogue.font = font
add_child(dialogue)
dialogue.say("Mira", "Your bag is light today. Take this potion before the bridge.")
grid.get_child(0).grab_focus.call_deferred()
func _on_slot_pressed(index: int) -> void:
dialogue.say("Mira", "Slot %d. Choose wisely." % (index + 1))
The window is sized from its contents: the 125 x 41 grid plus the window piece's content padding (7 px left, right and bottom, 20 px at the top for the title bar) gives 139 x 68. The title sits at y = 5 in the 17 px title bar. Keep every position and size a whole number and the UI stays on the pixel grid at every scale.
8. Not pixel art? Use the same layout with a vector kit
For a painted or vector fantasy look at 960 x 540 to 1280 x 720, use the canvas_items stretch mode with the expand or keep aspect, leave the texture filter on Linear, and build the same scene from a vector kit such as Crisp Quest, whose 1x pieces are made for that range (its dialogue box is 320 x 104 with 32, 32, 32 and 38 px borders). The PixelFrame helper works unchanged; only the numbers differ.
How this was tested
Run headless in Godot 4.7.2 with the Crisp Pixel and Crisp Pixel Icons free sampler files and the Crisp Pixel font: the window reported the viewport stretch mode, integer scaling and a 320 x 180 visible area, the font imported with antialiasing, hinting and subpixel positioning off, the dialogue box typed its text, showed the whole line on the first ui_accept and finished on the second, a three-line message fitted, and all 12 slots sat on whole pixels with each icon 16 px at +2, +2.
Free assets to try
Crisp Pixel Free: 53 pixel-art pieces (35 from the Oak & Brass theme, including the dialogue box, name plate, window, panels and slots used here, and 18 icons) as PNG at 1x, 2x, 3x and 4x, with a sheets.json of sizes and 9-slice borders. Download Crisp Pixel Free.
Crisp Pixel Icons Free: 42 fantasy RPG icons at 16 x 16 (weapons, armour, potions, food, gems, keys, spells, loot) as PNG 1x to 4x. Download Crisp Pixel Icons Free.
Crisp Quest Free: 52 vector fantasy pieces (34 Carved Wood pieces and 18 icons) as PNG 1x and 2x and SVG. Download Crisp Quest Free, or get them as a ready-made Godot 4 Theme: crisp-quest-free-theme on GitHub. Try the pieces at any size in the UI kit 9-slice preview.
The full Crisp Pixel ($6.99) has 139 pieces in each of 6 themes, 157 icons, the two pixel fonts and ready-made Godot 4 themes. Crisp Pixel Icons ($4.99) has 485 icons at 16 x 16 (361 designs and 124 variants) with grid atlases and one Godot 4 AtlasTexture per icon. About Crisp Pixel Icons: Claude (an AI assistant) designed everything in this pack: every icon is written out pixel by pixel as code. Crisp Quest ($6.99) is the vector fantasy kit, with 6 material themes. All 19 Crisp packs are also in the Crisp Everything Bundle for $17.99.