【问题标题】:How to add a wordbank region to pygame如何在 pygame 中添加词库区域
【发布时间】:2017-08-09 13:43:08
【问题描述】:

我需要在我的实习期间为这个客户(没有任何编程背景的人)添加一个词库到这个 pygame。下面显示的 pygame 是我所拥有的框架(显然无法向 Internet 显示内部代码),我需要一个函数,它需要 2 个文本对象并将它们以垂直方式放置在单独的区域中,就像历史测试中的 wordbank或者其他的东西。

如何使用 pygame 库将此 GUI 类型元素实现到此代码中?

基本上下面的这个游戏运行填充两个文本框对象,以便用户在屏幕上拖动。我想要一个指定的(外部或内部)区域来放置这些单词,以便在游戏加载时这些单词在窗口一侧的列中被排序。

import sys
import pygame as pg


pg.init()
FONT = pg.font.Font(None, 42)


class Text(pg.sprite.Sprite):

    def __init__(self, text, pos, color, *groups):
        super().__init__(*groups)
        self.image = FONT.render(text, True, color)
        self.rect = self.image.get_rect(center=pos)


def main():
    screen = pg.display.set_mode((640, 480))
    clock = pg.time.Clock()
    all_sprites = pg.sprite.Group(
        Text('hello', (100, 300), pg.Color('steelblue1')),
        Text('world', (250, 300), pg.Color('sienna1')),
        )

    selected = None
    done = False

    while not done:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                done = True
            elif event.type == pg.MOUSEBUTTONDOWN:
                for sprite in all_sprites:
                    if sprite.rect.collidepoint(event.pos):
                        selected = sprite
                        break
            elif event.type == pg.MOUSEBUTTONUP:
                selected = None
            elif event.type == pg.MOUSEMOTION:
                if selected:
                    selected.rect.center = event.pos

        all_sprites.update()
        screen.fill((30, 30, 30))
        all_sprites.draw(screen)

        pg.display.flip()
        clock.tick(30)


if __name__ == '__main__':
    main()
    pg.quit()
    sys.exit()

【问题讨论】:

  • 您的“我需要一个函数,它将两个文本对象以垂直方式放置在单独的区域中”太模糊了。请edit您的问题并更详细地解释它与您问题中的代码当前的作用有何关系。
  • 编辑有帮助吗?
  • 是的,这样更好。 pygame 本身不做多个窗口。可以使用多处理并创建一个子任务,该子任务也使用它并运行它自己的主显示循环,但是它和主进程需要使用IPC 进行通信,这可能会变得复杂。在单个pygame 程序中,您可以使单词列成为一组精灵,这将允许您执行一些操作,例如集体移动它们。用户/程序与他们的交互究竟会是什么?
  • 交互涉及用户将单词对象放置在 4 个子类别中,分为 4 个象限。想象一下窗口被平均分为 4 个正方形。我希望这些单词具有某种类似于 GUI 的功能,当它们运行游戏时,它会加载到旁边的单词列表中,但是您说单独的窗口不起作用,所以嗯...
  • 编辑背景图像以获得更宽的分辨率并填充该区域中的单词可能会更简单。我想下一个问题是如何在屏幕上对列列表中的所有单词进行 blit?

标签: python user-interface object pygame python-3.6


【解决方案1】:

这是一个演示我在评论中提到的示例。我只是使用area rect 来定义一个区域,您可以在其中删除文本对象并将它们附加到text_objects 列表中。如果我选择其中一个对象,我只需将它们从列表中删除。

import sys
import pygame as pg


pg.init()
FONT = pg.font.Font(None, 42)


class Text(pg.sprite.Sprite):

    def __init__(self, text, pos, color, *groups):
        super().__init__(*groups)
        self.image = FONT.render(text, True, color)
        self.rect = self.image.get_rect(center=pos)


def main():
    screen = pg.display.set_mode((640, 480))
    clock = pg.time.Clock()
    all_sprites = pg.sprite.Group(
        Text('hello', (300, 300), pg.Color('steelblue1')),
        Text('world', (450, 300), pg.Color('sienna1')),
        )

    area = pg.Rect(100, 70, 100, 300)
    text_objects = []
    selected = None
    done = False

    while not done:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                done = True
            elif event.type == pg.MOUSEBUTTONDOWN:
                for sprite in all_sprites:
                    if sprite.rect.collidepoint(event.pos):
                        selected = sprite
                        if selected in text_objects:
                            text_objects.remove(selected)
                        break
            elif event.type == pg.MOUSEBUTTONUP:
                if selected and selected.rect.colliderect(area):
                    selected.rect.centerx = area.centerx
                    text_objects.append(selected)
                selected = None
            elif event.type == pg.MOUSEMOTION:
                if selected:
                    selected.rect.center = event.pos

        all_sprites.update()

        screen.fill((30, 30, 30))
        pg.draw.rect(screen, (190, 120, 20), area, 2)
        for y, sprite in enumerate(text_objects):
            sprite.rect.y = y * sprite.rect.h + area.top
        all_sprites.draw(screen)

        pg.display.flip()
        clock.tick(30)


if __name__ == '__main__':
    main()
    pg.quit()
    sys.exit()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-07
    • 1970-01-01
    • 2018-01-02
    • 2011-08-21
    • 2011-12-07
    • 1970-01-01
    • 1970-01-01
    • 2012-12-20
    相关资源
    最近更新 更多