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:'a -> n:int -> dx:'b * dy:'c -> unit
val image : 'a
val n : int
val dx : 'b
val dy : 'c
val y : int
val x : int
val bit : int
type Color =
  struct
    member A : byte
    member B : byte
    member Equals : obj:obj -> bool + 1 overload
    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
property Color.Black: Color with get
val generate : cols:int * rows:int -> 'a
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 + 1 overload
  member NextDouble : unit -> float

--------------------
System.Random() : System.Random
System.Random(Seed: int) : System.Random
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 : obj

More information

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