A simple pygame-based text scroller.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
4.2 KiB

import pygame
import math
from scroller import scroller
from scroller import char_sheet
pygame.init()
# For this example, I just wanted to work with a small 320x240 space.
# Most of the old scrollers didn't have a lot of pixels to play with, after
# all.
size = width, height = 320, 240
black = 0, 0, 0
red = 255, 0, 0
# A character list to match up the available ASCII characters with the
# spritesheet. You could probably use ints instead. I used chars because I
# forgot Python treat chars like strings. x_X
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', '{', '|', '}', '~', '©']
# A CharSheet object holds the spritesheet and the character list, as well as
# the dimensions of each sprite.
char_sheet = char_sheet.CharSheet("jeromBD-metal-CCBY3.png",
characters, 16, 16)
# Placeholder co-ordinates just to centre the scroller while I built this.
x = 320
y = 120
# We need a clock to restrain the framerate and not simply have the scroller
# zipping across before the view even realises the program has started.
clock = pygame.time.Clock()
# Creates a new Scroller object, with the text we want to put into it.
# Some space added to create a gap between repetitions.
scroller = scroller.Scroller("IT'S BEEN... one week since you looked at me, " +
"cocked your head to the side and said \"I'm " +
"angry\" ")
# Opens a pygame window to the previously-specified dimentions.
screen = pygame.display.set_mode(size)
while 1:
# Obtain a time delta that will be used to determine the full extent of the
# scroller's movement.
dt = clock.tick(60)
# Blank the screen first. Don't want any trails (At least not in this case.
screen.fill(black)
# Because for loops can't count themselves.
i = 0
# If a character is not present on the scroller, create one.
# Else an exception will happen.
if not scroller.sheet_chars:
scroller.get_next_char(320, 120, 0.1, 2, 0, 20)
for sheet_char in scroller.sheet_chars:
# Move each character to the left.
sheet_char.x -= sheet_char.x_speed * dt
# Move the character up and down according to a sine wave.
sheet_char.angle += sheet_char.y_speed
sheet_char.y = ((sheet_char.radius *
math.sin(sheet_char.angle * math.pi / 180)) +
120)
# Delete the character from memory if has left the visible screen.
# i needs to be decreased too, as it has effectively stepped back one.
if (sheet_char.x < -(char_sheet.char_width)):
scroller.delete_first_char()
i -= 1
else:
# If the newest character has fully entered the visible screen,
# a new one can be introduced.
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)
# This was just some debug stuff to figure out an issue.
print(str(len(scroller.sheet_chars) - 1) + " | " + str(i) +
" | " + str(scroller.next_char))
# All done, onto the next character, if there is one.
i += 1
# Create a rect that will serve as a bounding box into which the
# character sprite will be drawn.
sect = char_sheet.get_sheet(sheet_char)
sprite = pygame.Rect((sect[0] * 16),
(sect[1] * 16), 16, 16)
# Draw the character to the screen.
screen.blit(char_sheet.sheet, (sheet_char.x,
sheet_char.y), sprite)
# Bring the finished frame to the foreground.
pygame.display.flip()