0 people like it.

Pong

Pong video game runnable inside fable.io. Controls are "A" for left, and "D" for right.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
53: 
54: 
55: 
56: 
57: 
58: 
59: 
60: 
61: 
62: 
63: 
64: 
65: 
66: 
67: 
68: 
69: 
70: 
71: 
72: 
73: 
74: 
75: 
76: 
77: 
78: 
79: 
module App

open Browser
open Browser.Types
open Fable.Core

let canvas = document.getElementById("game") :?> HTMLCanvasElement
let ctx = canvas.getContext_2d()

let mutable paddleX = 200.0
let paddleWidth = 80.0
let paddleHeight = 12.0

let mutable ballX = 250.0
let mutable ballY = 150.0
let mutable dx = 10.0
let mutable dy = -2.0
let ballRadius = 8.0

let mutable leftPressed = false
let mutable rightPressed = false

document.addEventListener("keydown", fun e ->
    let ke = e :?> KeyboardEvent
    if ke.key = "a" then leftPressed <- true
    if ke.key = "d" then rightPressed <- true
)

document.addEventListener("keyup", fun e ->
    let ke = e :?> KeyboardEvent
    if ke.key = "a" then leftPressed <- false
    if ke.key = "d" then rightPressed <- false
)

let drawBall() =
    ctx.beginPath()
    ctx.arc(ballX, ballY, ballRadius, 0., System.Math.PI * 2.)
    ctx.fillStyle <- U3.Case1 "red"
    ctx.fill()
    ctx.closePath()

let drawPaddle() =
    ctx.beginPath()
    ctx.rect(paddleX, 280., paddleWidth, paddleHeight)
    ctx.fillStyle <- U3.Case1 "black"
    ctx.fill()
    ctx.closePath()

let rec update (_: float) =
    ctx.clearRect(0.,0., canvas.width, canvas.height)

    drawBall()
    drawPaddle()

    if ballX + dx > float canvas.width - ballRadius || ballX + dx < ballRadius then
        dx <- -dx

    if ballY + dy < ballRadius then
        dy <- -dy
    elif ballY + dy > 280. - ballRadius then
        if ballX > paddleX && ballX < paddleX + paddleWidth then
            dy <- -dy
        else
            ballX <- 250.
            ballY <- 150.
            dx <- 2.
            dy <- -2.

    ballX <- ballX + dx
    ballY <- ballY + dy

    if leftPressed && paddleX > 0. then
        paddleX <- paddleX - 4.
    if rightPressed && paddleX < float canvas.width - paddleWidth then
        paddleX <- paddleX + 4.

    window.requestAnimationFrame(update) |> ignore

window.requestAnimationFrame(update) |> ignore
module App
val canvas : obj
val ctx : obj
val mutable paddleX : float
val paddleWidth : float
val paddleHeight : float
val mutable ballX : float
val mutable ballY : float
val mutable dx : float
val mutable dy : float
val ballRadius : float
val mutable leftPressed : bool
val mutable rightPressed : bool
val drawBall : unit -> 'a
namespace System
type Math =
  static member Abs : value: float -> float + 6 overloads
  static member Acos : d: float -> float
  static member Acosh : d: float -> float
  static member Asin : d: float -> float
  static member Asinh : d: float -> float
  static member Atan : d: float -> float
  static member Atan2 : y: float * x: float -> float
  static member Atanh : d: float -> float
  static member BigMul : a: int * b: int -> int64
  static member BitDecrement : x: float -> float
  ...
field System.Math.PI: float = 3.14159265359
val drawPaddle : unit -> 'a
val update : float -> unit
Multiple items
val float : value:'T -> float (requires member op_Explicit)

--------------------
[<Struct>]
type float = System.Double

--------------------
type float<'Measure> =
  float
val ignore : value:'T -> unit

More information

Link:http://fssnip.net/aa
Posted:yesterday
Author:Phillip Trelford
Tags: silverlight , game , async