This commit is contained in:
wittmannzl103469 committed 2026-11-05 15:08:42 +01:00
1 parent de41a02749
commit 9af09cebd3
764 files changed
+54218 -78859

No files matched your search

@@ -1,4 +1,4 @@
# pygame-ce - Python Game Library
# pygame - Python Game Library
# Copyright (C) 2000-2001 Pete Shinners
#
# This library is free software; you can redistribute it and/or
@@ -21,42 +21,24 @@
It is written on top of the excellent SDL library. This allows you
to create fully featured games and multimedia programs in the python
language. The package is highly portable, with games running on
Windows, macOS, OS X, BeOS, FreeBSD, IRIX, and Linux."""
Windows, MacOS, OS X, BeOS, FreeBSD, IRIX, and Linux."""
import os
import sys
import platform
import os
# Choose Windows display driver
if os.name == "nt":
pygame_dir = os.path.split(__file__)[0]
dll_parents = {pygame_dir}
try:
# For editable support, add some more folders where DLLs are available.
# This block only executes under an editable install. In a "normal"
# install, the json file will not be installed at the supplied path.
with open(
os.path.join(
os.path.dirname(pygame_dir), "buildconfig", "win_dll_dirs.json"
),
encoding="utf-8",
) as f:
import json
dll_parents.update(json.load(f))
del json
except (FileNotFoundError, ValueError):
pass
# pypy does not find the dlls, so we add package folder to PATH.
os.environ["PATH"] = os.environ["PATH"] + ";" + pygame_dir
d = "" # define variable here so that we can consistently delete it
for d in dll_parents:
# adding to PATH is the legacy way, os.add_dll_directory is the new
# and recommended method. For extra safety we do both
os.environ["PATH"] = os.environ["PATH"] + ";" + d
os.add_dll_directory(d)
# windows store python does not find the dlls, so we run this
if sys.version_info > (3, 8):
os.add_dll_directory(pygame_dir) # only available in 3.8+
# cleanup namespace
del pygame_dir, dll_parents, d
del pygame_dir
# when running under X11, always set the SDL window WM_CLASS to make the
# window managers correctly match the pygame window.
@@ -73,13 +55,9 @@ class MissingModule:
def __init__(self, name, urgent=0):
self.name = name
exc_type, exc_msg, _ = sys.exc_info()
if exc_type is not None:
self.info = str(exc_msg)
self.reason = f"{exc_type.__name__}: {self.info}"
else:
self.info = "<no info>"
self.reason = f"<no exception>: {self.info}"
exc_type, exc_msg = sys.exc_info()[:2]
self.info = str(exc_msg)
self.reason = f"{exc_type.__name__}: {self.info}"
self.urgent = urgent
if urgent:
self.warn()
@@ -106,31 +84,6 @@ class MissingModule:
print(message)
# This is a special loader for WebAssembly platform
# where pygame is in fact statically linked
# mixing single phase (C) and multiphase modules (cython)
if sys.platform in ("wasi", "emscripten"):
try:
import pygame.base as pygame_static
except ModuleNotFoundError:
pygame_static = None
if pygame_static:
pygame = sys.modules[__name__]
# cython modules use multiphase initialisation when not in builtin Inittab.
from pygame import _sdl2
import importlib.machinery
loader = importlib.machinery.FrozenImporter
spec = importlib.machinery.ModuleSpec("", loader)
pygame_static.import_cython(spec)
del loader, spec
del pygame_static
# we need to import like this, each at a time. the cleanest way to import
# our modules is with the import command (not the __import__ function)
# isort: skip_file
@@ -139,33 +92,31 @@ if sys.platform in ("wasi", "emscripten"):
from pygame.base import * # pylint: disable=wildcard-import; lgtm[py/polluting-import]
from pygame.constants import * # now has __all__ pylint: disable=wildcard-import; lgtm[py/polluting-import]
from pygame.version import * # pylint: disable=wildcard-import; lgtm[py/polluting-import]
from pygame.rect import Rect
from pygame.rwobject import encode_string, encode_file_path
import pygame.rect
Rect = pygame.rect.Rect
FRect = pygame.rect.FRect
import pygame.surflock
import pygame.color
Color = pygame.color.Color
import pygame.bufferproxy
BufferProxy = pygame.bufferproxy.BufferProxy
import pygame.math
Vector2 = pygame.math.Vector2
Vector3 = pygame.math.Vector3
from pygame.base import __version__
__version__ = ver
# next, the "standard" modules
# we still allow them to be missing for stripped down pygame distributions
if get_sdl_version() < (2, 0, 0):
# cdrom only available for SDL 1.2.X
try:
import pygame.cdrom
except (ImportError, OSError):
cdrom = MissingModule("cdrom", urgent=1)
try:
import pygame.display
except (ImportError, OSError):
@@ -178,7 +129,6 @@ except (ImportError, OSError):
try:
import pygame.event
from pygame.event import Event
except (ImportError, OSError):
event = MissingModule("event", urgent=1)
@@ -189,7 +139,6 @@ except (ImportError, OSError):
try:
import pygame.joystick
from pygame.joystick import Joystick
except (ImportError, OSError):
joystick = MissingModule("joystick", urgent=1)
@@ -213,6 +162,16 @@ except (ImportError, OSError):
_attribute_undefined("pygame.Cursor")
try:
import pygame.sprite
except (ImportError, OSError):
sprite = MissingModule("sprite", urgent=1)
try:
import pygame.threads
except (ImportError, OSError):
threads = MissingModule("threads", urgent=1)
try:
import pygame.pixelcopy
except (ImportError, OSError):
@@ -220,7 +179,6 @@ except (ImportError, OSError):
try:
import pygame.surflock
from pygame.surface import Surface, SurfaceType
except (ImportError, OSError):
@@ -229,13 +187,6 @@ except (ImportError, OSError):
SurfaceType = Surface
# sprite.py is using pygame.surface.Surface type
try:
import pygame.sprite
except (ImportError, OSError):
sprite = MissingModule("sprite", urgent=1)
try:
import pygame.mask
from pygame.mask import Mask
@@ -254,9 +205,16 @@ except (ImportError, OSError):
_attribute_undefined("pygame.PixelArray")
try:
from pygame.overlay import Overlay
except (ImportError, OSError):
def Overlay(format, size): # pylint: disable=unused-argument
_attribute_undefined("pygame.Overlay")
try:
import pygame.time
from pygame.time import Clock
except (ImportError, OSError):
time = MissingModule("time", urgent=1)
@@ -277,15 +235,13 @@ try:
import pygame.font
import pygame.sysfont
from pygame.font import Font
pygame.font.SysFont = pygame.sysfont.SysFont
pygame.font.get_fonts = pygame.sysfont.get_fonts
pygame.font.match_font = pygame.sysfont.match_font
except (ImportError, OSError):
font = MissingModule("font", urgent=0)
# try to load pygame.mixer_music before mixer, for py2app...
# try and load pygame.mixer_music before mixer, for py2app...
try:
import pygame.mixer_music
@@ -296,8 +252,6 @@ except (ImportError, OSError):
try:
import pygame.mixer
from pygame.mixer import Sound
from pygame.mixer import Channel
except (ImportError, OSError):
mixer = MissingModule("mixer", urgent=0)
@@ -306,86 +260,20 @@ try:
except (ImportError, OSError):
scrap = MissingModule("scrap", urgent=0)
# Two lazily imported modules to avoid loading numpy unnecessarily
from importlib.util import LazyLoader, find_spec, module_from_spec
def lazy_import(name):
"""Lazily import a pygame module.
See https://docs.python.org/3/library/importlib.html#implementing-lazy-imports
Only load the module upon its first attribute access.
Lazily imported modules are directly referenced in packager_imports function.
"""
fullname = "pygame." + name
spec = find_spec(fullname)
if spec is None or spec.loader is None:
return MissingModule(name, urgent=0)
loader = LazyLoader(spec.loader)
spec.loader = loader
module = module_from_spec(spec)
sys.modules[fullname] = module
loader.exec_module(module)
return module
# Check if numpy is available for surfarray and sndarray modules
numpy_missing = find_spec("numpy") is None
try:
if numpy_missing:
# Always fails here. Need the error message for MissingModule.reason
import numpy # pylint: disable=ungrouped-imports
# Check that module dependencies are not missing, or get error message
import pygame.pixelcopy # pylint: disable=ungrouped-imports
import pygame.surfarray
except (ImportError, OSError):
surfarray = MissingModule("surfarray", urgent=0)
else:
surfarray = lazy_import("surfarray")
try:
if numpy_missing:
# Always fails here. Need the error message for MissingModule.reason
import numpy # pylint: disable=ungrouped-imports
# Check that module dependencies are not missing, or get error message
import pygame.mixer # pylint: disable=ungrouped-imports
import pygame.sndarray
except (ImportError, OSError):
sndarray = MissingModule("sndarray", urgent=0)
else:
sndarray = lazy_import("sndarray")
del LazyLoader, find_spec, lazy_import, module_from_spec, numpy_missing
try:
import pygame._debug
from pygame._debug import print_debug_info
import pygame.fastevent
except (ImportError, OSError):
debug = MissingModule("_debug", urgent=0)
try:
import pygame.system
from pygame._data_classes import PowerState as power_state
power_state.__module__ = "pygame.system"
del power_state
except (ImportError, OSError):
system = MissingModule("system", urgent=0)
try:
from pygame.window import Window
except (ImportError, OSError):
def Window(title="pygame window", size=(640, 480), position=None, **kwargs): # pylint: disable=unused-argument
_attribute_undefined("pygame.Window")
try:
import pygame.typing
except (ImportError, OSError):
typing = MissingModule("typing", urgent=0)
fastevent = MissingModule("fastevent", urgent=0)
# there's also a couple "internal" modules not needed
# by users, but putting them here helps "dependency finder"
@@ -407,21 +295,13 @@ except (ImportError, OSError):
def packager_imports():
"""Some additional imports that py2app/py2exe will want to see.
This function is never executed.
Some tools scan the source code for import statements.
"""
"""some additional imports that py2app/py2exe will want to see"""
import atexit
import numpy
import OpenGL.GL
import pygame.macosx
import pygame.colordict
# lazy imports
import pygame.surfarray
import pygame.sndarray
# make Rects pickleable
@@ -452,23 +332,14 @@ def __color_reduce(c):
copyreg.pickle(Color, __color_reduce, __color_constructor)
# Thanks for supporting pygame. Without support now, there won't be pygame later.
if "PYGAME_HIDE_SUPPORT_PROMPT" not in os.environ:
import sysconfig
python_version = platform.python_version()
if (
sys.platform not in ("wasi", "emscripten")
and (sys.version_info >= (3, 13, 0))
and sysconfig.get_config_var("Py_GIL_DISABLED")
):
python_version += f"t, {'' if sys._is_gil_enabled() else 'No '}GIL"
print(
f"pygame-ce {ver} (SDL {'.'.join(map(str, get_sdl_version()))}, "
f"Python {python_version})"
"pygame {} (SDL {}.{}.{}, Python {}.{}.{})".format( # pylint: disable=consider-using-f-string
ver, *get_sdl_version() + sys.version_info[0:3]
)
)
del python_version, sysconfig
print("Hello from the pygame community. https://www.pygame.org/contribute.html")
# cleanup namespace
del pygame, os, sys, platform, MissingModule, copyreg, packager_imports
del pygame, os, sys, MissingModule, copyreg, packager_imports