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.1.0.tar.gz (19.8 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.1.0-py3-none-any.whl (19.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pygame_node-0.1.0.tar.gz
  • Upload date:
  • Size: 19.8 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.1.0.tar.gz
Algorithm Hash digest
SHA256 7c12f2211068f118d031d5853b250d13a78cc721fa50eabc896535ef363e7b2e
MD5 4653c60536e6d0f41d091c4e6389c922
BLAKE2b-256 de0e565c04a3f0cfe210cb5913a3776dd596d02c61d92d06fc7998b16c6b3c12

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pygame_node-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 19.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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9eef7afb25bababf7a28242359cc94c3d831ac99896b861e43092fd401f30c29
MD5 7a8a65e089febe3f47d8dd222c2bee11
BLAKE2b-256 c9b04545b53339da443f4b1f16df84aef97cafe2df0fab1074f88b9b57496aa3

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