Game Development Reference
In-Depth Information
12 - Cartesian Coordinates
black knight is at position -2. To find out the distance between them, you would find the
difference by subtracting their positions and taking the absolute value of that number.
It works no matter what the order of the numbers is. -2 - 4 (that is, negative two minus
four) is -6, and the absolute value of -6 is 6. However, 4 - -2 (that is, four minus negative
two) is 6, and the absolute value of 6 is 6. Using the absolute value of the difference is a
good way of finding the distance between two points on a number line (or axis).
The abs() function can be used to return the absolute value of an integer. The abs()
function is a built-in function, so you do not need to import any modules to use it. Pass it an
integer or float value and it will return the absolute value:
>>> abs(-5)
5
>>> abs(42)
42
>>> abs(-10.5)
10.5
Coordinate System of a Computer Monitor
It is common that computer
monitors use a coordinate system
that has the origin (0, 0) at the top
left corner of the screen, which
increases going down and to the
right. There are no negative
coordinates. This is because text
is printed starting at the top left,
and is printed going to the right
and downwards. Most computer
graphics use this coordinate
system, and we will use it in our
games. Also it is common to
assume that monitors can display
80 text characters per row and 25
text characters per column (look
at Figure 12-12). This used to be
the maximum screen size that
monitors could support. While
today's monitors can usually display much more text, we will not assume that the user's
screen is bigger than 80 by 25.
Figure 12-12: The Cartesian coordinate system on a computer monitor.
Search WWH ::




Custom Search