Well, welcome to lecture. In this lecture, we're going to talk about control.
Now Scott says I have control issues when it comes to running this course, but we're
not going to talk about that kind of control.
We're going to talk about the kind of control you need whenever you want to play
pong and you want to control the position of the paddle on your screen.
In the second lecture this week, Scott talked about using keyboard input to
control the position of the circle on the canvass.
We're going to go back and revisit that example.
And, I'm going to talk about a way you can change the scheme for controlling the
position of the wall where as if controlling the position directly, you're
actually controlling the velocity of how the ball moves around on the canvas.
And, we'll talk a little bit on one note and then we'll go and I'll actually just
walk you through a second example that will actually have a lot of similar
functionality to that which you'll need inside pong.
So, let's pay attention to this lecture, I don't want you to have control issues now.
So let's look at Scott's example of controlling the position of a circle on
the canvas, using the arrow keys. So let's just run the application to
start. So here we have our canvas, and we've got
a white circle in the middle, and I'm going to go through, and I'm going to
just, if you can hear me, I'm just going to start banging the arrow keys here, and
I'm slowly moving the circle to the left. I can go back and make the right arrow
key. You can actually see there in the status
area, that I've got, continually hitting the arrow key, and go up.
I can go around and then kind of a rectangle.
And You could see over here actually, I am printing out the position so you can kind
of keep track of the list just to see and really just matching the keys a lot.
Now, If you're not an Arcane Mage on the World
of Warcraft, where button mashing is your main skill, okay, maybe this is a, a good
control for you. But, in general, if I want the, the circle
to go up, I should probably just hit the key once and it starts moving up.
And then, maybe if I hit again, it stops or release the key, it stops, just
something like that. That's what you see in most games.
So, this is a not, not a great way to control the position of this circle.
Let's take a quick peek at the code and see kind of what the need the code was
that Scott used here. So what do we have?
Well, we have a kind of one critical variable here, it was the ball position,
it was a global and in our key down handler.
We simply checked to see if we hit an arrow key, and if that we did, depending
on which arrow key, as we changed one of the components of this ball position, and
we changed it by some step size. We've actually moved it four pixels.
So every time I hit the key, left key, the horizontal component of the ball position
was decreased by four. And so essentially, just, I mashed the key
a lot. The key in would get called a lot, and I
changed the position of the ball. Another thing I know here is that I didn't
do anything interesting the drawing handler, drawing handler just set there.
It was just drawing the ball over and over.
So, what I'm going to do is I'm going to try and go back and revisit how does this
ball pause as an example and show you a different way to control the position of
the ball. So, just pop on the one note real quick
and I'll roll out what I'm going to do and then, we'll go back and we'll kind of look
at a modified version of this. Alright, let's think about the code we
just took a look at and see if we can improve the manner in which we move the
ball around on the canvas. So, the critical thing here was that we
had, the ball had a center p here. So, we're going to control the position of
that point p. So, this was a positional control scheme
we looked at. And there are some critical things that we
noted, there was no update on the draw handler so the only place that we modify
things were inside the key handler. So in the key handler, if you recall, we
updated this position P, was a global variable.
If we hit the left arrow, we took the horizontal component, and we decreased it.
Okay, that's what this equation says here. We hit the right arrow, we took the
horizontal component, and we increased it, and the same thing for up and down.
So that's why, if we want to move the ball a lot, we had to hit the keys a lot.