Creating a graphical blackjack game

In order to display images within our blackjack game, we need to obtain them first. If you are the arty kind, you can draw them yourself. I am not a great artist so I have chosen to acquire artwork online. The images used in the pictures in this book came from the Open Game Art website, and can be downloaded from https://opengameart.org/content/playing-cards-0.

As usual, we will begin our file with the necessary imports:

import os
import random
import tkinter as tk

This project will need three imports now:

  • os: To access the assets folder
  • random: To shuffle the Deck
  • tkinter: To use graphical features

We begin with a variable that will be used by multiple classes, and so is defined outside of the scope of a class, the assets_folder. We use the os module to construct the full path to our images so that this will work on multiple machines:

assets_folder = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'assets/'))

In this case, the images are stored in a folder named assets, which is in the same directory as our code folder.

We use os.path.dirname to get the directory of the current file, then we use os.path.join to join up .., which goes back one directory, then assets, which is the name of the folder full of our images.

We wrap this all up in os.path.abspath, so that we have the absolute path to this folder, allowing us to use it anywhere.

To clarify the directory structure being used, it looks like this:

- D: Code/
- D: assets/
- F: image1.png
- F: image2.png
- F: ...
- F: blackjack.py

D represents a directory and F represents a file. All of our images are placed into the assets folder.

Now that we have this variable available, we can begin defining the classes which will make up our game.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset