<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/r-cade-games-simulating-the-legendary-game-of-pong/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r.prevos.net/pong/"> The Devil is in the Data</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><img sizes="(max-width: 300px) 100vw, 300px" data-image-description="" data-orig-file="http://r.prevos.net/wp-content/uploads/sites/11/2017/04/pong.png" data-orig-size="848,598" data-image-title="Pong" class="wp-image-621" data-comments-opened="1" data-recalc-dims="1" data-attachment-id="621" data-permalink="http://r.prevos.net/pong/pong-2/" alt="The legendary game of Pong" data-medium-file="https://i1.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/04/pong-300x212.png?resize=300%2C212" data-large-file="http://r.prevos.net/wp-content/uploads/sites/11/2017/04/pong.png" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://i1.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/04/pong-300x212.png?resize=300%2C212">Pong is one of the earliest <a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/Arcade_game">arcade games</a> on the market, first released in 1972. From the day I first saw this miracle box, I wanted to know more about computers.</p>
<p>I learnt how to write code from the 1983 book <a target="_blank" rel="nofollow" href="https://archive.org/details/Dr_Wackos_Guide_to_Creating_Arcade_Games">Dr. C. Wacko’s Miracle Guide to Designing and Programming your own Atari Computer Arcade Games</a>. 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.</p>
<p>I showed in a previous post that R can be used for board <a target="_blank" rel="nofollow" href="http://r.prevos.net/tag/games/">games</a>. The question is whether we create arcade games in R. My challenge is to recreate the look and feel of 1980s arcade games, or <em>R-Cade</em> games, using R? The code shown below simulates the legendary game of pong.</p>
<h2>Playing Pong in R</h2>
<p>The code is based on the Wacko’s<em> Boing Program</em> 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 <a target="_blank" rel="nofollow" href="https://support.rstudio.com/hc/en-us/articles/200488548-Problem-with-Plots-or-Graphics-Device">RStudio</a>. Only the native R Terminal displays the animation correctly.</p>
<p>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.</p>
<div class="wp-video"><br>
<video controls="controls" preload="metadata" width="450" class="wp-video-shortcode"><source src="http://r.prevos.net/wp-content/uploads/sites/11/2017/04/Rpong.mp4?_=1" type="video/mp4"><a target="_blank" rel="nofollow" href="http://r.prevos.net/wp-content/uploads/sites/11/2017/04/Rpong.mp4">http://r.prevos.net/wp-content/uploads/sites/11/2017/04/Rpong.mp4</a></video></div>
<h2>The Pong Code</h2>
<pre>
# Sound library
library(beepr) 

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

# Define playing field
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;)

# Playing field boundaries (depend on cex)
xmin &lt;- 0.5
xmax &lt;- 29.4
ymin &lt;- 0.5
ymax &lt;- 29.4

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

# Paddle control
psize &lt;- 4
ypaddle &lt;- y

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

# Game play 
while (x > 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 > xmax) {
        dx &lt;- -dx * runif(1, .9, 1.1) # Bounce 
        if (x > xmin) x &lt;- xmax # Boundary
        sound &lt;- 10 # Set sound
        }
    if (y &lt; ymin | y > ymax) {
        if (y &lt; ymin) y &lt;- ymin 
        if (y > ymax) y &lt;- ymax
        dy &lt;- -dy * runif(1, .9, 1.1)
        sound &lt;- 10
    }
    # Caught by paddle?
    if (x &lt; xmin &amp; (y > 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)
    # Move paddle
    if (runif(1, 0, 1) &lt; skill) ypaddle &lt;- ypaddle + dy # Imperfect follow
    # Draw paddle
    # Erase back line
    lines(c(0, 0), c(0, 30), type = &quot;l&quot;, lwd = 8, col = &quot;black&quot;)
    # Keep paddle inside window
    if (ypaddle &lt; (psize / 2)) ypaddle &lt;- (psize / 2) 
    if (ypaddle > 30 - (psize / 2)) ypaddle &lt;- 30 - (psize / 2) 
    # Draw paddle 
    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;&quot;, &quot;s&quot;)
text(15,5, paste0(score, &quot; Point&quot;, s), cex=3, col = &quot;white&quot;) 
</pre>
<p>The post <a target="_blank" rel="nofollow" href="http://r.prevos.net/pong/">R-Cade Games: Simulating the Legendary Game of Pong</a> appeared first on <a target="_blank" rel="nofollow" href="http://r.prevos.net/">The Devil is in the Data</a>.</p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://r.prevos.net/pong/"> The Devil is in the Data</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/TMxVvhQR2G8"></a></div>