WEBVTT 1 00:00:01.920 --> 00:00:07.580 ok so moving on with our blackjack program I've made a bit of a typo on line 24 it should read 2 00:00:07.580 --> 00:00:14.519 embedded frame to hold the card images ok so moving on line 31 we are about 3 00:00:14.519 --> 00:00:21.029 to do the player card frame now so we will just type a comment about that so.... 4 00:00:21.029 --> 00:00:32.219 ..... 5 00:00:34.069 --> 00:00:41.789 ....... 6 00:00:43.069 --> 00:00:52.449 ...... 7 00:00:52.449 --> 00:00:59.119 ...so hopefully you're following along with a lot of 8 00:00:59.119 --> 00:01:02.659 this TKinter code now because we have discussed a pretty well all of this in 9 00:01:02.659 --> 00:01:07.119 previous videos so I just a bit more of a complex interface we've created before 10 00:01:07.119 --> 00:01:15.899 alright so next is the buttons so lets add a button frame so... 11 00:01:15.899 --> 00:01:39.840 ..... 12 00:01:39.840 --> 00:01:49.240 ...... 13 00:01:49.240 --> 00:01:59.310 .... 14 00:02:00.149 --> 00:02:06.890 ...... 15 00:02:06.890 --> 00:02:20.040 ..... 16 00:02:21.290 --> 00:02:32.010 ....so that's the basic code done so the main windows 17 00:02:32.010 --> 00:02:36.209 got three widgets the label at the top to display the result and two frames one 18 00:02:36.209 --> 00:02:38.840 below the other and I'll just bring up our little 19 00:02:38.840 --> 00:02:44.180 image again ok so here it is on the screen again what I'm talking so the moment 20 00:02:44.180 --> 00:02:48.780 as I said its got 3 widgets a label at the top to display the result and it's 21 00:02:48.780 --> 00:02:53.140 also got two frames one below the other the top most of the two frames is gonna 22 00:02:53.140 --> 00:02:56.880 contain the cards as you can see on the screen there and as well as the scores for the 23 00:02:56.880 --> 00:03:01.550 dealer and player and the one below that will hold the buttons as you can see right down the 24 00:03:01.550 --> 00:03:06.570 bottom the card frame itself contained two other frames went one for the dealer 25 00:03:06.570 --> 00:03:10.610 and one for the player as well as the labels to hold the name as well as the score 26 00:03:10.610 --> 00:03:15.519 so the card frame contains as I said another two frames that will hold the cards and by 27 00:03:15.519 --> 00:03:19.320 putting the card images into separate frames starting a new game is easier 28 00:03:19.320 --> 00:03:23.459 because we just have to destroy the entire frame rather than trying to find 29 00:03:23.459 --> 00:03:28.340 out each of the card in it and destroy them separately so this will make more sense 30 00:03:28.340 --> 00:03:33.670 when we come to do it so let's move onto loading the card images so the file names of 31 00:03:33.670 --> 00:03:37.720 the individual images are quite well formed and made up of the card's value 32 00:03:37.720 --> 00:03:41.110 when sweep followed by an underscore so we can have a look at that we can 33 00:03:41.110 --> 00:03:44.330 go into here and have a look now 34 00:03:44.330 --> 00:03:48.450 so you can see that's the card number and underscore and then the sweep so 35 00:03:48.450 --> 00:03:52.560 it means that we can pragmatically load these in pretty easily so for the cards from 36 00:03:52.560 --> 00:03:53.780 1 to 10 37 00:03:53.780 --> 00:03:57.130 we can actually load that with a simple loop to load them all in 38 00:03:57.650 --> 00:04:00.670 now the face card images can be retrieved using a second loop that runs 39 00:04:00.670 --> 00:04:04.940 through a list of names these ones down here jack king queen etc 40 00:04:04.940 --> 00:04:08.170 and by wrapping all that in another loop that runs through the list 41 00:04:08.170 --> 00:04:12.640 of sweep names all 52 card images can be loaded and this is something that the 42 00:04:12.640 --> 00:04:16.109 program will only have to do once but I'm still going to create a function to 43 00:04:16.109 --> 00:04:20.340 do it by placing this code in its own function the main program is going to be far 44 00:04:20.340 --> 00:04:24.750 less cluttered and more readable as a result so let's go ahead and do that i'm 45 00:04:24.750 --> 00:04:29.870 gonna come up here and start on line 9 and make bit of a space and put our 46 00:04:29.870 --> 00:04:36.700 function in their so we are going to call this load images so..... 47 00:04:36.700 --> 00:04:48.320 ..... 48 00:04:48.320 --> 00:04:54.290 ...because these will be ultimately be matching what's the what's in the file name so.... 49 00:04:54.290 --> 00:05:05.600 ..... 50 00:05:11.800 --> 00:05:19.210 and remember earlier I mentioned about to the TK version 8.6 is needed to use PNG 51 00:05:19.210 --> 00:05:28.289 files we're gonna put a test in here so... 52 00:05:28.289 --> 00:05:48.210 ..... 53 00:05:48.210 --> 00:05:49.400 ...so now we need to create our first loop so..... 54 00:05:49.400 --> 00:06:14.440 ..... 55 00:06:15.539 --> 00:06:25.490 ...remembering our range finishes one number 56 00:06:25.490 --> 00:06:35.050 earlier so we starting it at 1 11 and we put....which is the subfolder remembering that 57 00:06:35.050 --> 00:06:38.779 we add as sub folder to store the card files in or the came out of the 58 00:06:38.779 --> 00:06:43.169 zip file like that and were going to use..... 59 00:06:43.949 --> 00:07:16.900 ..... 60 00:07:16.900 --> 00:07:17.980 ...... 61 00:07:17.980 --> 00:07:22.940 .....so that's going to be the name that we need but now we need to retrieve it 62 00:07:22.940 --> 00:07:34.440 so do....and the name is the name 63 00:07:34.440 --> 00:07:40.790 that we've created on line 23 and then we need to save it so.... 64 00:07:40.790 --> 00:07:52.100 ....and then finished off we've 65 00:07:52.100 --> 00:08:01.900 done that first bit of the loop we'll let hat loop finish and then we do next the face cards......so from 66 00:08:01.900 --> 00:08:10.790 their we are going to put.....and you remember face cards on line 12 jack king and queen so we need to 67 00:08:10.790 --> 00:08:17.190 also get those so......and the same thing with 68 00:08:17.190 --> 00:08:26.910 the replacement field....... 69 00:08:26.910 --> 00:08:32.060 ..... 70 00:08:33.840 --> 00:08:38.910 ...so we got the right type off image depending on the version of tkinter and 71 00:08:38.910 --> 00:08:48.540 then the same deal..... 72 00:08:48.540 --> 00:08:56.540 ...... 73 00:08:57.970 --> 00:09:01.170 ..... 74 00:09:01.170 --> 00:09:07.730 ..so we've now got our load images function written and that should now be able to retrieve all 75 00:09:07.730 --> 00:09:12.050 those images we need for our game into memory and just to note their that if you extracted 76 00:09:12.050 --> 00:09:16.000 the images to a different folder than what I did make sure you do change this first 77 00:09:16.000 --> 00:09:19.950 part here cards / because that is going to be looking at the current folder wherever that 78 00:09:19.950 --> 00:09:26.330 is for the sub folder called cards and whatever the name of the file is so make sure you do change that if you 79 00:09:26.330 --> 00:09:31.170 put it in somewhere else so you need to change both their on line 23 and also 80 00:09:31.170 --> 00:09:36.920 on line 29 so the card list should after this contain a list of tuples each tuples storing 81 00:09:36.920 --> 00:09:42.060 the value and image each for one of the 52 cards and the images have been loaded into 82 00:09:42.060 --> 00:09:46.529 Tkinter photo image objects which are fairly basic but do allow images to be 83 00:09:46.529 --> 00:09:51.120 displayed easily that's what we need to do here and what we're going to do is 84 00:09:51.120 --> 00:09:54.600 right on the bottom add some code to just to test that this actually works s 85 00:09:54.600 --> 00:09:57.600 lets try doing so add some code so put.... 86 00:09:59.470 --> 00:10:06.639 ...... 87 00:10:06.639 --> 00:10:14.600 ...and will print cards to see what it actually happens and will just add our 88 00:10:14.600 --> 00:10:26.690 main loop so....and we got an error here so what have I done their I didn't put a colon their so 89 00:10:26.690 --> 00:10:32.610 put a colon and its pretty good and its complaining about 2 lines their so we'll add another line their to humour that so its 90 00:10:32.610 --> 00:10:44.170 actually happy and this other error is probably relating to tkinter camel case that probably want an extra space in their as well probably a good idea to fix those while we can you can't fix this 91 00:10:44.170 --> 00:10:48.420 first one up because that's got to do with Python 2 so we'll leave that at that so 92 00:10:48.420 --> 00:10:52.620 if we run this now we should be able to get something it won't be very exciting but we can at least demonstrate 93 00:10:52.620 --> 00:11:00.269 that things are actually working and if we tab over to our interface its obviously not working yet 94 00:11:00.269 --> 00:11:04.390 because we haven't added the cards so close it down but we should be able to confirm 95 00:11:04.390 --> 00:11:09.060 that we got our various objects in here and you can see this multiple objects that 96 00:11:09.060 --> 00:11:13.569 are the loaded so assuming it has work we'll just have to print out 97 00:11:13.569 --> 00:11:17.260 the cards or display them =to confirm that they've been loaded but it 98 00:11:17.260 --> 00:11:22.769 seems to have loaded into the list which is good so a new deck of cars can 99 00:11:22.769 --> 00:11:26.430 be created from the cards list now we imported them then we can shuffle them using 100 00:11:26.430 --> 00:11:30.089 the shuffle function from the random module that we imported remember 101 00:11:30.089 --> 00:11:33.339 we did that in the previous video so the program will also need to store the 102 00:11:33.339 --> 00:11:37.120 cards dealt to each player so before we start creating the functions to make all 103 00:11:37.120 --> 00:11:38.080 of this work 104 00:11:38.080 --> 00:11:42.460 I'm going to initialize a dealer hand player hand list we might do that and might 105 00:11:42.460 --> 00:11:47.990 finish of this video and continue on to the next one so after here we can type..... 106 00:11:47.990 --> 00:12:01.480 ..... 107 00:12:02.200 --> 00:12:11.950 .... 108 00:12:11.950 --> 00:12:17.050 ...its gonna sort of shuffle them in a random order which is obviously what we want we don't our cards coming 109 00:12:17.050 --> 00:12:20.940 out in any sort of order the whole idea is that we don't know what the next card is coming 110 00:12:20.940 --> 00:12:23.940 it's entirely random we want to create the lists to store 111 00:12:26.610 --> 00:12:44.330 dealers and players hand so...so just 112 00:12:44.330 --> 00:12:50.220 before I finish this video just a couple of other things to note on line 73 I 113 00:12:50.220 --> 00:12:54.360 used the list function to create a new list from the cards and if I've done 114 00:12:54.360 --> 00:12:59.020 something different for example if I hadn't change that code and made this instead 115 00:12:59.020 --> 00:13:06.270 commenting that out and make that....which is another way of doing it 116 00:13:06.270 --> 00:13:10.230 deck would then be exactly the same list as cards so that's not necessarily a 117 00:13:10.230 --> 00:13:13.780 problem when just shuffling but as the cards are dealt they may be removed from the 118 00:13:13.780 --> 00:13:17.460 list which could cause problems so every game will then have fewer and fewer cards 119 00:13:17.460 --> 00:13:20.510 until their are none left to create new decks from so we definitely don't want to do it 120 00:13:20.510 --> 00:13:24.810 that way we want to create a new and separate list which is the way we've done on 121 00:13:24.810 --> 00:13:29.140 lines 73 so I'm going to finish the video here now in the next video we're 122 00:13:29.140 --> 00:13:34.390 gonna go ahead and write some code to deal the card we are gonna deal cards to the 123 00:13:34.390 --> 00:13:39.650 players each time the button is click so we need a function called deal_card 124 00:13:39.650 --> 00:13:41.270 and we'll work on that in the next video