Bataille Navale

Mon premier jeu en Python 🎮

Qu'est-ce que c'est ?

Un jeu simple et amusant en Python où tu dois trouver des bateaux cachés en cliquant sur des cases. Parfait pour apprendre à programmer !

Les règles

💻 Le code

import tkinter as tk
import random
grille_taille = 10
bateaux = []
def creer_fenetre():

    fenetre = tk.Tk()
    fenetre.title("Bataille Navale")
    titre = tk.Label(fenetre, text="Clique pour trouver les bateaux!")
    titre.pack()    
    for ligne in range(grille_taille):
        for colonne in range(grille_taille):
            bouton = tk.Button(fenetre, text=" ", width=4, height=2)
            bouton.grid(row=ligne, column=colonne)
            bouton.config(command=lambda b=bouton, l=ligne, c=colonne: clic(b, l, c))
    
    fenetre.mainloop()

def placer_bateaux():
    for i in range(5):
        ligne = random.randint(0, 9)
        colonne = random.randint(0, 9)
        bateaux.append([ligne, colonne])

def clic(bouton, ligne, colonne):
    # Quand on clique sur un bouton
    if [ligne, colonne] in bateaux:
        bouton.config(text="X", bg="red")
        print("Touché!")
    else:
        bouton.config(text="O", bg="blue")
        print("Raté!")
    
    bouton.config(state="disabled")
placer_bateaux()
creer_fenetre()

Comment ça marche ?