You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

GTC-Pygame-Runtime-Support

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

GTC-Pygame-Runtime-Support

Integrated Pygame application development support.

0.0.18
pipPyPI
Maintainers
1

GTC Pygame Runtime Support

集成 Pygame 应用开发支持

Finding an English version? click here

нашли русскую версию? кликните сюда

此软件包可用于 Pygame 应用开发,包括但不限于游戏、窗体应用,应于版本大于 3.4.0 的 Python 环境中运行。

软件包安装

向绝大多数软件包一样,本软件包也可使用 pip 下载并安装

pip install GTC_Pygame_Runtime_Support

如果 pip 不慎爆炸,您可以从 Github 处直接复制源码

使用示例

按钮

PRS 提供三种按钮类型,全部位于button.py文件内,此处以其中的FeedbackButton为例进行展示

import pygame
import GTC_Pygame_Runtime_Support as gPRS
screen = pygame.display.set_mode((200, 200))
button = gPRS.button.FeedbackButton([40, 40], [20, 20], '114', 15, screen, bg_color=[0, 145, 220], 
                                    border_color=[209, 240, 255], text_color=[255, 255, 255],
                                    change_color=((0, 145, 220), (0, 225, 0))) # 生成按钮
running = 1
clock = pygame.time.Clock()
while running: # 主循环
    screen.fill((255, 255, 255))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = 0
    button.operate(pygame.mouse.get_pos(), pygame.mouse.get_pressed(3)) # 按钮贴图处
    if button.on_click:
        print("clicked")
    pygame.display.flip()
    clock.tick(60)

运行如上代码,你将能够创建一个显示文字为 114 的按钮,并且点击这个按钮后,控制台会输出"clicked",并且根据用户的动作,按钮将会有不同的反馈。

页面

PRS 提供的页面类型位于page.py内,此处以PlainPage为例进行展示

import sys
import pygame
import GTC_Pygame_Runtime_Support as gPRS

screen = pygame.display.set_mode((500, 500))
bp = gPRS.page.PlainPage([300, 300], [300, 1000], [100, 100], screen, 1.4, True)

if __name__ == '__main__':
    clock = pygame.time.Clock()
    bp.surface.fill((255, 255, 255))
    for i in range(100):
        pygame.draw.line(bp.surface, [0, 0, 0], [0, 10 * i], [300, 10 * i])
    bp.set_as_background()

    mw = [False, False]
    while True:
        mw = [False, False]
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 4:
                    mw[1] = True
                elif event.button == 5:
                    mw[0] = True
        bp.operate(pygame.mouse.get_pos(), pygame.mouse.get_pressed(3), mw, True)
        pygame.display.flip()
        clock.tick(60)

运行上述代码,你将能够创建一个可由鼠标滚轮滚动和鼠标左键拖动的页面,你可以自行更换其背景,只要你在主循环前调用函数bp.set_as_background()

PRS 中的嵌套操作

在 PRS 中,一切展示型组件都可通过托管 (trusteeship) 的方式进行嵌套。

在页面中嵌套按钮

PRS 中所有组件托管按钮的方式均相同,就像下面展示的:

import sys
import pygame
import GTC_Pygame_Runtime_Support as gPRS

screen = pygame.display.set_mode((500, 500))
bp = gPRS.page.PlainPage([300, 300], [300, 1000], [100, 100], screen, 1.4, True)

if __name__ == '__main__':
    clock = pygame.time.Clock()
    bp.surface.fill((255, 255, 255))
    for i in range(100):
        pygame.draw.line(bp.surface, [0, 0, 0], [0, 10 * i], [300, 10 * i])
    bp.set_as_background()
    bt = gPRS.button.FeedbackButton([40, 40], [20, 20], '114', 10, bp.surface)
    bp.add_button_trusteeship(bt)

    mw = [False, False]
    while True:
        mw = [False, False]
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 4:
                    mw[1] = True
                elif event.button == 5:
                    mw[0] = True
        bp.operate(pygame.mouse.get_pos(), pygame.mouse.get_pressed(3), mw, True)
        if bt.on_click:
            print('clicked')
        pygame.display.flip()
        clock.tick(60)

PRS 通常通过item.add_button_trusteeship(button)的形式添加嵌套按钮,同时按钮声明时的目标 surface 应指向被嵌套对象的 surface,通常是item.surface

在页面中嵌套页面

原理基本同上。

import sys
import pygame
import GTC_Pygame_Runtime_Support as gPRS

screen = pygame.display.set_mode((500, 500))
bp = gPRS.page.PlainPage([300, 300], [300, 1000], [100, 100], screen, 1.4, True)

if __name__ == '__main__':
    clock = pygame.time.Clock()
    bp.surface.fill((255, 255, 255))
    for i in range(100):
        pygame.draw.line(bp.surface, [0, 0, 0], [0, 10 * i], [300, 10 * i])
    bp.set_as_background()
    inner_p = gPRS.page.PlainPage([100, 100], [100, 300], [50, 200], bp.surface, wheel_support=True)
    inner_p.surface.fill((255, 255, 255))
    for i in range(100):
        pygame.draw.line(inner_p.surface, [0, 0, 255], [0, 9 * i], [300, 9 * i])
    inner_p.set_as_background()
    bp.add_page_trusteeship(inner_p)

    mw = [False, False]
    while True:
        mw = [False, False]
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 4:
                    mw[1] = True
                elif event.button == 5:
                    mw[0] = True
        bp.operate(pygame.mouse.get_pos(), pygame.mouse.get_pressed(3), mw, True)
        pygame.display.flip()
        clock.tick(60)

通过以上介绍,您应该能对 PRS 的使用特点有大概的了解。

想要获取完整文档?点击前往语雀文档

Keywords

software pygame button input page drag popup

FAQs

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.