Here's an Arduino project to create an electronic card deck. It reduces the deck each time you take a card so you won't have the same card reappearing. It is customizable for any game you want to create, and it's easy to build.
==================================================================================
I made the suits Tan, Brown, Olive Green, and Black. You can change them to whatever you want. The cards are also numbered 1 to 10 and have three generals to separate them (Jack, Queen, King). All of the Generals count as ten points.
The images (
,
,
,
) should be around 150 pixels wide by 200 pixels tall to match the code's resizing dimensions.
If the images are larger or smaller, the script will resize them to 150x200 pixels, but starting with images of similar dimensions will ensure they display well and maintain quality.
Suggested Image Dimensions:
============================================================================
INSTRUCTIONS ARE BELOW, PAST THIS INTO THE EDITOR> DON'T DELETE THE INSTRUCTIONS. They won't show when there's a # in front of the sentence.
=============================================================================
import random
import tkinter as tk
from PIL import Image, ImageTk
import random
import tkinter as tk
from PIL import Image, ImageTk
# Step-by-Step Instructions for a non-coders to Build This Project
# Parts List:
# 1. Computer with Python installed
# 2. Tkinter library (comes with Python)
# 3. PIL (Pillow) library for image handling (install with: pip install Pillow)
# 4. Images of cards saved in an 'images' folder:
# - tan.png
# - brown.png
# - olive_green.png
# - black.png
# Assembly Instructions for the Coding Project
# Step 1: Install Python and Libraries
# - First, install Python from the official website (https://www.python.org/downloads/).
# - Once Python is installed, open the Command Prompt (Windows) or Terminal (Mac/Linux).
# - Install the PIL (Pillow) library by typing the following command and pressing Enter:
# pip install Pillow
# Step 2: Set Up the Folder Structure
# - Create a new folder on your computer where you will save your game files. You can name it something like 'CardDrawingGame'.
# - Inside this folder, create another folder named 'images'.
# - Save the images of the cards inside the 'images' folder. The images should have the following names:
# - tan.png
# - brown.png
# - olive_green.png
# - black.png
# Step 3: Write the Python Code
# - Open a text editor like Notepad (Windows) or VS Code (recommended for beginners).
# - Copy and paste the Python code provided below into the text editor.
# - Save the file with the name 'card_game.py' inside the 'CardDrawingGame' folder you created earlier.
# Step 4: Run the Card Drawing Game
# - Open the Command Prompt or Terminal on your computer.
# - Navigate to the 'CardDrawingGame' folder using the 'cd' command. For example, if the folder is on your desktop, type:
# cd Desktop/CardDrawingGame
# - Once you are inside the 'CardDrawingGame' folder, run the Python code by typing:
# python card_game.py
# - You should see a window pop up that lets you draw cards by pressing the button.
End of instructions. Code is below
# Python Code for the Card Drawing Game
def create_deck():
suits = ['Tan', 'Brown', 'Olive Green', 'Black']
values = [str(i) for i in range(1, 11)] + ['Maj General', 'Lt General', 'General']
deck = [f'{value} of {suit}' for suit in suits for value in values]
deck.append('Ambush 1')
deck.append('Ambush 2')
return deck
def draw_card(deck):
if not deck:
return None
card = random.choice(deck)
deck.remove(card)
return card
def show_card(card):
if card:
card_label.config(text=card)
display_card_image(card)
else:
card_label.config(text="The deck is empty! Turn off and on to reshuffle the new deck.")
draw_button.config(state=tk.DISABLED)
def draw():
card = draw_card(deck)
show_card(card)
def display_card_image(card):
# Extract suit from card description
suit = card.split(' ')[-1].lower().replace(" ", "_")
image_path = f"images/{suit}.png"
try:
card_image = Image.open(image_path)
card_image = card_image.resize((150, 200), Image.ANTIALIAS)
card_image_tk = ImageTk.PhotoImage(card_image)
card_image_label.config(image=card_image_tk)
card_image_label.image = card_image_tk
except FileNotFoundError:
card_image_label.config(image='')
card_image_label.image = None
# Create the deck and initialize the GUI
deck = create_deck()
root = tk.Tk()
root.title("Card Drawing Game")
card_label = tk.Label(root, text="Press the button to draw a card.", font=("Helvetica", 16))
card_label.pack(pady=20)
card_image_label = tk.Label(root)
card_image_label.pack(pady=10)
draw_button = tk.Button(root, text="Draw a Card", command=draw, font=("Helvetica", 14))
draw_button.pack(pady=10)
root.mainloop()
==================================================================================
I made the suits Tan, Brown, Olive Green, and Black. You can change them to whatever you want. The cards are also numbered 1 to 10 and have three generals to separate them (Jack, Queen, King). All of the Generals count as ten points.
The images (
Code:
tan.pngCode:
brown.pngCode:
olive_green.pngCode:
black.pngIf the images are larger or smaller, the script will resize them to 150x200 pixels, but starting with images of similar dimensions will ensure they display well and maintain quality.
Suggested Image Dimensions:
- Width: 150 pixels
- Height: 200 pixels
============================================================================
INSTRUCTIONS ARE BELOW, PAST THIS INTO THE EDITOR> DON'T DELETE THE INSTRUCTIONS. They won't show when there's a # in front of the sentence.
=============================================================================
import random
import tkinter as tk
from PIL import Image, ImageTk
import random
import tkinter as tk
from PIL import Image, ImageTk
# Step-by-Step Instructions for a non-coders to Build This Project
# Parts List:
# 1. Computer with Python installed
# 2. Tkinter library (comes with Python)
# 3. PIL (Pillow) library for image handling (install with: pip install Pillow)
# 4. Images of cards saved in an 'images' folder:
# - tan.png
# - brown.png
# - olive_green.png
# - black.png
# Assembly Instructions for the Coding Project
# Step 1: Install Python and Libraries
# - First, install Python from the official website (https://www.python.org/downloads/).
# - Once Python is installed, open the Command Prompt (Windows) or Terminal (Mac/Linux).
# - Install the PIL (Pillow) library by typing the following command and pressing Enter:
# pip install Pillow
# Step 2: Set Up the Folder Structure
# - Create a new folder on your computer where you will save your game files. You can name it something like 'CardDrawingGame'.
# - Inside this folder, create another folder named 'images'.
# - Save the images of the cards inside the 'images' folder. The images should have the following names:
# - tan.png
# - brown.png
# - olive_green.png
# - black.png
# Step 3: Write the Python Code
# - Open a text editor like Notepad (Windows) or VS Code (recommended for beginners).
# - Copy and paste the Python code provided below into the text editor.
# - Save the file with the name 'card_game.py' inside the 'CardDrawingGame' folder you created earlier.
# Step 4: Run the Card Drawing Game
# - Open the Command Prompt or Terminal on your computer.
# - Navigate to the 'CardDrawingGame' folder using the 'cd' command. For example, if the folder is on your desktop, type:
# cd Desktop/CardDrawingGame
# - Once you are inside the 'CardDrawingGame' folder, run the Python code by typing:
# python card_game.py
# - You should see a window pop up that lets you draw cards by pressing the button.
End of instructions. Code is below
# Python Code for the Card Drawing Game
def create_deck():
suits = ['Tan', 'Brown', 'Olive Green', 'Black']
values = [str(i) for i in range(1, 11)] + ['Maj General', 'Lt General', 'General']
deck = [f'{value} of {suit}' for suit in suits for value in values]
deck.append('Ambush 1')
deck.append('Ambush 2')
return deck
def draw_card(deck):
if not deck:
return None
card = random.choice(deck)
deck.remove(card)
return card
def show_card(card):
if card:
card_label.config(text=card)
display_card_image(card)
else:
card_label.config(text="The deck is empty! Turn off and on to reshuffle the new deck.")
draw_button.config(state=tk.DISABLED)
def draw():
card = draw_card(deck)
show_card(card)
def display_card_image(card):
# Extract suit from card description
suit = card.split(' ')[-1].lower().replace(" ", "_")
image_path = f"images/{suit}.png"
try:
card_image = Image.open(image_path)
card_image = card_image.resize((150, 200), Image.ANTIALIAS)
card_image_tk = ImageTk.PhotoImage(card_image)
card_image_label.config(image=card_image_tk)
card_image_label.image = card_image_tk
except FileNotFoundError:
card_image_label.config(image='')
card_image_label.image = None
# Create the deck and initialize the GUI
deck = create_deck()
root = tk.Tk()
root.title("Card Drawing Game")
card_label = tk.Label(root, text="Press the button to draw a card.", font=("Helvetica", 16))
card_label.pack(pady=20)
card_image_label = tk.Label(root)
card_image_label.pack(pady=10)
draw_button = tk.Button(root, text="Draw a Card", command=draw, font=("Helvetica", 14))
draw_button.pack(pady=10)
root.mainloop()

