Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #r "../src/bin/Debug/FunSharp.Library.dll"
- open Library
- let size = 8.0
- let w,h = 80.,60.
- GraphicsWindow.Width <- int (size * w)
- GraphicsWindow.Height <- int (size * h)
- let directions = [(1.,0.);(0.,1.);(-1.,0.);(0.,-1.)]
- let opposite dir = (dir + 2) % 4
- let position shape =
- (Shapes.GetLeft(shape), Shapes.GetTop(shape))
- let create_snake dir =
- let x,y = 30.,30.
- let (dx,dy) = directions.[opposite dir]
- let snake = ResizeArray()
- for i = 0 to 3 do
- let segment = Shapes.AddRectangle(size,size)
- Shapes.Move(segment, size*(x+dx*float i), size*(y+dy*float i))
- snake.Add(segment)
- snake
- let move (snake:ResizeArray<_>) (dx,dy) =
- let length = Seq.length snake
- let last = snake.[length-1]
- let first = snake.[0]
- let x = Shapes.GetLeft(first)
- let y = Shapes.GetTop(first)
- for i = length - 1 downto 1 do
- snake.[i] <- snake.[i-1]
- Shapes.Move(last, x+size*dx, y+size*dy)
- snake.[0] <- last
- let self_harm (snake:ResizeArray<_>) =
- let (x,y) = position(snake.[0])
- let mutable collided = false
- for i = 1 to snake.Count - 1 do
- let (x',y') = position(snake.[i])
- if x = x' && y = y' then
- collided <- true
- collided
- let hit_wall (snake:ResizeArray<_>) =
- let (x,y) = position(snake.[0])
- x <= 0.0 || y <= 0.0 || x >= w*size || y >= h*size
- let mutable dir = 0
- GraphicsWindow.KeyDown <- fun () ->
- let key = GraphicsWindow.LastKey
- if key = "Right" then dir <- 0
- elif key = "Down" then dir <- 1
- elif key = "Left" then dir <- 2
- elif key = "Up" then dir <- 3
- GraphicsWindow.FontSize <- 32.0
- while true do
- let prompt = Shapes.AddText("Click to Start")
- Shapes.Move(prompt, 100, 200)
- while not Mouse.IsLeftButtonDown do ()
- Shapes.Remove(prompt)
- let snake = create_snake dir
- let mutable spawn_count = 10
- let apple = Shapes.AddEllipse(size,size)
- Shapes.Move(apple, -size,-size)
- while not (self_harm snake || hit_wall snake) do
- Program.Delay(300)
- move snake (directions.[dir])
- if spawn_count > 0 then
- spawn_count <- spawn_count - 1
- if spawn_count = 0 then
- Shapes.Move(apple, float(Math.GetRandomNumber(int(w-1.))) * size, float(Math.GetRandomNumber(int(h-1.)))*size)
- else
- let (x,y) = position snake.[0]
- let (x',y') = position apple
- if x = x' && y = y' then
- let segment = Shapes.AddRectangle(size,size)
- let dx,dy = directions.[dir]
- Shapes.Move(segment,x+size*dx,y+size*dy)
- snake.Insert(0,segment)
- Shapes.Move(apple, -size, -size)
- spawn_count <- 10
- let go = Shapes.AddText("Game Over")
- Shapes.Move(go, 100, 200)
- Program.Delay(5000)
- Shapes.Remove(go)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement