2 people like it.

Procedural Invaders

Procedurally generates a sheet of invader graphics

 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: 
#if INTERACTIVE
#r "System.Drawing.dll"
#endif

open System.Drawing

let drawTo (image:Bitmap) n (dx,dy) =
   for y = 0 to 4 do
      for x = 0 to 4 do
         let bit = [|0;5;10;5;0|].[x] + y
         if n &&& (1 <<< bit) <> 0
         then image.SetPixel(x+dx,y+dy, Color.Black)

let generate (cols,rows) =
   let rnd = System.Random()
   let width, height = 7*cols, 7 * rows
   let image = new Bitmap(width, height)   
   for y = 0 to rows-1 do
      for x = 0 to cols-1 do
         let n = rnd.Next()
         drawTo image n (7*x+1, 7*y+1)
   image

let sheet = generate (80,60)
sheet.Save("invaders.png", Imaging.ImageFormat.Png)
namespace System
namespace System.Drawing
val drawTo : image:Bitmap -> n:int -> dx:int * dy:int -> unit

Full name: Script.drawTo
val image : Bitmap
Multiple items
type Bitmap =
  inherit Image
  new : filename:string -> Bitmap + 11 overloads
  member Clone : rect:Rectangle * format:PixelFormat -> Bitmap + 1 overload
  member GetHbitmap : unit -> nativeint + 1 overload
  member GetHicon : unit -> nativeint
  member GetPixel : x:int * y:int -> Color
  member LockBits : rect:Rectangle * flags:ImageLockMode * format:PixelFormat -> BitmapData + 1 overload
  member MakeTransparent : unit -> unit + 1 overload
  member SetPixel : x:int * y:int * color:Color -> unit
  member SetResolution : xDpi:float32 * yDpi:float32 -> unit
  member UnlockBits : bitmapdata:BitmapData -> unit
  ...

Full name: System.Drawing.Bitmap

--------------------
Bitmap(filename: string) : unit
   (+0 other overloads)
Bitmap(stream: System.IO.Stream) : unit
   (+0 other overloads)
Bitmap(original: Image) : unit
   (+0 other overloads)
Bitmap(filename: string, useIcm: bool) : unit
   (+0 other overloads)
Bitmap(type: System.Type, resource: string) : unit
   (+0 other overloads)
Bitmap(stream: System.IO.Stream, useIcm: bool) : unit
   (+0 other overloads)
Bitmap(width: int, height: int) : unit
   (+0 other overloads)
Bitmap(original: Image, newSize: Size) : unit
   (+0 other overloads)
Bitmap(width: int, height: int, format: Imaging.PixelFormat) : unit
   (+0 other overloads)
Bitmap(width: int, height: int, g: Graphics) : unit
   (+0 other overloads)
val n : int
val dx : int
val dy : int
val y : int
val x : int
val bit : int
Bitmap.SetPixel(x: int, y: int, color: Color) : unit
type Color =
  struct
    member A : byte
    member B : byte
    member Equals : obj:obj -> bool
    member G : byte
    member GetBrightness : unit -> float32
    member GetHashCode : unit -> int
    member GetHue : unit -> float32
    member GetSaturation : unit -> float32
    member IsEmpty : bool
    member IsKnownColor : bool
    ...
  end

Full name: System.Drawing.Color
property Color.Black: Color
val generate : cols:int * rows:int -> Bitmap

Full name: Script.generate
val cols : int
val rows : int
val rnd : 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 width : int
val height : int
System.Random.Next() : int
System.Random.Next(maxValue: int) : int
System.Random.Next(minValue: int, maxValue: int) : int
val sheet : Bitmap

Full name: Script.sheet
Image.Save(filename: string) : unit
Image.Save(stream: System.IO.Stream, format: Imaging.ImageFormat) : unit
Image.Save(filename: string, format: Imaging.ImageFormat) : unit
Image.Save(stream: System.IO.Stream, encoder: Imaging.ImageCodecInfo, encoderParams: Imaging.EncoderParameters) : unit
Image.Save(filename: string, encoder: Imaging.ImageCodecInfo, encoderParams: Imaging.EncoderParameters) : unit
namespace System.Drawing.Imaging
Multiple items
type ImageFormat =
  new : guid:Guid -> ImageFormat
  member Equals : o:obj -> bool
  member GetHashCode : unit -> int
  member Guid : Guid
  member ToString : unit -> string
  static member Bmp : ImageFormat
  static member Emf : ImageFormat
  static member Exif : ImageFormat
  static member Gif : ImageFormat
  static member Icon : ImageFormat
  ...

Full name: System.Drawing.Imaging.ImageFormat

--------------------
Imaging.ImageFormat(guid: System.Guid) : unit
property Imaging.ImageFormat.Png: Imaging.ImageFormat
Next Version Raw view Test code New version

More information

Link:http://fssnip.net/tC
Posted:2 years ago
Author:Phillip Trelford
Tags: art