Hardware Reference
In-Depth Information
Listing 4-3 continued
numberOfLines = 0
newsLines = list()
nfile = open(fName,”r”)
for line in nfile.readlines():
newsLines.append(line)
numberOfLines +=1
nfile.close()
def drawScreen(offset) : # draw to the screen
global segment
screen.blit(background,[0,0]) # set background colour
for index in range(0,segments+1):
segment +=1
if(segment > segments): # wraparound segment number
segment = 0
drawWords(segment,offset)
pygame.display.update()
def setWords(index,segment) :
endOfLine = False
margin = 30 # total gap for the two sides
words = newsLines[index].split() ;
# get an array of words from the line
wordsAvailable = len(words)
wordsToUse = 0
wordsWidth = 0
tryLine = “”
while wordsWidth < screenWidth - margin ;
and wordsAvailable >= wordsToUse + 1:
tryLine = “”
wordsToUse += 1
for test in range(0, wordsToUse):
tryLine = tryLine + words[test] + “ “
textSurface[segment] = font.render(tryLine, ;
True, cText, cBackground )
tryWidth = textSurface[segment].get_rect()
wordsWidth = tryWidth.right
useLine = “”
if wordsWidth > screenWidth - margin : ;
# for the end of a line
wordsToUse -= 1 # use one less word
else :
Search WWH ::




Custom Search