3 people like it.

Flappy Bird clone using MonoGame

Flappy bird clone script using MonoGame, 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: 
/// 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)]

open System.IO
open Microsoft.Xna.Framework
open Microsoft.Xna.Framework.Graphics
open Microsoft.Xna.Framework.Input

let loadImage (device:GraphicsDevice) file =
   let path = Path.Combine(__SOURCE_DIRECTORY__, file)
   use stream = File.OpenRead(path)
   let texture = Texture2D.FromStream(device, stream)
   let textureData = Array.create<Color> (texture.Width * texture.Height) Color.Transparent
   texture.GetData(textureData)
   texture

type FlappyBird() as this =
   inherit Game()
   do this.Window.Title <- "Flap me"
   let graphics = new GraphicsDeviceManager(this)
   do graphics.PreferredBackBufferWidth <- 288
   do graphics.PreferredBackBufferHeight <- 440
   let mutable spriteBatch : SpriteBatch = null
   let mutable bg : Texture2D = null
   let mutable ground : Texture2D = null
   let mutable tube1 : Texture2D = null
   let mutable tube2 : Texture2D = null
   let mutable bird_sing : Texture2D = null
   let mutable lastKeyState = KeyboardState()
   let mutable lastMouseState = MouseState()
   let level = generateLevel 10
   let mutable flappy = { X = 30.0; Y = 150.0; VY = 0.0; IsAlive=true }
   let flapMe () = if flappy.IsAlive then flappy <- flap flappy
   let mutable scroll = 0

   override this.LoadContent() =
      spriteBatch <- new SpriteBatch(this.GraphicsDevice)
      let load = loadImage this.GraphicsDevice
      bg <- load "bg.png"
      ground <- load "ground.png"
      tube1 <- load "tube1.png"
      tube2 <- load "tube2.png"
      bird_sing <- load "bird_sing.png"
   
   override this.Update(gameTime) =
      scroll <- scroll - 1
      let currentKeyState = Keyboard.GetState()
      let currentMouseState = Mouse.GetState()
      let isKeyPressedSinceLastFrame key =
         currentKeyState.IsKeyDown(key) && lastKeyState.IsKeyUp(key)
      let isMouseClicked () =
         currentMouseState.LeftButton = ButtonState.Pressed &&
         lastMouseState.LeftButton = ButtonState.Released
      if isKeyPressedSinceLastFrame Keys.Space || isMouseClicked () 
      then flapMe ()
      flappy <- update flappy
      lastKeyState <- currentKeyState
      lastMouseState <- currentMouseState     
   
   override this.Draw(gameTime) =
      this.GraphicsDevice.Clear Color.White
      spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied)
      let draw (texture:Texture2D) (x,y) =
         spriteBatch.Draw(texture, Rectangle(x,y,texture.Width,texture.Height), Color.White)      
      draw bg (0,0)
      draw bird_sing (int flappy.X,int flappy.Y)
      for (x,y) in level do
         let x = x+scroll        
         draw tube1 (x,-320+y)
         draw tube2 (x,y+100)
      draw ground (0,360)
      spriteBatch.End()

do
   use game = new FlappyBird()
   game.Run()
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 Bird =
  {X: float;
   Y: float;
   VY: float;
   IsAlive: bool;}

Full name: Script.Bird


 Bird type
namespace System
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
namespace System.IO
namespace Microsoft
namespace Microsoft.Xna
namespace Microsoft.Xna.Framework
namespace Microsoft.Xna.Framework.Graphics
namespace Microsoft.Xna.Framework.Input
val loadImage : device:GraphicsDevice -> file:string -> Texture2D

Full name: Script.loadImage
val device : GraphicsDevice
Multiple items
type GraphicsDevice =
  new : adapter:GraphicsAdapter * graphicsProfile:GraphicsProfile * presentationParameters:PresentationParameters -> GraphicsDevice
  member Adapter : GraphicsAdapter with get, set
  member BlendState : BlendState with get, set
  member Clear : color:Color -> unit + 2 overloads
  member DepthStencilState : DepthStencilState with get, set
  member DisplayMode : DisplayMode
  member Dispose : unit -> unit
  member DrawIndexedPrimitives : primitiveType:PrimitiveType * baseVertex:int * minVertexIndex:int * numVertices:int * startIndex:int * primitiveCount:int -> unit
  member DrawPrimitives : primitiveType:PrimitiveType * vertexStart:int * primitiveCount:int -> unit
  member DrawUserIndexedPrimitives<'T> : primitiveType:PrimitiveType * vertexData:'T[] * vertexOffset:int * numVertices:int * indexData:int16[] * indexOffset:int * primitiveCount:int -> unit + 3 overloads
  ...

Full name: Microsoft.Xna.Framework.Graphics.GraphicsDevice

--------------------
GraphicsDevice(adapter: GraphicsAdapter, graphicsProfile: GraphicsProfile, presentationParameters: PresentationParameters) : unit
val file : 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
val stream : FileStream
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.OpenRead(path: string) : FileStream
val texture : Texture2D
Multiple items
type Texture2D =
  inherit Texture
  new : graphicsDevice:GraphicsDevice * width:int * height:int -> Texture2D + 2 overloads
  member Bounds : Rectangle
  member GetData<'T> : data:'T[] -> unit + 3 overloads
  member Height : int
  member Reload : textureStream:Stream -> unit
  member SaveAsJpeg : stream:Stream * width:int * height:int -> unit
  member SaveAsPng : stream:Stream * width:int * height:int -> unit
  member SetData<'T> : data:'T[] -> unit + 3 overloads
  member Width : int
  static member CreateTex2DFromBitmap : bsource:BitmapSource * device:GraphicsDevice -> Texture2D
  ...

Full name: Microsoft.Xna.Framework.Graphics.Texture2D

--------------------
Texture2D(graphicsDevice: GraphicsDevice, width: int, height: int) : unit
Texture2D(graphicsDevice: GraphicsDevice, width: int, height: int, mipmap: bool, format: SurfaceFormat) : unit
Texture2D(graphicsDevice: GraphicsDevice, width: int, height: int, mipmap: bool, format: SurfaceFormat, arraySize: int) : unit
Texture2D.FromStream(graphicsDevice: GraphicsDevice, stream: Stream) : Texture2D
val textureData : Color []
module Array

from Microsoft.FSharp.Collections
val create : count:int -> value:'T -> 'T []

Full name: Microsoft.FSharp.Collections.Array.create
Multiple items
type Color =
  struct
    new : color:Vector4 -> Color + 7 overloads
    member A : byte with get, set
    member B : byte with get, set
    member Equals : obj:obj -> bool + 1 overload
    member G : byte with get, set
    member GetHashCode : unit -> int
    member PackedValue : uint32 with get, set
    member R : byte with get, set
    member ToString : unit -> string
    member ToVector3 : unit -> Vector3
    ...
  end

Full name: Microsoft.Xna.Framework.Color

--------------------
Color()
Color(color: Vector4) : unit
Color(color: Vector3) : unit
Color(color: Color, alpha: int) : unit
Color(color: Color, alpha: float32) : unit
Color(r: float32, g: float32, b: float32) : unit
Color(r: int, g: int, b: int) : unit
Color(r: int, g: int, b: int, alpha: int) : unit
Color(r: float32, g: float32, b: float32, alpha: float32) : unit
property Texture2D.Width: int
property Texture2D.Height: int
property Color.Transparent: Color
Texture2D.GetData<'T (requires default constructor and value type and 'T :> System.ValueType)>(data: 'T []) : unit
Texture2D.GetData<'T (requires default constructor and value type and 'T :> System.ValueType)>(data: 'T [], startIndex: int, elementCount: int) : unit
Texture2D.GetData<'T (requires default constructor and value type and 'T :> System.ValueType)>(level: int, rect: System.Nullable<Rectangle>, data: 'T [], startIndex: int, elementCount: int) : unit
Texture2D.GetData<'T (requires default constructor and value type and 'T :> System.ValueType)>(level: int, arraySlice: int, rect: System.Nullable<Rectangle>, data: 'T [], startIndex: int, elementCount: int) : unit
Multiple items
type FlappyBird =
  inherit Game
  new : unit -> FlappyBird
  override Draw : gameTime:GameTime -> unit
  override LoadContent : unit -> unit
  override Update : gameTime:GameTime -> unit

Full name: Script.FlappyBird

--------------------
new : unit -> FlappyBird
val this : FlappyBird
Multiple items
type Game =
  new : unit -> Game
  member Components : GameComponentCollection
  member Content : ContentManager with get, set
  member Dispose : unit -> unit
  member Exit : unit -> unit
  member GraphicsDevice : GraphicsDevice
  member InactiveSleepTime : TimeSpan with get, set
  member IsActive : bool
  member IsFixedTimeStep : bool with get, set
  member IsMouseVisible : bool with get, set
  ...

Full name: Microsoft.Xna.Framework.Game

--------------------
Game() : unit
val graphics : GraphicsDeviceManager
Multiple items
type GraphicsDeviceManager =
  new : game:Game -> GraphicsDeviceManager
  member ApplyChanges : unit -> unit
  member BeginDraw : unit -> bool
  member CreateDevice : unit -> unit
  member Dispose : unit -> unit
  member EndDraw : unit -> unit
  member GraphicsDevice : GraphicsDevice
  member GraphicsProfile : GraphicsProfile with get, set
  member HardwareModeSwitch : bool with get, set
  member IsFullScreen : bool with get, set
  ...

Full name: Microsoft.Xna.Framework.GraphicsDeviceManager

--------------------
GraphicsDeviceManager(game: Game) : unit
property GraphicsDeviceManager.PreferredBackBufferWidth: int
property GraphicsDeviceManager.PreferredBackBufferHeight: int
val mutable spriteBatch : SpriteBatch
Multiple items
type SpriteBatch =
  inherit GraphicsResource
  new : graphicsDevice:GraphicsDevice -> SpriteBatch
  member Begin : ?sortMode:SpriteSortMode * ?blendState:BlendState * ?samplerState:SamplerState * ?depthStencilState:DepthStencilState * ?rasterizerState:RasterizerState * ?effect:Effect * ?transformMatrix:Nullable<Matrix> -> unit
  member Draw : texture:Texture2D * position:Vector2 * color:Color -> unit + 7 overloads
  member DrawString : spriteFont:SpriteFont * text:string * position:Vector2 * color:Color -> unit + 5 overloads
  member End : unit -> unit

Full name: Microsoft.Xna.Framework.Graphics.SpriteBatch

--------------------
SpriteBatch(graphicsDevice: GraphicsDevice) : unit
val mutable bg : Texture2D
val mutable ground : Texture2D
val mutable tube1 : Texture2D
val mutable tube2 : Texture2D
val mutable bird_sing : Texture2D
val mutable lastKeyState : KeyboardState
Multiple items
type KeyboardState =
  struct
    new : [<ParamArray>] keys:Keys[] -> KeyboardState
    member Equals : obj:obj -> bool
    member GetHashCode : unit -> int
    member GetPressedKeys : unit -> Keys[]
    member IsKeyDown : key:Keys -> bool
    member IsKeyUp : key:Keys -> bool
    member Item : Keys -> KeyState
  end

Full name: Microsoft.Xna.Framework.Input.KeyboardState

--------------------
KeyboardState()
KeyboardState([<System.ParamArray>] keys: Keys []) : unit
val mutable lastMouseState : MouseState
Multiple items
type MouseState =
  struct
    new : x:int * y:int * scrollWheel:int * leftButton:ButtonState * middleButton:ButtonState * rightButton:ButtonState * xButton1:ButtonState * xButton2:ButtonState -> MouseState
    member Equals : obj:obj -> bool
    member GetHashCode : unit -> int
    member LeftButton : ButtonState with get, set
    member MiddleButton : ButtonState with get, set
    member Position : Point
    member RightButton : ButtonState with get, set
    member ScrollWheelValue : int with get, set
    member X : int with get, set
    member XButton1 : ButtonState with get, set
    ...
  end

Full name: Microsoft.Xna.Framework.Input.MouseState

--------------------
MouseState()
MouseState(x: int, y: int, scrollWheel: int, leftButton: ButtonState, middleButton: ButtonState, rightButton: ButtonState, xButton1: ButtonState, xButton2: ButtonState) : unit
val level : (int * int) list
val mutable flappy : Bird
val flapMe : (unit -> unit)
val mutable scroll : int
override FlappyBird.LoadContent : unit -> unit

Full name: Script.FlappyBird.LoadContent
property Game.GraphicsDevice: GraphicsDevice
val load : (string -> Texture2D)
override FlappyBird.Update : gameTime:GameTime -> unit

Full name: Script.FlappyBird.Update
val gameTime : GameTime
val currentKeyState : KeyboardState
type Keyboard =
  static member GetState : unit -> KeyboardState + 1 overload

Full name: Microsoft.Xna.Framework.Input.Keyboard
Keyboard.GetState() : KeyboardState
val currentMouseState : MouseState
type Mouse =
  static member GetState : unit -> MouseState + 1 overload
  static member SetPosition : x:int * y:int -> unit
  static member WindowHandle : nativeint with get, set

Full name: Microsoft.Xna.Framework.Input.Mouse
Mouse.GetState() : MouseState
Mouse.GetState(window: GameWindow) : MouseState
val isKeyPressedSinceLastFrame : (Keys -> bool)
val key : Keys
KeyboardState.IsKeyDown(key: Keys) : bool
KeyboardState.IsKeyUp(key: Keys) : bool
val isMouseClicked : (unit -> bool)
property MouseState.LeftButton: ButtonState
type ButtonState =
  | Released = 0
  | Pressed = 1

Full name: Microsoft.Xna.Framework.Input.ButtonState
field ButtonState.Pressed = 1
field ButtonState.Released = 0
type Keys =
  | None = 0
  | Back = 8
  | Tab = 9
  | Enter = 13
  | CapsLock = 20
  | Escape = 27
  | Space = 32
  | PageUp = 33
  | PageDown = 34
  | End = 35
  ...

Full name: Microsoft.Xna.Framework.Input.Keys
field Keys.Space = 32
override FlappyBird.Draw : gameTime:GameTime -> unit

Full name: Script.FlappyBird.Draw
GraphicsDevice.Clear(color: Color) : unit
GraphicsDevice.Clear(options: ClearOptions, color: Vector4, depth: float32, stencil: int) : unit
GraphicsDevice.Clear(options: ClearOptions, color: Color, depth: float32, stencil: int) : unit
property Color.White: Color
SpriteBatch.Begin(?sortMode: SpriteSortMode, ?blendState: BlendState, ?samplerState: SamplerState, ?depthStencilState: DepthStencilState, ?rasterizerState: RasterizerState, ?effect: Effect, ?transformMatrix: System.Nullable<Matrix>) : unit
type SpriteSortMode =
  | Deferred = 0
  | Immediate = 1
  | Texture = 2
  | BackToFront = 3
  | FrontToBack = 4

Full name: Microsoft.Xna.Framework.Graphics.SpriteSortMode
field SpriteSortMode.Deferred = 0
Multiple items
type BlendState =
  inherit GraphicsResource
  new : unit -> BlendState
  member AlphaBlendFunction : BlendFunction with get, set
  member AlphaDestinationBlend : Blend with get, set
  member AlphaSourceBlend : Blend with get, set
  member BlendFactor : Color with get, set
  member ColorBlendFunction : BlendFunction with get, set
  member ColorDestinationBlend : Blend with get, set
  member ColorSourceBlend : Blend with get, set
  member ColorWriteChannels : ColorWriteChannels with get, set
  member ColorWriteChannels1 : ColorWriteChannels with get, set
  ...

Full name: Microsoft.Xna.Framework.Graphics.BlendState

--------------------
BlendState() : unit
field BlendState.NonPremultiplied
val draw : (Texture2D -> int * int -> unit)
val x : int
val y : int
SpriteBatch.Draw(texture: Texture2D, destinationRectangle: Rectangle, color: Color) : unit
SpriteBatch.Draw(texture: Texture2D, position: Vector2, color: Color) : unit
SpriteBatch.Draw(texture: Texture2D, destinationRectangle: Rectangle, sourceRectangle: System.Nullable<Rectangle>, color: Color) : unit
SpriteBatch.Draw(texture: Texture2D, position: Vector2, sourceRectangle: System.Nullable<Rectangle>, color: Color) : unit
SpriteBatch.Draw(texture: Texture2D, destinationRectangle: Rectangle, sourceRectangle: System.Nullable<Rectangle>, color: Color, rotation: float32, origin: Vector2, effects: SpriteEffects, layerDepth: float32) : unit
SpriteBatch.Draw(texture: Texture2D, position: Vector2, sourceRectangle: System.Nullable<Rectangle>, color: Color, rotation: float32, origin: Vector2, scale: float32, effects: SpriteEffects, layerDepth: float32) : unit
SpriteBatch.Draw(texture: Texture2D, position: Vector2, sourceRectangle: System.Nullable<Rectangle>, color: Color, rotation: float32, origin: Vector2, scale: Vector2, effects: SpriteEffects, layerDepth: float32) : unit
SpriteBatch.Draw(texture: Texture2D, ?position: System.Nullable<Vector2>, ?destinationRectangle: System.Nullable<Rectangle>, ?sourceRectangle: System.Nullable<Rectangle>, ?origin: System.Nullable<Vector2>, ?rotation: float32, ?scale: System.Nullable<Vector2>, ?color: System.Nullable<Color>, ?effects: SpriteEffects, ?layerDepth: float32) : unit
Multiple items
type Rectangle =
  struct
    new : location:Point * size:Point -> Rectangle + 1 overload
    val X : int
    val Y : int
    val Width : int
    val Height : int
    member Bottom : int
    member Center : Point
    member Contains : value:Point -> bool + 7 overloads
    member Equals : obj:obj -> bool + 1 overload
    member GetHashCode : unit -> int
    ...
  end

Full name: Microsoft.Xna.Framework.Rectangle

--------------------
Rectangle()
Rectangle(location: Point, size: Point) : unit
Rectangle(x: int, y: int, width: int, height: int) : unit
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<_>
SpriteBatch.End() : unit
val game : FlappyBird
Game.Run() : unit
Game.Run(runBehavior: GameRunBehavior) : unit
Raw view Test code New version

More information

Link:http://fssnip.net/rC
Posted:9 years ago
Author:Phillip Trelford
Tags: game , monogame