O'Reilly logo

Program Arcade Games: With Python and Pygame, Fourth Edition by Paul Vincent Craven

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required