Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 36 ยท 2D Games

Sprites and TileMaps in 2D

Draw 2D art in Godot 4: Sprite2D, AnimatedSprite2D, sprite sheets, texture filtering, TileSet atlases, TileMapLayer painting and terrain autotiling, with Python stand-ins.

Almost every 2D game is made of two kinds of pictures: characters and objects that move on their own, and a world built from a grid of reusable tiles. Godot gives you a node for each. Sprite2D and AnimatedSprite2D show images and animations. TileMapLayer paints thousands of tiles from one small texture and keeps them cheap to draw and to collide with.

This lesson shows how both work and why. The real Godot code is shown read-only, because the Godot editor and engine cannot run on this site. Wherever an idea can be shown in plain logic, you get a Python stand-in: a tiny model of what the engine does, clearly labelled, that you can edit and run. Continue with player movement once you can place art on the screen.

Sprite2D: one image, one node

A Sprite2D has a texture property and a position, rotation and scale inherited from Node2D. The origin is the center of the image by default (centered = true), which makes rotation around the middle easy.

GDSCRIPT
extends Sprite2D

func _ready() -> void:
	texture = preload("res://art/hero.png")
	position = Vector2(160, 90)
	scale = Vector2(2, 2)
	flip_h = true   # mirror horizontally instead of making a second image

Pixel art needs one project setting: Project Settings > Rendering > Textures > Default Texture Filter = Nearest. Without it, Godot smooths the pixels and the art looks blurry. You can also set texture_filter per node.

Sprite sheets: hframes, vframes and frame

A sprite sheet packs many frames in one image. Tell the Sprite2D how many columns (hframes) and rows (vframes) it has, then pick a cell with frame:

GDSCRIPT
@onready var sprite: Sprite2D = $Sprite2D

func _ready() -> void:
	sprite.hframes = 4
	sprite.vframes = 2
	sprite.frame = 5   # row 1, column 1 (counting from 0)

The engine converts the frame number into a rectangle of the texture. The idea is simple enough to model.

Python Stand-in (Python, not Godot): frame number to pixel rectangle

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

AnimatedSprite2D and SpriteFrames

When a sprite plays several named animations ("idle", "run", "jump"), use AnimatedSprite2D. In the Inspector, create a SpriteFrames resource, add animations, drop frames in, and set the speed in frames per second (FPS).

GDSCRIPT
@onready var anim: AnimatedSprite2D = $AnimatedSprite2D

func _physics_process(_delta: float) -> void:
	if velocity.x != 0.0:
		anim.play("run")
		anim.flip_h = velocity.x < 0.0
	else:
		anim.play("idle")

play() with the name that is already playing does not restart it, so calling it every frame is safe. Use animation_finished for one-shot animations such as "attack". For richer control (moving hitboxes and sounds in sync), use an AnimationPlayer instead.

Python Stand-in (Python, not Godot): choosing a frame from time and FPS

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

TileSet and TileMapLayer

A TileSet is a resource that describes your tiles: it points at a texture atlas, says how large each tile is (for example 16 by 16 pixels), and can attach physics, navigation and custom data to each tile. A TileMapLayer node (Godot 4.3 and later; older 4.x versions used one TileMap node with several layers) paints tiles from a TileSet on a grid.

The workflow in the editor:

  1. Add a TileMapLayer node to your level scene.
  2. In the Inspector, create a new TileSet and set the Tile Size.
  3. Open the TileSet panel at the bottom, drag in your atlas texture, and accept the automatic tile creation.
  4. In the same TileSet resource, add a Physics Layer so tiles can block the player, then paint a collision polygon on the tiles that should be solid.
  5. Switch to the TileMap panel, pick a tile, and paint with the pencil, rectangle or bucket tools.

Each painted cell is addressed by integer grid coordinates (Vector2i), not pixels. Convert between them with local_to_map() and map_to_local().

GDSCRIPT
@onready var ground: TileMapLayer = $Ground

func _ready() -> void:
	# source_id 0 = the first atlas, atlas_coords = which tile in it
	ground.set_cell(Vector2i(3, 5), 0, Vector2i(1, 0))
	var cell: Vector2i = ground.local_to_map(get_global_mouse_position())
	print("Mouse is over cell ", cell)
	ground.erase_cell(Vector2i(3, 5))
Python Stand-in (Python, not Godot): world position to grid cell and back

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that (-1, -1) lands in cell (-1, -1), not (0, 0): floor division rounds toward negative infinity, so tiles to the left of and above the origin work too.

Terrains: autotiling

Painting every edge and corner tile by hand is slow. A terrain set in the TileSet tells Godot which tiles connect to which neighbours. You paint with the "Terrains" tab and Godot picks the correct edge, corner or middle tile for you. The classic technique is a bitmask: each of the four (or eight) neighbours that has the same terrain sets one bit, and the resulting number selects the tile.

Python Stand-in (Python, not Godot): a 4-neighbour autotile bitmask

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Each number picks one of 16 tile variants. Godot's terrain system does the same thing, including diagonals, so you never calculate it by hand.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A sprite sheet has 4 columns and 2 rows. Which properties of Sprite2D select the cell?

    Set hframes (columns) and vframes (rows), then choose a cell with frame. region_rect is a different way to show a rectangle of the texture.
  2. 2. Why does pixel art look blurry in a new Godot project?

    Linear filtering blends neighbouring pixels. Set Default Texture Filter to Nearest in Project Settings (or per node) for crisp pixels.
  3. 3. What do TileMapLayer.local_to_map() and map_to_local() convert between?

    local_to_map() turns a position into a Vector2i cell; map_to_local() returns the center of a cell as a position.
  4. 4. What is a terrain set used for?

    A terrain set describes how tiles connect, so Godot can pick the right variant based on neighbours.

Summary

  • Sprite2D shows one texture; hframes, vframes and frame pick a cell from a sheet.
  • AnimatedSprite2D plays named animations stored in a SpriteFrames resource.
  • Use Nearest texture filtering for pixel art.
  • A TileSet describes tiles, collisions and terrains; TileMapLayer paints them on an integer grid.
  • local_to_map() and map_to_local() convert between pixels and grid cells.
  • Terrains autotile by looking at neighbours, like the bitmask above.

Next: Player Movement with CharacterBody2D