![Two-frame sprite animation of Link from The Legend of Zelda walking forwards.](/post/2021-06-28-sprite-grid_files/link.gif)
It’s dangerous to code alone…
tl;dr
You can use R’s image()
function to convert a matrix to a pixelly graphic.
Pixel fixation
My last post was about the {emojiscape} package, which makes a little scene out of sampled emojis.
Following a similar approach, you could write a matrix by hand and plot it via the base function image()
. Here’s a very basic example with a ‘glider’ from Conway’s Game of Life. Values of 0
are ‘dead’ cells and values of 1
are ‘live’.
glider_v <- c(0,0,0,0,0, 0,0,1,0,0, 0,0,0,1,0, 0,1,1,1,0, 0,0,0,0,0)
glider_m <- matrix(glider_v, 5) # convert to matrix
glider_m <- glider_m[, ncol(glider_m):1] # reverse cols
par(mar = rep(0, 4)) # ensure no margins
image(glider_m) # plot it
Note that I input the vector values from what would become the top left to bottom right of the output matrix. The image()
function doesn’t read them in this order, however, so I’ve added a step to reverse the column order so the plot output appears as I intended.
Also, image()
normally outputs with labelled axes, but we can effectively hide those by minimising the mar
gins par()
ameter of the plot to 0.
Reprologoducibility
But really my motivation is to make a reproducible version of this blog’s logo: an insect composed of ‘pixels’ in a 16-by-16 square.
So, I’ve hand-coded a binary vector of length 256 (i.e. 16 * 16). The 0
s and 1
s here represent background and insect pixels, respectively. I’ve used line breaks to make it easier to create and edit the vector manually.
Update
I’ve now written a little R package called {pixeltrix}, which lets you click on squares in a plot window to generate a matrix of ‘pixels’. This means you don’t have to type out any vectors by hand. You can read more in some other blog posts.
Here’s the vector that represents the logo:
logo_v <- c(
0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,
0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,
0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,
0,0,1,0,0,1,0,1,1,0,1,0,0,1,0,0,
0,0,0,1,0,1,0,1,1,0,1,0,1,0,0,0,
0,0,0,1,0,0,1,1,1,1,0,0,1,0,0,0,
0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,
0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,
0,0,1,0,1,1,0,1,1,0,1,1,0,1,0,0,
0,0,0,1,0,1,0,1,1,0,1,0,1,0,0,0,
0,0,0,0,0,1,0,1,1,0,1,0,0,0,0,0,
0,0,0,0,1,1,0,1,1,0,1,1,0,0,0,0,
0,0,0,1,0,1,0,1,1,0,1,0,1,0,0,0,
0,0,0,1,0,1,0,1,1,0,1,0,1,0,0,0,
0,0,1,0,0,1,0,1,1,0,1,0,0,1,0,0,
0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0
)
I might as well make a (non-generic) function to matrixify (definitely a word) and plot the vector, so I can then tweak a few parameters as I please.
plot_logo <- function(
x = logo_v, # vector
px = 16, # width/length of output (square)
col_0 = "black", # colour for values of 0
col_1 = "#1e8016", # colour for values of 1
lwd = 8 # to separate the squares
) {
par(mar = rep(0, 4)) # set margins outside plot region
m <- matrix(x, px) # create a matrix from the vector
m <- m[, ncol(m):1] # reverse cols
image(m, col = c(col_0, col_1)) # plot matrix, colour by number
# If line width provided, draw lines between squares
if (!is.null(lwd)) {
px_half <- px * 2
s <- seq(-1 / px_half, 1 + (1 / px_half), 1 / (px - 1))
abline(h = s, v = s, col = col_0, lwd = lwd)
}
}
Note that I added a line width argument (lwd
). If specified, horizontal and vertical lines are drawn to give the impression that the squares are ‘separated’ from each other.
Here’s the logo.
plot_logo(lwd = 2)
And here’s what happens if we remove the lines and swap the colours, for example.
plot_logo(col_0 = "#1e8016", col_1 = "black", lwd = NULL)
And given it’s Pride Month:
for (i in rainbow(7)) {
plot_logo(lwd = 1, col_0 = "white", col_1 = i)
}
Sprite delight
This approach is basically pixel-art-by-numbers, right?
So I’ve written and animated two frames of a classic videogame character, Link from The Legend of Zelda on the NES, using the {magick} package to create a gif.
There’s four colours in this one, so the vectors are no longer binary: there’s 0
for the background, 1
for green, 2
for skin and 3
for the darker spots.
The top part of the sprite doesn’t change between frames, but the bottom does. To avoid repetition, we can store the top part as a separate vector, then combine it with each frame’s lower section. It’s still a bit of a slog to input these by hand!
link_v_top <- c(
0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,
0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,
0,0,2,0,1,3,3,3,3,3,3,1,0,2,0,0,
0,0,2,0,3,3,3,3,3,3,3,3,0,2,0,0,
0,0,2,2,3,2,1,2,2,1,2,3,2,2,0,0,
0,0,2,2,3,2,3,2,2,3,2,3,2,2,0,0,
0,0,0,2,2,2,2,2,2,2,2,2,2,3,0,0
)
link_v_b1 <- c(
0,0,0,1,1,2,2,3,3,2,2,1,1,3,0,0,
0,3,3,3,3,3,2,2,2,2,1,1,3,3,3,0,
3,3,2,3,3,3,3,1,1,1,1,1,2,3,3,0,
3,2,2,2,3,3,2,3,3,1,1,2,2,2,3,0,
3,3,2,3,3,3,2,1,3,3,3,3,2,2,2,0,
3,3,2,3,3,3,2,3,3,1,1,1,1,2,0,0,
3,3,3,3,3,3,2,1,1,1,1,1,0,0,0,0,
0,2,2,2,2,2,3,0,0,3,3,3,0,0,0,0,
0,0,0,0,3,3,3,0,0,0,0,0,0,0,0,0
)
link_v_b2 <- c(
0,0,0,0,1,2,2,3,3,2,2,1,3,3,0,0,
0,0,3,3,3,3,3,2,2,2,1,1,1,2,0,0,
0,3,3,2,3,3,3,3,1,1,1,1,1,2,0,0,
0,3,2,2,2,3,3,2,3,3,1,1,3,0,0,0,
0,3,3,2,3,3,3,2,1,3,3,3,1,0,0,0,
0,3,3,2,3,3,3,2,3,3,1,1,1,0,0,0,
0,3,3,3,3,3,3,2,1,1,1,3,0,0,0,0,
0,0,2,2,2,2,2,0,0,3,3,3,0,0,0,0,
0,0,0,0,0,0,0,0,0,3,3,3,0,0,0,0
)
# Combine vectors to get frames
link_f1 <- c(link_v_top, link_v_b1)
link_f2 <- c(link_v_top, link_v_b2)
Now we have the vectors representing Link for each frame of the animation. The approach now is like before: convert this to a 16 by 16 matrix and plot it. This time I’ve got a function that also saves the plots by first opening a png()
graphics device and closing it at the end with dev.off()
. I’ve saved these to a temporary directory for the purposes of the post, rather than my local disk.
tmp <- tempdir() # store temporary folder path
# Function to write frame to temporary folder
write_link <- function(vec) {
write_path <- file.path(tmp, paste0(substitute(vec), ".png"))
png(write_path, width = 160, height = 160)
link_m <- matrix(vec, 16)
link_m <- link_m[, ncol(link_m):1]
par(mar = rep(0, 4))
link_cols <- c("white", "#7bc702", "#cc8f2d", "#6c430a")
image(link_m, col = link_cols)
dev.off()
}
# Write the frames
write_link(link_f1); write_link(link_f2)
## quartz_off_screen
## 2
## quartz_off_screen
## 2
We get a couple of messages to say that the devices have been closed, confirming the save.
Now we can use the {magick} package to create a gif: image_read()
to load both PNG frames into a single object from their save location, and then image_animate()
to combine the images into an output that flips between the two frames. You could also use image_write()
to save this object to gif format.
# Generate a gif from the saved frames
png_paths <- list.files(tmp, "*.png$", full.names = TRUE) # get file paths
frames <- magick::image_read(png_paths) # load the files
magick::image_animate(frames, fps = 2, dispose = "previous") # combine frames
I’m not sure I’ll be coding the graphics for the whole game anytime soon…
Hip to be square
I’m not the first person to think or do this in R, I’m sure.
Update
Turns out that mikefc, aka coolbutuseless, (who else?) wrote a great blog post with a method for grabbing, plotting and animating sprites with the packages {png}, {raster}, {ggplot2} and {gganimate}. Slightly less painful than writing vectors by hand!
If you want to design your own sprites rather than copy others, try my little {pixeltrix} package for interactive pixel selection from a plot window, which returns a matrix.
I did come across a really neato {pixelart} package and Shiny app by Florian Privé where you upload an image and it gets converted into a pixel form. As Florian said in his blogpost:
Kids and big kids can quickly become addicted to this
Yes. And that’s exactly why this post exists.
Let me know if you know of any more packages or whatever that do this sort of thing.
Session info
## ─ Session info ───────────────────────────────────────────────────────────────
## setting value
## version R version 4.2.0 (2022-04-22)
## os macOS Big Sur/Monterey 10.16
## system x86_64, darwin17.0
## ui X11
## language (EN)
## collate en_GB.UTF-8
## ctype en_GB.UTF-8
## tz Europe/London
## date 2022-12-11
## pandoc 2.18 @ /Applications/RStudio.app/Contents/MacOS/quarto/bin/tools/ (via rmarkdown)
##
## ─ Packages ───────────────────────────────────────────────────────────────────
## package * version date (UTC) lib source
## blogdown 1.9 2022-03-28 [1] CRAN (R 4.2.0)
## bookdown 0.26 2022-04-15 [1] CRAN (R 4.2.0)
## bslib 0.3.1 2021-10-06 [1] CRAN (R 4.2.0)
## cli 3.3.0 2022-04-25 [1] CRAN (R 4.2.0)
## digest 0.6.29 2021-12-01 [1] CRAN (R 4.2.0)
## evaluate 0.15 2022-02-18 [1] CRAN (R 4.2.0)
## fastmap 1.1.0 2021-01-25 [1] CRAN (R 4.2.0)
## fontawesome 0.2.2 2021-07-02 [1] CRAN (R 4.2.0)
## highr 0.9 2021-04-16 [1] CRAN (R 4.2.0)
## htmltools 0.5.2 2021-08-25 [1] CRAN (R 4.2.0)
## jquerylib 0.1.4 2021-04-26 [1] CRAN (R 4.2.0)
## jsonlite 1.8.0 2022-02-22 [1] CRAN (R 4.2.0)
## knitr 1.39 2022-04-26 [1] CRAN (R 4.2.0)
## magick 2.7.3 2021-08-18 [1] CRAN (R 4.2.0)
## magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.2.0)
## R6 2.5.1 2021-08-19 [1] CRAN (R 4.2.0)
## Rcpp 1.0.8.3 2022-03-17 [1] CRAN (R 4.2.0)
## rlang 1.0.2 2022-03-04 [1] CRAN (R 4.2.0)
## rmarkdown 2.14 2022-04-25 [1] CRAN (R 4.2.0)
## rstudioapi 0.14 2022-08-22 [1] CRAN (R 4.2.0)
## sass 0.4.1 2022-03-23 [1] CRAN (R 4.2.0)
## sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.2.0)
## stringi 1.7.6 2021-11-29 [1] CRAN (R 4.2.0)
## stringr 1.4.0 2019-02-10 [1] CRAN (R 4.2.0)
## xfun 0.30 2022-03-02 [1] CRAN (R 4.2.0)
## yaml 2.3.5 2022-02-21 [1] CRAN (R 4.2.0)
##
## [1] /Library/Frameworks/R.framework/Versions/4.2/Resources/library
##
## ──────────────────────────────────────────────────────────────────────────────