def handle_error(
    error: Exception,
    popup: bool = False,
    msg: str = "Oh no!",
    program_name: str = "The program",
) -> None:
    """Premade error logging system for crashes."""
    try:
        pygame.quit()
    except:
        pass

    with open(path.handle("errors.log"), "a+") as file:
        try:
            file.write(f"Error at {datetime.now()}\n")
        except:
            file.write("Error at undetermined time\n")

        try:
            sdl_ver = ".".join([str(v) for v in pygame.get_sdl_version()])
            os = f"{platform.system()} {platform.version()}"
            file.write(
                f"System Info: OS- {os}, SDL- {sdl_ver}, CPU- {platform.processor()}\n"
            )
        except:
            file.write("Error in getting system information\n")

        error_trace = traceback.format_tb(error.__traceback__) + [
            f"{_get_full_class_name(error)}: " + str(error) + "\n"
        ]
        for line in error_trace:
            file.write(line)

        file.write("\n")  # spacing for next error

    if popup:
        try:
            if pygame.version.vernum[0] == 2:
                from pygame._sdl2 import messagebox

                messagebox(
                    msg,
                    f"{program_name} has crashed. An error log has generated at {path.handle('errors.log')}",
                    error=True,
                )
        except:
            pass

    raise error
Beispiel #2
0

def load_img(file):
    return pygame.image.load(os.path.join(data_dir, file))


pygame.display.init()
pygame.key.set_repeat(1000, 10)

for driver in get_drivers():
    print(driver)

import random
answer = messagebox("I will open two windows! Continue?",
                    "Hello!",
                    info=True,
                    buttons=('Yes', 'No', 'Chance'),
                    return_button=0,
                    escape_button=1)
if answer == 1 or (answer == 2 and random.random() < .5):
    import sys
    sys.exit(0)

win = Window('asdf', resizable=True)
renderer = Renderer(win)
tex = Texture(renderer, load_img('alien1.gif'))

running = True

x, y = 250, 50
clock = pygame.time.Clock()
Beispiel #3
0
def load_img(file):
    return pg.image.load(os.path.join(data_dir, file))


pg.display.init()
pg.key.set_repeat(1000, 10)

for driver in get_drivers():
    print(driver)

import random

answer = messagebox(
    "I will open two windows! Continue?",
    "Hello!",
    info=True,
    buttons=("Yes", "No", "Chance"),
    return_button=0,
    escape_button=1,
)
if answer == 1 or (answer == 2 and random.random() < 0.5):
    import sys

    sys.exit(0)

win = Window("asdf", resizable=True)
renderer = Renderer(win)
tex = Texture.from_surface(renderer, load_img("alien1.gif"))

running = True

x, y = 250, 50