-->

Vertically Scrolling Text on Python

What’s not to like about vertically scrolling text? Its uses are many: the beginning of a game or introduction to something epic, like the beginning of every Star Wars movie; a list of credits at the end of something, such as a Python presentation. The list goes on.


Vertically Scrolling Text on Python
Vertically Scrolling Text on Python


Vertically Scrolling Text on Python



EPICSCROLL.PY

We’ve used the poem Cimmeria by Robert E. Howard for the code’s scrolling text, along with a dramatic black background and red text. We think you’ll agree, it’s quite epic.

  1. import pygame as pg
  2. from pygame.locals import *
  3.  
  4. pg.init()
  5.  
  6. text _ list = ‘’’
  7.  
  8. I remember
  9. The dark woods, masking slopes of sombre hills;
  10. The grey clouds’ leaden everlasting arch;
  11. The dusky streams that flowed without a sound,
  12. And the lone winds that whispered down the passes.
  13.  
  14. Vista on vista marching, hills on hills,
  15. Slope beyond slope, each dark with sullen trees,
  16. Our gaunt land lay. So when a man climbed up
  17. A rugged peak and gazed, his shaded eye
  18. Saw but the endless vista – hill on hill,
  19. Slope beyond slope, each hooded like its brothers.
  20.  
  21. It was a gloomy land that seemed to hold
  22. All winds and clouds and dreams that shun the sun,
  23. With bare boughs rattling in the lonesome winds,
  24. And the dark woodlands brooding over all,
  25. Not even lightened by the rare dim sun
  26. Which made squat shadows out of men; they called it
  27. Cimmeria, land of Darkness and deep Night.
  28.  
  29. It was so long ago and far away
  30. I have forgot the very name men called me.
  31. The axe and flint-tipped spear are like a dream,
  32. And hunts and wars are shadows. I recall
  33. Only the stillness of that sombre land;
  34. The clouds that piled forever on the hills,
  35. The dimness of the everlasting woods.
  36.  
  37. Cimmeria, land of Darkness and the Night.
  38. Oh, soul of mine, born out of shadowed hills,
  39. To clouds and winds and ghosts that shun the sun,
  40. How many deaths shall serve to break at last
  41. This heritage which wraps me in the grey
  42. Apparel of ghosts? I search my heart and find
  43. Cimmeria, land of Darkness and the Night!
  44.  
  45. ‘’’.split(‘\n’)
  46.  
  47.  
  48.  
  49. class Credits:
  50.     def _ _ init _ _ (self, screen _ rect, lst):
  51.         self.srect = screen _ rect
  52.         self.lst = lst
  53.         self.size = 16
  54.         self.color = (255,0,0)
  55.         self.buff _ centery = self.srect.height/2 + 5
  56.         self.buff _ lines = 50
  57.         self.timer = 0.0
  58.         self.delay = 0
  59.         self.make _ surfaces()
  60.     def make _ text(self,message):
  61.         font = pg.font.SysFont(‘Arial’, self.size)
  62.         text = font.render(message,True,self.color)
  63.         rect = text.get _ rect(center = (self.srect.
  64.         centerx, self.srect.centery + self.buff _ centery) )
  65.         return text,rect
  66.     def make _ surfaces(self):
  67.         self.text = []
  68.         for i, line in enumerate(self.lst):
  69.             l = self.make _ text(line)
  70.             l[1].y += i*self.buff _ lines
  71.             self.text.append(l)
  72.     def update(self):
  73.         if pg.time.get _ ticks()-self.timer > self.delay:
  74.             self.timer = pg.time.get _ ticks()
  75.             for text, rect in self.text:
  76.                 rect.y -= 1
  77.     def render(self, surf):
  78.         for text, rect in self.text:
  79.             surf.blit(text, rect)
  80. screen = pg.display.set _ mode((800,600))
  81. screen _ rect = screen.get _ rect()
  82. clock = pg.time.Clock()
  83. running=True
  84. cred = Credits(screen _ rect, text _ list)
  85.  
  86. while running:
  87.     for event in pg.event.get():
  88.         if event.type == QUIT:
  89.             running = False
  90.     screen.fill((0,0,0))
  91.     cred.update()
  92.     cred.render(screen)
  93.     pg.display.update()
  94.     clock.tick(60)

A Long Time Ago…

The obvious main point of enhancement is the actual text itself. Replace it with a list of credits, or an equally epic opening storyline to your Python game, and it will certainly hit the mark with whoever plays it. Don’t forget to change the screen resolution if needed; we’re currently running it at 800 x 600.








0 Response to "Vertically Scrolling Text on Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel