0 people like it.
Like the snippet!
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:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
|
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 = 2.0
let mutable dy = -2.0
let ballRadius = 8.0
let mutable leftPressed = false
let mutable rightPressed = false
let mutable score = 0
let mutable gameOver = false
document.addEventListener("keydown", fun e ->
let ke = e :?> KeyboardEvent
if ke.key = "a" then leftPressed <- true
if ke.key = "d" then rightPressed <- true
if ke.key = "r" && gameOver then // restart feature
ballX <- 250.
ballY <- 150.
dx <- 2.0
dy <- -2.0
paddleX <- 200.
score <- 0
gameOver <- false
)
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 drawScore() =
ctx.font <- "16px Arial"
ctx.fillStyle <- U3.Case1 "black"
ctx.fillText($"Score: {score}", 10., 20.)
let drawGameOver() =
ctx.font <- "32px Arial"
ctx.fillStyle <- U3.Case1 "red"
ctx.fillText("GAME OVER", float canvas.width / 2. - 100., float canvas.height / 2.)
ctx.font <- "20px Arial"
ctx.fillStyle <- U3.Case1 "black"
ctx.fillText("Press R to Restart", float canvas.width / 2. - 100., float canvas.height / 2. + 40.)
let rec update (_: float) =
ctx.clearRect(0.,0., canvas.width, canvas.height)
drawBall()
drawPaddle()
drawScore()
if not gameOver then
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
score <- score + 1
// optional: gradually increase speed
dx <- if dx > 0. then 2.0 + float(score) * 0.1 else -2.0 - float(score) * 0.1
dy <- if dy > 0. then 2.0 + float(score) * 0.1 else -2.0 - float(score) * 0.1
else
gameOver <- true
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.
else
drawGameOver()
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 mutable score : int
val mutable gameOver : 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 drawScore : unit -> 'a
val drawGameOver : unit -> 'a
Multiple items
val float : value:'T -> float (requires member op_Explicit)
--------------------
[<Struct>]
type float = System.Double
--------------------
type float<'Measure> =
float
val update : float -> unit
val not : value:bool -> bool
val ignore : value:'T -> unit
More information