Translation of 'Getting Started' sample into IronPython
Just a simple line-by-line translation. This runs out of the box, no special mojo needed.
import clr
clr.AddReference('Microsoft.Xna.Framework')
clr.AddReference('Microsoft.Xna.Framework.Game')
import Microsoft.Xna.Framework.Game
from Microsoft.Xna.Framework import *
from Microsoft.Xna.Framework.Components import *
from Microsoft.Xna.Framework.Graphics import *
class MyGame(Microsoft.Xna.Framework.Game):
def __init__(self):
self.InitializeComponent()
def InitializeComponent(self):
self.graphics = Microsoft.Xna.Framework.Components.GraphicsComponent()
self.GameComponents.Add(self.graphics)
def Update(self):
self.UpdateComponents()
def Draw(self):
if not self.graphics.EnsureDevice():
return
self.graphics.GraphicsDevice.Clear(Color.CornflowerBlue)
self.graphics.GraphicsDevice.BeginScene()
self.DrawComponents()
self.graphics.GraphicsDevice.EndScene()
self.graphics.GraphicsDevice.Present()
game = MyGame()
game.Run()
[1029 byte] By [
psykotic] at [2007-12-23]
Here's a translation of the bouncing sprite sample:
import clr
clr.AddReference('System')
clr.AddReference('Microsoft.Xna.Framework')
clr.AddReference('Microsoft.Xna.Framework.Game')
import Microsoft.Xna.Framework.Game
from System import EventHandler
from Microsoft.Xna.Framework import *
from Microsoft.Xna.Framework.Components import *
from Microsoft.Xna.Framework.Graphics import *
class MyGame(Microsoft.Xna.Framework.Game):
def __init__(self):
self.spriteX = self.spriteY = 0
self.spriteSpeedX = self.spriteSpeedY = 1
self.InitializeComponent()
def InitializeComponent(self):
self.graphics = Microsoft.Xna.Framework.Components.GraphicsComponent()
self.GameComponents.Add(self.graphics)
def OnStarting(self):
Microsoft.Xna.Framework.Game.OnStarting(self)
self.graphics.GraphicsDevice.DeviceReset += self.GraphicsDevice_DeviceReset
self.LoadResources()
def GraphicsDevice_DeviceReset(self, sender, eventArgs):
self.LoadResources()
def LoadResources(self):
self.texture = Texture2D.FromFile(self.graphics.GraphicsDevice, "mytexture.bmp")
self.spriteBatch = SpriteBatch(self.graphics.GraphicsDevice)
def Update(self):
self.UpdateSprite()
self.UpdateComponents()
def UpdateSprite(self):
print self.spriteX
self.spriteX += self.spriteSpeedX
self.spriteY += self.spriteSpeedY
maxX = self.Window.ClientWidth - self.texture.Width
if self.spriteX > maxX:
self.spriteSpeedX *= -1
self.spriteX = maxX
elif self.spriteX < 0:
self.spriteSpeedX *= -1
self.spriteX = 0
maxY = self.Window.ClientHeight - self.texture.Height
if self.spriteY > maxY:
self.spriteSpeedY *= -1
self.spriteX = maxY
elif self.spriteY < 0:
self.spriteSpeedY *= -1
self.spriteY = 0
def Draw(self):
if not self.graphics.EnsureDevice(): return
self.graphics.GraphicsDevice.Clear(Color.CornflowerBlue)
self.graphics.GraphicsDevice.BeginScene()
self.DrawSprite()
self.DrawComponents()
self.graphics.GraphicsDevice.EndScene()
self.graphics.GraphicsDevice.Present()
def DrawSprite(self):
self.spriteBatch.Begin()
self.spriteBatch.Draw(self.texture, Rectangle(self.spriteX, self.spriteY, self.texture.Width, self.texture.Height), Color.White)
self.spriteBatch.End()
game = MyGame()
game.Run()
hi guys.
what is ironpython?
python for net?
is this useful?
Yes, IronPython is essentially a version of Python for the .NET platform with really slick support for passing between the Python and .NET worlds with a minimum of fuzz.
Here's a version of the sprite bouncing example that doesn't use GraphicsComponent, i.e. it creates and manages the GraphicsDevice directly. The 'xna' module referenced in the import directive contains all the gunk included at the top of the previous two code samples I posted.
from xna import *
class App(Game):
def __init__(self):
self.x = self.y = 0
self.dx = self.dy = 1
self.gfx = Microsoft.Xna.Framework.Components.GraphicsComponent()
self.GameComponents.Add(self.gfx)
def OnStarting(self):
Microsoft.Xna.Framework.Game.OnStarting(self)
self.create_device()
self.device.DeviceReset += lambda self, *args: self.load_resources()
self.load_resources()
def create_device(self):
self.device = GraphicsDevice(GraphicsAdapter.DefaultAdapter,
DeviceType.Hardware,
self.Window.Handle,
CreateOptions.HardwareVertexProcessing,
PresentationParameters())
def load_resources(self):
self.texture = Texture2D.FromFile(self.device, "texture.bmp")
self.sprite_batch = SpriteBatch(self.device)
def Update(self):
self.update_sprite()
def update_sprite(self):
self.x += self.dx
self.y += self.dy
max_x = self.Window.ClientWidth - self.texture.Width
if self.x > max_x:
self.dx *= -1
self.x = max_x
elif self.x < 0:
self.dx *= -1
self.x = 0
max_y = self.Window.ClientHeight - self.texture.Height
if self.y > max_y:
self.dy *= -1
self.y = max_y
elif self.y < 0:
self.dy *= -1
self.y = 0
def Draw(self):
if self.gfx.EnsureDevice():
self.device.Clear(Color.CornflowerBlue)
self.device.BeginScene()
self.draw_sprite()
self.device.EndScene()
self.device.Present()
def draw_sprite(self):
self.sprite_batch.Begin()
self.sprite_batch.Draw(self.texture, Rectangle(self.x, self.y, self.texture.Width, self.texture.Height), Color.White)
self.sprite_batch.End()
app = App()
app.Run()
I stumbled across this post by accident and decided to update the code for v1.0, for anyone else who might be using IronPython.
import clr
clr.AddReference('Microsoft.Xna.Framework')
clr.AddReference('Microsoft.Xna.Framework.Game')
from Microsoft.Xna.Framework import *
from Microsoft.Xna.Framework.Graphics import *
from Microsoft.Xna.Framework.Content import *
class MyGame(Game):
def __init__(self):
self.spriteX = self.spriteY = 0
self.spriteSpeedX = self.spriteSpeedY = 1
self.InitializeComponent()
def InitializeComponent(self):
self.graphics = GraphicsDeviceManager(self)
self.content = ContentManager(self.Services)
def LoadGraphicsContent(self, loadAllContent):
if loadAllContent:
self.texture = Texture2D.FromFile(self.graphics.GraphicsDevice, "sprite.jpg")
self.spriteBatch = SpriteBatch(self.graphics.GraphicsDevice) def UnloadGraphicsContent(self, unloadAllContent):
if unloadAllContent:
self.texture.Dispose()
self.spritebatch.Dispose()
def Update(self, gameTime):
self.UpdateSprite() Game.Update(self, gameTime)
def UpdateSprite(self):
self.spriteX += self.spriteSpeedX
self.spriteY += self.spriteSpeedY
maxX = self.graphics.GraphicsDevice.Viewport.Width - self.texture.Width
if self.spriteX > maxX:
self.spriteSpeedX *= -1
self.spriteX = maxX
elif self.spriteX < 0:
self.spriteSpeedX *= -1
self.spriteX = 0
maxY = self.graphics.GraphicsDevice.Viewport.Height - self.texture.Height
if self.spriteY > maxY:
self.spriteSpeedY *= -1
self.spriteX = maxY
elif self.spriteY < 0:
self.spriteSpeedY *= -1
self.spriteY = 0
def Draw(self, gameTime):
self.graphics.GraphicsDevice.Clear(Color.CornflowerBlue) self.DrawSprite()
Game.Draw(self, gameTime)
def DrawSprite(self):
self.spriteBatch.Begin()
self.spriteBatch.Draw(self.texture, Rectangle(self.spriteX, self.spriteY, self.texture.Width, self.texture.Height), Color.White)
self.spriteBatch.End()
game = MyGame()
game.Run()
Cheers,
Leaf.