Hardware Reference
In-Depth Information
Although Listing 4-1 is only a partial implementation of what you need, it is a fully working
program, so let's look more closely at what it does and how it does it. he code starts by load-
ing in modules you need and then initialising the Pygame's window, which needs to be large
and is going to be where the action takes place. he size is determined by variables, and
although the raw numbers could be typed into the commands where needed, this provides a
one stop shop for any changes in size you might want to make. he variable segments
determines how many lines to have on the display; as a lot of the time there will be lines
clipped, this variable is one less than the total number of surfaces you need to use. he
textSurface list is deined initially as just some blank areas of memory. hen the numbers
determining the colours of the text and the background are deined, which for now are white
text on a black background.
Moving on to the functions terminate() and checkForEvent() - these are what I call
housekeeping functions . hey provide an easy way to quit an application using the window's
close icon or by pressing Esc. Later these functions will be developed to control the applica-
tion. he simplest function is setWords() , which simply renders a line of words in a speci-
ied text surface. Again, later this will be greatly expanded as you cope with real text.
he drawWords() function takes in an index or number to identify what text surface to use,
and an ofset. It then gets the rectangle associated with that surface and centers it horizon-
tally, and blits it to the screen memory in a position that depends on the index and the ofset
values. his function is used exclusively by the drawScreen() function that places each
text segment, or line, on the screen. his is controlled by the segment variable, which is
constricted to the values between zero and the maximum number of text surfaces you have;
we say this number is “wrapped around”. Note here that segment and segments are two
diferent variables; segments deines how many you have, and segment deines the irst
one to use.
he main() function simply writes ive lines of text to each segment and then scrolls a line's
worth by changing the ofset each time. he for loop that does this has an increment of 4 to
give it some decent scroll speed; change that number, and you change the speed of the scroll.
I found that this loop didn't need slowing down, so the sleep time was commented out. he
result is that this program will scroll forever with the line numbers getting larger. he smaller
the ofset increment is, the smoother the scroll, but it is also slower. With a large value here,
the scroll is a lot faster, but it's more jerky. Fortunately, the Raspberry Pi is just about fast
enough for your purposes here.
A Step Closer to a Usable Program
So now it is time to get a lot closer to what you want to do. First of, you want to make the
scrolling a lot more eicient by not rendering all the text every time you want to scroll a
Search WWH ::




Custom Search