pygame 纵向滚动页面——来自 GTC Pygame Runtime Support 的解决方案

没有安装 GTC PRS 的小伙伴可以通过下面的命令安装:

pip install GTC-Pygame-Runtime-Support

PlainPage 可以使用鼠标拖动或者鼠标滚轮滚动,并伴随有较为平滑的动画。

PlainPage 的灵感来源于游戏 Metalstorm 的电脑版(但我已经很久没玩了),并且是 PRS 中开发时间最长的组件(整整花费了作者一节信息技术课加上数个课间!)。

为解决页面内显示页面及按钮的需求,作者创造(捏造)了托管 (trusteeship) 方法。下面就会进行展示。

用法示例

纯页面

下面是快速开始中的滚动页面示例:

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)

在主循环前或循环中,你可以通过更改item.surface(直接在上面乱涂乱画)的方式

定制你想要的背景,并在更改完毕后执行函数item.set_as_background()

嵌套方式

通过调用页面对象的add_button_trusteeship(button)add_page_trusteeship(page)add_surface_trusteeship(surface)add_input_trusteeship(input_box)便可以分别使页面内嵌套按钮、页面、表面或输入框,且可将add更换为show以获取相应的被托管对象列表。下面以页面嵌套页面为例展示:

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()
    # 被嵌套页面声明,已将目标 surface 指向主页面
    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)

需要嵌套对象的目标 surface 必须指向主页面,同时将对象添加到页面的托管列表。注意,需要嵌套的对象无需在主循环内再次执行operate()函数,以免程序抽搐。除了operate(),需要嵌套的元素需要在主循环内执行的所有操作(如输入框的handle()操作)都需要在主循环内执行。

函数原型

class PlainPage(BasicPage):
    def __init__(self, show_size, real_size, pos, screen=None, acc=1.4, wheel_support=True, grounding=(220, 220, 220), drag_index=0):
        ...
    def set_as_background(self):
        ...
    def change_blit_pos(self, pos):
        ...
    def _conflict_check(self, mouse_pos) -> bool:
        ...
    def in_area(self, mouse_pos):
        ...
    def _reverse(self):
        ...
    def operate(self, mouse_pos, mouse_press, mouse_wheel_status=None, operate_addons=False):
        ...

class BasicPage(object):
    def __init__(self):
        ...
    def add_button_trusteeship(self, button: BasicButton):
        ...
    def show_button_trusteeship(self):
        ...
    def add_surface_trusteeship(self, surface: BasicSurface):
        ...
    def show_surface_trusteeship(self):
        ...
    def add_page_trusteeship(self, page):
        ...
    def show_page_trusteeship(self):
        ...
    def add_input_trusteeship(self, input_box):
        ...
    def show_input_trusteeship(self):
        ...

参数

生成函数

  • show_size:显示的页面大小,由一个二元整型列表或元组传入。
  • real_size:真实的页面大小,由一个二元整型列表或元组传入。
  • pos:页面在目标 surface 的位置,由一个二元整型列表或元组传入。
  • screen:目标 surface,由一个 surface 对象传入。
  • acc:动画的加速度,由一个整型变量传入,此处的加速度值为反比例函数的k值。
  • wheel_support:是否支持滚轮,由一个布尔型变量传入,默认为 True(0.0.3+)或 False(0.0.2-)
  • grounding:过度滚动页面时显示的虚空部分底色,由一个三元整型列表或元组传入。
  • drag_index:拖动页面的鼠标键索引,由一个二元整型列表或元组传入,表示生效的鼠标键在传入的鼠标状态列表中的索引。

change_blit_pos函数

  • pos:更改后的页面位置,由一个二元整型列表或元组传入。

in_area函数

  • mouse_pos:鼠标位置,由一个二元整型列表或元组传入。
  • 返回值:如果在页面显示区域内则返回True,否则返回False

operate函数

  • mouse_pos:鼠标位置,由一个二元整型列表或元组传入。
  • mouse_press:鼠标状态列表,由一个三元或五元数组或列表传入,通常由pygame.mouse.get_pressed(3)pygame.mouse.get_pressed(5)生成。
  • mouse_wheel_status:鼠标滚轮状态,由一个二元布尔型列表或元组传入,也可为 None。为获取该状态,用户可如示例中创建一个数组并在事件循环中处理以获取鼠标滚轮状态。
  • operate_addons:是否操作被托管的组件,布尔型,默认为False

添加托管函数

  • 第一个参数:要被添加的对象。

状态获取

  • pos_y(0.0.4+):拖动前的页面相对初始位置在纵向的偏移量。
  • delta(0.0.4+):拖动中的页面纵向偏移量。

此软件包由 GTC Software Studio 编写,现托管于 Github,并有中文版和英文版,并可以在 python 软件包索引源找到此软件包。

本软件包于 2025年2月12日上传至 pypi.org,各个镜像源将在下次同步时同步此软件包。

0.0.4 版本计划于2025年2月14日上传 pypi.org,敬请期待。

欢迎各位小伙伴 star 我的仓库!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值