WEBVTT 1 00:00:01.960 --> 00:00:06.460 so we are gonna leave geometric functions where we left in the previous videos but 2 00:00:06.460 --> 00:00:11.700 we are gonna carry on working with Tkinter to produce a very simple card game now there's a couple of 3 00:00:11.700 --> 00:00:15.849 things that we haven't fully explored with functions yet and as I mentioned 4 00:00:15.849 --> 00:00:20.450 earlier there's another reason why IntelliJ warns about shadowing variables 5 00:00:20.450 --> 00:00:24.160 in the outer scope this game is gonna give us a way to look at that in more 6 00:00:24.160 --> 00:00:29.450 detail now blackjack itself is quite a simple card game the aim is for players to 7 00:00:29.450 --> 00:00:34.160 get a high score or higher total than dealer without going above the score of 21 now if 8 00:00:34.160 --> 00:00:38.100 the total value of the cards in a hand comes to more than twenty one then the 9 00:00:38.100 --> 00:00:43.030 payers said to have bust or have busted and that is game over now their is variations on the 10 00:00:43.030 --> 00:00:46.949 way the game is played so our version as I mentioned is gonna be simple version 11 00:00:46.949 --> 00:00:52.249 and will have the following conditions the dealer deals one card to 12 00:00:52.249 --> 00:00:57.800 each player then to him or herself each player but not the dealer then gets a second card 13 00:00:57.800 --> 00:01:02.280 the player then decided wether to stick with a total they have or hit which means to get 14 00:01:02.280 --> 00:01:07.060 another card now a player can hit as many times as they like but as soon as their total goes above 15 00:01:07.060 --> 00:01:14.119 21 their bust and its basically their goal stops effectively the cards that a player 16 00:01:14.119 --> 00:01:18.460 holds when they go finish this is call their hand once all the players have either 17 00:01:18.460 --> 00:01:23.090 stuck or bust which of the two options the dealer gets a second card and the dealer 18 00:01:23.090 --> 00:01:27.479 then decides whether to stick or hit this works the same as for the players and that 19 00:01:27.479 --> 00:01:32.329 the dealer has over 21 they bust then all players that haven't bust effectively win 20 00:01:32.329 --> 00:01:37.210 their is another constraint to the dealer though the dealer cannot stick on less than 17 so in 21 00:01:37.210 --> 00:01:40.930 our version game we will also play the dealer must also stick if they have 17 or 22 00:01:40.930 --> 00:01:45.840 more now once the dealer has finished any player whose total is more than the 23 00:01:45.840 --> 00:01:50.640 dealer wins in any player with a smaller total losses so if the dealer and the player have 24 00:01:50.640 --> 00:01:55.450 the same score then they draw so to keep it simple we gonna player with a single 25 00:01:55.450 --> 00:02:00.060 player but you can add more players yourself once that game is working now in case for anyone who isn't 26 00:02:00.060 --> 00:02:02.289 familiar with Western playing cards 27 00:02:02.289 --> 00:02:07.200 their is 13 cards in each of 4 suits and that makes a total of 52 cards in the pack 28 00:02:07.200 --> 00:02:08.240 effectively 29 00:02:08.240 --> 00:02:12.130 the first 10 cards are 1 to 10 and then there were three face cards called jack 30 00:02:12.130 --> 00:02:17.310 queen and king each also having a value of 10 in this game in other games the queen 31 00:02:17.310 --> 00:02:22.760 will rank higher than the jack and king higher than a queen so the suits are called hearts clubs diamonds and 32 00:02:22.760 --> 00:02:25.890 spades now their is one final complication to make things interesting 33 00:02:26.450 --> 00:02:31.450 the ace which is the card with the value 1 can also have the value 11 then the player can decide 34 00:02:31.450 --> 00:02:36.470 which of the two values it should take so they get the best total in their hand now before 35 00:02:36.470 --> 00:02:39.640 we start I'm gonna show the cards on the screen rather than printing three 36 00:02:39.640 --> 00:02:43.650 of spades queen of hearts so we are gonna draw it so to do that we need some 37 00:02:43.650 --> 00:02:48.450 graphics for the cards and their is a very good collection created by David Bellot and 38 00:02:48.450 --> 00:02:55.310 I'm going bring up this file or bring up the link and you can download it 39 00:02:55.310 --> 00:03:03.930 so I'm gonna open a browser svg-cards.sourceforge.net so it's actually the 40 00:03:03.930 --> 00:03:08.960 image are license under the GNU LGPL Lesser General Public License so that basically 41 00:03:08.960 --> 00:03:12.290 allows you to do what you want with it as long as anyone you give or some of 42 00:03:12.290 --> 00:03:16.710 your program to also has the same rights but keep in mind if your actually uses in 43 00:03:16.710 --> 00:03:20.780 a program that you need to give people the same rights and that rights include 44 00:03:20.780 --> 00:03:24.170 full access to the source code so if you were releasing an app you have to give 45 00:03:24.170 --> 00:03:27.920 that source code access that's what you're giving up for this particular 46 00:03:27.920 --> 00:03:32.480 type of licensing and so basically by giving you the link to download these 47 00:03:32.480 --> 00:03:37.120 images are complying with the LGPL and if you create and distributed a program 48 00:03:37.120 --> 00:03:40.590 using these images read the license carefully and make sure that you 49 00:03:40.590 --> 00:03:45.600 comply and you can see the licensing on the screen so all 52 cards 50 00:03:45.600 --> 00:03:51.050 plus 2 jokers an image of the reversal with a single .svg file possible to 51 00:03:51.050 --> 00:03:54.040 leave them in that format and to extract the individual images as the 52 00:03:54.040 --> 00:03:58.660 program runs their are libraries to help with this but to make a life easier what 53 00:03:58.660 --> 00:04:01.930 I've done is I've extracted all the images and you can download them from the link in the 54 00:04:01.930 --> 00:04:07.190 lecture at the end of the course to get to all the images in .png and .ppm 55 00:04:07.190 --> 00:04:08.050 format 56 00:04:08.050 --> 00:04:12.180 now it's interesting thing to keep in mind with Tkinter that support for png 57 00:04:12.180 --> 00:04:17.690 .png files are added into Tkinter 8.6 so if your using an earlier version which 58 00:04:17.690 --> 00:04:21.870 I am because I'm running on a Mac which has got 8.5 then I need to use 59 00:04:21.870 --> 00:04:26.940 the PPM files so to save you downloading the images that you don't need if you're a slow or metered Internet 60 00:04:26.940 --> 00:04:27.990 connection 61 00:04:27.990 --> 00:04:33.080 the PNG images only are in the cards_png.zip and the PPM images are only 62 00:04:33.080 --> 00:04:38.830 in the cards_ppm.zip so check your version of Tkinter if your running 8.6 63 00:04:38.830 --> 00:04:46.380 might as well use the PNG so here's the files you can see there I've got three here on my in 64 00:04:46.380 --> 00:04:53.940 my folder that I've actually download so PNG if you got tkinter 8.6 otherwise use PPM if you're 8.5 65 00:04:53.940 --> 00:04:56.670 or if your unsure which version your running because that will work for both 66 00:04:56.670 --> 00:05:00.890 alright so that's enough talking we talk quite a bit lets get 67 00:05:00.890 --> 00:05:06.450 into starting some coding so I've created a new project called blackjack and a new 68 00:05:06.450 --> 00:05:11.640 python file called blackjack.py as well but before we enter any code we need to extract the 69 00:05:11.640 --> 00:05:15.760 cards folder from the zip file and place it in the same directory as 70 00:05:15.760 --> 00:05:20.419 blackjack.py now you can put it somewhere else if you prefer but the path 71 00:05:20.419 --> 00:05:23.840 will be using to find the coded images will then need to be changed to reflect 72 00:05:23.840 --> 00:05:27.950 your actual location so I'm gonna grabbed the PPM files 73 00:05:30.150 --> 00:05:38.500 ...I'm gonna unzip those you can see the cards folder and that's all the various cards that are showing so I'm going to take a copy of this 74 00:05:38.500 --> 00:05:46.200 gonna go back and go to the right click here which you have seen me do before click on reveal in finder 75 00:05:46.200 --> 00:05:49.800 to see the location that is going to show me the location of where the blackjack 76 00:05:49.800 --> 00:05:54.530 folder is that I have just created today and go into that 77 00:05:54.530 --> 00:06:00.090 folder and then paste cards into that folder so either you download the PNJs or 78 00:06:00.090 --> 00:06:00.850 the PPM 79 00:06:00.850 --> 00:06:07.350 and unzip the zip version into a cards folder within the blackjack project directory that 80 00:06:07.350 --> 00:06:12.790 we just created so I've done that now ok so we are gonna start the program by importing TKinter 81 00:06:12.790 --> 00:06:16.330 as normal and because this is a card game and we need it 82 00:06:16.330 --> 00:06:19.660 degree of randomness to make an interesting we are gonna also import the random 83 00:06:19.660 --> 00:06:28.940 module so type.... 84 00:06:28.940 --> 00:06:41.730 ..... 85 00:06:41.730 --> 00:07:01.440 ...and lets also setup the screen and frames for 86 00:07:01.440 --> 00:07:02.320 the dealer 87 00:07:02.320 --> 00:07:12.040 player..... 88 00:07:15.110 --> 00:07:24.460 ..... 89 00:07:24.460 --> 00:07:29.680 now the program needs to have space for the dealers and the players cards and some way to 90 00:07:29.680 --> 00:07:34.170 display the result and also buttons for the player to choose to hit or stick so 91 00:07:34.170 --> 00:07:37.060 we can change the captions for the buttons later but while we're 92 00:07:37.060 --> 00:07:40.720 developing the program we are going to use one button to deal cards to the player and 93 00:07:40.720 --> 00:07:44.680 another to deal to the dealer that game is going to look something like this so 94 00:07:44.680 --> 00:07:45.520 gonna load this 95 00:07:45.520 --> 00:07:50.960 up on the screen ok so that's what our screen is gonna look like so the main windows has 96 00:07:50.960 --> 00:07:55.160 got three widgets a label at the top to display the result and two frames one 97 00:07:55.160 --> 00:07:58.620 below the other top most of the two frames is going to contain the 98 00:07:58.620 --> 00:08:03.460 cards and score for the dealer and player and the ones below will hold the buttons as you 99 00:08:03.460 --> 00:08:08.040 can see on the screen their now the card frame will itself contained two other 100 00:08:08.040 --> 00:08:12.020 frames one for the dealer and one for the player as well as labels to hold the 101 00:08:12.020 --> 00:08:16.140 name as well as the score so lets start typing a bit of code in to start building this 102 00:08:16.140 --> 00:08:16.800 interface.... 103 00:08:16.800 --> 00:08:30.270 .... 104 00:08:31.180 --> 00:08:37.040 ..... 105 00:08:38.270 --> 00:08:41.270 ..... 106 00:08:42.860 --> 00:08:59.790 .... 107 00:09:04.940 --> 00:09:29.420 ..... 108 00:09:31.649 --> 00:10:48.329 ..... 109 00:10:48.889 --> 00:11:26.929 ..... 110 00:11:27.649 --> 00:11:41.179 ..... 111 00:11:41.179 --> 00:11:50.319 .... 112 00:11:50.319 --> 00:11:58.029 ..... 113 00:11:58.029 --> 00:12:35.119 ...... 114 00:12:35.119 --> 00:12:38.119 ..... 115 00:12:40.980 --> 00:12:51.019 ..... 116 00:12:51.019 --> 00:12:56.089 ....... 117 00:12:56.089 --> 00:13:04.410 ...so we still got some more typing 118 00:13:04.410 --> 00:13:08.260 to do so what I might do is stop this vide now and in the next video will 119 00:13:08.260 --> 00:13:14.209 continue on we still need to add the embedded frame to hold the card images and just 120 00:13:14.209 --> 00:13:17.250 while I'm talking I'll bring this up on the screen again this is the interface we are trying to build 121 00:13:17.250 --> 00:13:22.470 here so we still need to we have done a lot of these but we still need to finish off and 122 00:13:22.470 --> 00:13:29.209 add the embedded frame to store the card images for the player we need to add the buttons 123 00:13:29.209 --> 00:13:33.510 the frame for the buttons and also buttons itself the dealer button and also 124 00:13:33.510 --> 00:13:37.610 the player button so will continue on with our blackjack game in the next video