Python 在pygame中滚动文本

Python 在pygame中滚动文本,python,pygame,sprite,livewires,Python,Pygame,Sprite,Livewires,如何在pygame中实现滚动文本?由于Text类是从Sprite派生的,我想我可以用Sprite对象通常的方式来包装它。相反,它只是从屏幕的左边缘消失,再也不会回来(我也不能让它从两边反弹) 我参加这个项目的目的是加强我刚才在一些教育材料中提到的内容。然而,滚动文本不是其中之一(尽管看起来很酷) 谢谢 戴夫 代码(特定块位于注释“在屏幕底部添加滚动文本”下): 西蒙说 #显示用户必须重复的声音和/或颜色序列 从livewires导入游戏、颜色 随机输入 #初始化屏幕 games.init(屏幕宽

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

我参加这个项目的目的是加强我刚才在一些教育材料中提到的内容。然而,滚动文本不是其中之一(尽管看起来很酷)

谢谢

戴夫

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

西蒙说 #显示用户必须重复的声音和/或颜色序列 从livewires导入游戏、颜色 随机输入 #初始化屏幕 games.init(屏幕宽度=640,屏幕高度=480,fps=50) 班级游戏(对象): “”“序列重复游戏”“” 定义初始化(自): “”“初始化游戏对象”“” #创建密钥 self.menu_title=games.Text(value=“Key”, 尺寸=25, 颜色=color.red, top=5, 左=games.screen.width-100, is_Collidable=False) self.menu\u choice0=games.Text(value=“0-退出”, 尺寸=20, 颜色=color.red, 顶部=self.menu\u title.bottom+5, 左=games.screen.width-120, is_Collidable=False) self.menu\u choice1=games.Text(value=“1-黄色”, 尺寸=20, 颜色=color.red, 顶部=自选菜单_选项0.bottom+5, 左=games.screen.width-120, is_Collidable=False) self.menu\u choice2=games.Text(value=“2-推力声音”, 尺寸=20, 颜色=color.red, 顶部=自选菜单\选项1.底部+5, 左=games.screen.width-120, is_Collidable=False) 游戏.屏幕.添加(自我菜单标题) 游戏.屏幕.添加(自我菜单\u选项0) 游戏.屏幕.添加(自我菜单\选择1) 游戏。屏幕。添加(自我菜单选择2) #在屏幕底部添加滚动文本 self.instructions=games.Text(value=“通过输入 “相应号码。”, 尺寸=20, color=color.green, x=games.screen.width/2, 底部=games.screen.height-2, dx=-0.75) 游戏。屏幕。添加(自我说明) #包裹 如果self.instructions.left<0: self.instructions.left=games.screen.width def播放(自我): “玩游戏” #加载并设置背景 黑色背景=游戏。加载图像(“blackbackground.jpg”,透明=假) games.screen.background=黑色背景 games.screen.mainloop() def main(): 顺序=游戏() sequence.play() main()
livewires
中,我看不到任何东西可以为您处理这一切。您可能可以将
livewires.Object
与一些自定义代码一起使用。像
livewires.game.Text那样构建曲面,然后移动曲面。或者甚至让它变得容易记,并跟踪文本的哪一部分应该显示,并将该部分仅显示到对象表面。

您使用的是什么版本的
livewires
?最新版本(2.1)与您的代码不匹配。
# 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()