R Console Gaming

Writing Retro Games that run in the R Console

Is it even possible?

R is a great language for doing statistics and data science, but it is not designed to develop games. So obviously writing games in R is just a fun project. During the corona-lockdown I had a lot of time and started getting interested in this topic. I found out, that there are some games written in R that are running in the R console:

Other games uses the plot-window like {fun} and {Snake}. I found R games that run in shiny like Hangman and pong. Even running HTML-games in the RStudio-viewer is an option (see here)

Peter Prevos wrote a great summary article.

So, yes it is possible to write games in R!

Text Input / Output

Base R provides functions for reading a line and writing text to the console.

name <- readline("your name = ")
cat("Hello ", name)

So, thats probably all you need to write a text adventure. But I wanted to add at least some color and some sound.


To get some color output in the console, I used the package {cli}. You can select the foreground-color using col_{color}() and the background-color using bg_{color}().



So I was able to write a function, that displays a very basic “sprite” by using a space (“ “) and setting a background color. The sprite is defined as text with letters representing the color (e.g. R = red)


sprite_show <- function(txt)  {
  for (i in seq_len(nchar(txt))) {
    char <- substr(txt, i, i)
    if(char == "R") {cat(bg_red(" "))}
    if(char == "B") {cat(bg_blue(" "))}
    if(char == "C") {cat(bg_cyan(" "))}
    if(char == "G") {cat(bg_green(" "))}
    if(char == "M") {cat(bg_magenta(" "))}
    if(char == "Y") {cat(bg_yellow(" "))}
    if(char == "W") {cat(bg_white(" "))}
    if(char == "X") {cat(bg_black(" "))}
    if(char == ".") {cat(" ")}
    if(!char %in% c("R","B","C","G","M","Y","W","Y","W","X",".")) {cat(char)}
} # sprite_show() 

Now I can “draw” a golden key in the R console!

txt <- paste0(
    "..............YYYY.", "\n",
    ".............YY..YY", "\n",
    "...YYYYYYYYYYYY..YY", "\n",
    "...Y.Y.......YY..YY", "\n",
    "...Y.Y........YYYY.", "\n")

Golden Key


I found the package {beepr} that enables R to play some nice sounds that can be used for games.


These two lines of code play the “super mario” sound.


So I got all I needed and was ready to create my own Retro-Game running in the R console!

I decided to write a game that is inspired by “Mastermind”, a game that became popular in the 1970s.


You are a codebreaker and play against the computer who is the codemaker. The computer chooses a pattern of four colors (duplicates possible). You try to break the code by trying color patterns. The computer tells you the number of correct colors. Try to break the code in less steps as possible! There are 3 game-modes: Single Game, Race and Xtreme.

You find the package on CRAN and on Github.

Have fun!

Written on December 25, 2020