# R-Cade Games: Simulating the Legendary Game of Pong

Pong is one of the earliest arcade games on the market, first released in 1972. From the day I first saw this miracle box, I wanted to know more about computers.

I learnt how to write code from the 1983 book Dr. C. Wacko’s Miracle Guide to Designing and Programming your own Atari Computer Arcade Games. This book explains in a very clear and humorous way how to write computer games in Atari basic. I devoured this book and spent many hours developing silly games. This article is an ode to Dr Wacko, a computer geek’s midlife-crisis and an attempt to replicate the software I developed thirty years ago.

I showed in a previous post that R can be used for board games or play a game of elliptical pool. The question is whether we create arcade games in R. My challenge is to recreate the look and feel of 1980s arcade games, or R-Cade games, using R? The code shown below simulates the legendary game of pong.

## Playing Pong in R

The code is based on the Wacko’s Boing Program in the above-mentioned book. The R code is fully commented and speaks for itself. Please note that the animation is very clunky when you run it in RStudio. Only the native R Terminal displays the animation correctly.

Perhaps somebody can help me perfect this little ditty. I love to know how to read real-time USB input to control the game, so we get a step closer to the first R-Cade game.

## The Pong Code

```library(beepr)

skill &lt;- 0.87 # Skill (0-1)
score &lt;- 0
high.score &lt;- 0

par(mar = rep(1,4), bg = &quot;black&quot;)
plot.new()
plot.window(xlim = c(0, 30), ylim = c(0, 30))
lines(c(1, 30, 30, 1), c(0, 0, 30, 30), type = &quot;l&quot;, lwd = 5, col = &quot;white&quot;)

xmin &lt;- 0.5
xmax &lt;- 29.4
ymin &lt;- 0.5
ymax &lt;- 29.4

x &lt;- sample(5:25, 1)
y &lt;- sample(5:25, 1)
points(x, y, pch = 15, col = &quot;white&quot;, cex = 2)

psize &lt;- 4

dx &lt;- runif(1, .5, 1)
dy &lt;- runif(1, .5, 1)

while (x &gt; xmin - 1) {
sound &lt;- 0 # Silence
Sys.sleep(.05) # Pause screen. Reduce to increase speed
points(x, y, pch = 15, col = &quot;black&quot;, cex = 2) # Erase ball
# Move ball
x &lt;- x + dx
y &lt;- y + dy
# Collision detection
if (x &gt; xmax) {
dx &lt;- -dx * runif(1, .9, 1.1) # Bounce
if (x &gt; xmin) x &lt;- xmax # Boundary
sound &lt;- 10 # Set sound
}
if (y &lt; ymin | y &gt; ymax) {
if (y &lt; ymin) y &lt;- ymin
if (y &lt; ymax) y &lt;- ymax
dy &lt;- -dy * runif(1, .9, 1.1)
sound &lt;- 10
}
if (x &lt; xmin &amp; (y &gt; ypaddle - (psize / 2)) &amp; y &lt; ypaddle + (psize / 2)) {
if (x &lt; xmin)
x &lt;- xmin
dx &lt;- -dx * runif(1, .9, 1.1)
sound &lt;- 2
score &lt;- score + 1
}
# Draw ball
points(x, y, pch = 15, col = &quot;white&quot;, cex = 2)
if (sound !=0) beep(sound)
if (runif(1, 0, 1) &lt; skill)
# Erase back line
lines(c(0, 0), c(0, 30), type = &quot;l&quot;, lwd = 8, col = &quot;black&quot;)
if (ypaddle &gt; 30 - (psize / 2)) ypaddle &lt;- 30 - (psize / 2)
lines(c(0, 0), c(ypaddle - (psize / 2), ypaddle + (psize / 2)), type = &quot;l&quot;, lwd = 8, col = &quot;white&quot;)
}
beep(8)
text(15,15, &quot;GAME OVER&quot;, cex = 5, col = &quot;white&quot;)
s &lt;- ifelse(score == 1, &quot;s &quot;)
text(15,5, paste0(score, &quot; Point&quot;, s), cex=3, col = &quot;white&quot;)
```

## 7 thoughts on “R-Cade Games: Simulating the Legendary Game of Pong”

1. Thank you – this is such a lovely idea! I remember the marvel of seeing Pong for the first time.

I tried running it with Rgui on an aged windows laptop, but it had a few problems updating and crashed. I haven’t delved into the cause. Perhaps someone could take the logic and implement it as a Shiny app or similar.

1. Thanks for the kind words. I would love to know how to capture keyboard input so that a human can control the paddle.

1. Yes that would be nice. I just tried putting the scan function inside a withTimeout (R.utils package) call to simulate a background thread watching for keystrokes (e.g. ‘k’ for up, ‘j’ for down) but couldn’t get it to work. However it could be done with a little C or Rcpp function.

1. That sounds promising. I am a C noob but if somebody can create this than I would love to create some Rcade games.

1. Hi Chase,

That is a great adaptation of my original idea.

Thanks for sharing.

Peter

This site uses Akismet to reduce spam. Learn how your comment data is processed.