Skip to content

Instantly share code, notes, and snippets.

@ptrelford
Created August 24, 2015 23:05
Show Gist options
  • Save ptrelford/8881f4cc4137f1d3858f to your computer and use it in GitHub Desktop.
Save ptrelford/8881f4cc4137f1d3858f to your computer and use it in GitHub Desktop.
MiniGolf with SmallSharp
open Library
GraphicsWindow.BackgroundColor <- Colors.LawnGreen
GraphicsWindow.BrushColor <- Colors.Black
let w,h = float GraphicsWindow.Width, float GraphicsWindow.Height
let holeX, holeY = w/2.0, h/4.0
GraphicsWindow.FillEllipse(holeX-20.,holeY-20.,40.,40.)
let mutable leftPressed,rightPressed,spacePressed = false, false, false
let pressed down =
if GraphicsWindow.LastKey = "Left" then leftPressed <- down
if GraphicsWindow.LastKey = "Right" then rightPressed <- down
if GraphicsWindow.LastKey = "Space" then spacePressed <- down
GraphicsWindow.KeyDown <- fun () -> pressed true
GraphicsWindow.KeyUp <- fun () -> pressed false
GraphicsWindow.BrushColor <- Colors.White
let ball = Shapes.AddEllipse(30, 30)
let rand = System.Random()
let rec play () =
let x,y = w/2.0-150.+rand.NextDouble()*300.,(h*0.8)-50.+rand.NextDouble()*100.
Shapes.Move(ball, x-15., y-15.)
let mutable a = 90.
while not spacePressed do
let r = a * System.Math.PI / 180.0
let dx, dy = cos r, sin r
let line = Shapes.AddLine(x,y,x-dx*50.,y-dy*50.)
if leftPressed then a <- a - 1.
if rightPressed then a <- a + 1.
Program.Delay(10)
Shapes.Remove(line)
put (x,y,a)
and put (x,y,a) =
let mutable x,y = x,y
let r = a * System.Math.PI / 180.0
let dx, dy = cos r, sin r
let mutable count = 0
while not (pot(x,y)) && count < 320 do
Shapes.Move(ball, x-15., y-15.)
Program.Delay(10)
y <- y - dy
x <- x - dx
count <- count + 1
if pot(x,y) then GraphicsWindow.Title <- "Pot!"
else GraphicsWindow.Title <- "Miss"
play ()
and pot (ballX,ballY) =
let d = sqrt ((holeX - ballX) ** 2.0 + (holeY - ballY) ** 2.0)
d < 8.0
play()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment