PyObjC SpriteKit Framework

12.1 · active · verified Tue Apr 14

PyObjC provides Python bindings for Objective-C frameworks on macOS, enabling Python developers to interact with Apple's system APIs. `pyobjc-framework-spritekit` offers wrappers for the SpriteKit framework, which is Apple's high-performance 2D graphics framework used for games and other graphics-intensive applications. The library is currently at version 12.1 and maintains an active release cadence, typically aligning with new macOS SDK and Python version updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a basic macOS application using PyObjC, display an `SKView` within an `NSWindow`, and present a simple `SpriteKit.SKScene` with a 'Hello, PyObjC SpriteKit!' label. It requires a running macOS environment. The `PyObjCTools.AppHelper` is used to manage the Cocoa event loop.

import AppKit
import SpriteKit
from PyObjCTools import AppHelper

# Define a simple SpriteKit scene
class MyScene(SpriteKit.SKScene):
    def didMoveToView_(self, view):
        if not self.contentCreated:
            self.createSceneContents()
            self.contentCreated = True

    def createSceneContents(self):
        self.backgroundColor = SpriteKit.SKColor.redColor()
        self.scaleMode = SpriteKit.SKSceneScaleMode.aspectFit

        helloNode = SpriteKit.SKLabelNode.labelNodeWithFontNamed_('Chalkduster')
        helloNode.text = 'Hello, PyObjC SpriteKit!'
        helloNode.fontSize = 24
        helloNode.position = AppKit.CGPointMake(self.frame().size.width / 2, self.frame().size.height / 2)
        self.addChild_(helloNode)

# Define an Application Delegate to set up the window and scene
class AppDelegate(AppKit.NSObject):
    def applicationDidFinishLaunching_(self, notification):
        rect = AppKit.NSMakeRect(0, 0, 800, 600)
        self.window = AppKit.NSWindow.alloc().initWithContentRect_styleMask_backing_defer_(
            rect, 
            AppKit.NSWindowStyleMaskTitled | AppKit.NSWindowStyleMaskClosable | AppKit.NSWindowStyleMaskMiniaturizable | AppKit.NSWindowStyleMaskResizable,
            AppKit.NSBackingStoreBuffered,
            False
        )
        self.window.center()
        self.window.setTitle_('PyObjC SpriteKit Example')

        # Create an SKView and set it as the window's content view
        skView = SpriteKit.SKView.alloc().initWithFrame_(rect)
        self.window.setContentView_(skView)

        # Create and present the scene
        scene = MyScene.sceneWithSize_(skView.bounds().size)
        skView.presentScene_(scene)
        skView.setShowsFPS_(True)
        skView.setShowsNodeCount_(True)

        self.window.makeKeyAndOrderFront_(None)

    def applicationShouldTerminateAfterLastWindowClosed_(self, sender):
        return True

# Start the Cocoa application
if __name__ == '__main__':
    app = AppKit.NSApplication.sharedApplication()
    delegate = AppDelegate.alloc().init()
    app.setDelegate_(delegate)
    AppHelper.runEventLoop()

view raw JSON →