Space Invaders in R

space_invaders_flyer_1978So, for no particular reason except that it was kinda fun, I made some Space Invaders graphics in R. I even made the cool ‘Game Over’ text. Side note: this might be the first time that I’ve actually found the “green” colour useful.

There’s no analysis here, no fancy statistics, not even any real key message. Just some number lists that make some pixelated images to remind you of your 80s upbringing.

If there’s one redeeming feature here, it’s the (brute force) code to get a vector of pixel values to convert to a matrix and plot properly. This was a bit trickier than expected (not using the raster package) and involves some odd reversing and transposing of the values. I’m open to a better solution, as I need to do this now and then for, you know, actual work.

Why Space Invaders (if that’s even a question)? Simply because, as Wikipedia notes, it was “one of the forerunners of modern video gaming and helped expand the video game industry from a novelty to a global industry”.

The game features a series of alien invaders that must be destroyed by the player cannon before they advance to the bottom of the screen. It was one of those games that just got faster and faster until you died. No “finishing” the game; just the inevitability of destruction. Classic.

Anyway, here are the 3 sprites/aliens in both positions:

# Set the colours
sprite.colours 

# SPRITE 1
sprite1.rows <- 10
sprite1.cols <- 14
sprite1a <- c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,1,1,1,
  1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,0,0,1,1,0,0,1,1,1,0,0,1,
  1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,1,1,0,1,1,0,1,1,0,0,
  0,0,1,1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
sprite1b <- c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,1,1,1,
  1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,0,0,1,1,0,0,1,1,1,0,0,1,
  1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,1,1,0,0,1,1,1,0,0,0,0,0,1,1,0,0,1,1,0,0,1,1,0,
  0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
# Plotting
graphics.off(); x11(w=sprite1.cols/5*2,h=sprite1.rows/5)
par(mar=c(0,0,0,0), mfrow=c(1,2))
image(t(matrix(sprite1a, byrow=T, ncol=sprite1.cols)[sprite1.rows:1,]), 
  col=sprite.colours, xlab="", ylab="", xaxt='n')
image(t(matrix(sprite1b, byrow=T, ncol=sprite1.cols)[sprite1.rows:1,]),
  col=sprite.colours, xlab="", ylab="", xaxt='n', yaxt='n')

# SPRITE 2
sprite2.rows <- 10
sprite2.cols <- 13
sprite2a <- c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,
  0,1,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,1,1,0,1,1,1,0,1,1,0,0,0,1,1,1,1,1,1,
  1,1,1,1,1,0,0,1,0,1,1,1,1,1,1,1,0,1,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,1,0,
  1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
sprite2b <- c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0,0,1,0,0,
  0,1,0,0,1,0,0,1,0,1,1,1,1,1,1,1,0,1,0,0,1,1,1,0,1,1,1,0,1,1,1,0,0,1,1,1,1,1,1,
  1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,
  0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
# Plotting
graphics.off(); x11(w=sprite2.cols/5*2,h=sprite2.rows/5)
par(mar=c(0,0,0,0), mfrow=c(1,2))
image(t(matrix(sprite2a, byrow=T, ncol=sprite2.cols)[sprite2.rows:1,]), 
  col=sprite.colours, xlab="", ylab="", xaxt='n')
image(t(matrix(sprite2b, byrow=T, ncol=sprite2.cols)[sprite2.rows:1,]),
  col=sprite.colours, xlab="", ylab="", xaxt='n', yaxt='n')

# SPRITE 3
sprite3.rows <- 10
sprite3.cols <- 10
sprite3a <- c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,1,
  1,1,1,1,1,0,0,0,1,1,0,1,1,0,1,1,0,0,1,1,1,1,1,1,1,1,0,0,0,1,0,1,1,0,1,0,0,0,1,
  0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0)
sprite3b <- c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,1,
  1,1,1,1,1,0,0,0,1,1,0,1,1,0,1,1,0,0,1,1,1,1,1,1,1,1,0,0,0,0,1,0,0,1,0,0,0,0,0,
  1,0,1,1,0,1,0,0,0,1,0,1,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0)
# Plotting
graphics.off(); x11(w=sprite3.cols/5*2,h=sprite3.rows/5)
par(mar=c(0,0,0,0), mfrow=c(1,2))
image(t(matrix(sprite3a, byrow=T, ncol=sprite3.cols)[sprite3.rows:1,]),
  col=sprite.colours, xlab="", ylab="", xaxt='n')
image(t(matrix(sprite3b, byrow=T, ncol=sprite3.cols)[sprite3.rows:1,]),
  col=sprite.colours, xlab="", ylab="", xaxt='n', yaxt='n')

allsprites

Here’s the mothership:

# MOTHERSHIP
mothership.colours <- c("black","green")
mothership.rows <- 9
mothership.cols <- 17
mothership <- c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,
  0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,1,
  1,0,1,0,1,0,1,0,1,0,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,1,1,0,1,
  1,1,0,1,1,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0)
# Plotting
graphics.off(); x11(w=mothership.cols/5,h=mothership.rows/5); par(mar=c(0,0,0,0))
image(t(matrix(mothership, byrow=T, ncol=mothership.cols)[mothership.rows:1,]), 
  col=mothership.colours, xlab="", ylab="", xaxt='n', yaxt='n')

Mothership.png

Player 1:

# PLAYER 1 
player1.colours <- c("black","green")
player1.rows <- 14
player1.cols <- 15
player1 <- c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,
  1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,
  1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,
  1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
# Plotting
graphics.off(); x11(w=player1.cols/5,h=player1.rows/5); par(mar=c(0,0,0,0))
image(t(matrix(player1, byrow=T, ncol=player1.cols )[player1.rows:1,]),
  col=player1.colours, xlab="", ylab="", xaxt='n', yaxt='n')

player1

And last of all:

# GAME OVER
gameover.colours <- c("black",heat.colors(7))
gameover.rows <- 11
gameover.cols <- 70
gameover <- c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0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,0,2,2,0,0,0,0,0,0,2,2,0,0,2,2,0,0,0,0,0,0,0,1,1,1,1,1,0,1,1,0,0,0,1,1,0,1,
  1,0,0,0,1,1,0,1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,1,0,0,0,0,1,1,1,1,1,
  1,1,0,1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
# Plotting
graphics.off(); x11(w=gameover.cols/5,h=gameover.rows/5); par(mar=c(0,0,0,0))
image(t(matrix(gameover, byrow=T, ncol=gameover.cols)[gameover.rows:1,]),
  col=gameover.colours, xlab="", ylab="", xaxt='n', yaxt='n')

gameover

I have another project in mind for this one too. Stay tuned.

Advertisements
This entry was posted in graphics, R and tagged , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s