Sunday, June 3, 2007

XNA - IronPython

IronPython et la version du célèbre langage (mon favori en fait) porté sur la plateforme .NET.
Xna et le framework de développement de jeux pour XBOX et Windows .Net. Voici un petit exemple écrit par Leaf:


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()

1 comment:

  1. en fait tu peux facilement convertir cet exemple en Boo, avec le bénéfice de ne pas avoir tout ces vilains "self" :)

    ReplyDelete