Hardware Reference
In-Depth Information
Listing 4-3
continued
if event.key == pygame.K_m :
mirror = not mirror
if event.key == pygame.K_0 :
anymore = True
fName = “news.txt”
if event.key == pygame.K_1 :
anymore = True
fName = “news1.txt”
if event.key == pygame.K_2 :
anymore = True
fName = “news2.txt”
if event.key == pygame.K_3 :
anymore = True
fName = “news3.txt”
if __name__ == '__main__':
main()
Looking at the last thing irst, you will see that the
checkForEvents()
function has been
extended to react to the spacebar, which toggles a variable for pausing the scrolling, and the
M
key, which toggles the mirror display. he numeric keys set a variable called
anymore
to true and
a variable
fName
to one of four ilenames. You can see how this can easily be extended to cover all
the numbers, allowing some degree of keyboard control over what news ile is displayed.
In the
main()
function the updating of the screen and increment of the ofset variable are
now dependent on the value of the
pause
variable, which is controlled by the spacebar. he
whole function is not an ininite loop; when a ile has inished being output the code is held
in a
while
loop until the
anymore
variable is set to true with perhaps a change in ilename
as controlled by the number keys. he
getNews()
function now zeros the count of the
number of lines and the list of lines themselves. he rest of the function is unchanged.
he
setWords()
function has the printing removed, which makes it much faster. he
drawWords()
function will render the text either normally or lipped around according to
the value of the
mirror
variable. Note this will only be seen to take efect when the screen is
actively scrolling; if it is paused or the end of the text has been reached, you will not see the
reverse efect until it starts again.
hat is about it for the code.
When running Listing 4-3 it is best to have the desktop background set to black and all the
other windows minimised to reduce stray light.