Skip to main content

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

Project description

PygameNode

使用 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 场景

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

import pygame.font
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=pygame.font.Font(size=72))

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

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

import pygame
from pygame_node import *

pygame.init()
Node.font = pygame.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.1.tar.gz (16.1 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.1-py3-none-any.whl (16.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pygame_node-0.0.1.tar.gz
  • Upload date:
  • Size: 16.1 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.1.tar.gz
Algorithm Hash digest
SHA256 bc9141a3913ba4ecb5fcdb487e74caaac9dded991a973db2a30a7a11697396bf
MD5 79bc6f7086eb9d5889ef50fb259f9d4b
BLAKE2b-256 b0bc5d0a0c8889101e04e63deb2fff458ccde1f31e1373b0fa2aa2f5b3eeed75

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pygame_node-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 16.1 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7b13c6da73152528f088fa6ad48f3a7c756500e61ab8c15acefd84da7b381fc8
MD5 21f766cb9107094a9dab81a36e094f7c
BLAKE2b-256 27e8cd76da099c828c0b3ee93f9bdb74cde037f28b3e75b3e42822399bb1ee71

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