frommathimport*fromkandinskyimport*fromionimportkeydownfromrandomimport*fromtimeimport*gp=get_pixelfr=fill_rectds=draw_stringN=(0,0,0)G=(120,120,120)B=(255,255,255)R=(255,0,0)Bl=(0,0,255)X,Y=0,0Partie=Truedefcol_os():try:get_keys()return (192,53,53-5)except:return (255,183,52)deftexte_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)classbateau:'''
Classe définissant l'objet bateau, prend comme attribut les coordonnées
des extrêmes du bateau (x1,y1,x2,y2) ainsi que l'état
(0,1,2 pour safe,touché,coulé)
'''def__init__(self,x1,y1,x2,y2,etat=0):self.C_debut=[x1,y1]#Cordonnées début
self.C_fin=[x2,y2]#Cordonnées fin
self.L_etats=[]ifx2>x1:self.orient="h"self.y=y1else:self.orient="v"self.x=x1self.etat=etat#Def liste des cases
self.LC=[]ifself.orient=="h":foriinrange(x1,x2+1):self.LC.append([i,self.y])else:foriinrange(y1,y2+1):self.LC.append([i,self.x])foriinrange(len(self.LC)):self.L_etats.append(0)defis_touch(self,x,y):'''Méthode verifiant si le bateau est touché'''l=[x,y]fori,eltinenumerate(self.LC):ifelt==l:self.etat=1self.L_etats[i]=1returnTruereturnFalsedefis_coule(self):'''Méthode revoyant Vrai si le bateau est coulé
et Faux si il ne l'est pas
'''ifself.etat==2:returnTrueforiinself.L_etats:ifi!=1:returnFalseforjinself.LC:ca(j[0],j[1],N)self.L_etats=[]foriinrange(len(self.LC)):self.L_etats.append(2)self.etat=2returnTruedef__repr__(self):return"( {}, {}, {} )".format(self.C_debut,self.C_fin,self.etat)defcompo(n):'''Générateur de composition aléatoire'''L=[]foriinrange(n):ori=randint(0,1)sens=randrange(-1,2,2)longueur=randint(2,4)x1=randint(0,10)y1=randint(0,10)ifori==0:x2=x1y2=y1+(longueur*sens)c1=y1c2=y2else:x2=x1+(longueur*sens)y2=y1c1=x1c2=x2ifc2>9:c1=c1-(c2-9)c2=c1+(longueur*sens)elifc2<0:c1=c1-c2c2=c1+(longueur*sens)ifori==0:L.append(bateau(x1,c1,x2,c2))else:L.append(bateau(c1,y1,c2,y2))returnLdeftestwin():'''Condition de Victoire'''globalLBforiinLB:ifi.is_coule()==False:returnFalsereturnTrueLB=[]ListeCoups=[]NBcoups=0NBtouches=0A_couleur=BN_couleur=Bdeftouche(x,y):globalR,LB,A_couleur,Bl,N_couleur,NBcoups,NBtouches,ListeCoups,PartieListeCoups.append([x,y])NBcoups+=1foriinLB:to=i.is_touch(x,y)ifto==True:ca(x,y,R)N_couleur=RNBtouches+=1ifi.is_coule()==True:N_couleur=Niftestwin()==True:Partie=FalsereturnNoneca(x,y,Bl)N_couleur=Bldefgrille():'''
Tracé de la grille
'''foriinrange(11):ifi<11:fr(50,10+20*i,200,1,N)fr(50+20*i,10,1,200,N)defca(x,y,c=G):'''
Remplis la case x,y avec la couleur c
'''fr(51+20*x,11+20*y,19,19,c)defwait(b):whileTrue:foriinb:ifkeydown(i):whilekeydown(i):TruereturnidefMain(n):'''Boucle principale'''globalX,Y,Partie,NBcoups,NBtouches,LBLB=compo(5)print(LB)grille()ca(X,Y,G)whileTrue:t=wait([0,1,2,3,4])ift==3andX+1<10:majcase(X,Y,X+1,Y)X+=1elift==0andX>0:majcase(X,Y,X-1,Y)X-=1elift==1andY>0:majcase(X,Y,X,Y-1)Y-=1elift==2andY+1<10:majcase(X,Y,X,Y+1)Y+=1elift==4:touche(X,Y)ds("Coups",255,10,Bl)ds(str(NBcoups),255,30)ds("Touches",251,50,R)ds(str(NBtouches),255,70)ifPartie==False:end()breakdefmajcase(x1,y1,x2,y2):globalA_couleur,N_couleur,NBcoups'''
Gère le déplacement et les changements de couleur qui vont avec
'''A_couleur=N_couleurN_couleur=gp((51+(20*x2))+10,(11+(20*y2))+10)ca(x1,y1,A_couleur)ca(x2,y2,G)defend():globalNBcoupsfill_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=epsn=(0,0,0)g=(120,120,120)defplay():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))texte_center("Pierre Moucadeau",160,120,(42,42,42))texte_center("Baptiste Kerneis ",160,140,(42,42,42))sleep(5)fill_rect(0,0,320,222,(255,255,255))Main(5)play()
During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:
Ensure the proper functioning of the site (essential cookies); and
Track your browsing to send you personalized communications if you have created a professional account on the site and can be contacted (audience measurement cookies).
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.