2015-11-18 89 views
2

如何在pygame中實現滾動文本?由於Text類是從Sprite派生的,所以我認爲我可以用通常的方式將它包裝成一個Sprite對象。相反,它只是從屏幕的左邊緣消失,永遠不會返回(我無法讓它從兩側反彈)。在pygame中滾動文本

我的這個計劃的目標是加強我剛剛在一些教育材料中提到的內容。但是,滾動文本不是其中之一(儘管看起來很酷)。

感謝,

戴夫

代碼(特定塊是在評論「添加滾動畫面底部的文字」):

# 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() 
+0

您使用的是什麼版本的'livewires'?最新的(2.1)與您的代碼不匹配。 – cmd

回答

1

我看不到任何會處理它全部爲你在livewires。您可以使用livewires.Object以及一些自定義代碼。像livewires.game.Text那樣建立你的表面,然後移動它。甚至可以讓它成爲可打印的,並且跟蹤文本的哪一部分應該顯示,並且僅僅是對象表面的那部分。

+0

謝謝你:) –