6 people like it.

Flappy Bird clone using WPF

Flappy bird clone script using WPF, 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: 
88: 
89: 
90: 
91: 
92: 
93: 
94: 
95: 
96: 
97: 
98: 
99: 
#if INTERACTIVE
#r "PresentationCore.dll"
#r "PresentationFramework.dll"
#r "System.Xaml.dll"
#r "UIAutomationTypes.dll"
#r "WindowsBase.dll"
#endif

open System
open System.IO
open System.Windows
open System.Windows.Controls
open System.Windows.Input
open System.Windows.Media
open System.Windows.Media.Imaging

/// Converts specified bitmap to an image
let toImage (bitmap:#BitmapSource) =
   let w, h = float bitmap.PixelWidth, float bitmap.PixelHeight  
   Image(Source=bitmap,Stretch=Stretch.Fill,Width=w,Height=h) 
/// Loads image from file if it exists or the url otherwise
let load file url =
   let path = Path.Combine(__SOURCE_DIRECTORY__, file)
   let uri = 
      if File.Exists(path) 
      then Uri(path, UriKind.Relative)
      else Uri(url, UriKind.Absolute)
   BitmapImage(uri)

let bg = 
   load "bg.png" "http://flappycreator.com/default/bg.png"
   |> toImage
let ground = 
   load "ground.png" "http://flappycreator.com/default/ground.png"
   |> toImage
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"
   |> toImage

let canvas = Canvas()
let move image (x,y) =
   Canvas.SetLeft(image, x)
   Canvas.SetTop(image, y)
let add image (x,y) = 
   canvas.Children.Add(image) |> ignore
   move image (float x, float y)

/// 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)]

let level = generateLevel 10

add bg (0,0)
add bird_sing (30,150)
// Level's tubes
let tubes =
   [for (x,y) in level ->
      let tube1 = toImage tube1
      let tube2 = toImage tube2
      add tube1 (x,y-320)
      add tube2 (x,y+100)
      (x,y), tube1, tube2]
add ground (0,360)

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 window = Window(Title="Flap me",Width=288.0,Height=440.0)
window.Content <- canvas
window.MouseDown.Add(fun _ -> flapme())
window.KeyDown.Add(fun args -> if args.Key = Key.Space then flapme())
window.Show()

// Update scene
CompositionTarget.Rendering.Add(fun _ ->
   flappy := update !flappy
   let bird = !flappy
   move bird_sing (bird.X, bird.Y)
   for ((x,y),tube1,tube2) in tubes do
      move tube1 (float (x + !scroll),float (y-320))
      move tube2 (float (x + !scroll),float (y+100))
   decr scroll
)
namespace System
namespace System.IO
namespace System.Windows
namespace System.Windows.Controls
namespace System.Windows.Input
namespace System.Windows.Media
namespace System.Windows.Media.Imaging
val toImage : bitmap:#BitmapSource -> Image

Full name: Script.toImage


 Converts specified bitmap to an image
val bitmap : #BitmapSource
type BitmapSource =
  inherit ImageSource
  member Clone : unit -> BitmapSource
  member CloneCurrentValue : unit -> BitmapSource
  member CopyPixels : pixels:Array * stride:int * offset:int -> unit + 2 overloads
  member DpiX : float
  member DpiY : float
  member Format : PixelFormat
  member Height : float
  member IsDownloading : bool
  member Metadata : ImageMetadata
  member Palette : BitmapPalette
  ...

Full name: System.Windows.Media.Imaging.BitmapSource
val w : float
val h : float
Multiple items
val float : value:'T -> float (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.float

--------------------
type float = Double

Full name: Microsoft.FSharp.Core.float

--------------------
type float<'Measure> = float

Full name: Microsoft.FSharp.Core.float<_>
property BitmapSource.PixelWidth: int
property BitmapSource.PixelHeight: int
Multiple items
type Image =
  inherit FrameworkElement
  new : unit -> Image
  member Source : ImageSource with get, set
  member Stretch : Stretch with get, set
  member StretchDirection : StretchDirection with get, set
  event ImageFailed : EventHandler<ExceptionRoutedEventArgs>
  static val SourceProperty : DependencyProperty
  static val StretchProperty : DependencyProperty
  static val StretchDirectionProperty : DependencyProperty
  static val ImageFailedEvent : RoutedEvent

Full name: System.Windows.Controls.Image

--------------------
Image() : unit
type Stretch =
  | None = 0
  | Fill = 1
  | Uniform = 2
  | UniformToFill = 3

Full name: System.Windows.Media.Stretch
field Stretch.Fill = 1
val load : file:string -> url:string -> BitmapImage

Full name: Script.load


 Loads image from file if it exists or the url otherwise
val file : 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([<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
val uri : Uri
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
Multiple items
type Uri =
  new : uriString:string -> Uri + 5 overloads
  member AbsolutePath : string
  member AbsoluteUri : string
  member Authority : string
  member DnsSafeHost : string
  member Equals : comparand:obj -> bool
  member Fragment : string
  member GetComponents : components:UriComponents * format:UriFormat -> string
  member GetHashCode : unit -> int
  member GetLeftPart : part:UriPartial -> string
  ...

Full name: System.Uri

--------------------
Uri(uriString: string) : unit
Uri(uriString: string, uriKind: UriKind) : unit
Uri(baseUri: Uri, relativeUri: string) : unit
Uri(baseUri: Uri, relativeUri: Uri) : unit
type UriKind =
  | RelativeOrAbsolute = 0
  | Absolute = 1
  | Relative = 2

Full name: System.UriKind
field UriKind.Relative = 2
field UriKind.Absolute = 1
Multiple items
type BitmapImage =
  inherit BitmapSource
  new : unit -> BitmapImage + 2 overloads
  member BaseUri : Uri with get, set
  member BeginInit : unit -> unit
  member CacheOption : BitmapCacheOption with get, set
  member Clone : unit -> BitmapImage
  member CloneCurrentValue : unit -> BitmapImage
  member CreateOptions : BitmapCreateOptions with get, set
  member DecodePixelHeight : int with get, set
  member DecodePixelWidth : int with get, set
  member EndInit : unit -> unit
  ...

Full name: System.Windows.Media.Imaging.BitmapImage

--------------------
BitmapImage() : unit
BitmapImage(uriSource: Uri) : unit
BitmapImage(uriSource: Uri, uriCachePolicy: Net.Cache.RequestCachePolicy) : unit
val bg : Image

Full name: Script.bg
val ground : Image

Full name: Script.ground
val tube1 : BitmapImage

Full name: Script.tube1
val tube2 : BitmapImage

Full name: Script.tube2
val bird_sing : Image

Full name: Script.bird_sing
val canvas : Canvas

Full name: Script.canvas
Multiple items
type Canvas =
  inherit Panel
  new : unit -> Canvas
  static val LeftProperty : DependencyProperty
  static val TopProperty : DependencyProperty
  static val RightProperty : DependencyProperty
  static val BottomProperty : DependencyProperty
  static member GetBottom : element:UIElement -> float
  static member GetLeft : element:UIElement -> float
  static member GetRight : element:UIElement -> float
  static member GetTop : element:UIElement -> float
  static member SetBottom : element:UIElement * length:float -> unit
  ...

Full name: System.Windows.Controls.Canvas

--------------------
Canvas() : unit
val move : image:UIElement -> x:float * y:float -> unit

Full name: Script.move
val image : UIElement
val x : float
val y : float
Canvas.SetLeft(element: UIElement, length: float) : unit
Canvas.SetTop(element: UIElement, length: float) : unit
val add : image:UIElement -> x:int * y:int -> unit

Full name: Script.add
val x : int
val y : int
property Panel.Children: UIElementCollection
UIElementCollection.Add(element: UIElement) : int
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
type Bird =
  {X: float;
   Y: float;
   VY: float;
   IsAlive: bool;}

Full name: Script.Bird


 Bird type
Bird.X: float
Bird.Y: float
Bird.VY: float
Bird.IsAlive: bool
type bool = 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 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 : 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

--------------------
Random() : unit
Random(Seed: int) : unit
val i : int
Random.Next() : int
Random.Next(maxValue: int) : int
Random.Next(minValue: int, maxValue: int) : int
val level : (int * int) list

Full name: Script.level
val tubes : ((int * int) * Image * Image) list

Full name: Script.tubes
val tube1 : Image
val tube2 : Image
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 window : Window

Full name: Script.window
Multiple items
type Window =
  inherit ContentControl
  new : unit -> Window
  member Activate : unit -> bool
  member AllowsTransparency : bool with get, set
  member Close : unit -> unit
  member DialogResult : Nullable<bool> with get, set
  member DragMove : unit -> unit
  member Hide : unit -> unit
  member Icon : ImageSource with get, set
  member IsActive : bool
  member Left : float with get, set
  ...

Full name: System.Windows.Window

--------------------
Window() : unit
property ContentControl.Content: obj
event UIElement.MouseDown: IEvent<MouseButtonEventHandler,MouseButtonEventArgs>
member IObservable.Add : callback:('T -> unit) -> unit
event UIElement.KeyDown: IEvent<KeyEventHandler,KeyEventArgs>
val args : KeyEventArgs
property KeyEventArgs.Key: Key
type Key =
  | None = 0
  | Cancel = 1
  | Back = 2
  | Tab = 3
  | LineFeed = 4
  | Clear = 5
  | Return = 6
  | Enter = 6
  | Pause = 7
  | Capital = 8
  ...

Full name: System.Windows.Input.Key
field Key.Space = 18
Window.Show() : unit
type CompositionTarget =
  inherit DispatcherObject
  member Dispose : unit -> unit
  member RootVisual : Visual with get, set
  member TransformFromDevice : Matrix
  member TransformToDevice : Matrix
  static event Rendering : EventHandler

Full name: System.Windows.Media.CompositionTarget
event CompositionTarget.Rendering: IEvent<EventHandler,EventArgs>
val decr : cell:int ref -> unit

Full name: Microsoft.FSharp.Core.Operators.decr
Raw view Test code New version

More information

Link:http://fssnip.net/rB
Posted:8 years ago
Author:Phillip Trelford
Tags: wpf , game