WEBVTT 1 00:00:01.930 --> 00:00:05.819 so moving we now need to start creating some functions that will ultimately 2 00:00:05.819 --> 00:00:10.350 drive the game so we are gonna start out by just dealing cards to the players each 3 00:00:10.350 --> 00:00:14.930 time the button is clicked so we need a function called deal_card after 4 00:00:14.930 --> 00:00:20.670 the load images functions so gonna come up here their is our load images after that gonna 5 00:00:20.670 --> 00:00:26.680 start putting the code in their so putting 2 spaces as we have to do between 2 functions and start with 6 00:00:26.680 --> 00:00:35.800 .....so first thing we need to pop 7 00:00:35.800 --> 00:00:43.670 the next card off the top of the deck that's gonna be the one that the card the 8 00:00:43.670 --> 00:00:50.200 player's gonna get so... 9 00:00:52.219 --> 00:00:55.219 ...... 10 00:00:56.030 --> 00:01:04.239 ..... 11 00:01:04.899 --> 00:01:09.320 ..... 12 00:01:09.830 --> 00:01:22.310 ...... 13 00:01:22.310 --> 00:01:25.310 ..... 14 00:01:26.700 --> 00:01:32.770 ...... 15 00:01:35.100 --> 00:01:43.539 ...so the functions got a single parameter which you can see on 16 00:01:43.539 --> 00:01:48.170 line 35 and that's the frame that the image should be displayed on now up until 17 00:01:48.170 --> 00:01:52.770 now we placed all the widgets using the grid manager when it comes to adding the 18 00:01:52.770 --> 00:01:58.360 card images a pack manager is a much simpler one to use as you add new ones to the left 19 00:01:58.360 --> 00:02:02.580 they just stack up against the ones already there so in this case for cards its obviously 20 00:02:02.580 --> 00:02:07.179 perfect every time we add a cards its gonna be stacked now generally speaking its 21 00:02:07.179 --> 00:02:12.099 a very bad idea to mix grid and pack in the same window and the Python 3 22 00:02:12.099 --> 00:02:19.040 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:19.040 --> 00:02:24.780 widget is a window packing the images into this frame is fine as long as we don't try to add 24 00:02:24.780 --> 00:02:29.069 anything else to the frame using grid now we had a good look at list at 25 00:02:29.069 --> 00:02:33.060 one of the earlier section but we didn't look at the pop method that we are using on line 26 00:02:33.060 --> 00:02:38.380 37 now pop is a way to retrieve an item from a list that also removes it from the 27 00:02:38.380 --> 00:02:42.609 list at the same time so think about it just the opposite of append now 28 00:02:42.609 --> 00:02:45.040 append adds an item at the end of the list 29 00:02:45.040 --> 00:02:48.420 pop takes an item from the specified position in the list 30 00:02:48.420 --> 00:02:51.590 defaulting to the end if the position to be 31 00:02:51.590 --> 00:02:57.190 taken from is not specified by specifying position 0 we can take cards 32 00:02:57.190 --> 00:03:02.269 from the top of the deck both append and pop can also be used with an index to add 33 00:03:02.269 --> 00:03:07.160 an item at the specified position or remove one from the specified position but if we 34 00:03:07.160 --> 00:03:10.340 started popping cars from the middle of the deck the players will not be very happy 35 00:03:10.340 --> 00:03:14.410 with the dealer now once the next card has been retrieved from the deck the 36 00:03:14.410 --> 00:03:18.329 function creates a Tkinter label in the frame that's passed into the function 37 00:03:18.329 --> 00:03:23.810 and sets its image to the photo image stored in the next card tuple the 38 00:03:23.810 --> 00:03:27.370 label is then pack against the left side of the frames so that all cards should stack up 39 00:03:27.370 --> 00:03:31.540 against each other from left to right as they are being added so the newest one is to the left 40 00:03:31.540 --> 00:03:36.450 now the function then returns the next card tuples so that whatever it is calling it can also 41 00:03:36.450 --> 00:03:36.990 check 42 00:03:36.990 --> 00:03:42.730 the face value of the card so that's the function to deal a card we also need to link 43 00:03:42.730 --> 00:03:47.250 up the function to the buttons now we briefly saw in the videos introducing TKinter 44 00:03:47.250 --> 00:03:51.840 but the only button we actually got working was the cancel button now 45 00:03:51.840 --> 00:03:56.610 a function is associated with a widget using the command property so it might be 46 00:03:56.610 --> 00:04:05.300 tempting to come down here to look at the code on line 72 and maybe add 47 00:04:05.300 --> 00:04:15.070 something along the lines of.... 48 00:04:15.070 --> 00:04:19.330 so in a earlier tkinter 49 00:04:19.330 --> 00:04:23.550 video I mention to you that you have to be very careful when setting up the 50 00:04:23.550 --> 00:04:27.320 command property of widgets the value that you assigned has to be the 51 00:04:27.320 --> 00:04:32.010 function you want to be executed when the button is clicked now you definintey don't 52 00:04:32.010 --> 00:04:35.850 want to call the function instead of assigning the command and that's actually what 53 00:04:35.850 --> 00:04:42.610 I've done here....and that's what I've actually done here by 54 00:04:42.610 --> 00:04:46.750 attempting to pass the frame to the function at the same time as assigning the 55 00:04:46.750 --> 00:04:52.390 function of the button so the actual correct code would be..... 56 00:04:52.390 --> 00:04:59.470 ....but of course that's now introduced another problem because 57 00:04:59.470 --> 00:05:03.210 there's now no way to specify the frame parameter that the function actually 58 00:05:03.210 --> 00:05:08.170 needs and in fact that's true there's really no way to pass a parameter when a 59 00:05:08.170 --> 00:05:12.140 assigning a function in this way so if you include the parenthesis after 60 00:05:12.140 --> 00:05:15.890 the function name then what you're doing is you're assigning the result of calling 61 00:05:15.890 --> 00:05:19.970 the function rather than assigning the functions itself and obviously we want to assign 62 00:05:19.970 --> 00:05:24.080 the function here so that it actually executed or called when the button is 63 00:05:24.080 --> 00:05:27.810 clicked now if you don't use parentheses and you can't specify an argument to a 64 00:05:27.810 --> 00:05:30.700 functions so it sounds like a catch 22 65 00:05:30.700 --> 00:05:35.030 well thing is we've only got two buttons so the approach that we're going to take here is 66 00:05:35.030 --> 00:05:39.410 to create one function for the dealer and one for the player and assign them 67 00:05:39.410 --> 00:05:43.640 to the corresponding button now this is not the only approach and for something like the 68 00:05:43.640 --> 00:05:47.419 calculator that's got loads of buttons it wouldn't be a suitable solution it wouldn't 69 00:05:47.419 --> 00:05:51.610 be practical and I'll be looking at other ways to deal with this later in the 70 00:05:51.610 --> 00:05:56.610 course but for now what we gonna do is just create two functions so gonna add 71 00:05:56.610 --> 00:05:57.150 this 72 00:05:57.150 --> 00:06:00.449 after the code that we've just added to deal card 73 00:06:00.449 --> 00:06:16.279 so the first one is gonna be.... 74 00:06:16.279 --> 00:06:28.069 ...so you can see what we've done their is 75 00:06:28.069 --> 00:06:31.669 we've just created another function that's gonna call the function that we 76 00:06:31.669 --> 00:06:36.620 need with the required parameter being the dealer or the player card frame depending on 77 00:06:36.620 --> 00:06:41.699 which method we actually called either dealer or deal_player so now that we actually 78 00:06:41.699 --> 00:06:46.099 done that we can actually connect the buttons so we come back down here and where we've 79 00:06:46.099 --> 00:06:52.779 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:52.779 --> 00:07:00.899 ...removing the parenthesis because again 81 00:07:00.899 --> 00:07:04.919 we're assigning the function so that will be executed when the button is 82 00:07:04.919 --> 00:07:07.899 actually clicked and down here on line 83 83 00:07:07.899 --> 00:07:16.259 we are gonna do the same....again 84 00:07:16.259 --> 00:07:19.249 minus the parenthesis 85 00:07:19.249 --> 00:07:22.300 so now we've done that the program should now runs so lets try giving it a go 86 00:07:32.460 --> 00:07:38.100 you can see its working dealer and player...and 87 00:07:39.110 --> 00:07:44.819 we could keep doing that and notice it does seem to be in random order so it was 88 00:07:44.819 --> 00:07:52.919 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:52.919 --> 00:07:57.930 haven't done is change the minimum and maximum size at the moment it's actually 90 00:07:57.930 --> 00:08:02.199 going a lot wider than what would normally like it to be but will look at fixing that in 91 00:08:02.199 --> 00:08:06.130 the next video for now the code is actually working and it is actually 92 00:08:06.130 --> 00:08:11.590 adding a new card every time you click on it which is pretty awesome and if we 93 00:08:11.590 --> 00:08:15.320 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:15.320 --> 00:08:19.070 creating an error when there's no more card stack because of course it can't 95 00:08:19.070 --> 00:08:23.720 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:23.720 --> 00:08:27.750 an empty list isn't allowed now as the maximum number of cards a player can be 97 00:08:27.750 --> 00:08:34.060 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:34.060 --> 00:08:37.690 single game we don't need to worry about all the cards being dealt and so we can 99 00:08:37.690 --> 00:08:41.149 actually ignore that error for now so the next thing we want to do is to calculate 100 00:08:41.149 --> 00:08:45.660 the players scoring and display it in the frame and we'll look at doing that in the next 101 00:08:45.660 --> 00:08:45.880 video