XNA Example - Luminance
From IronPython Cookbook
Contents |
Introduction
XNA is is a set of tools with a managed runtime environment provided by Microsoft that facilitates computer game development and management.
You should install Microsoft XNA Framework Redistributable 3.1 first. It is required for both developing and running XNA games. DirectX 9.0c is also required.
Full Code
import clr
clr.AddReference('Microsoft.Xna.Framework')
clr.AddReference('Microsoft.Xna.Framework.Game')
from Microsoft.Xna.Framework import Game, GraphicsDeviceManager
from Microsoft.Xna.Framework.Graphics import Color
from System import Math
class LuminanceWindow(Game):
def __init__(self):
self.initializeComponent()
def initializeComponent(self):
self.Window.Title = "XNA - Luminance"
self.graphics = GraphicsDeviceManager(self)
def LoadContent(self):
#stuff to do when game starts goes here
super(LuminanceWindow,self).LoadContent()
def UnloadContent(self):
#stuff to do when game closes goes here
super(LuminanceWindow, self).UnloadContent()
def Update(self, gameTime):
super(LuminanceWindow, self).Update(gameTime)
def Draw(self, gameTime):
luminosity = (255.0/2.0)+Math.Sin(gameTime.TotalGameTime.Ticks/5000000.0)*(255.0/2.0)
self.graphics.GraphicsDevice.Clear(Color(R=luminosity,G=luminosity,B=luminosity))
super(LuminanceWindow, self).Draw(gameTime)
game = LuminanceWindow()
game.Run()
Analysis
Setting things up
Look into the first few lines:
import clr
clr.AddReference('Microsoft.Xna.Framework')
clr.AddReference('Microsoft.Xna.Framework.Game')
from Microsoft.Xna.Framework import Game, GraphicsDeviceManager
from Microsoft.Xna.Framework.Graphics import Color
from System import Math
References are added to the assemblies Microsoft.Xna.Framework and Microsoft.Xna.Framework.Game. Both are required in XNA. We also import the required modules.
Namespaces in the Framework
- Microsoft.Xna.Framework
Contains commonly needed game classes such as timers and game loops.
- Microsoft.Xna.Framework.Graphics
Contains API methods that use hardware to display graphics.
- Microsoft.Xna.Framework.Content
Contains content pipeline components.
- Microsoft.Xna.Framework.Input
Allows games to receive input from the keyboard or mouse.
- Microsoft.Xna.Framework.Audio
Contains API methods that allow playing or manipulating sounds.
- Microsoft.Xna.Framework.Media
Allows games to view and play media files.
- Microsoft.Xna.Framework.Net
Allows games to use networking and multiplayer.
- Microsoft.Xna.Framework.Design
For the conversion of types.
- Microsoft.Xna.Framework.Storage
File input and output.
Class definition/init
class LuminanceWindow(Game): def __init__(self): self.initializeComponent() def initializeComponent(self): self.Window.Title = "XNA - Luminance" self.graphics = GraphicsDeviceManager(self)
Here, we define a class LuminanceWindow, which will inherit from Game. We initiate the game by setting the window title as "XNA - Luminance", and then initiating the GraphicsDeviceManager, which handles the graphics.
Defining what LoadContent, Update, and UnloadContent do
def LoadContent(self):
#stuff to do when game starts goes here
super(LuminanceWindow,self).LoadContent()
def UnloadContent(self):
#stuff to do when game closes goes here
super(LuminanceWindow, self).UnloadContent()
def Update(self, gameTime):
super(LuminanceWindow, self).Update(gameTime)
In this example, LoadContent, UnloadContent, and Update serve no purpose. In fact, it would be better if those lines were simply deleted. See this more complex example, where LoadContent, UnloadContent, and Update are used.
Note: In that example, UnloadGraphicsContent is used instead of UnloadContent, and LoadGraphicsContent is used instead of LoadContent. Just use UnloadContent and LoadContent. Also in that example, each component is manually Dispose()'d during UnloadGraphicsContent()'ing. That is not needed anymore.
Methods in Microsoft.Xna.Framework.Game
Here are the important ones that we may override.
- LoadContent
Called once when the game is run. This is before any game window is shown.
- Draw
Called many times a second, in order to draw a frame.
- Update
Like draw, also called many times a second, and it processes game logic.
- UnloadContent
Called once when the game is closing.
Draw
def Draw(self, gameTime): luminosity = (255.0/2.0)+Math.Sin(gameTime.TotalGameTime.Ticks/5000000.0)*(255.0/2.0) self.graphics.GraphicsDevice.Clear(Color(R=luminosity,G=luminosity,B=luminosity)) super(LuminanceWindow, self).Draw(gameTime)
Here is where things are actually done. Start remembering what you learned in high school math now. Here is a chart that you can examine in order to fully understand the mathematical expression.
Running the Game
game = LuminanceWindow() game.Run()
Finally, we create an instance of the LuminanceWindow, and we call it's Run method. When we create its instance, __init__ is called. When it is run, LoadContent is first called. Then Update and Draw are called continually until the game is closed, which is when UnloadContent is called.
You may also call game.Exit(), which is very useful.
Conclusion
The full source code is full of unnecessary lines. Now that the code and it's design has been sufficiently explained, a more concise version can be written.
import clr
clr.AddReference('Microsoft.Xna.Framework')
clr.AddReference('Microsoft.Xna.Framework.Game')
from Microsoft.Xna.Framework import Game, GraphicsDeviceManager
from Microsoft.Xna.Framework.Graphics import Color
from System import Math
class LuminanceWindow(Game):
def __init__(self):
self.Window.Title = "XNA - Luminance"
self.graphics = GraphicsDeviceManager(self)
def Draw(self, gameTime):
luminosity = (255.0/2.0)+Math.Sin(gameTime.TotalGameTime.Ticks/5000000.0)*(255.0/2.0)
self.graphics.GraphicsDevice.Clear(Color(R=luminosity,G=luminosity,B=luminosity))
super(LuminanceWindow, self).Draw(gameTime)
LuminanceWindow().Run()
XNA programming resource (MSDN)
Back to Contents.


