1
00:00:05,240 --> 00:00:09,120
so moving we now need to start creating
some functions that will ultimately

2
00:00:09,120 --> 00:00:13,660
drive the game so we are gonna start out by
just dealing cards to the players each

3
00:00:13,660 --> 00:00:18,240
time the button is clicked so we need a
function called deal_card after

4
00:00:18,240 --> 00:00:23,980
the load images functions so gonna come up
here their is our load images after that gonna

5
00:00:23,980 --> 00:00:29,990
start putting the code in their so putting 2 spaces as we have to do between 2 functions and start with

6
00:00:29,990 --> 00:00:39,110
.....so first thing we need to pop

7
00:00:39,110 --> 00:00:46,980
the next card off the top of the deck that's
gonna be the one that the card the

8
00:00:46,980 --> 00:00:53,510
player's gonna get so...

9
00:00:55,520 --> 00:00:58,520
......

10
00:00:59,340 --> 00:01:07,540
.....

11
00:01:08,200 --> 00:01:12,630
.....

12
00:01:13,140 --> 00:01:25,620
......

13
00:01:25,620 --> 00:01:28,620
.....

14
00:01:30,010 --> 00:01:36,080
......

15
00:01:38,410 --> 00:01:46,840
...so the functions got a
single parameter which you can see on

16
00:01:46,840 --> 00:01:51,480
line 35 and that's the frame that the
image should be displayed on now up until

17
00:01:51,480 --> 00:01:56,080
now we placed all the widgets using the
grid manager when it comes to adding the

18
00:01:56,080 --> 00:02:01,670
card images a pack manager is a much
simpler one to use as you add new ones to the left

19
00:02:01,670 --> 00:02:05,890
they just stack up against the ones already
there so in this case for cards its obviously

20
00:02:05,890 --> 00:02:10,479
perfect every time we add a cards its gonna be
stacked now generally speaking its

21
00:02:10,479 --> 00:02:15,400
a very bad idea to mix grid and pack in the same window and the Python 3

22
00:02:15,400 --> 00:02:22,350
compiler will give an error if you try but it is ok to use the pack manager in its own window now as every

23
00:02:22,350 --> 00:02:28,090
widget is a window packing the images into this frame is
fine as long as we don't try to add

24
00:02:28,090 --> 00:02:32,370
anything else to the frame using grid now we had a good look at list at

25
00:02:32,370 --> 00:02:36,370
one of the earlier section but we didn't look
at the pop method that we are using on line

26
00:02:36,370 --> 00:02:41,690
37 now pop is a way to retrieve an item
from a list that also removes it from the

27
00:02:41,690 --> 00:02:45,910
list at the same time so think about
it just the opposite of append now

28
00:02:45,910 --> 00:02:48,350
append adds an item at the end of the
list

29
00:02:48,350 --> 00:02:51,730
pop takes an item from the specified
position in the list

30
00:02:51,730 --> 00:02:54,900
defaulting to the end if the position to be

31
00:02:54,900 --> 00:03:00,500
taken from is not specified by
specifying position 0 we can take cards

32
00:03:00,500 --> 00:03:05,570
from the top of the deck both insert and
pop can also be used with an index to add

33
00:03:05,570 --> 00:03:10,470
an item at the specified position or remove
one from the specified position but if we

34
00:03:10,470 --> 00:03:13,650
started popping cars from the middle of
the deck the players will not be very happy

35
00:03:13,650 --> 00:03:17,720
with the dealer now once the next card
has been retrieved from the deck the

36
00:03:17,720 --> 00:03:21,630
function creates a Tkinter label in the
frame that's passed into the function

37
00:03:21,630 --> 00:03:27,120
and sets its image to the photo image
stored in the next card tuple the

38
00:03:27,120 --> 00:03:30,680
label is then pack against the left side of the
frames so that all cards should stack up

39
00:03:30,680 --> 00:03:34,850
against each other from left to right as
they are being added so the newest one is to the left

40
00:03:34,850 --> 00:03:39,760
now the function then returns the next card tuples so
that whatever it is calling it can also

41
00:03:39,760 --> 00:03:40,300
check

42
00:03:40,300 --> 00:03:46,040
the face value of the card so that's
the function to deal a card we also need to link

43
00:03:46,040 --> 00:03:50,560
up the function to the buttons now we briefly saw in the videos introducing TKinter

44
00:03:50,560 --> 00:03:55,150
but the only button we actually got
working was the cancel button now

45
00:03:55,150 --> 00:03:59,920
a function is associated with a widget
using the command property so it might be

46
00:03:59,920 --> 00:04:08,610
tempting to come down here to look at
the code on line 72 and maybe add

47
00:04:08,610 --> 00:04:18,380
something along the lines of....

48
00:04:18,380 --> 00:04:22,640
so in a earlier tkinter

49
00:04:22,640 --> 00:04:26,860
video I mention to you that you have
to be very careful when setting up the

50
00:04:26,860 --> 00:04:30,630
command property of widgets the value
that you assigned has to be the

51
00:04:30,630 --> 00:04:35,320
function you want to be executed when
the button is clicked now you definintey don't

52
00:04:35,320 --> 00:04:39,160
want to call the function instead of assigning
the command and that's actually what

53
00:04:39,160 --> 00:04:45,920
I've done here....and
that's what I've actually done here by

54
00:04:45,920 --> 00:04:50,060
attempting to pass the frame to the
function at the same time as assigning the

55
00:04:50,060 --> 00:04:55,700
function of the button so the actual
correct code would be.....

56
00:04:55,700 --> 00:05:02,780
....but of course that's now
introduced another problem because

57
00:05:02,780 --> 00:05:06,520
there's now no way to specify the frame
parameter that the function actually

58
00:05:06,520 --> 00:05:11,480
needs and in fact that's true there's
really no way to pass a parameter when a

59
00:05:11,480 --> 00:05:15,450
assigning a function in this way so if
you include the parenthesis after

60
00:05:15,450 --> 00:05:19,200
the function name then what you're doing
is you're assigning the result of calling

61
00:05:19,200 --> 00:05:23,280
the function rather than assigning the
functions itself and obviously we want to assign

62
00:05:23,280 --> 00:05:27,390
the function here so that it actually
executed or called when the button is

63
00:05:27,390 --> 00:05:31,120
clicked now if you don't use parentheses and
you can't specify an argument to a

64
00:05:31,120 --> 00:05:34,010
functions so it sounds like a catch 22

65
00:05:34,010 --> 00:05:38,340
well thing is we've only got two buttons so the approach that we're going to take here is

66
00:05:38,340 --> 00:05:42,720
to create one function for the dealer
and one for the player and assign them

67
00:05:42,720 --> 00:05:46,950
to the corresponding button now this is not the
only approach and for something like the

68
00:05:46,950 --> 00:05:50,720
calculator that's got loads of buttons
it wouldn't be a suitable solution it wouldn't

69
00:05:50,720 --> 00:05:54,920
be practical and I'll be looking at other
ways to deal with this later in the

70
00:05:54,920 --> 00:05:59,920
course but for now what we gonna do is
just create two functions so gonna add

71
00:05:59,920 --> 00:06:00,460
this

72
00:06:00,460 --> 00:06:03,750
after the code that we've just added to deal card

73
00:06:03,750 --> 00:06:19,580
so the first one is gonna be....

74
00:06:19,580 --> 00:06:31,370
...so you can see what we've done their is

75
00:06:31,370 --> 00:06:34,970
we've just created another function
that's gonna call the function that we

76
00:06:34,970 --> 00:06:39,930
need with the required parameter being the dealer or the player card frame depending on

77
00:06:39,930 --> 00:06:45,000
which method we actually called either dealer or
deal_player so now that we actually

78
00:06:45,000 --> 00:06:49,400
done that we can actually connect the
buttons so we come back down here and where we've

79
00:06:49,400 --> 00:06:56,080
got on line 80 text = dealer and I'll add an extra space
their to keep it happy and that should now be

80
00:06:56,080 --> 00:07:04,200
...removing the parenthesis because again

81
00:07:04,200 --> 00:07:08,220
we're assigning the function so that
will be executed when the button is

82
00:07:08,220 --> 00:07:11,200
actually clicked and down here on line 83

83
00:07:11,200 --> 00:07:19,560
we are gonna do the same....again

84
00:07:19,560 --> 00:07:22,550
minus the parenthesis

85
00:07:22,550 --> 00:07:25,610
so now we've done that the program should now runs so lets try giving it a go

86
00:07:35,770 --> 00:07:41,410
you can see its working dealer and player...and

87
00:07:42,420 --> 00:07:48,120
we could keep doing that and notice it does seem to be
in random order so it was

88
00:07:48,120 --> 00:07:56,220
correctly shuffled and if we just go a bit wider you can see they are still being added and one thing I

89
00:07:56,220 --> 00:08:01,240
haven't done is change the minimum and
maximum size at the moment  it's actually

90
00:08:01,240 --> 00:08:05,500
going a lot wider than what would normally
like it to be but will look at fixing that in

91
00:08:05,500 --> 00:08:09,440
the next video for now the code is
actually working and it is actually

92
00:08:09,440 --> 00:08:14,900
adding a new card every time you click
on it which is pretty awesome and if we

93
00:08:14,900 --> 00:08:18,630
kept going actually I'm not going to do that now
but if we did get to 52 the program will end up

94
00:08:18,630 --> 00:08:22,380
creating an error when there's no more
card stack because of course it can't

95
00:08:22,380 --> 00:08:27,030
deal anymore card if their is no more left for the deck and that's
because attempting to pop an item from

96
00:08:27,030 --> 00:08:31,060
an empty list isn't allowed now as the
maximum number of cards a player can be

97
00:08:31,060 --> 00:08:37,370
dealt without going bust is 11 that's
4 aces 4 2's and 3 3's it wouldn't ordinarily in a

98
00:08:37,370 --> 00:08:41,000
single game we don't need to worry about all the
cards being dealt and so we can

99
00:08:41,000 --> 00:08:44,450
actually ignore that error for now so the
next thing we want to do is to calculate

100
00:08:44,450 --> 00:08:48,970
the players scoring and display it in the
frame and we'll look at doing that in the next

101
00:08:48,970 --> 00:08:49,190
video

