Skip to content

Instantly share code, notes, and snippets.

@WillSams
Last active September 28, 2022 02:48
Show Gist options
  • Save WillSams/3896411 to your computer and use it in GitHub Desktop.
Save WillSams/3896411 to your computer and use it in GitHub Desktop.
MonoGame w/ IronPython Example
import os, clr
clr.AddReferenceToFile("MonoGame.Framework.dll")
clr.AddReferenceToFile("OpenTK.dll")
from Microsoft.Xna.Framework import *
from Microsoft.Xna.Framework.Graphics import *
class App(Game):
def __init__(self):
self.graphics = GraphicsDeviceManager(self)
self.Content.RootDirectory = os.getcwd() + "/Content"
def Initialize(self):
""" Allows the game to perform any initialization it needs to before starting to run. This is where it can query for any required
services and load any non-graphic related content. Calling base.Initialize will enumerate through any components and initialize them as well. """
# TODO: Add your initialization logic here
self.Initialize()
def LoadContent(self):
""" LoadContent will be called once per game and is the place to load all of your content. """
# Create a new SpriteBatch, which can be used to draw textures.
self.spriteBatch = SpriteBatch(GraphicsDevice)
#TODO: use this.Content to load your game content here
def Update(self, gameTime):
""" Allows the game to run logic such as updating the world, checking for collisions, gathering input, and playing audio. """
# For Mobile devices, this logic will close the Game when the Back button is pressed
if GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed:
self.Exit()
# TODO: Add your update logic here
Game.Update(gameTime)
def Draw(self, gameTime):
""" This is called when the game should draw itself. """
self.graphics.GraphicsDevice.Clear(Color.CornflowerBlue)
#TODO: Add your drawing code here
Game.Draw(gameTime)
game = App()
game.Run()
@SilentPenguin
Copy link

Hey WillSams, just played around with this. I thought you might be interested to learn that this appears to work adequately. I don't know what difficulties you came up against, maybe your argument errors on line 39 and 45?

I'm using the latest version of MonoGame (3.4) and Ironpython (2.7.5) at time of writing, along with PTVS (2.2). Don't forget to include the search paths somehow.

I've included my code below for your perusal. It should produce that wonderful cornflower blue.

edit, I've modified the code slightly to handle content correctly, Monogame cannot find content in your working directory correctly. This means the value of RootDirectory must be set to the absolute path of your content, specifically, which would be the output directory of the Content.mgcb pipeline. I've configured my pipeline to point to the regular Content folder, rather than bin.

import os, clr
clr.AddReferenceToFile("MonoGame.Framework.dll")
clr.AddReferenceToFile("OpenTK.dll")

from Microsoft.Xna.Framework import *
from Microsoft.Xna.Framework.Graphics import *

class App(Game):
    def __init__(self):
        self.graphics = GraphicsDeviceManager(self)
        self.Content.RootDirectory = os.getcwd() + "/Content"

    def Draw(self, gameTime):
        self.graphics.GraphicsDevice.Clear(Color.CornflowerBlue)
        Game.Draw(self, gameTime)

if __name__ == "__main__":
    app = App()
    app.Run()

@nightblade9
Copy link

I got this to work with MonoGame 3.7.0.7 by copying libSDL2-2 and the OpenAL DLLs into bin\Debug by hand ... there's no process to handle that for you (NuGet takes care of it with all C# code).

@WillSams
Copy link
Author

WillSams commented Jun 10, 2020

Funny how timely this is, I haven't looked at much Python in years until about 3 days ago because I'm trying to teach modding Minecraft via Python to a kid. Been out of the loop, I seriously didn't realize Iron Python was still being worked on. The latest version supports .NET Core! I need to jump back in.

@nightblade9
Copy link

@WillSams IronPython came back into the spotlight recently - they released a new version a couple of months ago, which is fantastic. Word is they're slowly progressing on IronPython 3 (Python 3), but 2.x (Python 2.7) seems quite stable/functional enough to actually build production things on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment