1
00:00:05,240 --> 00:00:09,740
so we are gonna leave geometric functions where we
left in the previous videos but

2
00:00:09,740 --> 00:00:14,980
we are gonna carry on working with Tkinter to produce a very simple card game now there's a couple of

3
00:00:14,980 --> 00:00:19,120
things that we haven't fully explored
with functions yet and as I mentioned

4
00:00:19,120 --> 00:00:23,730
earlier there's another reason why
IntelliJ warns about shadowing variables

5
00:00:23,730 --> 00:00:27,440
in the outer scope this game is gonna
give us a way to look at that in more

6
00:00:27,440 --> 00:00:32,729
detail now blackjack itself is quite a
simple card game the aim is for players to

7
00:00:32,729 --> 00:00:37,440
get a high score or higher total than dealer without going above the score of 21 now if

8
00:00:37,440 --> 00:00:41,380
the total value of the cards in a hand
comes to more than twenty one then the

9
00:00:41,380 --> 00:00:46,310
payers said to have bust or have busted and that is game over now their is variations on the

10
00:00:46,310 --> 00:00:50,220
way the game is played so our version
as I mentioned is gonna be simple version

11
00:00:50,220 --> 00:00:55,520
and will have the following conditions
the dealer deals one card to

12
00:00:55,520 --> 00:01:01,080
each player then to him or herself each player
but not the dealer then gets a second card

13
00:01:01,080 --> 00:01:05,560
the player then decided wether to stick with a
total they have or hit which means to get

14
00:01:05,560 --> 00:01:10,340
another card now a player can hit as many times
as they like but as soon as their total goes above

15
00:01:10,340 --> 00:01:17,390
21 their bust and its basically their goal stops effectively the cards that a player

16
00:01:17,390 --> 00:01:21,740
holds when they go finish this is call their hand once all the players have either

17
00:01:21,740 --> 00:01:26,370
stuck or bust which of the two options
the dealer gets a second card and the dealer

18
00:01:26,370 --> 00:01:30,750
then decides whether to stick or hit
this works the same as for the players and that

19
00:01:30,750 --> 00:01:35,600
the dealer has over 21 they bust
then all players that haven't bust effectively win

20
00:01:35,600 --> 00:01:40,490
their is another constraint to the dealer though the
dealer cannot stick on less than 17 so in

21
00:01:40,490 --> 00:01:44,210
our version game we will also play the dealer must also stick if they have 17 or

22
00:01:44,210 --> 00:01:49,120
more now once the dealer has finished any
player whose total is more than the

23
00:01:49,120 --> 00:01:53,920
dealer wins in any player with a smaller
total losses so if the dealer and the player have

24
00:01:53,920 --> 00:01:58,730
the same score then they draw so to keep it
simple we gonna player with a single

25
00:01:58,730 --> 00:02:03,340
player but you can add more players yourself once that game is working now in case for anyone who isn't

26
00:02:03,340 --> 00:02:05,560
familiar with Western playing cards

27
00:02:05,560 --> 00:02:10,479
their is 13 cards in each of 4 suits and that makes a
total of 52 cards in the pack

28
00:02:10,479 --> 00:02:11,520
effectively

29
00:02:11,520 --> 00:02:15,410
the first 10 cards are 1 to 10 and
then there were three face cards called jack

30
00:02:15,410 --> 00:02:20,590
queen and king each also having a value of 10 in this game in other games the queen

31
00:02:20,590 --> 00:02:26,040
will rank higher than the jack and king higher than a queen so the suits are called hearts clubs diamonds and

32
00:02:26,040 --> 00:02:29,170
spades now their is one final complication to make
things interesting

33
00:02:29,730 --> 00:02:34,730
the ace which is the card with the value 1 can
also have the value 11 then the player can decide

34
00:02:34,730 --> 00:02:39,750
which of the two values it should take so they get the best total in their hand now before

35
00:02:39,750 --> 00:02:42,920
we start I'm gonna show the cards on the
screen rather than printing three

36
00:02:42,920 --> 00:02:46,930
of spades queen of hearts so we are gonna draw it so to do that we need some

37
00:02:46,930 --> 00:02:51,730
graphics for the cards and their is a very good
collection created by David Bellot and

38
00:02:51,730 --> 00:02:58,590
I'm going bring up this file or bring
up the link and you can download it

39
00:02:58,590 --> 00:03:07,210
so I'm gonna open a browser svg-cards.sourceforge.net so it's actually the

40
00:03:07,210 --> 00:03:12,240
image are license under the GNU LGPL Lesser
General Public License so that basically

41
00:03:12,240 --> 00:03:15,570
allows you to do what you want with it
as long as anyone you give or some of

42
00:03:15,570 --> 00:03:19,990
your program to also has the same rights
but keep in mind if your actually uses in

43
00:03:19,990 --> 00:03:24,060
a program that you need to give people
the same rights and that rights include

44
00:03:24,060 --> 00:03:27,450
full access to the source code so if you
were releasing an app you have to give

45
00:03:27,450 --> 00:03:31,200
that source code access that's what
you're giving up for this particular

46
00:03:31,200 --> 00:03:35,760
type of licensing and so basically by
giving you the link to download these

47
00:03:35,760 --> 00:03:40,400
images are complying with the LGPL
and if you create and distributed a program

48
00:03:40,400 --> 00:03:43,870
using these images read the license
carefully and make sure that you

49
00:03:43,870 --> 00:03:48,880
comply and you can see the licensing
on the screen so all 52 cards

50
00:03:48,880 --> 00:03:54,330
plus 2 jokers an image of the reversal with
a single .svg file possible to

51
00:03:54,330 --> 00:03:57,320
leave them in that format and to
extract the individual images as the

52
00:03:57,320 --> 00:04:01,940
program runs their are libraries to help with
this but to make a life easier what

53
00:04:01,940 --> 00:04:05,210
I've done is I've extracted all the images and
you can download them from the link in the

54
00:04:05,210 --> 00:04:10,470
lecture at the end of the course to get
to all the images in .png and .ppm

55
00:04:10,470 --> 00:04:11,330
format

56
00:04:11,330 --> 00:04:15,460
now it's interesting thing to keep in
mind with Tkinter that support for png

57
00:04:15,460 --> 00:04:20,970
.png files are added into Tkinter 8.6 so if your using an earlier version which

58
00:04:20,970 --> 00:04:25,150
I am because I'm running on a Mac which
has got 8.5 then I need to use

59
00:04:25,150 --> 00:04:30,220
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:30,220 --> 00:04:31,270
connection

61
00:04:31,270 --> 00:04:36,360
the PNG images only are in the cards_png.zip and the PPM images are only

62
00:04:36,360 --> 00:04:42,110
in the cards_ppm.zip so check
your version of Tkinter if your running 8.6

63
00:04:42,110 --> 00:04:49,660
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:49,660 --> 00:04:57,220
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:57,220 --> 00:04:59,950
or if your unsure which version your
running because that will work for both

66
00:04:59,950 --> 00:05:04,170
alright so that's enough talking we
talk quite a bit lets get

67
00:05:04,170 --> 00:05:09,730
into starting some coding so I've created
a new project called blackjack and a new

68
00:05:09,730 --> 00:05:14,920
python file called blackjack.py as well but before
we enter any code we need to extract the

69
00:05:14,920 --> 00:05:19,040
cards folder from the zip file
and place it in the same directory as

70
00:05:19,040 --> 00:05:23,690
blackjack.py now you can put it
somewhere else if you prefer but the path

71
00:05:23,690 --> 00:05:27,120
will be using to find the coded images
will then need to be changed to reflect

72
00:05:27,120 --> 00:05:31,230
your actual location so I'm gonna
grabbed the PPM files

73
00:05:33,430 --> 00:05:41,780
...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:41,780 --> 00:05:49,480
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:49,480 --> 00:05:53,080
to see the location that is going to show me
the location of where the blackjack

76
00:05:53,080 --> 00:05:57,810
folder is that I have just created today and go into that

77
00:05:57,810 --> 00:06:03,370
folder and then paste cards into that
folder so either you download the PNJs or

78
00:06:03,370 --> 00:06:04,130
the PPM

79
00:06:04,130 --> 00:06:10,630
and unzip the zip version into a cards folder within the
blackjack project directory that

80
00:06:10,630 --> 00:06:16,070
we just created so I've done that now ok so we are
gonna start the program by importing TKinter

81
00:06:16,070 --> 00:06:19,610
as normal and because this is a card
game and we need it

82
00:06:19,610 --> 00:06:22,940
degree of randomness to make an
interesting we are gonna also import the random

83
00:06:22,940 --> 00:06:32,220
module so type....

84
00:06:32,220 --> 00:06:45,010
.....

85
00:06:45,010 --> 00:07:04,720
...and lets also setup the screen and frames for

86
00:07:04,720 --> 00:07:05,600
the dealer

87
00:07:05,600 --> 00:07:15,320
player.....

88
00:07:18,390 --> 00:07:27,740
.....

89
00:07:27,740 --> 00:07:32,960
now the program needs to have space for the
dealers and the players cards and some way to

90
00:07:32,960 --> 00:07:37,450
display the result and also buttons for
the player to choose to hit or stick so

91
00:07:37,450 --> 00:07:40,340
we can change the captions for the
buttons later but while we're

92
00:07:40,340 --> 00:07:44,000
developing the program we are going to use one
button to deal cards to the player and

93
00:07:44,000 --> 00:07:47,960
another to deal to the dealer that game is
going to look something like this so

94
00:07:47,960 --> 00:07:48,800
gonna load this

95
00:07:48,800 --> 00:07:54,240
up on the screen ok so that's what our
screen is gonna look like so the main windows has

96
00:07:54,240 --> 00:07:58,440
got three widgets a label at the top
to display the result and two frames one

97
00:07:58,440 --> 00:08:01,900
below the other top most of the two
frames is going to contain the

98
00:08:01,900 --> 00:08:06,740
cards and score for the dealer and player and the ones below will hold the buttons as you

99
00:08:06,740 --> 00:08:11,320
can see on the screen their now the card
frame will itself contained two other

100
00:08:11,320 --> 00:08:15,300
frames one for the dealer and one for
the player as well as labels to hold the

101
00:08:15,300 --> 00:08:19,420
name as well as the score so lets start
typing a bit of code in to start building this

102
00:08:19,420 --> 00:08:20,080
interface....

103
00:08:20,080 --> 00:08:33,549
....

104
00:08:34,460 --> 00:08:40,320
.....

105
00:08:41,549 --> 00:08:44,550
.....

106
00:08:46,140 --> 00:09:03,070
....

107
00:09:08,220 --> 00:09:32,700
.....

108
00:09:34,920 --> 00:10:51,600
.....

109
00:10:52,160 --> 00:11:30,200
.....

110
00:11:30,920 --> 00:11:44,450
.....

111
00:11:44,450 --> 00:11:53,590
....

112
00:11:53,590 --> 00:12:01,300
.....

113
00:12:01,300 --> 00:12:38,390
......

114
00:12:38,390 --> 00:12:41,390
.....

115
00:12:44,260 --> 00:12:54,290
.....

116
00:12:54,290 --> 00:12:59,360
.......

117
00:12:59,360 --> 00:13:07,690
...so we still got some more typing

118
00:13:07,690 --> 00:13:11,540
to do so what I might do is stop this
vide now and in the next video will

119
00:13:11,540 --> 00:13:17,480
continue on we still need to add the embedded
frame to hold the card images and just

120
00:13:17,480 --> 00:13:20,530
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:20,530 --> 00:13:25,750
here so we still need to we have done a lot of
these but we still need to finish off and

122
00:13:25,750 --> 00:13:32,480
add the embedded frame to store the card images for
the player we need to add the buttons

123
00:13:32,480 --> 00:13:36,790
the frame for the buttons and also buttons itself the dealer button and also

124
00:13:36,790 --> 00:13:40,890
the player button so will continue on with our blackjack game in the next video

