Skip to main content

A hot reload enhancer for pygame developement

Project description

pygame-hotreload

PyPI - Version PyPI - Python Version


Table of Contents

Installation

pip install pygame-hotreload

How it work

The pygame-hotreload module is a simple hot-reload module for pygame. It is partialy parse the main python file provided.

It looks for these comments in the main file:

imports-start-hotreload

# imports-start-hotreload
...
# imports-end-hotreload

This is where the module will look for the imports that needed for the game loop.

globals-start-hotreload

# globals-start-hotreload
...
# globals-end-hotreload

This is where the module will look for the global variables that needed for the game loop.

init-start-hotreload

# init-start-hotreload
...
# init-end-hotreload

This is where the module will look for the initialization of the game loop. There is where you should initialize your global variables.

loop-start-hotreload

# loop-start-hotreload
...
# loop-end-hotreload

This is where the module will look for the game loop. On changes within these comments the screen surface is updated.

Example

game.py

# game.py

from pygame_hotreload import HotReload, set_caption
# imports-start-hotreload
import pygame
# imports-end-hotreload

# Initialize pygame
pygame.init()

# Set the window caption
set_caption("Hot Reload Example")
clock = pygame.time.Clock()
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))

# globals-start-hotreload
global dt
global player_pos
global player_pos_2
# globals-end-hotreload

# init-start-hotreload
def init():
    global dt
    global player_pos
    global player_pos_2
    dt = 0
    player_pos = pygame.Vector2(screen.get_width() / 2, screen.get_height() / 2)
    player_pos_2 = pygame.Vector2(screen.get_width() // 3, screen.get_height() // 3)
# init-end-hotreload


# loop-start-hotreload
def loop():
    global dt
    global player_pos
    global player_pos_2
    screen.fill("black")
    pygame.draw.rect(screen, "purple", 
                    pygame.Rect(player_pos.x, player_pos.y, 100, 100))
    pygame.draw.rect(screen, "purple", 
                    pygame.Rect(player_pos_2.x, player_pos_2.y, 100, 100), 1)

    keys = pygame.key.get_pressed()
    if keys[pygame.K_z] or keys[pygame.K_UP]:
        player_pos.y -= 300 * dt
    if keys[pygame.K_s] or keys[pygame.K_DOWN]:
        player_pos.y += 300 * dt
    if keys[pygame.K_q] or keys[pygame.K_LEFT]:
        player_pos.x -= 300 * dt
    if keys[pygame.K_d] or keys[pygame.K_RIGHT]:
        player_pos.x += 300 * dt

    dt = clock.tick(60) / 1000
# loop-end-hotreload

# Initialize the hot reload
hotreload = HotReload(
    main_file=__file__,
    screen=screen,
    clock=clock,
    clock_tick=60,
    gen_script_name="gen.py",
)

hotreload.run()

gen.py

import pygame
global dt
global player_pos
global player_pos_2
def init():
    global dt
    global player_pos
    global player_pos_2
    dt = 0
    player_pos = pygame.Vector2(screen.get_width() / 2, screen.get_height() / 2)
    player_pos_2 = pygame.Vector2(screen.get_width() // 3, screen.get_height() // 3)
def loop():
    global dt
    global player_pos
    global player_pos_2
    screen.fill("black")
    pygame.draw.rect(screen, "purple", 
                    pygame.Rect(player_pos.x, player_pos.y, 100, 100))
    pygame.draw.rect(screen, "purple", 
                    pygame.Rect(player_pos_2.x, player_pos_2.y, 100, 100), 1)

    keys = pygame.key.get_pressed()
    if keys[pygame.K_z] or keys[pygame.K_UP]:
        player_pos.y -= 300 * dt
    if keys[pygame.K_s] or keys[pygame.K_DOWN]:
        player_pos.y += 300 * dt
    if keys[pygame.K_q] or keys[pygame.K_LEFT]:
        player_pos.x -= 300 * dt
    if keys[pygame.K_d] or keys[pygame.K_RIGHT]:
        player_pos.x += 300 * dt

    dt = clock.tick(60) / 1000

Contributing

Contributions are welcome, and they are greatly appreciated! Every little bit helps, and credit will always be given.

Development

To set up pygame-hotreload for local development:

  • Fork the pygame-hotreload repository.
  • Clone your fork locally
  • Use pipenv shell to enter the virtual environment
  • Use pipenv install to install all dependencies
  • Make a change, and push your local branch to your fork
  • Make a pull request
  • You can build your own version of the project with hatch version beta && hatch build --wheel and install it with pipenv install dist/pygame_hotreload-<VERSION>-py3-none-any.whl

License

pygame-hotreload is distributed under the terms of the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pygame_hotreload-0.0.33.tar.gz (24.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pygame_hotreload-0.0.33-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file pygame_hotreload-0.0.33.tar.gz.

File metadata

  • Download URL: pygame_hotreload-0.0.33.tar.gz
  • Upload date:
  • Size: 24.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for pygame_hotreload-0.0.33.tar.gz
Algorithm Hash digest
SHA256 4e877ec241f5dd37423a81fae952c49d43590ce8d9743d965bff6a40df09e3fb
MD5 41b46553987d75ad4aceb1a32d5f1980
BLAKE2b-256 0f7be3d71f8f5ea9bf46032fc26f9079988b35d379a5f3f1deaa6538a1eca791

See more details on using hashes here.

File details

Details for the file pygame_hotreload-0.0.33-py3-none-any.whl.

File metadata

File hashes

Hashes for pygame_hotreload-0.0.33-py3-none-any.whl
Algorithm Hash digest
SHA256 68eb9934ee9efb0c9b3fe6dd989b9c7818f12f5d33c1b2ff166952defdd2f0cd
MD5 7bfcb8292ea2cc8b1b99956b602b3c15
BLAKE2b-256 ff6f4c289aec81a6b03f4281dd9e97cd377dc1fc454d122cfd976da0e733669f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page