P. 1
IYOCGwP_book1

IYOCGwP_book1

|Views: 19|Likes:
Published by italoppl
Invente seus próprios jogos com pygme.

obs: Pygame é uma biblioteca para python
Invente seus próprios jogos com pygme.

obs: Pygame é uma biblioteca para python

More info:

Published by: italoppl on Oct 28, 2012
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

12/06/2013

pdf

text

original

Bagels is a fairly simple game to program but can be difficult to win at. But if you keep
playing, you will eventually discover better ways to guess and make use of the clues the
game gives you.

This chapter introduced a few new functions and methods (random.shuffle(), sort
(), and join()), along with a couple handy shortcuts. Using the augmented assignment
operators involve less typing when you want to change a variable's relative value (such as in
spam = spam + 1, which can be shortend to spam += 1). String interpolation can
make your code much more readable by placing %s (called a conversion specifier) inside the
string instead of using many string concatenation operations.

The join() string method is passed a list of strings that will be concatenated together,
with the original associated string in between them. For example, 'X'.join
( ['hello', 'world', 'yay'] ) will evaluate to the string,

198

'helloXworldXyay'.

The sort() list method will rearrange the items in the list to be in alphabetical order.

The append() list method will add a value to the end of the associated list. If spam
contains the list ['a', 'b', 'c'], then calling spam.append('d') will change
the list in spam to be ['a', 'b', 'c', 'd'].

The next chapter is not about programming directly, but will be necessary for the games
we want to create in the later chapters of this book. We will learn about the math concepts
of Cartesian coordinates and negative numbers. These will be used in the Sonar, Reversi,
and Dodger games, but Cartesian coordinates and negative numbers are used in almost all
games (especially graphical games). If you already know about these concepts, give the
next chapter a brief reading anyway just to freshen up. Let's dive in!

199

11 - Bagels

Topics Covered In This Chapter:

Cartesian coordinate systems.
The X-axis and Y-axis.
The Commutative Property of Addition.
Absolute values and the abs() function.

This chapter does not introduce a new game, but instead goes over some simple
mathematical concepts that we will use in the rest of the games in this book.

When you look at 2D games (such as Tetris or old Super Nintendo or Sega Genesis
games) you can see that most of the graphics on the screen can move left or right (the first
dimension) and up or down (the second dimension, hence 2D). In order for us to create
games that have objects moving around two dimensions (such as the two dimensional
computer screen), we need a system that can translate a place on the screen to integers that
our program can deal with.

This is where Cartesian coordinate systems come in. The coordinates can point to a very
specific point on the screen so that our program can keep track of different areas on the
screen.

Negative numbers are often used with Cartesian coordinate systems as well. The second
half of this chapter will explain how we can do math with negative numbers.

You may already know about Cartesian coordinate systems and negative numbers from
math class. In that case, you can just give this chapter a quick read anyway to refresh
yourself.

200

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->