【问题标题】:Scrolling text in pygame在pygame中滚动文本
【发布时间】:2016-02-20 09:47:40
【问题描述】:

我将如何在 pygame 中实现滚动文本?由于 Text 类是从 Sprite 派生的,我想我可以像使用 Sprite 对象一样将它包装起来。相反,它只是从屏幕的左边缘消失,永远不会返回(我也无法让它从每一侧反弹)。

我参加此计划的目的是加强我刚刚在一些教育材料中介绍的内容。然而,滚动文本不是其中之一(虽然看起来很酷)。

谢谢,

戴夫

代码(具体块在注释“在屏幕底部添加滚动文本”下):

# Simon Says
# Displays sequences of sounds and/or colors which the user must repeat

from livewires import games, color
import random

# initialise screen
games.init(screen_width = 640, screen_height = 480, fps = 50)

class Game(object):
    """A sequence repeat game"""
    def __init__(self):
        """Initialize Game object"""

        # create key
        self.menu_title = games.Text(value = "Key",
                          size = 25,
                          color  = color.red,
                          top = 5,
                          left = games.screen.width - 100,
                          is_collideable = False)
        self.menu_choice0 = games.Text(value = "0 - Quit",
                          size = 20,
                          color  = color.red,
                          top = self.menu_title.bottom + 5,
                          left = games.screen.width - 120,
                          is_collideable = False)
        self.menu_choice1 = games.Text(value = "1 - Yellow",
                          size = 20,
                          color  = color.red,
                          top = self.menu_choice0.bottom + 5,
                          left = games.screen.width - 120,
                          is_collideable = False)
        self.menu_choice2 = games.Text(value = "2 -Thrust sound",
                          size = 20,
                          color  = color.red,
                          top = self.menu_choice1.bottom + 5,
                          left = games.screen.width - 120,
                          is_collideable = False)
        games.screen.add(self.menu_title)
        games.screen.add(self.menu_choice0)
        games.screen.add(self.menu_choice1)
        games.screen.add(self.menu_choice2)

        # add scrolling text at bottom of screen
        self.instructions = games.Text(value = "Repeat the sequence by entering the "
                                               "corresponding number.",
                                               size = 20,
                                               color = color.green,
                                               x = games.screen.width/2,
                                               bottom = games.screen.height - 2,
                                               dx = - 0.75)

        games.screen.add(self.instructions)

        # wrap
        if self.instructions.left < 0:
            self.instructions.left = games.screen.width



    def play(self):
        """Play game"""
        # load and set background
        black_background = games.load_image("blackbackground.jpg", transparent = False)
        games.screen.background = black_background
        games.screen.mainloop()

def main():
    sequence = Game()
    sequence.play()

main()

【问题讨论】:

  • 您使用的是哪个版本的livewires?最新的 (2.1) 与您的代码不匹配。

标签: python pygame sprite livewires


【解决方案1】:

我在livewires 中看不到任何可以为您处理这一切的东西。您可能可以将livewires.Object 与一些自定义代码一起使用。然后像livewires.game.Text 那样构建你的表面,然后移动它。或者甚至让它可勾选并跟踪应该显示文本的哪一部分,blit 只是该部分到对象表面。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-14
    相关资源
    最近更新 更多