Here's a small Nim game engine using the jester and glfw libraries (via nimgame2d) that:
- Loads a sprite sheet from
2B.png
- Splits it into 44x48 pixel sprites
- Reads them in rows, left to right
- Allows defining ranges for different animations/actions
- Supports directional input (
WASD or arrow keys) with different animations per direction
- Runs animations at 4 frames per second
- Handles jumping with the spacebar
✅ Prerequisites
You need to install these Nim packages:
nimble install glfw
nimble install nimgame2d
📁 File: 2B.png
Make sure your sprite sheet is named 2B.png and placed in the same directory as the executable.
🧠 Sprite Sheet Assumptions
Each row contains animation frames. For example:
- Row 0 = idle facing down
- Row 1 = walk down
- Row 2 = walk up
- Row 3 = walk left
- Row 4 = walk right
- Row 5 = jump (or any other action)
Each frame is 44x48 pixels.
🕹️ Code: main.nim
import nimgame2d
import math
const
TileWidth = 44
TileHeight = 48
FPS = 60
AnimationFPS = 4
FrameDelay = FPS div AnimationFPS # ~15 frames per frame change
type
Direction = enum
Down, Up, Left, Right
Player = object
x, y: float
speed: float
currentFrame: int
frameTimer: int
animStart, animEnd: int
direction: Direction
jumping: bool
jumpVelocity: float
var
player = Player(
x: 300.0,
y: 200.0,
speed: 100.0,
currentFrame: 0,
frameTimer: 0,
animStart: 0,
animEnd: 3,
direction: Down,
jumping: false,
jumpVelocity: 0.0
)
let
spriteSheet = loadTexture("2B.png")
columns = spriteSheet.width div TileWidth
rows = spriteSheet.height div TileHeight
proc getAnimationFrame(p: var Player): (int, int) =
let row = case p.direction
of Down: 0
of Up: 2
of Left: 3
of Right: 4
let col = p.currentFrame + (row * 4)
result = (col mod columns, col div columns)
proc updateAnimation(p: var Player) =
inc(p.frameTimer)
if p.frameTimer >= FrameDelay:
p.frameTimer = 0
p.currentFrame = (p.currentFrame + 1) % (p.animEnd - p.animStart + 1)
proc handleInput(p: var Player) =
if isKeyPressed(Key.Space) and not p.jumping:
p.jumpVelocity = -300.0
p.jumping = true
if isKeyDown(Key.W) or isKeyDown(Key.Up):
p.direction = Up
elif isKeyDown(Key.S) or isKeyDown(Key.Down):
p.direction = Down
elif isKeyDown(Key.A) or isKeyDown(Key.Left):
p.direction = Left
elif isKeyDown(Key.D) or isKeyDown(Key.Right):
p.direction = Right
proc update(deltaTime: float) =
handleInput(player)
if player.jumping:
player.y += player.jumpVelocity * deltaTime
player.jumpVelocity += 980.0 * deltaTime
if player.y >= 200.0:
player.y = 200.0
player.jumping = false
updateAnimation(player)
proc render() =
clearScreen(rgb(30, 30, 30))
let (col, row) = getAnimationFrame(player)
let srcRect = Rect[float](col * TileWidth, row * TileHeight, TileWidth, TileHeight)
drawTexture(spriteSheet, srcRect, Rect[float](player.x, player.y, TileWidth*2, TileHeight*2), flipX = false)
runGame(update, render, fps = FPS)
🎮 Controls
| Key | Action |
|---|
| W / ↑ | Walk Up |
| A / ← | Walk Left |
| S / ↓ | Walk Down |
| D / → | Walk Right |
| Space | Jump |
🚀 Running the Game
Save the code above as main.nim and run:
nim c -r main.nim
Make sure 2B.png is in the same folder.
🛠️ Future Improvements
- Add more animation states (e.g., attack, idle, jump animation)
- Better physics and collision
- Camera scrolling
- Tiled map support
Let me know if you want to expand this into a full RPG-style character controller!