WEBVTT

0
00:00.300 --> 00:02.250
Now that we've created both paddles

1
00:02.310 --> 00:07.020
the next obvious step is to create the ball and get it to move.

2
00:07.500 --> 00:12.500
So this ball is going to be created as a separate ball class and the ball object

3
00:14.550 --> 00:19.350
that we're going to create from it will have a width of 20, height of 20, and it's

4
00:19.380 --> 00:23.310
X and Y position will start out at the center of the screen,

5
00:23.490 --> 00:27.300
so (0, 0). Now when the screen refreshes,

6
00:27.300 --> 00:32.300
the ball is automatically going to move on the screen and it's going to move up

7
00:33.390 --> 00:34.920
and also to the right.

8
00:34.920 --> 00:39.920
So it's X and Y positions will change on every refresh of the screen.

9
00:40.800 --> 00:45.000
So this is going to be a little bit more challenging and will require a little

10
00:45.000 --> 00:46.320
bit of thinking from your part.

11
00:46.680 --> 00:51.390
But I want you to pause the video and give this problem a bit of thought and see

12
00:51.390 --> 00:55.590
how far you can get in trying to get the ball to move to the top-

13
00:55.620 --> 00:59.580
right edge of the screen. Pause the video and give that a go.

14
01:03.260 --> 01:05.330
All right. So to start off, this time

15
01:05.330 --> 01:10.250
I'm going to create a ball.py file and inside 

16
01:10.280 --> 01:14.330
this ball.py is where we're going to create our ball object.

17
01:14.750 --> 01:15.830
So firstly,

18
01:15.830 --> 01:20.830
I'm going to import my turtle class and then I'm going to create my ball class

19
01:22.040 --> 01:24.320
which is going to inherit from the turtle class.

20
01:24.800 --> 01:28.130
And then I'm going to do all of the usual initialization.

21
01:30.440 --> 01:33.950
And now we're ready to create our ball class.

22
01:34.430 --> 01:39.080
This ball is going to, firstly, have a white color.

23
01:39.710 --> 01:44.300
And in addition, it's going to have a shape that is going to be a circle.

24
01:44.600 --> 01:47.510
Now I know that in the original pong game,

25
01:47.600 --> 01:52.250
the table tennis ball is actually a square. So you can keep it a square

26
01:52.250 --> 01:56.300
if you want to be historically accurate, or you can change it to

27
01:56.300 --> 02:00.650
a circle like I have here to make it look more like a ping pong ball.

28
02:01.370 --> 02:02.360
Now, in addition,

29
02:02.390 --> 02:06.980
we're going to need to get it to pen up so that it doesn't end up drawing across

30
02:06.980 --> 02:07.813
the screen.

31
02:08.210 --> 02:13.210
And now all we need is to initialize our ball from the ball.py

32
02:17.870 --> 02:20.600
and we're going to do that just below our paddles.

33
02:20.660 --> 02:24.500
So I'm going to create a new ball object from the ball class.

34
02:25.010 --> 02:26.390
And now if I hit run,

35
02:26.420 --> 02:30.380
you'll see our circular ball show up in the center of the screen.

36
02:31.430 --> 02:36.430
The next problem is how do we get the ball to move towards the top right corner

37
02:37.310 --> 02:38.143
of the screen?

38
02:38.360 --> 02:42.500
So that's going to involve a change in the X coordinate as well as the Y

39
02:42.500 --> 02:45.590
coordinate. In our while loop here

40
02:45.740 --> 02:47.720
where our screen is updating,

41
02:48.050 --> 02:51.410
we're going to call a method in the ball class

42
02:51.710 --> 02:56.000
which is going to be called move. And this move method

43
02:56.030 --> 03:01.030
which we'll define now is going to be responsible for moving our ball.

44
03:02.290 --> 03:07.290
And the way that it's going to move is it's going to increase on the X and also

45
03:08.350 --> 03:12.790
increase on the Y. Let's create a new X coordinate

46
03:12.820 --> 03:17.820
which is going to be the current self.xcor plus a arbitrary amount.

47
03:18.850 --> 03:20.770
So let's say increase by 10.

48
03:21.220 --> 03:26.220
And then the new Y is going to be the self.ycor increased by the same

49
03:27.730 --> 03:30.160
arbitrary amount. And then finally,

50
03:30.190 --> 03:35.190
we can get our ball to go to this new X and new Y.

51
03:37.690 --> 03:39.640
So now when we run our code,

52
03:39.880 --> 03:44.500
you can see that our ball immediately goes off the screen to the top right

53
03:44.500 --> 03:48.130
corner. If we want the ball to slow down a little bit,

54
03:48.190 --> 03:50.950
we can do one of two things. Either

55
03:50.980 --> 03:55.980
we can go into the move method and change this 10 here to say a 1.

56
03:57.010 --> 04:01.360
That way, every time our loop runs, our ball will only move one pixel.

57
04:02.080 --> 04:06.490
Alternatively, we can pause the loop for a short time during each iteration.

58
04:07.480 --> 04:10.180
Moving the ball at a tiny amount does work,

59
04:10.330 --> 04:14.350
but I'm going to go with the second option and import our time module.

60
04:14.770 --> 04:18.790
Then I'm going to get our while loop to sleep for a little bit in between each

61
04:18.790 --> 04:19.690
of the updates.

62
04:20.320 --> 04:24.460
So, I normally start off with just a 0.1 second sleep,

63
04:24.790 --> 04:28.450
and you can see now a ball moves at a more reasonable pace

64
04:28.720 --> 04:31.690
and we actually have a chance of catching it with one of the paddles.

65
04:32.530 --> 04:37.210
That's all there is to it. We've now created on the ball class, initialized

66
04:37.270 --> 04:42.270
a ball object and we've got the ball to move on every refresh of the screen.