3 people like it.
Like the snippet!
Flappy Bird clone using Xwt
Flappy bird clone script using Mono's Xwt toolkit, click the mouse or hit space to flap, no collision detection. To run you will need to build xwt from source https://github.com/mono/xwt
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:
|
#I @"<path to Xwt.dll>"
#r @"Xwt.dll"
open Xwt
open Xwt.Drawing
open System.IO
open System.Net
type CustomCanvas (onDraw) =
inherit Canvas()
override this.OnDraw(ctx, rect) =
base.OnDraw(ctx, rect)
onDraw ctx
Application.Initialize (ToolkitType.Gtk);
/// Bird type
type Bird = { X:float; Y:float; VY:float; IsAlive:bool }
/// Respond to flap command
let flap (bird:Bird) = { bird with VY = - System.Math.PI }
/// Applies gravity to bird
let gravity (bird:Bird) = { bird with VY = bird.VY + 0.1 }
/// Applies physics to bird
let physics (bird:Bird) = { bird with Y = bird.Y + bird.VY }
/// Updates bird with gravity & physics
let update = gravity >> physics
/// Generates the level's tube positions
let generateLevel n =
let rand = System.Random()
[for i in 1..n -> 50+(i*150), 32+rand.Next(160)]
/// Loads an image from a file or url
let load (file:string) (url:string) =
let path = Path.Combine(__SOURCE_DIRECTORY__, file)
if File.Exists path then Image.FromFile path
else
let request = HttpWebRequest.Create(url)
use response = request.GetResponse()
use stream = response.GetResponseStream()
Image.FromStream(stream)
let bg = load "bg.png" "http://flappycreator.com/default/bg.png"
let ground = load "ground.png" "http://flappycreator.com/default/ground.png"
let tube1 = load "tube1.png" "http://flappycreator.com/default/tube1.png"
let tube2 = load "tube2.png" "http://flappycreator.com/default/tube2.png"
let bird_sing = load "bird_sing.png" "http://flappycreator.com/default/bird_sing.png"
let level = generateLevel 10
let scroll = ref 0
let flappy = ref { X = 30.0; Y = 150.0; VY = 0.0; IsAlive=true }
let flapMe () = if (!flappy).IsAlive then flappy := flap !flappy
let onDraw (ctx:Context) =
let draw image (x:int,y:int) = ctx.DrawImage(image,float x,float y)
draw bg (0,0)
draw bird_sing (int (!flappy).X, int (!flappy).Y)
let drawTube (x,y) =
draw tube1 (x - !scroll, y - 320)
draw tube2 (x - !scroll, y + 100)
for (x,y) in level do drawTube (x,y)
draw ground (0,340)
let w = new Window(Title="Flap me", Width=288.0, Height=440.0)
w.Padding <- WidgetSpacing()
let canvas = new CustomCanvas(onDraw)
canvas.KeyPressed.Add (fun args -> if args.Key = Key.Space then flapMe())
canvas.ButtonPressed.Add (fun args -> flapMe())
canvas.CanGetFocus <- true
canvas.SetFocus()
w.Content <- canvas
w.Show ()
w.Closed.Add(fun e -> Application.Exit())
let invalidate () =
incr scroll
flappy := update !flappy
canvas.QueueDraw()
Application.TimeoutInvoke(15, fun () -> invalidate(); true)
Application.Run ()
|
namespace System
namespace System.IO
namespace System.Net
Multiple items
type CustomCanvas =
inherit obj
new : onDraw:(obj -> obj) -> CustomCanvas
override OnDraw : ctx:'a * rect:'b -> 'c
Full name: Script.CustomCanvas
--------------------
new : onDraw:(obj -> obj) -> CustomCanvas
val onDraw : (obj -> obj)
override CustomCanvas.OnDraw : ctx:'a * rect:'b -> 'c
Full name: Script.CustomCanvas.OnDraw
type Bird =
{X: float;
Y: float;
VY: float;
IsAlive: bool;}
Full name: Script.Bird
Bird type
Bird.X: float
Multiple items
val float : value:'T -> float (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.float
--------------------
type float = System.Double
Full name: Microsoft.FSharp.Core.float
--------------------
type float<'Measure> = float
Full name: Microsoft.FSharp.Core.float<_>
Bird.Y: float
Bird.VY: float
Bird.IsAlive: bool
type bool = System.Boolean
Full name: Microsoft.FSharp.Core.bool
val flap : bird:Bird -> Bird
Full name: Script.flap
Respond to flap command
val bird : Bird
type Math =
static val PI : float
static val E : float
static member Abs : value:sbyte -> sbyte + 6 overloads
static member Acos : d:float -> float
static member Asin : d:float -> float
static member Atan : d:float -> float
static member Atan2 : y:float * x:float -> float
static member BigMul : a:int * b:int -> int64
static member Ceiling : d:decimal -> decimal + 1 overload
static member Cos : d:float -> float
...
Full name: System.Math
field System.Math.PI = 3.14159265359
val gravity : bird:Bird -> Bird
Full name: Script.gravity
Applies gravity to bird
val physics : bird:Bird -> Bird
Full name: Script.physics
Applies physics to bird
val update : (Bird -> Bird)
Full name: Script.update
Updates bird with gravity & physics
val generateLevel : n:int -> (int * int) list
Full name: Script.generateLevel
Generates the level's tube positions
val n : int
val rand : System.Random
Multiple items
type Random =
new : unit -> Random + 1 overload
member Next : unit -> int + 2 overloads
member NextBytes : buffer:byte[] -> unit
member NextDouble : unit -> float
Full name: System.Random
--------------------
System.Random() : unit
System.Random(Seed: int) : unit
val i : int
System.Random.Next() : int
System.Random.Next(maxValue: int) : int
System.Random.Next(minValue: int, maxValue: int) : int
val load : file:string -> url:string -> 'a
Full name: Script.load
Loads an image from a file or url
val file : string
Multiple items
val string : value:'T -> string
Full name: Microsoft.FSharp.Core.Operators.string
--------------------
type string = System.String
Full name: Microsoft.FSharp.Core.string
val url : string
val path : string
type Path =
static val DirectorySeparatorChar : char
static val AltDirectorySeparatorChar : char
static val VolumeSeparatorChar : char
static val InvalidPathChars : char[]
static val PathSeparator : char
static member ChangeExtension : path:string * extension:string -> string
static member Combine : [<ParamArray>] paths:string[] -> string + 3 overloads
static member GetDirectoryName : path:string -> string
static member GetExtension : path:string -> string
static member GetFileName : path:string -> string
...
Full name: System.IO.Path
Path.Combine([<System.ParamArray>] paths: string []) : string
Path.Combine(path1: string, path2: string) : string
Path.Combine(path1: string, path2: string, path3: string) : string
Path.Combine(path1: string, path2: string, path3: string, path4: string) : string
type File =
static member AppendAllLines : path:string * contents:IEnumerable<string> -> unit + 1 overload
static member AppendAllText : path:string * contents:string -> unit + 1 overload
static member AppendText : path:string -> StreamWriter
static member Copy : sourceFileName:string * destFileName:string -> unit + 1 overload
static member Create : path:string -> FileStream + 3 overloads
static member CreateText : path:string -> StreamWriter
static member Decrypt : path:string -> unit
static member Delete : path:string -> unit
static member Encrypt : path:string -> unit
static member Exists : path:string -> bool
...
Full name: System.IO.File
File.Exists(path: string) : bool
val request : WebRequest
type HttpWebRequest =
inherit WebRequest
member Abort : unit -> unit
member Accept : string with get, set
member AddRange : range:int -> unit + 7 overloads
member Address : Uri
member AllowAutoRedirect : bool with get, set
member AllowWriteStreamBuffering : bool with get, set
member AutomaticDecompression : DecompressionMethods with get, set
member BeginGetRequestStream : callback:AsyncCallback * state:obj -> IAsyncResult
member BeginGetResponse : callback:AsyncCallback * state:obj -> IAsyncResult
member ClientCertificates : X509CertificateCollection with get, set
...
Full name: System.Net.HttpWebRequest
WebRequest.Create(requestUri: System.Uri) : WebRequest
WebRequest.Create(requestUriString: string) : WebRequest
val response : WebResponse
WebRequest.GetResponse() : WebResponse
val stream : Stream
WebResponse.GetResponseStream() : Stream
val bg : obj
Full name: Script.bg
val ground : obj
Full name: Script.ground
val tube1 : obj
Full name: Script.tube1
val tube2 : obj
Full name: Script.tube2
val bird_sing : obj
Full name: Script.bird_sing
val level : (int * int) list
Full name: Script.level
val scroll : int ref
Full name: Script.scroll
Multiple items
val ref : value:'T -> 'T ref
Full name: Microsoft.FSharp.Core.Operators.ref
--------------------
type 'T ref = Ref<'T>
Full name: Microsoft.FSharp.Core.ref<_>
val flappy : Bird ref
Full name: Script.flappy
val flapMe : unit -> unit
Full name: Script.flapMe
val onDraw : ctx:'a -> 'b
Full name: Script.onDraw
val ctx : 'a
val draw : ('c -> int * int -> 'd)
val image : 'c
val x : int
Multiple items
val int : value:'T -> int (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.int
--------------------
type int = int32
Full name: Microsoft.FSharp.Core.int
--------------------
type int<'Measure> = int
Full name: Microsoft.FSharp.Core.int<_>
val y : int
val drawTube : (int * int -> 'c)
val w : obj
Full name: Script.w
val canvas : CustomCanvas
Full name: Script.canvas
val invalidate : unit -> 'a
Full name: Script.invalidate
val incr : cell:int ref -> unit
Full name: Microsoft.FSharp.Core.Operators.incr
More information