Skip to content

Instantly share code, notes, and snippets.

@chasemc
Created October 22, 2021 14:10
Show Gist options
  • Save chasemc/a820500e8fa2a66e96da50dd018d082b to your computer and use it in GitHub Desktop.
Save chasemc/a820500e8fa2a66e96da50dd018d082b to your computer and use it in GitHub Desktop.
# Code is very loosely based on https://www.r-bloggers.com/r-cade-games-simulating-the-legendary-game-of-pong/
library(shiny)
library(ggvis)
library(dplyr)
# Playing field boundary lines
boundaries <- tibble(x = c(0, 50, 50, 0), y = c(0, 0, 50, 50))
paddleHeight <- tibble(x = c(0, 0), y = c(10, 20))
# Playing field boundaries (depend on cex)
xmin <- 0
xmax <- 50
ymin <- 0
ymax <- 50
# Initial position
initialBallPosition <<- tibble(x=40,y=40)
# Paddle control
psize <- 4
ypaddle <- 4
paddleSpeed <- 3
# Set direction
dx <- runif(1, .5, 1)
dy <- runif(1, .5, 1)
ui <-
fluidPage(
column(width=3),
column(width=6,
h1("PONG in R: A Proof of Concept"),
actionButton("reset","Reset"),
ggvisOutput("p"),
# The following code was modified based on https://stackoverflow.com/questions/24973549/r-shiny-key-input-binding
tags$style(type="text/css", ".recalculating {opacity: 1.0;}"),
tags$script('$(document).on("keydown", function (e) {
Shiny.onInputChange("mydata", [e.which, Math.random()] );
});')
),column(width=3)
)
server <- function(input,output,session) {
observeEvent(input$reset,{
initialBallPosition <<- tibble(x=40,y=40)
})
# Update paddle position based on keyboard input
# Key map
# left = 37
# right = 39
# up ter = 38
# down = 40
observeEvent(input$mydata,{
if((is.null(input$mydata))){
playerObjectInitial}else{
if(input$mydata[1] == 37){
# left arrow key is empty
}else if(input$mydata[1] == 39){
# right arrow key is empty
}else if(input$mydata[1] == 38 && max(paddleHeight$y) < ymax - paddleSpeed){
paddleHeight <<- paddleHeight %>% mutate(y=y+paddleSpeed)
}else if(input$mydata[1] == 40 && min(paddleHeight$y) > ymin + paddleSpeed){
paddleHeight <<- paddleHeight %>% mutate(y=y-paddleSpeed)
}
}
})
paddleMove <- reactive({
invalidateLater(100)
paddleHeight
})
ballPosition <- reactive({
invalidateLater(200)
if (initialBallPosition$x > xmax ) {
dx <<- -dx * runif(1, .9, 1.1) # Bounce
initialBallPosition <<- initialBallPosition %>% mutate(x = x + dx)
}else if (initialBallPosition$y > ymax | initialBallPosition$y < ymin){
dy <<- -dy * runif(1, .9, 1.1)
initialBallPosition <<- initialBallPosition %>% mutate(y = y + dy)
}else if((initialBallPosition$x < xmin+.1) && (initialBallPosition$y > min(paddleHeight$y) && initialBallPosition$y < max(paddleHeight$y)) ){
dx <<- -dx * runif(1, .9, 1.1) # Bounce
initialBallPosition <<- initialBallPosition %>% mutate(x = x + dx)
}else{
initialBallPosition <- initialBallPosition %>% mutate(y = y + dy)
initialBallPosition <- initialBallPosition %>% mutate(x = x + dx)
initialBallPosition <<- initialBallPosition
}
initialBallPosition
})
ggvis(x = ~x, y = ~y) %>%
layer_points(data=ballPosition,size:=100) %>%
layer_paths(data=boundaries) %>%
layer_paths(data=paddleMove, stroke := "red",strokeWidth := 5) %>%
scale_numeric( "x", domain = c(0,50)) %>%
scale_numeric( "y", domain = c(0,50)) %>%
hide_axis(scale = "x") %>%
hide_axis(scale = "y") %>%
bind_shiny("p")
}
# Run the application
shinyApp(ui = ui, server = server)
@chasemc
Copy link
Author

chasemc commented Oct 22, 2021

Copyright 2018 Chase Clark
The MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment