Okay. Let's look at one more application using
Modular Arithmetic. And this will be something she'll actually
do fairly often in the class. You're going to have a 2D game.
Objects may be moving around. And you have to deal with what happens
when an object gets near the edge of the canvas.
Maybe it's going to bang off of it. Maybe it's going to come to stop.
But sometimes, you'd like it to actually do maybe something where it wraps around.
It goes off one side and comes on the other side.
Let me just show you a quick little demo. Here's a part of a game you're going to
actually build later on. This is a spaceship.
And our spaceship starts flying, and we get close to the edge of the screen , and
what's going to happen? Well, it pops on the other side of the
screen, it wrapped around. If I go off the top of the screen,
I wrapped around. I go back on the bottom of the screen
after I round the top. So, how do we model this mathematically?
You could store kind of some, some information to represent the, the position
of the center of the ship. But when we go off one edge of the screen,
we better change that position so that it's kind of in a different position way
on the other side of the screen. So, we can use Modular Arithmetic to do
that. So, let's go back and look at this. So,
lets see where the situation where we are keeping track of the horizontal position
of the ship and so we're at this, the frame that I created was 800 pixels wide.
So, we are getting close to that right-hand edge, and we say, we need to go
right five pixels. How can we do that?
Well, I claimed we can do that using remainders, we can say, position is equal
to, well, the old position plus whatever the move was, we could do that remainder,
The width of the screen, and if we print out position what's going to come out is
two. The new, the position of the ship after we
move five units right has not overpassed the right-hand side of the screen.
The screen is actually over the left-hand screened, side of the screen it positioned
to. So, we've popped from the right-hand side
to the left-hand side. The interesting thing is this also works
the other way. And to do that, you have to understand how
a remainder works on negative numbers. If we take the remainder of something
that's negative. What happens is we're always going to get
a number back in the range from zero to width. Zero to the number that we're
taking the remainder of respect to. So, what's going to happen is when we take
the remainder of something that's negative we're going to get something that appears
on the screen, Something in the right range.
We'll just do that tangibly. Let's say, we were the other way.
Let's say, we were at position two. So, we're going to counter reverse this.
We were positioned to, and we did a move of -five.
We kind of move to the left. We're on the left-hand side of the screen
and we move left, that's going be -five. Run the same code.
Well, what's going to come out, we're going to get two minus five is negative
three. So, we have to find -three remainder 800.
And I told you that's always going to be a number between the zero and the width.
So, it comes out as 797. So, we kind of took -three, we added 800
to it. It gives a number between zero and 800,
it's 797, so we pop to the right-hand side of the screen.
So, notice using remainders here actually works, works in both directions.
It works when you're moving left, it works when you're moving right, it works when
you're going up, it works when you're going down.
So, remainder is going to be your friend when you're doing screen wrapping in your
games. The next topic that I'd like to cover is
talk a little bit more about how to convert back and forth between data types.
In the lecture on Arithmetic expressions, we talked about the operator int and the
operator float, and how those kind of converted data in other forms into either
an integer or floating-point number. There's times when you're going to have a
number and you're going to want to convert it into string so you can print it out.
your first three projects are going to involve really just doing lots of
printing. You won't get into using the canvas and
doing drawing until Week four. So, what I want to do here is just talk a
little bit about how you can convert things into a string, and so I'm going to
motivate it by just talking about a very simple problem.
So, we, we talked earlier about 24, the 24-hour clock.
And, in a 24-hour clock, you always kind of think about the, the hours, as being
two digit even if there's a zero in front of the, even if it's like three:000 a.m.,
it's really 03. So, here three:000 a.m. would be 0300.