Skip to main content
 首页 » 编程设计

python之在pygame中滚动文本

2025年05月04日16qq号

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

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

谢谢,

戴夫

代码(特定 block 在注释“在屏幕底部添加滚动文本”下):

# 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 中看不到任何可以为您处理这一切的东西。您可以将 livewires.Object 与一些自定义代码一起使用。像 livewires.game.Text 那样构建您的表面,然后移动它。或者甚至使其可勾选并跟踪应显示文本的哪一部分,并将该部分 blit 仅传输到对象表面。