Skip to main content

Use Node structures to manage Pygame resources for rapid game creation and management.

Project description

PygameNode

zread

使用 Node 结构管理Pygame资源, 以便快速制作/管理游戏.

还未开发完成

1. 快速开始

使用 pip install pygame_node 安装这个库.

接着导入库并写一个简单的示例

# 导入 pygame 和 pygame_node 库
import pygame
from pygame_node import *

# 定义作为屏幕大小的变量
SCREEN_WIDTH, SCREEN_HEIGHT = 1200, 700

# 创建一个场景
class MainScene(BaseScene):
    def __init__(self):
        # 初始化场景, 名称为Main
        super().__init__("Main")

# pygame 初始化
pygame.init()

# 创建场景管理器, 把场景传入创建管理器里
main_scene = MainScene()
manager = SceneManager(main_scene)

# 创建屏幕对象和时钟对象
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), pygame.HWSURFACE | pygame.DOUBLEBUF, vsync=True)
clock = pygame.time.Clock()

# 切换到 Main 场景
manager.switch("Main")

# 创建运行状态变量
running = True
while running:
    # 获取事件
    events = pygame.event.get()
    for event in events:
        # 程序退出
        if event.type == pygame.QUIT:
            running = False
            break
    
    # 场景处理事件
    manager.events(events)

    # 场景更新
    manager.update(clock.tick(0) / 1000.0)
    
    # 绘制场景
    manager.draw(screen)
    pygame.display.flip()

pygame.quit()

1.1 场景

可在场景对象里添加初始化函数

from pygame_node import *
from pygame.math import Vector3


class MainScene(BaseScene):
    def __init__(self):
        super().__init__("Main")

    # 定义初始化函数
    def init(self, params: 'BaseScene' = None):
        super().init(params)
        # 创建一个文本 "It's a Text Node." 的 TextNode
        # 位置在Vector3(200, 200, 0)
        text = TextNode("It's a Text Node.", position=Vector3(200, 200, 0), font=Font(size=72))

        # 添加到场景中
        self.addNode(text)

font 参数如果在 pygame.init()后定义了全局字体就可以不传

import pygame
from pygame_node import *

pygame.init()
Node.font = Font(size=72)   # 定义全局字体默认字体

运行后文字和背景是一个色的, 所以要修改背景色或文字颜色后才能看到.

# 在MainScene中添加, 修改背景色为绿色
def draw(self, screen: Surface):
    screen.fill((0x84, 0xC6, 0x69))
    super().draw(screen)

文字颜色可以通过以下代码更改

# 修改文本颜色为白色
text.style.color = Color(255, 255, 255)

可以通过设置文字的 style 不同值达到不同效果

text.style.shadow.enable = True                 # 启用影子
text.style.shadow.color = Color(64, 128, 255)   # 修改影子颜色

text.style.stroke.enable = True                 # 启用边缘
text.style.stroke.size = 0.5                    # 修改边缘大小

文本样式不止这些, 详情.不同效果可以同时使用

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_node-0.0.3.tar.gz (19.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_node-0.0.3-py3-none-any.whl (18.3 kB view details)

Uploaded Python 3

File details

Details for the file pygame_node-0.0.3.tar.gz.

File metadata

  • Download URL: pygame_node-0.0.3.tar.gz
  • Upload date:
  • Size: 19.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for pygame_node-0.0.3.tar.gz
Algorithm Hash digest
SHA256 a86e936a3185ff4775fb72a0edc7a540094340447f519aa66e9eaa64b27fba8c
MD5 4c3b20957b8eb63e69c394619f3e27a1
BLAKE2b-256 277d101fb034a3c9706d6e3a46189ceaa4d9f6a4e4cc37f03e42604ee18f8b23

See more details on using hashes here.

File details

Details for the file pygame_node-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: pygame_node-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 18.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for pygame_node-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 5c096ec881dbb2871ab9866ea5da8085a25018bb4c74e93922836d3a40af4adc
MD5 b703d421c25b4a45a496ecc9edfc4e63
BLAKE2b-256 a2ddd528f3e8dcd352b93ad4b9d9d4e9c5aa698294a129ad411f77d637199921

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