Paste number 24997: XNA sample in IronPython

Index of paste annotations: 1

Paste number 24997: XNA sample in IronPython
Pasted by: psykotic
When:7 years, 7 months ago
Share:Tweet this! | http://paste.lisp.org/+JAD
Channel:None
Paste contents:
Raw Source | XML | Display As
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.x = self.y = 0
        self.dx = self.dy = 1
        self.init_component()

    def init_component(self):
        self.gfx = Microsoft.Xna.Framework.Components.GraphicsComponent()
        self.GameComponents.Add(self.gfx)

    def OnStarting(self):
        Microsoft.Xna.Framework.Game.OnStarting(self)
        self.gfx.GraphicsDevice.DeviceReset += lambda self, *args: self.load_resources()
        self.load_resources()

    def load_resources(self):
        self.texture = Texture2D.FromFile(self.gfx.GraphicsDevice, "texture.bmp")
        self.sprite_batch = SpriteBatch(self.gfx.GraphicsDevice)

    def Update(self):
        self.update_sprite()
        self.UpdateComponents()

    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.gfx.GraphicsDevice.Clear(Color.CornflowerBlue)
            self.gfx.GraphicsDevice.BeginScene()
            self.draw_sprite()
            self.DrawComponents()
            self.gfx.GraphicsDevice.EndScene()
            self.gfx.GraphicsDevice.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()

game = MyGame()
game.Run()

Annotations for this paste:

Annotation number 1: Version that manages GraphicsDevice directly rather than use GraphicsComponent
Pasted by: psykotic
When:7 years, 7 months ago
Share:Tweet this! | http://paste.lisp.org/+JAD/1
Paste contents:
Raw Source | Display As
from xna import *

class App(Game):
    def __init__(self):
        self.x = self.y = 0
        self.dx = self.dy = 1

    def OnStarting(self):
        Microsoft.Xna.Framework.Game.OnStarting(self)
        self.device = GraphicsDevice(GraphicsAdapter.DefaultAdapter, DeviceType.Hardware, self.Window.Handle, CreateOptions.HardwareVertexProcessing, PresentationParameters())
        self.device.DeviceReset += lambda self, *args: self.load_resources()
        self.load_resources()

    def load_resources(self):
        self.texture = Texture2D.FromFile(self.device, "texture.bmp")
        self.sprite_batch = SpriteBatch(self.device)

    def Update(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):
        self.device.Clear(Color.CornflowerBlue)
        self.device.BeginScene()
        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()
        self.device.EndScene()
        self.device.Present()

app = App()
app.Run()

Colorize as:
Show Line Numbers

Lisppaste pastes can be made by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively.