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.