abs() function.
This chapter does not introduce a new game, but instead goes over some simple
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
This is where
You may already know about
(рис 12.1) A sample chessboard with a black knight at a, 4 and a white knight at e, 6.
A problem in many games is how to talk about exact points on the board. A common way of solving this is by marking each
In chess, the knight piece looks like a horse head. The white knight is located at the point e, 6 and the black knight is located at point a, 4. We can also see that every space on row 7 and every space in column c is empty.
A grid with labeled rows and columns like the chess board is a
The numbers going left and right that describe the columns are part of the X-axis. The numbers going up and down that describe the rows are part of the Y-axis. When we describe coordinates, we always say the X-coordinate first, followed by the Y-coordinate. That means the white knight in the above picture is located at the coordinate 5, 6 (and not 6, 5). The black knight is located at the coordinate 1, 4 (not to be confused with 4, 1).
Notice that for the black knight to move to the white knight's position, the black knight must move up two spaces, and then to the right by four spaces. (Or move right four spaces and then move up two spaces.) But we don't need to look at the board to figure this out. If we know the white knight is located at 5, 6 and the black knight is located at 1, 4, then we can just use subtraction to figure out this information.
Subtract the black knight's X-coordinate and white knight's X-coordinate: 5 - 1 = 4. That means the black knight has to move along the X-axis by four spaces.
Subtract the black knight's Y-coordinate and white knight's Y-coordinate: 6 - 4 = 2. That means the black knight has to move along the Y-axis by two spaces.
Another concept that
(рис 12.2) The same chessboard but with numeric coordinates for both rows and columns.
(рис 12.3) A number line.
The number line is really useful for doing subtraction and addition with 4 + 3 can be thought of as the white knight starting at position 4 and moving 3 spaces over to the right (addition means increasing, which is in the right direction).
(рис 12.4) Moving the white knight to the right adds to the coordinate.
As you can see, the white knight ends up at position 7. This makes sense, because 4 + 3 is 7.
Subtraction can be done by moving the white knight to the left. Subtraction means
(рис 12.5) Moving the white knight to the left subtracts from the coordinate.
The white knight ends up at position -2. That means 4 - 6 equals -2.
If we add or subtract a negative number, the white knight would move in the opposite direction. If you add a negative number, the knight moves to the left. If you subtract a negative number, the knight moves to the right. The expression -6 - -4 would be equal to -2. The knight starts at -6 and moves to the right by 4 spaces.
Notice that -6 - -4 has the same answer as -6 + 4.
(рис 12.6) Even if the white knight starts at a negative coordinate, moving right still adds to the coordinate.
(рис 12.7) Putting two number lines together creates a Cartesian coordinate system.
The number line is the same as the X-axis. If we made the number line go up and down instead of left and right, it would model the Y-axis. Adding a
The 0, 0 coordinate has a special name: the origin.
Subtracting
The first is if you are adding a negative number, for example; 4 + -2. The first trick is "a minus eats the plus sign on its left". When you see a 4 + -2 and 4 - 2 both evaluate to 2.
(рис 12.8) Trick 1 - Adding a positive and negative number.
The second trick is if you are subtracting a negative number, for example, 4 - -2. The second trick is "two minuses combine into a plus". When you see the two minus signs next to each other without a number in between them, they can combine into a plus sign. The answer is still the same, because subtracting a negative value is the same as adding a positive value.
(рис 12.9) Trick 2 - Subtracting a positive and negative number.
A third trick is to remember that when you add two numbers like 6 and 4, it doesn't matter what order they are in. (This is called the commutative property of addition.) That means that 6 + 4 and 4 + 6 both equal the same value, 10. If you count the boxes in the figure below, you can see that it doesn't matter what order you have the numbers for addition.
(рис 12.10) Trick 3 - The commutative property of addition.
Say you are adding a negative number and a -6 + 8. Because you are adding numbers, you can swap the order of the numbers without changing the answer. -6 + 8 is the same as 8 + -6. But when you look at 8 + -6, you see that the 8 - 6 = 2. But this means that -6 + 8 is also 2! We've
(рис 12.11) Using our math tricks together.
Of course, you can always use the interactive shell as a calculator to evaluate these expressions. It is still very useful to know the above three tricks when adding or subtracting
>>> 4 + -2 2 >>> -4 + 2 -2 >>> -4 + -2 -6 >>> 4 - -2 6 >>> -4 - 2 -6 >>> -4 - -2 -2 >>>
The absolute value of a number is the number without the negative sign in front of it. This means that positive numbers do not change, but -4 is 4. The absolute value of -7 is 7. The absolute value of 5 (which is positive) is just 5.
We can find how far away two things on a number line are from each other by taking the absolute value of their difference. Imagine that the white knight is at position 4 and the 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
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.
(рис 12.12) The Cartesian coordinate system on a computer monitor.
This hasn't been too much math to learn for programming. In fact, most programming does not require understanding a lot of math. Up until this chapter, we have been getting by on simple addition and multiplication.
For the rest of the book, we will use the concepts we learned in this chapter in our games because they have two
Для получения официальных документов о завершении программы дополнительного профессионального образования (удостоверения о повышении квалификации, дипломов о профессиональной переподготовке и MBA) необходимо предоставить:
Внимание! Вы можете не заказывать доставку бумажной версии официального документы, а скачать его в электронном виде и распечатать самостоятельно. Информация о выданном документе в течение 1 месяца загружается в Федеральную информационную систему «Федеральный реестр сведений о документах об образовании и (или) о квалификации, документах об обучении» - ФИС ФРДО.
Доступ на новый сайт осуществляется с использованием адреса электронной почты, который был указан вами при регистрации на "старом". Мы постарались перенести все ваши данные с прежнего ресурса, однако не исключена вероятность потери части информации.
При возникновении проблемы со входом, воспользуйтесь функцией сброса пароля
Если вы обнаружите несоответствия, пожалуйста, сообщите нам.