from turtle import * import sqlite3 #---------------------------------------------------------------- # DEFINITIONS DES FONCTIONS #---------------------------------------------------------------- s = [[0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0],] class Bateau: def __init__(self, size_x, size_y, number, position_x, position_y, color='red'): self.size_x = size_x self.size_y = size_y self.number = number self.position_x = position_x self.position_y = position_y self.color = color def dessin_boat(self): up() goto(self.position_x, self.position_y + 70) down() message = 'BATEAU ' + str(self.number) # write(message) up() goto(self.position_x, self.position_y) down() color('black', self.color) begin_fill() """ for _ in range(self.size_x): for _ in range(4): forward(50) left(90) forward(50) end_fill() if self.size_y == 2: up() goto(self.position_x, self.position_y - 50) down() begin_fill() for _ in range(self.size_x): for _ in range(4): forward(50) left(90) forward(50) end_fill() """ forward(50*self.size_x) right(90) forward(50*self.size_y) right(90) forward(50*self.size_x) right(90) forward(50*self.size_y) right(90) end_fill() def supprimer_bateau(self): up() goto(self.position_x, self.position_y) down() color('white', 'white') begin_fill() forward(50*self.size_x) right(90) forward(50*self.size_y) right(90) forward(50*self.size_x) right(90) forward(50*self.size_y) right(90) end_fill() class Bouton: def __init__(self, name, position_x, position_y): self.name = name self.x = position_x self.y = position_y def dessin_bouton(self): setheading(0) up() goto(self.x, self.y) down() color('black', 'gray') begin_fill() forward(200) left(90) forward(75) left(90) forward(200) left(90) forward(75) left(90) end_fill() up() goto(self.x + 15, self.y + 20) down() write(self.name, font=('Arial Unicode MS', 20)) def recommencer(self): global s, z setheading(0) grille1.supprimer_grille() grille1.dessin_grille() s = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], ] reset_bateau() z = 0 start_game() class Grille: def __init__(self, position_x, position_y, liste = s): self.x = position_x self.y = position_y self.liste = liste def dessin_grille(self): setheading(0) color('black') for i in range(11): x = self.x y = self.y + i * 50 up() goto(x, y) down() forward(500) left(90) for i in range(11): x = self.x + i * 50 y = self.y up() goto(x, y) down() forward(500) def cliquer(self, x, y, liste, n): global c,l,bateau c = int(1+abs(x - self.x) // 50) l = 1+abs(y - self.y) // 50 up() goto(self.x + (c-1) * 50 +10 , (self.y) + (l-1) * 50 +10) down() l = int(abs(l-11)) print("colonne : %d " % c) print("ligne : %d" % l) # write("X", font=('Arial Unicode MS', 20)) self.liste[l-1][c-1] = 1 print(liste) l = 1 + abs(y - self.y) // 50 if n <= 6: assert type(n)== type(5), "Attention n pas entier !!!!!!!!!!!!!" bateau[n-1].position_x = self.x + (c - 1) * 50 bateau[n-1].position_y = self.y + (l - 1) * 50 bateau[n-1].color = 'green' bateau[n-1].dessin_boat() reset_bateau() Bateau.supprimer_bateau(bateau[n-1]) return True def supprimer_grille(self): up() goto(self.x, self.y) down() color('white') self.dessin_grille() def cliquer_souris(x, y): global z liste = s if grille1.x < x < grille1.x + 500 and grille1.y < y < grille1.y + 500: grille1.cliquer(x, y, liste, z) z+=1 print('grille 1\n') elif bouton1.x < x < bouton1.x + 200 and bouton1.y < y < bouton1.y + 75: bouton1.recommencer() print('bouton1\n') else: print('Vous avez cliqué en dehors des grilles dehors des grilles') """ elif grille2.x < x < grille2.x + 500 and grille2.y < y < grille2.y + 500: grille2.cliquer(x, y, liste) print('grille 2\n') """ def reset_bateau(): bateau[0] = Bateau(3, 1, 1, 0, 100) bateau[1] = Bateau(4, 1, 2, 400, 100) bateau[2] = Bateau(5, 1, 3, 0, -50) bateau[3] = Bateau(6, 1, 4, 400, -50) bateau[4] = Bateau(3, 2, 5, 0, -200) bateau[5] = Bateau(5, 2, 6, 400, -200) def dessiner_bateau(x, y, direction, taille): color('black', 'gray') begin_fill() for i in range(taille): up() if direction == 'H': goto(x + i * 50, y) else: goto(x, y + i * 50) down() for _ in range(4): forward(50) left(90) end_fill() def start_game(): global grille_joueur, bateaux_a_placer hideturtle() speed(0) title('Bataille navale') setup(1700, 1250, 0, 0) grille1.dessin_grille() setheading(0) bateau[0].dessin_boat() bateau[1].dessin_boat() bateau[2].dessin_boat() bateau[3].dessin_boat() bateau[4].dessin_boat() bateau[5].dessin_boat() bouton1.dessin_bouton() def enregistrement(): conn = sqlite3.connect('Battaille_navale.db') cursor = conn.cursor() cursor = conn.cursor() grille_jeu = s cursor.execute(''' CREATE TABLE IF NOT EXISTS users( c1 INTEGER NOT FULL, c2 INTEGER NOT FULL, c3 INTEGER NOT FULL, c4 INTEGER NOT FULL, c5 INTEGER NOT FULL, c6 INTEGER NOT FULL, c7 INTEGER NOT FULL, c8 INTEGER NOT FULL, c9 INTEGER NOT FULL, c10 INTEGER NOT FULL, ) ''') for liste in grille_jeu: cursor.execute('INSERT INTO users (c1, c2, c3, c4, c5, c6, c7, c8, c9, c10) Values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?'), (liste[0], liste[1], liste[2], liste[3], liste[4],liste[5],liste[6],liste[7],liste[8],liste[9],liste[10],) conn.commit() conn.close() # -------------------------------------------------------------- # PROGRAMME PRINCIPAL # -------------------------------------------------------------- z = 1 p = 0 """ for bouton in boutons.values(): dessiner_bouton(bouton['x'], bouton['y'], bouton['text']) """ def clic1(x, y): setheading(0) cliquer_souris(x, y + 50) def clic2(x,y): setheading(90) cliquer_souris(x, y) c=0 l=0 # liste d'objet bateau=[] bateau.append(Bateau(3, 1, 1, 0, 100)) bateau.append(Bateau(4, 1, 2, 400, 100)) bateau.append(Bateau(5, 1, 3, 0, -50)) bateau.append(Bateau(6, 1, 4, 400, -50)) bateau.append(Bateau(3, 2, 5, 0, -200)) bateau.append(Bateau(5, 2, 6, 400, -200)) grille1 = Grille(-700, -300) grille2 = Grille(-750, -150) grille3 = Grille(750, -150) bouton1 = Bouton('Recommencer', 0, 200) boutons = { "valider": {"x": -200, "y": 300, "text": "Valider placement"}, "recommencer": {"x": 100, "y": 300, "text": "Recommencer"}, } print(s) onscreenclick(clic1,1) onscreenclick(clic2,3) onkeypress(bye,"b") start_game() listen() done() """ def dessiner_bouton(bouton_x, bouton_y, texte): up() goto(bouton_x, bouton_y) down() color('black', 'lightgray') begin_fill() for _ in range(2): forward(180) left(90) forward(40) left(90) end_fill() up() goto(bouton_x + 10, bouton_y + 10) color('black') write(texte, font=("Arial", 14, "bold")) def valider_placement(): print("Placement ok! La partie peut commencer.") def recommencer_placement(): global grille_logique print("Replacer les bateaux") grille_logique = [[0 for _ in range(10)] for _ in range(10)] for taille in [5, 4, 3, 3, 2, 0, 6]: place = False while not place: place= placer_bateau_souris(grille_logique, taille) clear() start_game() def gestion_clic(x, y): for key, bouton in boutons.items(): bx, by = bouton['x'], bouton['y'] if bx <= x <= bx + 180 and by <= y <= by + 40: if key == "valider": valider_placement() elif key == "recommencer": recommencer_placement() return cliquer_souris(x, y) """ """ def dessiner_bateau(x, y, direction): if direction == "H": bateau1.dessin_boat() elif direction == "V": setheading(90) bateau1.dessin_boat() def verifier_et_placer(grille, ligne, colonne, taille, direction): # Vérifie si le placement est valide et place le bateau if direction == 'H': if colonne + taille > 10: return False for i in range(taille): if grille[ligne][colonne + i] != 0: return False for i in range(taille): grille[ligne][colonne + i] = 1 else: if ligne + taille > 10: return False for i in range(taille): if grille[ligne + i][colonne] != 0: return False for i in range(taille): grille[ligne + i][colonne] = 1 return True """ """ def placer_bateau_souris(x, y, bouton): global bateaux_a_placer if not bateaux_a_placer: print("Tous les bateaux sont placés.") return gx, gy = grille1.x, grille1.y if gx <= x < gx + 10 * 50 and gy <= y < gy + 10 * 50: colonne = int((x - gx) // 50) ligne = int((y - gy) // 50) #direction = 'V' if bouton == 1 else ='H' taille = bateaux_a_placer[0] if verifier_et_placer: bateau_x = gx + colonne * 50 bateau_y = gy + ligne * 50 dessiner_bateau(bateau_x, bateau_y, taille, direction) bateaux_a_placer.pop(0) else: print("❌ Placement invalide") def placer_bateau(x, y,direction): for i in range(6): if direction == 'V': up() goto(x, y) down() setheading(90) bateau[i+1].dessin_boat(x, y ) if direction == 'H': up() goto(x, y) down() setheading(0) bateau[i+1].dessin_boat(x, y ) """