DEV Community

Cover image for XNA style Game class with Python/Pygame
Adam K Dean
Adam K Dean

Posted on

3 1

XNA style Game class with Python/Pygame

XNA/MonoGame are great. You have such a wealth of classes at your disposable. There have been some great games made with it; Terraria and Magicka among them. But when you just want to visualise something that you're coding, well, it just seems too much effort.

Take for example my Circle primitive class for XNA/MonoGame. It takes that entire class just to render a circle. You'd think XNA would come with it, but no, not circles. OK, let's just make lines, it comes with easy lines doesn't it. Not really. It seems like they didn't really want to offer you a simple solution, instead leaving that stuff up to you. Which is fine if you're making a big application/game, but when you just want to get something onto the screen, there has to be an easier way.

And there is: Pygames for Python. I know, it's just icky high-level pseudo language right. They don't even use braces! But it's actually pretty good. It's easy, it's quick, and it's simple.

One thing I do like about XNA is the way the Game class is laid out. I find it really intuitive and nice. So I went ahead and attempted to implement this in Python using Pygames.

Bear in mind this is my first ever real Python script, so it might have a few no-no's in there. Also, you may need this font if you want to run this. Either that or change "../fonts/visitor1.ttf" to None.

import sys
import pygame
from pygame.locals import * #@UnusedWildImport

class Game:
    def __init__(self, width, height, caption="Game"):
        self.width = width
        self.height = height
        self.caption = caption
        self.framerate = 30        
        self.foreground_color = (255, 255, 255)
        self.background_color = (100, 149, 237)
        self.initialize()
        self.loop()

    def initialize(self):
        pygame.init()
        pygame.display.set_caption(self.caption)
        self.screen = pygame.display.set_mode((self.width, self.height))        
        self.font = pygame.font.Font("../fonts/visitor1.ttf", 20)

    def loop(self):
        self.clock = pygame.time.Clock()
        while 1:
            gametime = self.clock.get_time()
            self.update(gametime)
            self.render(gametime)
            self.clock.tick(self.framerate)

    def update(self, gametime):        
        self.text = self.font.render("FPS: %d" % self.clock.get_fps(), 
                                     1, self.foreground_color)
        self.handle_input(pygame.event.get())

    def render(self, gametime):
        surface = pygame.Surface(self.screen.get_size())
        surface.convert()
        surface.fill(self.background_color)
        surface.blit(self.text, (8, 6))        
        self.screen.blit(surface, (0, 0))
        pygame.display.flip()

    def handle_input(self, events):
        for event in events:
            if event.type == pygame.QUIT: 
                sys.exit()
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_ESCAPE:
                    sys.exit()

if __name__ == "__main__":
    game = Game(800, 600, "A test game")
Enter fullscreen mode Exit fullscreen mode

Looks legit to me!

Looks legit, right?

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo πŸ“Šβœ¨

Top comments (0)

Image of PulumiUP 2025

From Cloud to Platforms: What Top Engineers Are Doing Differently

Hear insights from industry leaders about the current state and future of cloud and IaC, platform engineering, and security.

Save Your Spot

πŸ‘‹ Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay