6 changed files with 145 additions and 1 deletions
			
			
		| @ -1,3 +1,10 @@ | |||
| # PyScroller | |||
| 
 | |||
| A simple pygame-based text scroller. | |||
| A simple pygame-based text scroller. | |||
| 
 | |||
| # Attribution | |||
| 
 | |||
| This code uses the tileset | |||
| ["16x16 Studded Metal Fonts"](https://opengameart.org/content/16x16-studded-metal-fonts) | |||
| by [Jerom](https://opengameart.org/users/jerom), used under | |||
| [CC-BY 3.0](https://creativecommons.org/licenses/by/3.0/). | |||
|  | |||
| After Width: | Height: | Size: 12 KiB | 
| @ -0,0 +1,17 @@ | |||
| from pygame import image | |||
| 
 | |||
| 
 | |||
| class CharSheet: | |||
|     def __init__(self, filename, characters, char_width, char_height): | |||
|         self.sheet = image.load(filename) | |||
|         self.characters = characters | |||
|         self.char_width = char_width | |||
|         self.char_height = char_height | |||
| 
 | |||
|     def get_sheet(self, character): | |||
|         index = self.characters.index(character.char) | |||
| 
 | |||
|         y = index // self.char_height | |||
|         x = index - (self.char_height * y) | |||
| 
 | |||
|         return [x, y] | |||
| @ -0,0 +1,16 @@ | |||
| class Character: | |||
|     def __init__(self, | |||
|                  x=0, | |||
|                  y=0, | |||
|                  x_speed=0.1, | |||
|                  y_speed=0.1, | |||
|                  angle=0, | |||
|                  radius=20, | |||
|                  char=' '): | |||
|         self.x = x | |||
|         self.y = y | |||
|         self.x_speed = x_speed | |||
|         self.y_speed = y_speed | |||
|         self.angle = angle | |||
|         self.radius = radius | |||
|         self.char = char | |||
| @ -0,0 +1,31 @@ | |||
| from . import character | |||
| 
 | |||
| 
 | |||
| class Scroller: | |||
|     def __init__(self, text): | |||
|         self.text = text | |||
|         self.next_char = 0 | |||
|         self.sheet_chars = [] | |||
| 
 | |||
|     def get_next_char(self, | |||
|                       x=0, | |||
|                       y=0, | |||
|                       x_speed=0.1, | |||
|                       y_speed=0.1, | |||
|                       angle=0, | |||
|                       radius=20): | |||
|         self.sheet_chars.append(character.Character(x, | |||
|                                                     y, | |||
|                                                     x_speed, | |||
|                                                     y_speed, | |||
|                                                     angle, | |||
|                                                     radius, | |||
|                                                     self.text[self.next_char])) | |||
| 
 | |||
|         if (self.next_char < (len(self.text) - 1)): | |||
|             self.next_char += 1 | |||
|         else: | |||
|             self.next_char = 0 | |||
| 
 | |||
|     def delete_first_char(self): | |||
|         self.sheet_chars.pop(0) | |||
| @ -0,0 +1,73 @@ | |||
| import pygame | |||
| import math | |||
| from scroller import scroller | |||
| from scroller import char_sheet | |||
| 
 | |||
| pygame.init() | |||
| 
 | |||
| size = width, height = 320, 240 | |||
| 
 | |||
| black = 0, 0, 0 | |||
| red = 255, 0, 0 | |||
| characters = [' ', '!', '\"', '#', '$', '%',  '&', '\'', '(', ')', '*', '+', | |||
|               ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', | |||
|               '9', ':', ';', '<', '=', '>', '?', '@', 'A', 'B', 'C', 'D', 'E', | |||
|               'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', | |||
|               'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^', '_', | |||
|               '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', | |||
|               'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', | |||
|               'z', '{', '|', '}', '~', '©'] | |||
| 
 | |||
| char_sheet = char_sheet.CharSheet("jeromBD-metal-CCBY3.png", | |||
|                                   characters, 16, 16) | |||
| 
 | |||
| x = 320 | |||
| y = 120 | |||
| 
 | |||
| clock = pygame.time.Clock() | |||
| 
 | |||
| scroller = scroller.Scroller("IT'S BEEN... one week since you looked at me, " + | |||
|                              "cocked your head to the side and said \"I'm " + | |||
|                              "angry\"                         ") | |||
| 
 | |||
| screen = pygame.display.set_mode(size) | |||
| 
 | |||
| while 1: | |||
|     dt = clock.tick(60) | |||
| 
 | |||
|     screen.fill(black) | |||
| 
 | |||
|     i = 0 | |||
| 
 | |||
|     if not scroller.sheet_chars: | |||
|         scroller.get_next_char(320, 120, 0.1, 2, 0, 20) | |||
| 
 | |||
|     for sheet_char in scroller.sheet_chars: | |||
|         sheet_char.x -= sheet_char.x_speed * dt | |||
| 
 | |||
|         sheet_char.angle += sheet_char.y_speed | |||
| 
 | |||
|         sheet_char.y = ((sheet_char.radius * | |||
|                          math.sin(sheet_char.angle * math.pi / 180)) + | |||
|                         120) | |||
| 
 | |||
|         if (sheet_char.x < -(char_sheet.char_width)): | |||
|             scroller.delete_first_char() | |||
|             i -= 1 | |||
|         else: | |||
|             if (i == (len(scroller.sheet_chars) - 1) and | |||
|                sheet_char.x <= (width - char_sheet.char_width)): | |||
|                 scroller.get_next_char(320, 120, 0.1, 2, 0, 20) | |||
|                 print(str(len(scroller.sheet_chars) - 1) + " | " + str(i) + | |||
|                       " | " + str(scroller.next_char)) | |||
| 
 | |||
|             i += 1 | |||
| 
 | |||
|         sect = char_sheet.get_sheet(sheet_char) | |||
|         sprite = pygame.Rect((sect[0] * 16), | |||
|                              (sect[1] * 16), 16, 16) | |||
| 
 | |||
|         screen.blit(char_sheet.sheet, (sheet_char.x, | |||
|                     sheet_char.y), sprite) | |||
| 
 | |||
|     pygame.display.flip() | |||
					Loading…
					
					
				
		Reference in new issue