0% found this document useful (0 votes)
5 views10 pages

Untitled Documenta

The document is a Python script that implements a simple Blackjack game using the Turtle graphics library. It includes functionalities for dealing cards, calculating scores, handling player and dealer actions, and displaying results on the screen. The game allows players to choose whether to hit or stand and determines the outcome based on the rules of Blackjack.

Uploaded by

logan.mattie
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views10 pages

Untitled Documenta

The document is a Python script that implements a simple Blackjack game using the Turtle graphics library. It includes functionalities for dealing cards, calculating scores, handling player and dealer actions, and displaying results on the screen. The game allows players to choose whether to hit or stand and determines the outcome based on the rules of Blackjack.

Uploaded by

logan.mattie
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

import random as rand

import turtle as trtl


import time as time
# Turtle setup
wn = trtl.Screen()
writer = trtl.Turtle()
dealer_writer = trtl.Turtle()
restart = trtl.Turtle()
hit = trtl.Turtle()
# Individual turtle attrivutes (hit)
hit.penup()
hit.hideturtle()
hit.pencolor("pink")
# Individual turtle attributes (restart)
restart.pencolor("white")
restart.pendown()
restart.hideturtle()
# Individual turtle attributes (dealer_writer)
dealer_writer.pencolor("red")
dealer_writer.hideturtle()
dealer_writer.penup()
dealer_writer.goto(50, 0)
dealer_writer.write("Dealer Cards:", font=("Arial", 50, "bold"))
# Individual turtle attributes (writer)
writer.pencolor("white")
writer.hideturtle()
writer.penup()
writer.goto(-400,0)
writer.write("Your Cards:", font=("Arial", 50, "bold"))
writer.right(45)
writer.forward(100)
writer.right(45)
# Individual turtle attributes (wn)
wn.tracer(0, 0)
wn.setup(800, 800)
wn.bgpic("Blackjackbg (1).gif")
wn.update()
'''wn.addshape("Blackjackbg.gif")
wn.bgpic("Blackjackbg.gif")'''
# Card Values
face_card_values = {"J": 10, "Q": 10, "K": 10}
Cards = ["A", 2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K", "A", 2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K", "A",
2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K", "A", 2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K"]
# Values to be changed and modified
ace_choice = 0
dealer_score = []
dealer_ace_random = [1, 11]
score = []
pick = 0
total = 0
y = 200

'''wn.setup(width = 266, height = 148)'''


# Functions ----------------------------------------
# Allow user to pick the value of an ace
def Ace():
global pick
while True:
if pick == "A":
ace_choice = input("You got an Ace! Do you want it to count as 1 or 11?")
if ace_choice in ["1", "11"]:
pick = int(ace_choice)
break
else:
# If user did not type 1 or 11, restart this code
print("That isn't a valid answer, try again.")
# If Dealer gets an ace
def dealer_ace():
global pick, dealer_score
dealer_total = sum(dealer_score)
# This segment makes the dealer think about what
# Is happening and pick the best value for the situation
if dealer_total + 11 == 21:
pick = 11
elif dealer_total + 11 == 11:
pick = 11
elif dealer_total + 1 == 21:
pick = 1
elif dealer_total + 11 > 21:
pick = 1
else:
pick = rand.choice(dealer_ace_random)
# Helps face cards be turned into card values
def King():
global pick
if pick in ["K", "Q", "J"]:
# Face cards = 10
pick = 10
# Gives the player their 'hidden card'
def hidden_card():
global pick
# picks the hidden card from deck
pick = rand.choice(Cards)
print("Hidden Card:", pick)
'''write_card()
wn.update()'''
# Removes hidden card from deck
Cards.remove(pick)
write_card()
# Writes the choice you have for when an ace shows up
if pick == "A":
writer.forward(300)
writer.left(90)
writer.backward(35)
writer.write("You got an Ace! Do you want it to count as 1 or 11? (Type)", font=("Arial", 25,
"bold"))
writer.forward(35)
writer.right(90)
writer.backward(300)
wn.update()
# Runs Ace code to actually give the choice
if pick == "A":
Ace()
King()
# Adds to the score
score.append(int(pick))
write_total()
wn.update()
# Gives a new card to the player
def new_card():
global pick
global y
# Picks the new card
pick = rand.choice(Cards)
print("Your Card:", pick)
# removes the card from the deck
Cards.remove(pick)
writer.forward(50)
'''write_card()
wn.update()'''
# writes the card on the screen
write_card()
# Writes choice options for ace
if pick == "A":
writer.forward(y)
writer.left(90)
writer.backward(35)
writer.write("You got an Ace! Do you want it to count as 1 or 11? (Type)", font=("Arial", 25,
"bold"))

writer.forward(35)
writer.right(90)
writer.backward(y)
y = y - 50
wn.update()
# Does the stuff for actual decision for ace
if pick == "A":
Ace()

# Sets up Face cards


King()
# Adds to the score
score.append(int(pick))
write_total()
wn.update()

# Gives the dealer a hidden card


def dealer_hidden_card():
global pick
# Chooses a card
pick = rand.choice(Cards)
# Writes the fact that the dealer has a hidden card
print("Dealer has a hidden card")
dealer_writer.right(90)
dealer_writer.forward(70)
dealer_writer.write("Hidden Card", font=("Arial", 50, "bold"))
wn.update()
# Removes card from deck
Cards.remove(pick)
# runs ace decision if there is an ace
if pick == "A":
dealer_ace()
# Sets up face cards
King()
# Adds to the dealers score
dealer_score.append(int(pick))
# Gets a new card for the dealer
def dealer_new_card():
global pick
# Chooses the card for the deck
pick = rand.choice(Cards)
print("Dealer card:", pick)
Cards.remove(pick)
dealer_writer.forward(50)
# Writes the dealer's card
dealer_write_card()
wn.update()
# Runs ace function if an ace is given
if pick == "A":
dealer_ace()
# Assigns face cards values
King()
# adds to dealer score
dealer_score.append(int(pick))
# Happens after the player chooses whether to go again, or end
def hit_or_stand(total):
global get_card
# Sets up loop for hitting
get_card = input("hit or stand?")
# shows choices for the if statement
if get_card == "hit" and total < 21:
# grabs a new card
new_card()
total = sum(score)
print("Total:", total)
elif get_card == "stand":
#ends game
total = sum(score)
print("Final Total:", total)

'''def dealer_ai():
dealer_total = sum(dealer_score)
if dealer_total < 17:
dealer_new_card()
dealer_total = sum(dealer_score)'''
# Lets the dealer choose whether to take again or stop
def dealer_ai():
dealer_total = sum(dealer_score)
for num in range(1):
if dealer_total < 17:
# Gives the dealer a new card
dealer_new_card()
dealer_total = sum(dealer_score)
return dealer_total
def hit_or_stand2():
# I dont even think this function does anything fr
global get_card
if get_card == "hit" and total < 21:
hit_or_stand(total)
def end_game(total, score):
# Builds the end screen
writer.pencolor("black")
writer.fillcolor("black")
writer.speed(1)
writer.goto(-400,400)
writer.begin_fill()
writer.pendown()
writer.goto(400, 400)
writer.goto(400, -400)
writer.goto(-400, -400)
writer.end_fill()
# sets up for writing end
writer.pencolor("khaki")
global dealer_score
total = sum(score)
'''if total == 21 and dealer_total > 21 or total == 21 and dealer_total < 21:
writer.write("BlackJack!", font=("Arial", 75, "bold"))
writer.left(80)
writer.backward(75)
writer.right(80)'''
Final_total = sum(score)
dealer_final_total = sum(dealer_score)
writer.pencolor("khaki")
writer.right(60)
writer.goto(-400, 0)
# Puts its total on board
writer.write("Dealer Final Total:", dealer_final_total, font=("Arial", 75, "bold"))
writer.left(160)
writer.forward(30)
writer.write(dealer_final_total, font=("Arial", 65, "bold"))
writer.goto(-400, 0)
# Writes if there was a blackjack
if total == 21 and dealer_total > 21 or total == 21 and dealer_total < 21:
writer.left(80)
writer.backward(75)
writer.right(80)
writer.pencolor("white")
writer.write("BlackJack!", font=("Arial", 75, "bold"))
writer.pencolor("khaki")
# Writes if there was a dealer bust
if dealer_total > 21 and Final_total < 22:
writer.left(80)
writer.backward(75)
writer.right(80)
writer.write("Dealer busted", font=("Arial", 65, "bold"))
# Writes if there was a dealer blackjack
if dealer_final_total == 21 and Final_total < 21:
writer.left(80)
writer.backward(75)
writer.right(80)
writer.write("Dealer Blackjack", font=("Arial", 65, "bold"))
# Writes if there you won
if dealer_final_total < Final_total and Final_total < 22:
writer.goto(-100, -200)
writer.write("You Win",font=("Arial", 65, "bold"))
# Writes if you busted
if Final_total > 21 and dealer_final_total > 22:
writer.left(80)
writer.backward(75)
writer.right(80)
writer.write("You Busted", font=("Arial", 65, "bold"))
# Writes if you both busted
if Final_total > 21 and dealer_final_total > 21:
writer.left(80)
writer.backward(75)
writer.right(80)
writer.write("Both busted, no winner", font=("Arial", 65, "bold"))
# Writes if the Dealer won
if dealer_final_total > Final_total and dealer_final_total < 22:
writer.left(80)
writer.backward(75)
writer.right(80)
writer.write("Dealer Won", font=("Arial", 65, "bold"))
# Writes if the dealer busted, but you won
if dealer_final_total > 21 and Final_total < 22:
writer.left(80)
writer.backward(75)
writer.right(80)
writer.write("Dealer busted, you win", font=("Arial", 65, "bold"))
# Writes if you busted, but the dealer won
if dealer_final_total < 22 and Final_total > 21:
writer.left(80)
writer.backward(75)
writer.right(80)
writer.write("You busted, dealer wins", font=("Arial", 65, "bold"))
# Writes if there was a tie
if dealer_final_total == 21 and Final_total == 21:
writer.left(80)
writer.backward(75)
writer.right(80)
writer.write("Tie", font=("Arial", 65, "bold"))
# Writes if there was a tie
if dealer_final_total == Final_total and Final_total < 21:
writer.left(80)
writer.backward(75)
writer.right(80)
writer.write("Tie", font=("Arial", 65, "bold"))
# The function for actually writing the Card
def write_card():
global pick
global score
global total
# sets total
total = sum(score)
writer.write(pick, font=("Arial", 50, "bold"))
# The Function for actually writing the total
def write_total():
global pick
global score
global total
# sets total
total = sum(score)
writer.left(90)
writer.forward(75)
# Writes total
writer.write("Total:", font=("Arial", 50, "bold"))
writer.forward(150)
writer.write(total, font=("Arial", 50, "bold"))
writer.backward(225)
writer.right(90)
# goes back to original spot
# Writes dealers code
def dealer_write_card():
global pick
dealer_writer.write(pick, font=("Arial", 50, "bold"))
# Im not sure if this is gonna be in the code yet
def new_game():
restart.pendown()
restart.goto(0,-100)
restart.pensize(45)
restart.forward(30)
# Writes hit or stand
def written_hit():
hit.goto(-100, -300)
hit.write("Hit or Stand? (Type)", font=("Arial", 50, "bold"))
# Game Function ----------------------------
# Gives you your hidden and new card
written_hit()
hidden_card()
new_card()
# Counts your first totals
total = sum(score)
print("Total:", total)
# Gives dealer hidden and new cards
dealer_hidden_card()
dealer_new_card()
global dealer_total
# sets dealers total
dealer_total = sum(dealer_score)
global get_card
# Fixing alternating dealer and your card picks
# Sets values to be used in the hit_or_stand function
if total < 21:
get_card = "hit"
else:
get_card = "stand"
# uses the dealer AI to choose what it does
while dealer_total < 17 and get_card != "stand" and total < 21:
hit_or_stand(total)
# Utilizes dealer ai
dealer_ai()
# Takes dealers total
dealer_total = sum(dealer_score)
total = sum(score)
if total > 21:
# leaves loop if the total goes above 21
break
# makes dealer continue taking more cards until it gets 17+
while dealer_total < 17:
dealer_ai()
# Takes dealers total
dealer_total = sum(dealer_score)
# Lets you take more cards
while total < 21 and get_card != "stand":
hit_or_stand(total)
# Gets total
total = sum(score)
# Done with that
# gets totals one more time for dealer
dealer_final_total = sum(dealer_score)
# gives results
end_game(total, score)
new_game()
wn.mainloop()

You might also like