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.2.tar.gz (16.2 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.2-py3-none-any.whl (16.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pygame_node-0.0.2.tar.gz
  • Upload date:
  • Size: 16.2 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.2.tar.gz
Algorithm Hash digest
SHA256 9b175c82e81d9a3f78f2d94a4bf27a971d0cd03eda6b1c488ea3542fca6397d8
MD5 0115a52fcf5f4fed58cd12bf47e936ec
BLAKE2b-256 005ef7b9d93e6208becbf2e5df8f14c42e335068efda372b2ec78464802d7a22

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pygame_node-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 16.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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6b16f7d87335ccd0e3d18adbe458dc74fa561ed1e39e05dfdc50b36b9f64e993
MD5 04a19670ad2e722d2cc4900b0d507c65
BLAKE2b-256 97a062744c568fd9dc457454221e50ef13c8f7e724d1996a65aab5d9cd8999d5

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