# battleship.py

Created by alex-juge84

Created on November 13, 2020

6.1 KB

Bataille navale

```from math import *
from kandinsky import *
from ion import keydown
from random import *
from time import sleep

gp = get_pixel
fr = fill_rect
ds = draw_string
N = (0,0,0)
G =(120,120,120)
B = (255,255,255)
R = (255,0,0)
Bl = (0,0,255)
X, Y = 0, 0
Partie = True

def col_os():
try:
get_keys()
return (192, 53, 53 - 5)
except:
return (255, 183, 52)

def texte_center(texte, x=160, y=110, col1=(255, 255, 255), col2=(255, 255, 255)):
'''Cette fonction affiche un texte et le centre'''
draw_string(texte, x - len(texte) * 10 // 2, y, col1, col2)

class bateau:
'''
Classe définissant l'objet bateau, prend comme attribut les coordonnées
des extrêmes du bateau (x1,y1,x2,y2) ainsi que l'état
(0,1,2 pour safe,touché,coulé)
'''

def __init__(self, x1, y1, x2, y2, etat=0):

self.C_debut = [x1,y1] #Cordonnées début
self.C_fin = [x2,y2]   #Cordonnées fin
self.L_etats = []

if x2 > x1:
self.orient = "h"
self.y = y1
else :
self.orient = "v"
self.x = x1

self.etat = etat

#Def liste des cases
self.LC = []
if self.orient == "h":
for i in range(x1,x2+1):
self.LC.append([i,self.y])
else :
for i in range(y1,y2+1):
self.LC.append([i,self.x])

for i in range(len(self.LC)):
self.L_etats.append(0)

def is_touch(self,x,y):
'''Méthode verifiant si le bateau est touché'''
l = [x,y]
for i,elt in enumerate(self.LC):
if elt == l:
self.etat = 1
self.L_etats[i] = 1
return True

return False

def is_coule(self):
'''Méthode revoyant Vrai si le bateau est coulé
et Faux si il ne l'est pas
'''
if self.etat == 2:
return True

for i in self.L_etats:
if i != 1:
return False

for j in self.LC:
ca(j[0],j[1],N)

self.L_etats = []
for i in range(len(self.LC)):
self.L_etats.append(2)

self.etat=2
return True

def __repr__(self):
return "( {}, {}, {} )".format(self.C_debut, self.C_fin, self.etat)

def compo(n):
'''Générateur de composition aléatoire'''
L= []

for i in range(n):
ori = randint(0,1)
sens = randrange(-1,2,2)
longueur = randint(2,4)
x1 = randint(0,10)
y1 = randint(0,10)

if ori == 0:
x2 = x1
y2 = y1+(longueur*sens)
c1 = y1
c2 = y2
else :
x2 = x1+(longueur*sens)
y2 = y1
c1 = x1
c2 = x2

if c2 > 9:
c1 = c1 - (c2-9)
c2 = c1+(longueur*sens)
elif c2 < 0:
c1 = c1 - c2
c2 = c1+(longueur*sens)

if ori == 0:
L.append(bateau(x1,c1,x2,c2))
else :
L.append(bateau(c1,y1,c2,y2))

return L

def testwin():
'''Condition de Victoire'''
global LB
for i in LB:
if i.is_coule()==False:
return False

return True

ListeCoups = []
NBcoups = 0
NBtouches = 0
A_couleur = B
N_couleur = B

def touche(x,y):
global R,LB,A_couleur,Bl,N_couleur,NBcoups,NBtouches,ListeCoups,Partie
ListeCoups.append([x,y])
NBcoups+=1
for i in LB:
to = i.is_touch(x,y)
if to == True:
ca(x,y,R)
N_couleur = R
NBtouches+=1
if i.is_coule() == True:
N_couleur = N

if testwin() == True:
Partie = False
return None
ca(x,y,Bl)
N_couleur = Bl

def grille():
'''
Tracé de la grille
'''
for i in range(11):
if i<11:fr(50,10+20*i,200,1,N)
fr(50+20*i,10,1,200,N)

def ca(x,y,c=G):
'''
Remplis la case x,y avec la couleur c
'''

fr(51+20*x,11+20*y,19,19,c)

def wait(b):
while True:
for i in b:
if keydown(i):
while keydown(i):True
return i

def Main(n):
'''Boucle principale'''
global X,Y,Partie,NBcoups,NBtouches,LB
LB = compo(5)
print(LB)
grille()
ca(X,Y,G)
while True:
t = wait([0,1,2,3,4])
if t == 3 and X+1<10:
majcase(X,Y,X+1,Y)
X+=1
elif t == 0 and X>0:
majcase(X,Y,X-1,Y)
X-=1
elif t == 1 and Y>0:
majcase(X,Y,X,Y-1)
Y-=1
elif t == 2 and Y+1<10:
majcase(X,Y,X,Y+1)
Y+=1
elif t == 4:
touche(X,Y)
ds("Coups",255,10,Bl)
ds(str(NBcoups),255,30)
ds("Touches",251,50,R)
ds(str(NBtouches),255,70)

if Partie == False:
end()
break

def majcase(x1,y1,x2,y2):
global A_couleur,N_couleur,NBcoups
'''
Gère le déplacement et les changements de couleur qui vont avec
'''
A_couleur = N_couleur

N_couleur = gp((51+(20*x2))+10,(11+(20*y2))+10)
ca(x1,y1,A_couleur)
ca(x2,y2,G)

def end():
global NBcoups
fill_rect(0,0,320,222,B)
texte_center("Battleship", 160, 20, col_os())
texte_center("Vous avez fait un score de :", 160, 50, N)
texte_center(str(NBcoups), 160, 80, col_os())
texte_center("Vous pouvez faire mieux !", 160, 100, (42,42,42))
sleep(10)

ome = (192,53,53)
eps = (255,183,52)
act = eps
n = (0,0,0)
g = (120,120,120)

def play():
fill_rect(0,0,320,222,(255,255,255))
texte_center("Battleship", 160, 20, col_os())
texte_center("nsi.xyz/battleship ", 160, 50, N)
texte_center("par", 160, 80, (101,101,101))
texte_center("Alexandre Juge", 160, 100, (42,42,42))