WEBVTT

00:00.670 --> 00:05.930
Work on an example to tie everything we've learned so far.

00:07.440 --> 00:18.480
Essentially what this program will do is we'll be able to control kind of a little ball or be a kid

00:18.910 --> 00:27.200
or something and we'll be able to move it around with our arrow keys.

00:27.780 --> 00:31.200
So first things first say it

00:33.780 --> 00:37.720
screen we don't want any echo

00:40.180 --> 00:43.630
in we don't want to see cursor

00:48.590 --> 00:55.850
falls in it we don't want any delay in our input

01:03.940 --> 01:11.500
and last thing is we want to keep it.

01:11.750 --> 01:18.910
So these functions will probably be using a lot for all the programs right.

01:18.950 --> 01:24.460
Well really just the Ngee that we're going to do in the next section.

01:24.790 --> 01:26.750
I'm going to have a quick.

01:26.930 --> 01:28.040
Just last time

01:31.780 --> 01:38.610
it means say it in a.

01:39.410 --> 01:55.510
Because otherwise it won't be able to understand or our arrow is in for it's yo Actually if you try

01:55.530 --> 02:05.240
with a character in a little warning and it'll say you can't compare each key down and keep it in everything

02:05.240 --> 02:06.440
like that with the input.

02:06.480 --> 02:09.670
So use it here.

02:09.820 --> 02:11.060
So say it while

02:13.640 --> 02:14.740
nocked quit

02:17.800 --> 02:21.050
and you will get the input.

02:24.620 --> 02:41.950
And if the input is not q then we will synch lead jousts clear the screen

02:44.890 --> 02:54.600
and then we'll just sort of check see what he were using and put each.

02:54.790 --> 03:02.120
He down.

03:02.350 --> 03:05.310
And then we'll move our ball down.

03:05.590 --> 03:10.370
If in each is he

03:13.070 --> 03:18.590
ball up it's input such.

03:23.210 --> 03:29.210
Laughs as if it's

03:31.920 --> 03:33.020
he right.

03:35.740 --> 03:36.150
OK.

03:36.350 --> 03:39.700
So what should we do in these cases.

03:39.710 --> 03:51.240
Well we'll need some variables here and we need the inch current exposition and set to be zero.

03:51.470 --> 04:02.100
You don't need current wives as well that G-0 and and in these since

04:04.500 --> 04:08.360
y goes down we'll say current age

04:10.900 --> 04:12.850
iPods plus plus.

04:12.880 --> 04:14.200
Right.

04:14.290 --> 04:20.650
And if it goes up we'll see current each minus minus.

04:20.730 --> 04:22.690
So we're going to move the ball.

04:22.870 --> 04:30.330
One one space every time we type the art is similarly

04:33.400 --> 04:36.680
it's minus here left.

04:36.880 --> 04:39.960
X is

04:43.350 --> 04:48.940
ch it correctly.

04:52.920 --> 04:58.880
Is pas plus.

04:59.120 --> 05:12.070
And then we actually draw the all ch you or Y pause or it's always Y then X

05:15.000 --> 05:17.500
in the ball will just draw.

05:17.690 --> 05:18.190
It's

05:22.210 --> 05:25.300
fresh.

05:25.990 --> 05:28.240
Otherwise we press.

05:28.240 --> 05:34.540
Q So which what caused this.

05:34.930 --> 05:38.760
So take a look at how this worked out.

05:39.070 --> 05:47.310
And it.

05:47.530 --> 05:52.840
Here we see ball.

05:53.000 --> 05:54.310
It's pretty good.

06:01.440 --> 06:05.800
Move it up and down right.

06:07.260 --> 06:09.250
Everything is looking pretty good.

06:18.300 --> 06:19.490
Is that

06:24.320 --> 06:28.990
and also at the end here.

06:31.490 --> 06:33.720
Don't forget that as well.

06:33.740 --> 06:36.490
Some could have some problems later.

06:39.440 --> 06:45.150
There is one more thing that we can do to this.

06:45.360 --> 06:47.830
It's kind of less obvious.

06:47.940 --> 06:54.650
But right now it runs basically acts kind of full speed.

06:54.650 --> 07:04.640
It's running as fast as you can possibly go which possibly could mess up our games in that our games

07:04.760 --> 07:06.740
might run far too fast.

07:06.800 --> 07:13.230
Maybe if the aliens are dropping bombs on us we won't be able to actually get out the way in time.

07:13.230 --> 07:14.410
Right.

07:14.610 --> 07:19.670
We're going to react so to fix this.

07:19.670 --> 07:22.040
I'm going to include

07:24.550 --> 07:36.410
mood seedtime and basically With time when I'm going to do is to limit how fast this loop will go as

07:36.450 --> 07:37.650
initially.

07:37.740 --> 07:49.730
So let's say I want to draw 60 times per second which is kind of the standard refresh rate of Monk.

07:49.810 --> 07:50.650
OK.

07:50.820 --> 07:57.150
And we say that it's 60 frames per second and you can think of one frame as being one time through the

07:57.150 --> 07:59.580
loop for our probes.

07:59.580 --> 08:07.940
So the question becomes how do we make sure that we're only doing 60 frames per second.

08:08.490 --> 08:19.080
And essentially what we're going to do is appear we're going to get the current time and we're going

08:19.080 --> 08:29.080
to use this type called clock clock and the squirty and it's going to give us the current time in clock

08:29.080 --> 08:32.320
ticks and it's playing with a clock tick is it.

08:32.320 --> 08:35.750
Second and to get the current time we need a function.

08:35.760 --> 08:41.040
And it's called clock.

08:41.100 --> 08:42.720
So this will give us

08:45.600 --> 08:46.800
current time

08:50.690 --> 08:52.870
in clock ticks

08:55.960 --> 09:15.400
and now we're going to do is up here are going to get the time again so we're I clock the new time

09:18.510 --> 09:33.000
equal to clock against the clock ticks again and we're going to say clock t delta time t is time between

09:33.000 --> 09:36.480
the new time and the current time

09:41.620 --> 09:51.770
so essentially DTC is how much time has passed from the last time we checked clock essentially.

09:51.780 --> 10:03.070
So this DTC is measured in clock ticks which is an arbitrary unit for measuring internal system time

10:03.540 --> 10:07.790
and it's dependent on your computer.

10:08.450 --> 10:12.360
But clock ticks by themselves don't really help too much.

10:12.420 --> 10:19.980
We need to somehow relate it to second since we want to only go 60 frames per second.

10:20.100 --> 10:32.370
So to get how many ticks per second time actually has a constant called clock ticks per second clocks

10:32.370 --> 10:39.560
per sec clocks her set like this.

10:39.560 --> 10:44.670
And this tells us how many clock ticks happen in a second for your system.

10:44.990 --> 10:54.650
OK so now we have to relate the EDT which is in Tick's two clocks per second.

10:54.980 --> 11:02.300
So to do that we're going to say clocks per second divided by some amount and the amount we're going

11:02.300 --> 11:08.080
to be or it's going to be is Konst in frames per second.

11:08.270 --> 11:16.130
And Ron said it to be 60 in this case and we're going to divide the clocks per second by our frames

11:16.130 --> 11:17.480
per second.

11:19.990 --> 11:28.040
This and essentially is going to be ticks per second divided by frames per second.

11:28.420 --> 11:35.470
And if you know your division you'll know that this kind of translates into ticks per frame.

11:35.970 --> 11:38.030
OK.

11:38.230 --> 11:44.130
So that's essentially what did he really is it's how many ticks per one frame.

11:45.320 --> 11:50.320
And so now we can actually compare this to the delta time.

11:50.330 --> 11:57.170
So we'll see if E.T. is greater than this.

11:57.370 --> 11:58.190
So if.

11:58.240 --> 12:05.430
How much time has passed is greater than our frame rate in ticks per frame.

12:05.690 --> 12:11.560
Then will do all of this.

12:13.440 --> 12:17.620
This is kind of format it's

12:21.050 --> 12:22.100
like that.

12:22.230 --> 12:30.220
They are into this whole thing in this kind of loop or in this case.

12:30.280 --> 12:30.870
Yes.

12:31.940 --> 12:35.940
So are going to check to see how much time is has

12:38.690 --> 12:41.860
as occurred since the last time it's a clock.

12:41.870 --> 12:49.970
And then if it's greater than our frame rate in ticks per frame rate this is what this basically amounts

12:49.970 --> 12:55.130
to is expert frame then we'll do all this.

12:55.130 --> 12:59.280
And the last thing we need to do is update current time to

13:03.090 --> 13:06.830
to the last times and since new time.

13:06.960 --> 13:15.260
OK so current time will be now what it was when we last checked which is equal to Newton.

13:15.550 --> 13:19.860
And so now if we run this

13:22.720 --> 13:29.050
you see that everything kind of looks exactly the same as what we did before.

13:29.230 --> 13:35.200
But like I said now it's going only 60 frames per second.

13:35.650 --> 13:44.880
And again that's going to help us with our game so that we can kind of limit how fast everything goes.

13:44.930 --> 13:48.210
So we might not use 60 frames per second.

13:48.410 --> 13:51.950
You might use it here.

13:52.400 --> 13:57.530
We might use like 20 French second to make it a bit nicer.

13:57.650 --> 13:58.680
OK.

13:58.680 --> 14:01.080
So that's it for this example.

14:01.430 --> 14:05.150
And in the next lecture I want to go over attributes.
