Hardware Reference
In-Depth Information
if event.key == pygame.K_ESCAPE :
terminate()
if event.key == pygame.K_DOWN :
scrollSize -= 1
if scrollSize == 0:
scrollsize = 1
if event.key == pygame.K_UP :
scrollSize += 1
if __name__ == '__main__':
main()
Most of the initialisation is the same as before, but now there is a new list deined called
newsLines , which is set to be initially just an empty list. he getNews() function opens up
the news.txt ile and copies each line into the newsLines list before closing the ile. As it
does this it counts the number of lines in the global variable called, appropriately enough,
numberOfLines .
he main() function is much as before, but it performs the scroll copying described earlier.
Also, the for loop that controlled the ofset has been replaced by a while loop, which is to
allow the ofset increment scrollSize to be altered on the ly by using the up and down
arrow keys on the keyboard. his is done with a few extra lines in the checkForEvents()
function. he main function is run only as long as there are lines to read from the text ile.
When it is inished there is a three-second delay, and the program shuts down.
What has changed substantially is the setWords() function, which previously consisted of
just a single line. Now it is by far the longest function in the program. It starts of by deining
some variables and using the split attribute to decompose the current line of news into a
list of words called, with staggering imagination, words . It is this list that is used to build up,
one word at a time, a trial line. his is done with the while loop deined by the line
while wordsWidth < screenWidth - margin and ;
wordsAvailable >= wordsToUse + 1:
his keeps on adding words to a string and measuring the width of the resulting text surface.
At each stage the line built up so far and its width is printed out so that you can see the prog-
ress. his while loop also keeps tabs on if the words list has actually run out of words. his
loop can inish when you have run out of words or when the width is too wide by one word.
When this loops exits the next line,
if wordsWidth > screenWidth - margin : # for the end of a line
Search WWH ::




Custom Search