5 people like it.

Flappy Bird clone using WinForms

Flappy bird clone script using WinForms, click the mouse or hit space to flap, no collision detection.

 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: 
// Flappy bird prototype using Windows Forms
// Note: no collision detection

#if INTERACTIVE
#r "System.Drawing.dll"
#r "System.Windows.Forms.dll"
#endif
open System.IO
open System.Drawing
open System.Windows.Forms
open System.Net

/// Double-buffered form
type CompositedForm () =
   inherit Form()
   override this.CreateParams = 
      let cp = base.CreateParams
      cp.ExStyle <- cp.ExStyle ||| 0x02000000
      cp

/// 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"

/// Bird type
type Bird = { X:float; Y:float; VY:float }
/// 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

/// Paints the game scene
let paint (graphics:Graphics) scroll level (flappy:Bird) =
   let draw (image:Image) (x,y) =
      graphics.DrawImage(image,x,y,image.Width,image.Height)
   draw bg (0,0)
   draw bird_sing (int flappy.X, int flappy.Y)
   let drawTube (x,y) =      
      draw tube1 (x-scroll,-320+y)
      draw tube2 (x-scroll,y+100)
   for (x,y) in level do drawTube (x,y)
   draw ground (0,340)
    
/// 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)]

let level = generateLevel 10
let scroll = ref 0
let flappy = ref { X = 30.0; Y = 150.0; VY = 0.0}

let form = new CompositedForm(Text="Flap me",Width=288,Height=440)
form.Paint.Add(fun args ->
   flappy := update !flappy
   paint args.Graphics !scroll level !flappy; 
   incr scroll)

let flapme () =  flappy := flap !flappy
// Respond to mouse clicks
form.Click.Add(fun args -> flapme())
// Respond to space key
form.KeyDown.Add(fun args -> if args.KeyCode = Keys.Space then flapme())
// Show form
form.Show()
// Update form asynchronously every 18ms
async { 
   while true do
      do! Async.Sleep(18)
      form.Invalidate() 
} |> Async.StartImmediate
namespace System
namespace System.IO
namespace System.Drawing
namespace System.Windows
namespace System.Windows.Forms
namespace System.Net
Multiple items
type CompositedForm =
  inherit Form
  new : unit -> CompositedForm
  override CreateParams : CreateParams

Full name: Script.CompositedForm


 Double-buffered form


--------------------
new : unit -> CompositedForm
Multiple items
type Form =
  inherit ContainerControl
  new : unit -> Form
  member AcceptButton : IButtonControl with get, set
  member Activate : unit -> unit
  member ActiveMdiChild : Form
  member AddOwnedForm : ownedForm:Form -> unit
  member AllowTransparency : bool with get, set
  member AutoScale : bool with get, set
  member AutoScaleBaseSize : Size with get, set
  member AutoScroll : bool with get, set
  member AutoSize : bool with get, set
  ...
  nested type ControlCollection

Full name: System.Windows.Forms.Form

--------------------
Form() : unit
val this : CompositedForm
Multiple items
override CompositedForm.CreateParams : CreateParams

Full name: Script.CompositedForm.CreateParams

--------------------
type CreateParams =
  new : unit -> CreateParams
  member Caption : string with get, set
  member ClassName : string with get, set
  member ClassStyle : int with get, set
  member ExStyle : int with get, set
  member Height : int with get, set
  member Param : obj with get, set
  member Parent : nativeint with get, set
  member Style : int with get, set
  member ToString : unit -> string
  ...

Full name: System.Windows.Forms.CreateParams

--------------------
CreateParams() : unit
val cp : CreateParams
Multiple items
type CreateParams =
  new : unit -> CreateParams
  member Caption : string with get, set
  member ClassName : string with get, set
  member ClassStyle : int with get, set
  member ExStyle : int with get, set
  member Height : int with get, set
  member Param : obj with get, set
  member Parent : nativeint with get, set
  member Style : int with get, set
  member ToString : unit -> string
  ...

Full name: System.Windows.Forms.CreateParams

--------------------
CreateParams() : unit
property CreateParams.ExStyle: int
val load : file:string -> url:string -> Image

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
type Image =
  inherit MarshalByRefObject
  member Clone : unit -> obj
  member Dispose : unit -> unit
  member Flags : int
  member FrameDimensionsList : Guid[]
  member GetBounds : pageUnit:GraphicsUnit -> RectangleF
  member GetEncoderParameterList : encoder:Guid -> EncoderParameters
  member GetFrameCount : dimension:FrameDimension -> int
  member GetPropertyItem : propid:int -> PropertyItem
  member GetThumbnailImage : thumbWidth:int * thumbHeight:int * callback:GetThumbnailImageAbort * callbackData:nativeint -> Image
  member Height : int
  ...
  nested type GetThumbnailImageAbort

Full name: System.Drawing.Image
Image.FromFile(filename: string) : Image
Image.FromFile(filename: string, useEmbeddedColorManagement: bool) : Image
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
Image.FromStream(stream: Stream) : Image
Image.FromStream(stream: Stream, useEmbeddedColorManagement: bool) : Image
Image.FromStream(stream: Stream, useEmbeddedColorManagement: bool, validateImageData: bool) : Image
val bg : Image

Full name: Script.bg
val ground : Image

Full name: Script.ground
val tube1 : Image

Full name: Script.tube1
val tube2 : Image

Full name: Script.tube2
val bird_sing : Image

Full name: Script.bird_sing
type Bird =
  {X: float;
   Y: float;
   VY: float;}

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
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 paint : graphics:Graphics -> scroll:int -> level:seq<int * int> -> flappy:Bird -> unit

Full name: Script.paint


 Paints the game scene
val graphics : Graphics
type Graphics =
  inherit MarshalByRefObject
  member AddMetafileComment : data:byte[] -> unit
  member BeginContainer : unit -> GraphicsContainer + 2 overloads
  member Clear : color:Color -> unit
  member Clip : Region with get, set
  member ClipBounds : RectangleF
  member CompositingMode : CompositingMode with get, set
  member CompositingQuality : CompositingQuality with get, set
  member CopyFromScreen : upperLeftSource:Point * upperLeftDestination:Point * blockRegionSize:Size -> unit + 3 overloads
  member Dispose : unit -> unit
  member DpiX : float32
  ...
  nested type DrawImageAbort
  nested type EnumerateMetafileProc

Full name: System.Drawing.Graphics
val scroll : int
val level : seq<int * int>
val flappy : Bird
val draw : (Image -> int * int -> unit)
val image : Image
val x : int
val y : int
Graphics.DrawImage(image: Image, destPoints: Point []) : unit
   (+0 other overloads)
Graphics.DrawImage(image: Image, destPoints: PointF []) : unit
   (+0 other overloads)
Graphics.DrawImage(image: Image, rect: Rectangle) : unit
   (+0 other overloads)
Graphics.DrawImage(image: Image, point: Point) : unit
   (+0 other overloads)
Graphics.DrawImage(image: Image, rect: RectangleF) : unit
   (+0 other overloads)
Graphics.DrawImage(image: Image, point: PointF) : unit
   (+0 other overloads)
Graphics.DrawImage(image: Image, x: int, y: int) : unit
   (+0 other overloads)
Graphics.DrawImage(image: Image, x: float32, y: float32) : unit
   (+0 other overloads)
Graphics.DrawImage(image: Image, destPoints: Point [], srcRect: Rectangle, srcUnit: GraphicsUnit) : unit
   (+0 other overloads)
Graphics.DrawImage(image: Image, destPoints: PointF [], srcRect: RectangleF, srcUnit: GraphicsUnit) : unit
   (+0 other overloads)
property Image.Width: int
property Image.Height: 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 drawTube : (int * int -> unit)
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 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 form : CompositedForm

Full name: Script.form
namespace System.Drawing.Text
event Control.Paint: IEvent<PaintEventHandler,PaintEventArgs>
member System.IObservable.Add : callback:('T -> unit) -> unit
val args : PaintEventArgs
property PaintEventArgs.Graphics: Graphics
val incr : cell:int ref -> unit

Full name: Microsoft.FSharp.Core.Operators.incr
val flapme : unit -> unit

Full name: Script.flapme
event Control.Click: IEvent<System.EventHandler,System.EventArgs>
val args : System.EventArgs
event Control.KeyDown: IEvent<KeyEventHandler,KeyEventArgs>
val args : KeyEventArgs
property KeyEventArgs.KeyCode: Keys
type Keys =
  | KeyCode = 65535
  | Modifiers = -65536
  | None = 0
  | LButton = 1
  | RButton = 2
  | Cancel = 3
  | MButton = 4
  | XButton1 = 5
  | XButton2 = 6
  | Back = 8
  ...

Full name: System.Windows.Forms.Keys
field Keys.Space = 32
Control.Show() : unit
Form.Show(owner: IWin32Window) : unit
val async : AsyncBuilder

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.async
Multiple items
type Async
static member AsBeginEnd : computation:('Arg -> Async<'T>) -> ('Arg * AsyncCallback * obj -> IAsyncResult) * (IAsyncResult -> 'T) * (IAsyncResult -> unit)
static member AwaitEvent : event:IEvent<'Del,'T> * ?cancelAction:(unit -> unit) -> Async<'T> (requires delegate and 'Del :> Delegate)
static member AwaitIAsyncResult : iar:IAsyncResult * ?millisecondsTimeout:int -> Async<bool>
static member AwaitTask : task:Task<'T> -> Async<'T>
static member AwaitWaitHandle : waitHandle:WaitHandle * ?millisecondsTimeout:int -> Async<bool>
static member CancelDefaultToken : unit -> unit
static member Catch : computation:Async<'T> -> Async<Choice<'T,exn>>
static member FromBeginEnd : beginAction:(AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromBeginEnd : arg:'Arg1 * beginAction:('Arg1 * AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromBeginEnd : arg1:'Arg1 * arg2:'Arg2 * beginAction:('Arg1 * 'Arg2 * AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromBeginEnd : arg1:'Arg1 * arg2:'Arg2 * arg3:'Arg3 * beginAction:('Arg1 * 'Arg2 * 'Arg3 * AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromContinuations : callback:(('T -> unit) * (exn -> unit) * (OperationCanceledException -> unit) -> unit) -> Async<'T>
static member Ignore : computation:Async<'T> -> Async<unit>
static member OnCancel : interruption:(unit -> unit) -> Async<IDisposable>
static member Parallel : computations:seq<Async<'T>> -> Async<'T []>
static member RunSynchronously : computation:Async<'T> * ?timeout:int * ?cancellationToken:CancellationToken -> 'T
static member Sleep : millisecondsDueTime:int -> Async<unit>
static member Start : computation:Async<unit> * ?cancellationToken:CancellationToken -> unit
static member StartAsTask : computation:Async<'T> * ?taskCreationOptions:TaskCreationOptions * ?cancellationToken:CancellationToken -> Task<'T>
static member StartChild : computation:Async<'T> * ?millisecondsTimeout:int -> Async<Async<'T>>
static member StartChildAsTask : computation:Async<'T> * ?taskCreationOptions:TaskCreationOptions -> Async<Task<'T>>
static member StartImmediate : computation:Async<unit> * ?cancellationToken:CancellationToken -> unit
static member StartWithContinuations : computation:Async<'T> * continuation:('T -> unit) * exceptionContinuation:(exn -> unit) * cancellationContinuation:(OperationCanceledException -> unit) * ?cancellationToken:CancellationToken -> unit
static member SwitchToContext : syncContext:SynchronizationContext -> Async<unit>
static member SwitchToNewThread : unit -> Async<unit>
static member SwitchToThreadPool : unit -> Async<unit>
static member TryCancelled : computation:Async<'T> * compensation:(OperationCanceledException -> unit) -> Async<'T>
static member CancellationToken : Async<CancellationToken>
static member DefaultCancellationToken : CancellationToken

Full name: Microsoft.FSharp.Control.Async

--------------------
type Async<'T>

Full name: Microsoft.FSharp.Control.Async<_>
static member Async.Sleep : millisecondsDueTime:int -> Async<unit>
Control.Invalidate() : unit
Control.Invalidate(rc: Rectangle) : unit
Control.Invalidate(invalidateChildren: bool) : unit
Control.Invalidate(region: Region) : unit
Control.Invalidate(rc: Rectangle, invalidateChildren: bool) : unit
Control.Invalidate(region: Region, invalidateChildren: bool) : unit
static member Async.StartImmediate : computation:Async<unit> * ?cancellationToken:System.Threading.CancellationToken -> unit
Next Version Raw view Test code New version

More information

Link:http://fssnip.net/rA
Posted:2 years ago
Author:Phillip Trelford
Tags: game , winforms