1
00:00:00,300 --> 00:00:02,250
Now that we've created both paddles

2
00:00:02,310 --> 00:00:07,020
the next obvious step is to create the ball and get it to move.

3
00:00:07,500 --> 00:00:12,500
So this ball is going to be created as a separate ball class and the ball object

4
00:00:14,550 --> 00:00:19,350
that we're going to create from it will have a width of 20, height of 20, and it's

5
00:00:19,380 --> 00:00:23,310
X and Y position will start out at the center of the screen,

6
00:00:23,490 --> 00:00:27,300
so (0, 0). Now when the screen refreshes,

7
00:00:27,300 --> 00:00:32,299
the ball is automatically going to move on the screen and it's going to move up

8
00:00:33,390 --> 00:00:34,920
and also to the right.

9
00:00:34,920 --> 00:00:39,920
So it's X and Y positions will change on every refresh of the screen.

10
00:00:40,800 --> 00:00:45,000
So this is going to be a little bit more challenging and will require a little

11
00:00:45,000 --> 00:00:46,320
bit of thinking from your part.

12
00:00:46,680 --> 00:00:51,390
But I want you to pause the video and give this problem a bit of thought and see

13
00:00:51,390 --> 00:00:55,590
how far you can get in trying to get the ball to move to the top-

14
00:00:55,620 --> 00:00:59,580
right edge of the screen. Pause the video and give that a go.

15
00:01:03,260 --> 00:01:05,330
All right. So to start off, this time

16
00:01:05,330 --> 00:01:10,250
I'm going to create a ball.py file and inside 

17
00:01:10,280 --> 00:01:14,330
this ball.py is where we're going to create our ball object.

18
00:01:14,750 --> 00:01:15,830
So firstly,

19
00:01:15,830 --> 00:01:20,830
I'm going to import my turtle class and then I'm going to create my ball class

20
00:01:22,040 --> 00:01:24,320
which is going to inherit from the turtle class.

21
00:01:24,800 --> 00:01:28,130
And then I'm going to do all of the usual initialization.

22
00:01:30,440 --> 00:01:33,950
And now we're ready to create our ball class.

23
00:01:34,430 --> 00:01:39,080
This ball is going to, firstly, have a white color.

24
00:01:39,710 --> 00:01:44,300
And in addition, it's going to have a shape that is going to be a circle.

25
00:01:44,600 --> 00:01:47,510
Now I know that in the original pong game,

26
00:01:47,600 --> 00:01:52,250
the table tennis ball is actually a square. So you can keep it a square

27
00:01:52,250 --> 00:01:56,300
if you want to be historically accurate, or you can change it to

28
00:01:56,300 --> 00:02:00,650
a circle like I have here to make it look more like a ping pong ball.

29
00:02:01,370 --> 00:02:02,360
Now, in addition,

30
00:02:02,390 --> 00:02:06,980
we're going to need to get it to pen up so that it doesn't end up drawing across

31
00:02:06,980 --> 00:02:07,813
the screen.

32
00:02:08,210 --> 00:02:13,210
And now all we need is to initialize our ball from the ball.py

33
00:02:17,870 --> 00:02:20,600
and we're going to do that just below our paddles.

34
00:02:20,660 --> 00:02:24,500
So I'm going to create a new ball object from the ball class.

35
00:02:25,010 --> 00:02:26,390
And now if I hit run,

36
00:02:26,420 --> 00:02:30,380
you'll see our circular ball show up in the center of the screen.

37
00:02:31,430 --> 00:02:36,430
The next problem is how do we get the ball to move towards the top right corner

38
00:02:37,310 --> 00:02:38,143
of the screen?

39
00:02:38,360 --> 00:02:42,500
So that's going to involve a change in the X coordinate as well as the Y

40
00:02:42,500 --> 00:02:45,590
coordinate. In our while loop here

41
00:02:45,740 --> 00:02:47,720
where our screen is updating,

42
00:02:48,050 --> 00:02:51,410
we're going to call a method in the ball class

43
00:02:51,710 --> 00:02:56,000
which is going to be called move. And this move method

44
00:02:56,030 --> 00:03:01,030
which we'll define now is going to be responsible for moving our ball.

45
00:03:02,290 --> 00:03:07,290
And the way that it's going to move is it's going to increase on the X and also

46
00:03:08,350 --> 00:03:12,790
increase on the Y. Let's create a new X coordinate

47
00:03:12,820 --> 00:03:17,820
which is going to be the current self.xcor plus a arbitrary amount.

48
00:03:18,850 --> 00:03:20,770
So let's say increase by 10.

49
00:03:21,220 --> 00:03:26,220
And then the new Y is going to be the self.ycor increased by the same

50
00:03:27,730 --> 00:03:30,160
arbitrary amount. And then finally,

51
00:03:30,190 --> 00:03:35,190
we can get our ball to go to this new X and new Y.

52
00:03:37,690 --> 00:03:39,640
So now when we run our code,

53
00:03:39,880 --> 00:03:44,500
you can see that our ball immediately goes off the screen to the top right

54
00:03:44,500 --> 00:03:48,130
corner. If we want the ball to slow down a little bit,

55
00:03:48,190 --> 00:03:50,950
we can do one of two things. Either

56
00:03:50,980 --> 00:03:55,980
we can go into the move method and change this 10 here to say a 1.

57
00:03:57,010 --> 00:04:01,360
That way, every time our loop runs, our ball will only move one pixel.

58
00:04:02,080 --> 00:04:06,490
Alternatively, we can pause the loop for a short time during each iteration.

59
00:04:07,480 --> 00:04:10,180
Moving the ball at a tiny amount does work,

60
00:04:10,330 --> 00:04:14,350
but I'm going to go with the second option and import our time module.

61
00:04:14,770 --> 00:04:18,790
Then I'm going to get our while loop to sleep for a little bit in between each

62
00:04:18,790 --> 00:04:19,690
of the updates.

63
00:04:20,320 --> 00:04:24,460
So, I normally start off with just a 0.1 second sleep,

64
00:04:24,790 --> 00:04:28,450
and you can see now a ball moves at a more reasonable pace

65
00:04:28,720 --> 00:04:31,690
and we actually have a chance of catching it with one of the paddles.

66
00:04:32,530 --> 00:04:37,210
That's all there is to it. We've now created on the ball class, initialized

67
00:04:37,270 --> 00:04:42,270
a ball object and we've got the ball to move on every refresh of the screen.

