Game Development Reference
In-Depth Information
There are also the conceptually more advanced
bitwise
operators, used to perform
operations at the
binary
data (
zeroes and ones
) level, the logic of which is beyond the
beginner scope of this topic and the use of which is not as common in Java game pro-
gramming as these other, more mainstream types of operators, each of which you will
be using over the course of this topic to accomplish various programming objectives in
your game play logic.
Java Arithmetic Operators
The Java
arithmetic operators
are the most commonly used in programming, espe-
cially in arcade type games, in which things are moving on the screen by a discrete
number of pixels. Many more complex equations can be created using these basic
arithmetic operators, as you have already learned in math class, from primary school
through college.
The only arithmetic operators shown in
Table 3-2
that you may not be that familiar
with are the
modulus
operator, which will return the remainder (what is left over) after
a divide operation is completed, and the
increment
and
decrement
operators, which
add or subtract 1, respectively, from a value. These are used to implement your
counter
logic. Counters (using increment and decrement operators) were originally
used for loops, (which I will be covering in the next section); however, these increment
and decrement operators are also extremely useful for game programming (point scor-
ing,
life span
loss, game piece movement, and similar progressions).
Table 3-2
.
Java Arithmetic Operators, Their Operation Type, and a Description of the
Arithmetic Operation
Operator
Operation
Description
Plus +
Addition
Adds the operands on either side of the operator
Minus -
Subtraction
Subtracts the right-hand operand from the left-hand operand
Multiply * Multiplication Multiplies the operands on either side of the operator
Divide /
Division
Divides the left-hand operand by the right-hand operand
Modulus
%
Remainder
Divides the left-hand operand by the right hand-operand, re-
turning remainder
Increment
++
Add 1
Increases the value of the operand by 1