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 5

image

Guessing Games with Random Numbers and Loops

The last step before we start with graphics is learning how to loop a section of code. Most games loop. They repeat the same code over and over. For example, the number guessing game below loops for each guess that the user makes:

Hi! I'm thinking of a random number between 1 and 100.--- Attempt 1Guess what number I am thinking of: 50Too high.--- Attempt 2Guess what number I am thinking of: 25Too high.--- Attempt 3Guess what number I am thinking of: 17Too high.--- Attempt 4Guess what number I am thinking of: 9Too low.--- Attempt 5Guess what number I am thinking of: 14Too high.--- Attempt ...

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