CHAPTER 10

image

Functions

Functions are used for two reasons. First, they make code easier to read and understand. Second, they allow code to be used more than once.

Imagine a set of code that draws a tree as shown below. To do this, the programmer executes the following commands:

pygame.draw.rect(screen, BROWN, [60, 400, 30, 45])pygame.draw.polygon(screen, GREEN, [[150, 400], [75, 250], [0, 400]])pygame.draw.polygon(screen, GREEN, [[140, 350], [75, 230], [10, 350]])

9781484217894_unFig10-01.jpg

Simple Tree

Those three lines of code don’t really pop out as obviously drawing ...

Get Program Arcade Games: With Python and Pygame, Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.