2006/12/11
How to Write XNA Game Studio Express Games with F# - Part 1 Getting Started
This blog entry explains how to write XNA Game Studio Express games in the F# programming language. To begin, let me explain what "XNA GSE" and "F#" are:
- XNA Game Studio Express is a product from Microsoft that enables you to write video games for the Windows and Xbox 360.
- The tools are free.
- Running XNA GSE games on Windows is free.
- The only thing that's not free is that you need a "XNA Creators Club" subscription to run games on the 360.
- F# (pronounced F-sharp) is a new programming language being developed by Microsoft Research
- It's based on the ML language family, specificly the O'Caml dialect.
- It runs on the .NET Common Language Runtime, the same as C# or Visual Basic.
- It's briefer and more expressive than C#.
- It's free, and it's regularly updated with powerful new features.
In this series of articles, I'm going to show how to:
- Part 1: Create a trivial F# XNA game that runs on Windows
- Part 2: Modify the game to work with the XNA Game StudioExpress IDE
- Part 3: Modify the game to on both Windows and Xbox 360
Let's get started with creating a trivial F# XNA game that runs on Windows. (And by trivial I mean really trivial: the game just draws a blank green screen.)
Step 1: Download and install Visual C# Express: Visual C# Express Home Page
Step 2: Download and install XNA Game Studio Express: XNA Game Studio Express Home Page
Step 3: Download and install F#: F# Home Page
Note: These articles are for F# version 1.1.13.8. Earlier version's won't work, later versions might work.
Step 4: Create an F# source file using your favorite text editor:
// An XNA game in F#
#light
#I @"C:\Program Files\Microsoft XNA\XNA Game Studio Express\v1.0\References\Windows\x86"
#r "Microsoft.Xna.Framework.dll"
#r "Microsoft.Xna.Framework.Game.dll"
// Not all of these opens are required for this sample, but you will need them to write a full game
open Collections
open Compatibility
open Idioms
open Microsoft.Xna.Framework
open Microsoft.Xna.Framework.Audio
open Microsoft.Xna.Framework.Content
open Microsoft.Xna.Framework.Graphics
open Microsoft.Xna.Framework.Input
open Microsoft.Xna.Framework.Storage
open System
open System.IO
type MyGame = class
inherit Game as base
val mutable graphics : GraphicsDeviceManager
new() as this =
{
graphics = null
}
then
this.graphics <- new GraphicsDeviceManager(this)
override this.Draw(gameTime) =
let gd = this.graphics.GraphicsDevice
gd.Clear(Color.Green)
end
let main() =
let game = new MyGame()
game.Run()
[<STAThread>]
do main()
Step 5: Save the F# file as "game.fs", and then open a cmd.exe shell window and compile the game using the F# compiler:
"C:\Program Files\FSharp-1.1.13.8\bin\fsc.exe" game.fs
Step 6: Run the game by double-clicking on the "game.exe" or by typing "game.exe" at the command line.
That's all there is to it! However, is are some drawbacks to this approach. By bypassing Visual C# Express:
- We lose the ability to use the Content Framework to build our game's art assets.
- We lose the ability to run our game on the Xbox 360.
In the Part 2 of this article I will explain how to work around these problems.