1
00:00:05,882 --> 00:00:08,822
Hi, welcome to the second
video of project four,

2
00:00:08,822 --> 00:00:12,395
we're going to continue working
on our pathfinding project.

3
00:00:12,395 --> 00:00:16,036
We're going to continue also from where
we left off in the last video, so

4
00:00:16,036 --> 00:00:17,154
please start there.

5
00:00:17,154 --> 00:00:19,953
And we're going to be working on
the environment customization.

6
00:00:19,953 --> 00:00:26,893
We want to give some ability to
our mouse to paint an environment.

7
00:00:26,893 --> 00:00:30,104
What we're describing an environment
is we're starting with tiles that

8
00:00:30,104 --> 00:00:32,861
are going to be floor tiles and
we're going to be painting.

9
00:00:32,861 --> 00:00:38,296
So we're going to be able to change the
type of some of these tiles to represent

10
00:00:38,296 --> 00:00:44,008
walls that would eventually become
obstacles in our pathfinding algorithm.

11
00:00:44,008 --> 00:00:49,842
So let's just jump into the code and
create this kind of customization.

12
00:00:49,842 --> 00:00:55,010
So here it is where we left off,
we have an environment which is grid, as

13
00:00:55,010 --> 00:01:01,314
you can see in the environment class, and
basically instantiating a series of tiles.

14
00:01:01,314 --> 00:01:05,893
Each one of them that could be
different types, floor, wall, or

15
00:01:05,893 --> 00:01:10,307
others, that we don't have
representation for them yet, and

16
00:01:10,307 --> 00:01:13,408
we don't have a display function for them.

17
00:01:13,408 --> 00:01:18,055
So we're going to just be transitioning
from only drawing floor tiles to also

18
00:01:18,055 --> 00:01:19,390
drawing wall tiles.

19
00:01:19,390 --> 00:01:20,827
So let's imagine what we want to do.

20
00:01:20,827 --> 00:01:25,372
We're going to probably,
like here in the draw function,

21
00:01:25,372 --> 00:01:30,495
do something along the lines of
if the mouse is pressed, right?

22
00:01:30,495 --> 00:01:34,757
So let's say if mouse pressed,

23
00:01:36,196 --> 00:01:40,554
And then we can check, right?

24
00:01:40,554 --> 00:01:43,927
We can check if the mouse

25
00:01:46,430 --> 00:01:50,580
Button is the left mouse button, right?

26
00:01:50,580 --> 00:01:54,254
Is it the left mouse button?

27
00:01:54,254 --> 00:01:57,715
Let's mark here, so
we're pressing the left mouse,

28
00:01:57,715 --> 00:02:03,147
let's paint in the position of the mouse,
the current cell, wherever the mouse is.

29
00:02:03,147 --> 00:02:08,707
Let's change that to be
a cell type of one, right?

30
00:02:08,707 --> 00:02:16,050
Change cell type 21,
let's call that a wall, right?

31
00:02:16,050 --> 00:02:18,576
And maybe if we press

32
00:02:23,138 --> 00:02:27,326
Equals, right, so maybe the other button,

33
00:02:27,326 --> 00:02:31,526
we can maybe revert it back to floor,
right?

34
00:02:31,526 --> 00:02:36,390
Again, you could have different types,
I'm going to do

35
00:02:36,390 --> 00:02:41,581
an example that is rather
simple between these two types.

36
00:02:41,581 --> 00:02:46,191
But again, it's quite interesting to
kind of take this problem further.

37
00:02:46,191 --> 00:02:50,460
We will have a conversation towards the
end of the week of where this system can

38
00:02:50,460 --> 00:02:51,718
actually go, right?

39
00:02:51,718 --> 00:02:53,982
So this is kind of a little
bit what we want to do now.

40
00:02:53,982 --> 00:02:58,742
The thing that we are realizing that
we're missing is some function that

41
00:02:58,742 --> 00:03:02,343
would allow us to paint or
to execute this line of code.

42
00:03:02,343 --> 00:03:06,208
This would probably be
a function of our environment.

43
00:03:06,208 --> 00:03:08,991
So let's go into our environment and

44
00:03:08,991 --> 00:03:12,863
let's think about the problems
that we have here.

45
00:03:12,863 --> 00:03:18,005
First of all,
we want to be able to convert.

46
00:03:18,005 --> 00:03:21,239
And we've done this already with the
vector field, it's a very similar problem,

47
00:03:21,239 --> 00:03:24,056
but because the data structure that
we're using, it's a nested list,

48
00:03:24,056 --> 00:03:26,158
it's slightly different,
we are going to do it again.

49
00:03:26,158 --> 00:03:30,827
So we're going to get a cell by
providing a coordinate, right?

50
00:03:30,827 --> 00:03:35,274
So we could call get_cell.

51
00:03:35,274 --> 00:03:41,243
So we want to be able to
provide a coordinate such as x,

52
00:03:41,243 --> 00:03:46,433
y and get a cell associated with that,
right?

53
00:03:46,433 --> 00:03:50,091
And I'm repeating something that we
did already in the vector field, but

54
00:03:50,091 --> 00:03:53,572
it's going to be slightly different
the way we access the cell, right?

55
00:03:53,572 --> 00:03:57,509
So let's do x,
it's going to be an integer.

56
00:03:57,509 --> 00:04:02,911
We know that where x is divided

57
00:04:02,911 --> 00:04:09,611
by the float(self.world_x) and

58
00:04:09,611 --> 00:04:14,806
divided by self.columns.

59
00:04:14,806 --> 00:04:17,701
I think this is also close here, right?

60
00:04:17,701 --> 00:04:26,369
So what we're trying to do is just divide
the world by the number of columns, right?

61
00:04:26,369 --> 00:04:34,760
And we can do that for

62
00:04:34,760 --> 00:04:39,700
the x and y,

63
00:04:46,314 --> 00:04:52,853
Right, so we are converting world
coordinates into indices, right?

64
00:04:52,853 --> 00:04:57,355
Indices in the grid, right?

65
00:04:57,355 --> 00:05:02,984
So we could check that if x is not smaller

66
00:05:02,984 --> 00:05:08,624
than the length of self cells, right?

67
00:05:08,624 --> 00:05:15,641
And this is just a check for us not to
provide an index that is out of bounds.

68
00:05:15,641 --> 00:05:22,416
So if the length of
the column is represented

69
00:05:22,416 --> 00:05:27,551
by self.cells[0], right?

70
00:05:27,551 --> 00:05:31,838
Any column, we know that all
columns will have the same length.

71
00:05:31,838 --> 00:05:39,124
So the first cell,
it's perfectly fine to evaluate here,

72
00:05:39,124 --> 00:05:44,515
then, well, the cell that we will return,

73
00:05:44,515 --> 00:05:48,901
it's going to be self.cells x and y.

74
00:05:48,901 --> 00:05:54,145
So notice that we're using
here this notation of index

75
00:05:54,145 --> 00:05:59,731
in this list that it's nested,
so first we access the x and

76
00:05:59,731 --> 00:06:04,523
then the y,
it's in the second square brackets.

77
00:06:04,523 --> 00:06:10,608
So here we can return the cell,

78
00:06:10,608 --> 00:06:18,340
else we could say return none, right?

79
00:06:18,340 --> 00:06:22,826
The cell will be returned correctly
if we provide a coordinate

80
00:06:22,826 --> 00:06:25,251
within the domain of the screen.

81
00:06:25,251 --> 00:06:30,137
So technically this shouldn't have
an issue because we're trying to

82
00:06:30,137 --> 00:06:35,188
access the mouse should always give
us information within the screen and

83
00:06:35,188 --> 00:06:39,512
that's where we actually get
the appropriate cell, right?

84
00:06:39,512 --> 00:06:42,922
So now that we have the get_cell function,

85
00:06:42,922 --> 00:06:48,269
which is allowing the mouse to
convert the coordinates from the mouse

86
00:06:48,269 --> 00:06:53,452
into an index coordinate,
we can actually do our paint function.

87
00:06:53,452 --> 00:06:56,951
We're going to call this paint_cell.

88
00:06:56,951 --> 00:07:01,833
So this is going to take the x and
y as well, x,

89
00:07:01,833 --> 00:07:05,921
y and the type in this case, right?

90
00:07:05,921 --> 00:07:10,920
Because we know that we can
define the type of a tile

91
00:07:10,920 --> 00:07:15,929
by changing its,
the type is an integer, right?

92
00:07:15,929 --> 00:07:21,333
So let's say that the cell is going to
be equal to self.get_cell here,

93
00:07:21,333 --> 00:07:28,019
we're using this function already that we
have get_cell is a function that we wrote,

94
00:07:28,019 --> 00:07:31,980
and we're going to pass on the x and
y information.

95
00:07:31,980 --> 00:07:36,490
And the type, sorry, not just x and
y, to collect the cell first.

96
00:07:36,490 --> 00:07:40,472
And then we can say if

97
00:07:40,472 --> 00:07:46,803
cell.current_type = type.

98
00:07:46,803 --> 00:07:49,048
So it's pretty straightforward,

99
00:07:49,048 --> 00:07:53,174
what we're just saying is collect
that cell and change its type.

100
00:07:55,114 --> 00:07:58,530
And this is the function that the mouse
is going to invoke from outside

101
00:07:58,530 --> 00:07:59,935
the environment, right?

102
00:07:59,935 --> 00:08:05,799
So basically we have everything we
need to allow for this execution now.

103
00:08:05,799 --> 00:08:10,541
Remember that the integer,
if you go to the tile, the type, or

104
00:08:10,541 --> 00:08:17,175
basically our current type is either 0 for
floor, wall, resource and water, right?

105
00:08:17,175 --> 00:08:22,204
So 0, 1, 2 and
3 in terms of indices, right?

106
00:08:22,204 --> 00:08:26,597
So let's use this function here,
paint_cell,

107
00:08:26,597 --> 00:08:31,512
in what we started doing at
the beginning of this video,

108
00:08:31,512 --> 00:08:36,863
right, which is call
the my_environment.paint_cell.

109
00:08:36,863 --> 00:08:41,407
And we're going to say that
the paint_cell, the x and

110
00:08:41,407 --> 00:08:44,924
y coordinate would be mouse x, mouse y.

111
00:08:46,344 --> 00:08:51,418
And in this case,
the wall will be an integer of 1,

112
00:08:51,418 --> 00:08:54,729
so the type of tile is going to be 1.

113
00:08:54,729 --> 00:08:59,062
We're going to do the same thing for

114
00:08:59,062 --> 00:09:04,325
our right mouse button that will convert

115
00:09:04,325 --> 00:09:08,966
things back to a floor by typing 0,

116
00:09:08,966 --> 00:09:14,510
0 is the index of the type floor, right?

117
00:09:14,510 --> 00:09:21,331
So let's see what we have so far here.

118
00:09:21,331 --> 00:09:24,034
So I'm running into one error here.

119
00:09:27,028 --> 00:09:32,589
So this is paint_self, let's see.

120
00:09:32,589 --> 00:09:37,042
Okay, so if we press left mouse button,
we can like construct

121
00:09:37,042 --> 00:09:42,118
right mouse button should allow us
to delete right or go back to tile.

122
00:09:42,118 --> 00:09:46,237
So this is going to be great because we're
going to be able to create an environment,

123
00:09:46,237 --> 00:09:49,382
something like a labyrinth or
something along those lines.

124
00:09:49,382 --> 00:09:54,057
And then start making a calculation of how
do we reach from one point to another one

125
00:09:54,057 --> 00:09:58,098
and create a kind of the optimal
path between those points, right?

126
00:09:58,098 --> 00:10:01,130
So, yeah, we have kind of
a customizable environment.

127
00:10:01,130 --> 00:10:02,831
So we continue in the next video.

128
00:10:02,831 --> 00:10:03,434
I'll see you then.