1
00:00:05,000 --> 00:00:09,820
Hi, welcome to the second
lesson on this final project,

2
00:00:09,820 --> 00:00:12,135
which is a complex
systems project,

3
00:00:12,135 --> 00:00:15,405
where we're actually using
a grid data structure in

4
00:00:15,405 --> 00:00:19,005
an object-oriented style as

5
00:00:19,005 --> 00:00:22,620
well as Langton's ants
walking on top of it.

6
00:00:22,620 --> 00:00:27,480
Some interesting ideas here
for the final project.

7
00:00:27,480 --> 00:00:29,280
Let's just continue working on

8
00:00:29,280 --> 00:00:32,135
the script that we started
from last session.

9
00:00:32,135 --> 00:00:35,150
Here we are. Let's just
remind ourselves we

10
00:00:35,150 --> 00:00:38,430
have a grid class.

11
00:00:38,430 --> 00:00:40,260
As you can see here,

12
00:00:40,260 --> 00:00:41,900
this grid class takes

13
00:00:41,900 --> 00:00:44,460
the arguments of columns
and rows and the size of

14
00:00:44,460 --> 00:00:47,680
the screen to
basically distribute

15
00:00:47,680 --> 00:00:49,700
the appropriate number
of cells and that

16
00:00:49,700 --> 00:00:51,580
distributes the entirety
of that screen.

17
00:00:51,580 --> 00:00:53,940
We want to have a
class that would cover

18
00:00:53,940 --> 00:00:57,205
the entirety of the screen and
is able to hold some data.

19
00:00:57,205 --> 00:01:00,020
We're not using this variable
yet, the data variable.

20
00:01:00,020 --> 00:01:02,000
As you can see, we
can actually draw

21
00:01:02,000 --> 00:01:06,840
the grid completely
without any data.

22
00:01:06,840 --> 00:01:10,530
Let's just create some functions

23
00:01:10,530 --> 00:01:14,830
to store some data first
and then display that data.

24
00:01:14,830 --> 00:01:19,320
We're going to see
later in future videos

25
00:01:19,320 --> 00:01:21,790
how a new entity will be able to

26
00:01:21,790 --> 00:01:24,805
manipulate or use that data
for their own behavior.

27
00:01:24,805 --> 00:01:27,890
Let's just start by
creating a function here

28
00:01:27,890 --> 00:01:31,890
in the data in the
grid data class.

29
00:01:31,890 --> 00:01:36,400
It's going to be the
definition of init data.

30
00:01:38,450 --> 00:01:40,910
This function, we want to run

31
00:01:40,910 --> 00:01:43,320
only at the very
beginning of the script.

32
00:01:43,320 --> 00:01:45,150
We need to do it only once,

33
00:01:45,150 --> 00:01:47,340
and this will be a way of

34
00:01:47,340 --> 00:01:50,430
potentially randomizing
or deciding that

35
00:01:50,430 --> 00:01:53,445
some entities in the grid

36
00:01:53,445 --> 00:01:54,870
actually have one kind of

37
00:01:54,870 --> 00:01:56,410
data versus a different
kind of data.

38
00:01:56,410 --> 00:01:59,190
We're only going to work
with a Boolean data or

39
00:01:59,190 --> 00:02:02,745
a zero and one number,

40
00:02:02,745 --> 00:02:04,430
and that should be enough for

41
00:02:04,430 --> 00:02:08,185
our algorithm to really be
able to take some decisions.

42
00:02:08,185 --> 00:02:09,980
We're going to write a for loop.

43
00:02:09,980 --> 00:02:12,425
We're going to actually
copy paste the for loop.

44
00:02:12,425 --> 00:02:15,855
I really like copy
pasting parts of my code.

45
00:02:15,855 --> 00:02:20,670
If you want to do that,
this is what you would do.

46
00:02:20,670 --> 00:02:24,460
We want to do nested
for loop once more.

47
00:02:24,890 --> 00:02:28,650
Within a loop, for
i in range for

48
00:02:28,650 --> 00:02:31,920
columns and for j in range
for the number of rows.

49
00:02:31,920 --> 00:02:36,030
Here, we are going to
use the random function.

50
00:02:36,030 --> 00:02:42,255
I don't think we have imported
random in this script.

51
00:02:42,255 --> 00:02:43,790
Remember that in this case,

52
00:02:43,790 --> 00:02:47,270
Python classes operate
a separate file.

53
00:02:47,270 --> 00:02:49,990
This particular class,

54
00:02:49,990 --> 00:02:52,170
if it's going to make use
of the random module,

55
00:02:52,170 --> 00:02:57,855
we want to make that available
to this particular script.

56
00:02:57,855 --> 00:03:04,890
The number, this

57
00:03:04,890 --> 00:03:07,815
is a variable that's
going to be an integer.

58
00:03:07,815 --> 00:03:11,290
We could say it's going to be

59
00:03:11,930 --> 00:03:17,460
random.randint zero and one.

60
00:03:17,460 --> 00:03:20,740
It's a random integer
between zero and one.

61
00:03:21,230 --> 00:03:31,080
At this point, we could do
self.data.append the number.

62
00:03:31,080 --> 00:03:32,760
What are we doing here? This is

63
00:03:32,760 --> 00:03:34,620
the first time we
are actually using

64
00:03:34,620 --> 00:03:39,405
this list that we stored.

65
00:03:39,405 --> 00:03:43,910
Within this list, we're going
to store a bit of data,

66
00:03:43,910 --> 00:03:46,260
either a zero or a one,

67
00:03:47,180 --> 00:03:49,530
based on this random function.

68
00:03:49,530 --> 00:03:52,050
We are initiating this data.

69
00:03:52,050 --> 00:03:55,550
We are going to call
this function only once,

70
00:03:55,550 --> 00:04:00,490
make it that we calculate the
randomization of the data,

71
00:04:00,490 --> 00:04:02,825
and then we're going to
be able to display it.

72
00:04:02,825 --> 00:04:05,790
At this point, we want to check

73
00:04:05,790 --> 00:04:08,590
if this function is running.

74
00:04:08,590 --> 00:04:14,015
We are going to call
this function init_data,

75
00:04:14,015 --> 00:04:15,950
but we need to call it
from within the grid.

76
00:04:15,950 --> 00:04:17,210
We're going to
call this function

77
00:04:17,210 --> 00:04:20,730
separately because
the run function,

78
00:04:20,730 --> 00:04:26,290
we are ultimately run it
every frame in the draw loop.

79
00:04:26,290 --> 00:04:28,370
But for now, we
are going to call

80
00:04:28,370 --> 00:04:30,600
the initialized data and

81
00:04:30,600 --> 00:04:33,295
then the execution
of the display.

82
00:04:33,295 --> 00:04:36,205
Let's see if we have any
errors at this point.

83
00:04:36,205 --> 00:04:38,210
I don't think we have errors,

84
00:04:38,210 --> 00:04:42,010
but we cannot see the data.

85
00:04:42,010 --> 00:04:43,650
The data is just some zero and

86
00:04:43,650 --> 00:04:46,750
one store within this list.

87
00:04:46,750 --> 00:04:48,770
If we want to render
that [inaudible],

88
00:04:48,770 --> 00:04:50,910
we would actually have to

89
00:04:50,910 --> 00:04:54,220
associate that data or
establish some relationship

90
00:04:54,220 --> 00:04:59,930
between the display and the
data that we generated.

91
00:04:59,930 --> 00:05:03,290
This is why we actually
created the data in

92
00:05:03,290 --> 00:05:06,310
the same fashion that

93
00:05:06,310 --> 00:05:10,530
the grid because if we
actually do it this way,

94
00:05:10,530 --> 00:05:14,530
we will know that we have
exactly the exact number of

95
00:05:14,530 --> 00:05:20,340
data points as we have grid
rectangles or grid cells.

96
00:05:20,340 --> 00:05:23,740
This is going to be useful
because we could actually now

97
00:05:23,740 --> 00:05:26,020
equate each one of

98
00:05:26,020 --> 00:05:29,850
these entities of data to one
of the cells in the grid.

99
00:05:29,850 --> 00:05:32,660
Let's just do that.
Let's just improve

100
00:05:32,660 --> 00:05:36,200
our display function so
that it would display.

101
00:05:36,200 --> 00:05:38,380
If an entity has
a value of zero,

102
00:05:38,380 --> 00:05:39,860
we're going to paint it black.

103
00:05:39,860 --> 00:05:43,545
If it has a value of one,
we're going to paint it white.

104
00:05:43,545 --> 00:05:45,880
In order to do so,

105
00:05:45,880 --> 00:05:50,300
we would need to know
if I would give or

106
00:05:50,300 --> 00:05:52,800
request the value of

107
00:05:52,800 --> 00:05:55,720
a particular entry
point and the data,

108
00:05:55,720 --> 00:05:58,080
I would need to do
some calculation.

109
00:05:58,080 --> 00:05:59,560
We're actually going to do

110
00:05:59,560 --> 00:06:02,715
a helper function that
is going to help us

111
00:06:02,715 --> 00:06:06,480
calculate if we provide an x and

112
00:06:06,480 --> 00:06:07,880
a y how do we access

113
00:06:07,880 --> 00:06:10,410
the data point that is
equivalent to that.

114
00:06:10,410 --> 00:06:13,870
Let's just create a
function of get_data.

115
00:06:14,450 --> 00:06:20,320
We're going to do
self, x, and y.

116
00:06:20,330 --> 00:06:23,550
Think of this as a
way of saying, hey,

117
00:06:23,550 --> 00:06:26,490
from the grid I want
the zero, zero,

118
00:06:26,490 --> 00:06:29,790
meaning the index 0 in x and y.

119
00:06:29,790 --> 00:06:33,550
But because the data is
distributed as a single list,

120
00:06:33,550 --> 00:06:38,110
we are trying to convert
what we would call a form of

121
00:06:38,110 --> 00:06:41,250
grid notation of
columns and rows

122
00:06:41,250 --> 00:06:46,395
positions to a flat notation.

123
00:06:46,395 --> 00:06:50,230
The result of this
function will be

124
00:06:50,230 --> 00:06:54,205
the index of the piece of
data we're looking for.

125
00:06:54,205 --> 00:06:55,865
Here, I will write

126
00:06:55,865 --> 00:06:59,750
a small formula that I've
used many times in the past,

127
00:06:59,750 --> 00:07:01,910
and it's actually really useful.

128
00:07:01,910 --> 00:07:04,520
We're going to do the
absolute, first of all.

129
00:07:04,520 --> 00:07:06,170
We definitely want
to make sure that we

130
00:07:06,170 --> 00:07:09,960
are working with the absolute.

131
00:07:09,970 --> 00:07:13,910
Let's just add a few
parentheses here just

132
00:07:13,910 --> 00:07:15,320
because there's going to

133
00:07:15,320 --> 00:07:17,450
be a lot of different
entities here,

134
00:07:17,450 --> 00:07:21,750
so y times self.rows.

135
00:07:24,640 --> 00:07:29,280
That's the first entity.

136
00:07:30,360 --> 00:07:38,260
We're going to divide that
by self.screen_height.

137
00:07:40,120 --> 00:07:42,680
Then this is going
to be multiplied

138
00:07:42,680 --> 00:07:49,280
by self.columns plus,

139
00:07:49,280 --> 00:07:53,460
and here again we're
going to do parentheses.

140
00:07:54,490 --> 00:07:56,660
We're going to
organize this a little

141
00:07:56,660 --> 00:07:58,280
bit once we have it written.

142
00:07:58,280 --> 00:08:00,720
Let's just do self.

143
00:08:09,630 --> 00:08:11,785
Here we go.

144
00:08:11,785 --> 00:08:15,380
I think there's [inaudible]
What are we doing here?

145
00:08:15,380 --> 00:08:18,920
This function might
seem quite difficult.

146
00:08:18,920 --> 00:08:21,515
If you imagine like a grid,

147
00:08:21,515 --> 00:08:23,060
you're basically trying to say,

148
00:08:23,060 --> 00:08:24,950
well, I know that

149
00:08:24,950 --> 00:08:29,105
the list of data is all spread
into one single sequence,

150
00:08:29,105 --> 00:08:30,860
and we need to be able to

151
00:08:30,860 --> 00:08:35,600
calculate just given
particular x and y, 0,

152
00:08:35,600 --> 00:08:39,095
0 or let's say 10 and 1,

153
00:08:39,095 --> 00:08:42,740
the second row in
the 10th column.

154
00:08:42,740 --> 00:08:47,480
We need to be able to start
spreading up that data.

155
00:08:47,480 --> 00:08:49,910
You do so by calculating

156
00:08:49,910 --> 00:08:55,850
how many rows have

157
00:08:55,850 --> 00:08:57,035
you gone in and multiplying

158
00:08:57,035 --> 00:08:58,805
by the number of
columns and then

159
00:08:58,805 --> 00:09:01,910
adding the final bit
that you're missing,

160
00:09:01,910 --> 00:09:03,560
which is the addition.

161
00:09:03,560 --> 00:09:07,635
This function will be
incredibly handy to return.

162
00:09:07,635 --> 00:09:09,340
We're going to do
something that I

163
00:09:09,340 --> 00:09:10,480
don't think we have done before,

164
00:09:10,480 --> 00:09:13,675
but you probably covered it
in the first specialization,

165
00:09:13,675 --> 00:09:17,500
which is how functions
can return a variable.

166
00:09:17,500 --> 00:09:24,140
We're going to do
self.data index.

167
00:09:24,400 --> 00:09:28,160
We are using this
index to access

168
00:09:28,160 --> 00:09:34,700
the data within our grid.
Think about the following.

169
00:09:34,700 --> 00:09:36,290
We're using the
get_data function.

170
00:09:36,290 --> 00:09:37,640
We're going to give an x and y,

171
00:09:37,640 --> 00:09:39,080
let's say 5 and 5,

172
00:09:39,080 --> 00:09:40,700
and it's going to be
returning us what

173
00:09:40,700 --> 00:09:43,190
is the data stored in that cell.

174
00:09:43,190 --> 00:09:46,865
This function is going
to be incredibly useful.

175
00:09:46,865 --> 00:09:48,740
It's one of those functions

176
00:09:48,740 --> 00:09:50,480
that once you have
it written once,

177
00:09:50,480 --> 00:09:51,800
you can copy paste it again

178
00:09:51,800 --> 00:09:54,140
and reuse it in different
projects because it's

179
00:09:54,140 --> 00:09:56,480
a function that
helps you convert

180
00:09:56,480 --> 00:09:59,735
the notation from an x and y,

181
00:09:59,735 --> 00:10:03,815
like two coordinate system
to a singular list index.

182
00:10:03,815 --> 00:10:05,780
What are we doing with
this function now?

183
00:10:05,780 --> 00:10:08,030
We actually would like to change

184
00:10:08,030 --> 00:10:11,120
the way these rectangles
are displayed.

185
00:10:11,120 --> 00:10:13,910
The only thing that is missing
is using this function.

186
00:10:13,910 --> 00:10:16,170
We could say if.

187
00:10:17,830 --> 00:10:22,775
Let's use our function
self.get_data.

188
00:10:22,775 --> 00:10:28,490
If we check this function
and we use the value

189
00:10:28,490 --> 00:10:35,555
of i times rec_size,

190
00:10:35,555 --> 00:10:40,520
which is this is the x coordinate
given this coordinate,

191
00:10:40,520 --> 00:10:43,820
and given the j
coordinate would be

192
00:10:43,820 --> 00:10:47,210
j times basically what
we're spelling out here,

193
00:10:47,210 --> 00:10:48,560
basically the same information.

194
00:10:48,560 --> 00:10:51,410
This is the x coordinate
and the y coordinate.

195
00:10:51,410 --> 00:10:57,230
Given these coordinates, if
this coordinate is zero,

196
00:10:57,230 --> 00:11:01,685
meaning that the data
inside that cell is zero,

197
00:11:01,685 --> 00:11:06,950
we are going to paint
this rectangle black.

198
00:11:06,950 --> 00:11:12,290
Let's just make that
statement saying fill is

199
00:11:12,290 --> 00:11:21,600
zero and then else fill is 255.

200
00:11:22,290 --> 00:11:27,830
Now we can actually
remove this two.

201
00:11:28,110 --> 00:11:31,330
The only really difference

202
00:11:31,330 --> 00:11:36,025
between these two scenarios
is that if the data is zero,

203
00:11:36,025 --> 00:11:37,750
if its else would be a one,

204
00:11:37,750 --> 00:11:40,015
we're going to paint
the ones as white.

205
00:11:40,015 --> 00:11:44,065
Let's see. We have this result,

206
00:11:44,065 --> 00:11:46,030
which is, as you can see,

207
00:11:46,030 --> 00:11:48,415
the data has been randomized.

208
00:11:48,415 --> 00:11:50,965
We are using a
random function to

209
00:11:50,965 --> 00:11:54,390
initially store data zero
or a one inside each cell.

210
00:11:54,390 --> 00:11:55,745
It's not technically inside,

211
00:11:55,745 --> 00:11:59,765
but the class contains a list

212
00:11:59,765 --> 00:12:02,315
that we are equating
and we have a way of

213
00:12:02,315 --> 00:12:05,705
retrieving the data inside
each one of the cell entities.

214
00:12:05,705 --> 00:12:09,830
We're doing so with
this function get_data,

215
00:12:09,830 --> 00:12:12,785
which we can provide
an x and y coordinate,

216
00:12:12,785 --> 00:12:15,770
and it will tell us
within the cell,

217
00:12:15,770 --> 00:12:18,770
it's in fact that's a
zero or that's a one.

218
00:12:18,770 --> 00:12:21,065
We're going to leave
this video here.

219
00:12:21,065 --> 00:12:24,320
We actually have the grid
working as intended.

220
00:12:24,320 --> 00:12:28,010
Again, I like doing
this test if this data

221
00:12:28,010 --> 00:12:32,510
would in fact work
for more units.

222
00:12:32,510 --> 00:12:36,270
Let's just double a little
bit the number here.

223
00:12:36,520 --> 00:12:40,895
As you can see, our
randomization of data

224
00:12:40,895 --> 00:12:45,845
as a class and as an object
in a way are working well.

225
00:12:45,845 --> 00:12:49,440
Let's just do that

226
00:12:49,450 --> 00:12:52,430
and here we go. We're
going to leave it here.

227
00:12:52,430 --> 00:12:54,320
In the next video, we're
going to start seeing

228
00:12:54,320 --> 00:12:57,260
how a new class, an ant class,

229
00:12:57,260 --> 00:12:58,610
can actually start operating

230
00:12:58,610 --> 00:12:59,960
on top of this grid and taking

231
00:12:59,960 --> 00:13:01,370
behaviors based on
the data that we

232
00:13:01,370 --> 00:13:04,080
created. I'll see you
in the next video.