Game Development Reference
In-Depth Information
80), 1)
43.
44. # draw the text's background rectangle onto the surface
45. pygame.draw.rect(windowSurface, RED, (textRect.left - 20,
textRect.top - 20, textRect.width + 40, textRect.height +
40))
46.
47. # get a pixel array of the surface
48. pixArray = pygame.PixelArray(windowSurface)
49. pixArray[480][380] = BLACK
50. del pixArray
51.
52. # draw the text onto the surface
53. windowSurface.blit(text, textRect)
54.
55. # draw the window onto the screen
56. pygame.display.update()
57.
58. # run the game loop
59. while True:
60. for event in pygame.event.get():
61. if event.type == QUIT:
62. pygame.quit()
63. sys.exit()
Running the Hello World Program
When you run this program, you should see a new GUI window appear which looks like
Figure 17-2.
What is nice about using a GUI instead of a console is that the text can appear anywhere
in the window, not just after the previous text we have printed. The text can be any color or
size.
One thing you may notice is that Pygame uses a lot of tuples instead of lists. Remember
that tuples are almost the same as lists (they can contain other values) except they are typed
with parentheses ( and ) , instead of square brackets [ and ] . The main difference is that
once you create a tuple, you cannot change, add, or remove any values in the tuple. For
technical reasons, knowing that the contents of the tuple never change allows Python to
handle this data more efficiently, which is why Pygame uses tuples instead of lists.
Search WWH ::




Custom Search