blackjack.py

Created by arthurmichel-scolaire

Created on January 05, 2025

4.29 KB

Affrontez le croupier dans ce jeu de cartes classique ! Essayez d’obtenir une main proche de 21 sans dépasser ce total.


from ion import keydown  
from kandinsky import *  
from random import randint  
from time import sleep  

# Cartes et Valeurs
cartes = ['A', 'R', 'D', 'V', '10', '9', '8', '7', '6', '5', '4', '3', '2']
valeurs = {'A': 11, 'R': 10, 'D': 10, 'V': 10, '10': 10, '9': 9, '8': 8, '7': 7,
          '6': 6, '5': 5, '4': 4, '3': 3, '2': 2}

# Fonction mélange
def shuffle(liste):
   n = len(liste)
   for i in range(n):
       j = randint(0, n - 1)
       liste[i], liste[j] = liste[j], liste[i]

# Mélange des cartes
paquet = cartes * 4
shuffle(paquet)

# Variables
joueur = []
croupier = []
score_joueur = 0
score_croupier = 0
tour_fini = False

# Affichage des cartes
x_joueur = 120  
x_croupier = 120  
espacement_cartes = 40  

# Fonction tapis 
def dessiner_tapis():
   fill_rect(0, 0, 320, 240, (0, 128, 0))  # Fond vert 
   draw_string("BLACKJACK", 120, 10, "yellow", (0, 128, 0))  # Titre centré
   draw_string("BANQUE", 130, 50, "white", (0, 128, 0))  # Banque centré
   draw_string("JOUEUR", 130, 140, "white", (0, 128, 0))  # Joueur centré

# Dessiner une carte + contour blanc + texte centré
def afficher_carte(x, y, texte, couleur):
   fill_rect(x - 2, y - 2, 34, 49, "white")
   fill_rect(x, y, 30, 45, couleur)
   largeur_texte = len(texte) * 6  
   position_texte_x = x + (30 - largeur_texte) // 2
   position_texte_y = y + (45 - 12) // 2  
   draw_string(texte, position_texte_x, position_texte_y, "white", couleur)

# Score
def calculer_score(main):
   score = sum(valeurs[carte] for carte in main)
   as_count = main.count('A')
   while score > 21 and as_count > 0:
       score -= 10
       as_count -= 1
   return score

# Afficher cartes + scores
def afficher_mains():
   global x_croupier, x_joueur

   # Cartes de la banque
   x_croupier = 120
   for i, carte in enumerate(croupier):
       if i == 0 and not tour_fini:
           afficher_carte(x_croupier, 70, carte, (0, 0, 255))  
       elif i > 0 and not tour_fini:
           afficher_carte(x_croupier, 70, "?", (0, 0, 0))  
       else:
           afficher_carte(x_croupier, 70, carte, (0, 0, 255))  
       x_croupier += espacement_cartes  

   # Afficher le score du croupier
   if not tour_fini:
       score_visible = valeurs[croupier[0]]  
       draw_string("Score: " + str(score_visible), 10, 50, "white", (0, 128, 0))  
   else:
       draw_string("Score: " + str(score_croupier), 10, 50, "white", (0, 128, 0)) 

   # Afficher le score du joueur 
   draw_string("Score: " + str(score_joueur), 10, 140, "white", (0, 128, 0))  
   
   # Cartes du joueur
   x_joueur = 120
   for carte in joueur:
       afficher_carte(x_joueur, 160, carte, (255, 0, 0))  
       x_joueur += espacement_cartes  

# Tour du joueur
def joueur_tour():
   global score_joueur, x_joueur
   joueur.append(paquet.pop())
   score_joueur = calculer_score(joueur)
   afficher_carte(x_joueur, 160, joueur[-1], (255, 0, 0))  
   x_joueur += espacement_cartes  
   draw_string("Score: " + str(score_joueur), 10, 140, "white", (0, 128, 0))  

   if score_joueur > 21:
       draw_string("Vous avez perdu !", 100, 120, "red", (0, 128, 0))
       return True
   return False

# Tour du croupier
def croupier_tour():
   global score_croupier, tour_fini
   tour_fini = True
   afficher_mains()
   while score_croupier < 17:
       sleep(1)
       croupier.append(paquet.pop())
       score_croupier = calculer_score(croupier)
       afficher_mains()
   if score_croupier > 21:
       draw_string("Vous gagnez !", 100, 120, "green", (0, 128, 0))
   elif score_croupier > score_joueur:
       draw_string("Le croupier gagne !", 80, 120, "red", (0, 128, 0))
   elif score_croupier < score_joueur:
       draw_string("Vous gagnez !", 100, 120, "green", (0, 128, 0))
   else:
       draw_string("Égalité !", 120, 120, "orange", (0, 128, 0))

# Distribution dés le debut 
joueur.append(paquet.pop())
joueur.append(paquet.pop())
croupier.append(paquet.pop())
croupier.append(paquet.pop())
score_joueur = calculer_score(joueur)
score_croupier = calculer_score(croupier)

# Affichage dés le debut 
dessiner_tapis()
afficher_mains()

# Boucle de jeu
while True:
   if keydown(3):  # Flèche droite pour tirer un carte 
       if joueur_tour():  # Tirer une carte
           break
       sleep(0.3)  # Pause afin d'éviter les doubles tirages
   elif keydown(52):  # Touche OK pour passer au croupier
       croupier_tour()
       break

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.