#! /usr/bin/env python # -*- coding: utf-8 -*- # colortest.py # # Author Vincent Vande Vyvre # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. from Tkinter import * from random import * class Statut: """Etat du jeu renvoie l'avance du jeu, les couleurs, la solution et les points""" np, ne, pt, orr, orv, orb, tar, tav, tab, diff = 10, 0, 0, 0, 0, 0, 0, 0, 0, "" #nombre de jeu, nombre d'essais, total des points,couleurs d'origine RVB #couleurs cible, couleur differente (string) def new(): #nouveau jeu (10>0) nbplay.configure(state=DISABLED) etat.ne = 3 if etat.np == 10: #si nouvelle partie score.configure(text="Points : 0",fg="black") essai.configure(text="Essais : 3") etat.diff = "" #creation des 3 valeurs aleatoires rr = randrange(0, 256) etat.orr, etat.tar = rr, rr #rouge rv = randrange(0, 256) etat.orv, etat.tav = rv, rv #vert rb = randrange(0, 256) etat.orb, etat.tab = rb, rb #bleu origcolhex = inttohex(rr, rv, rb) #designation aleatoire de la couleur modifiée #un increment inferieur a 127 permet un niveau plus difficile de jeu coldif = (randrange(2)) #choix d'une des 3 couleurs if coldif == 0: if rr > 126: rr -= 127 etat.diff = "-rouge" #necessaire pour afficher la solution else: rr += 127 etat.diff = "+rouge" etat.tar = rr elif coldif == 1: if rv > 126: rv -= 127 etat.diff = "-vert" else: rv += 127 etat.diff = "+vert" etat.tav = rv else: if rb > 126: rb -= 127 etat.diff = "-bleu" else: rb += 127 etat.diff = "+bleu" etat.tab = rb targcolhex = inttohex(rr, rv, rb) etat.np -= 1 nbp = "Jeux : "+str(etat.np) #affichage initial can1.itemconfigure(orig,fill=origcolhex) can1.itemconfigure(testa,fill=targcolhex) can1.itemconfigure(testb,fill=targcolhex) can1.itemconfigure(testc,fill=targcolhex) can1.itemconfigure(target,fill=targcolhex) nbplay.configure(text=nbp) soluce.configure(text="Solution :") def colorise(targcolhex): #affichage des essais de couleur essai.configure(text="Essai(s) :"+str(etat.ne)) if etat.ne == 2: can1.itemconfigure(testa,fill=targcolhex) elif etat.ne == 1: can1.itemconfigure(testb,fill=targcolhex) else : can1.itemconfigure(testc,fill=targcolhex) loosefonc() def inttohex(red, green, blue): #conversion au format str "#RRVVBB" rhex = str(hex(red)) rhex = rhex[2:] if len(rhex) == 1: rhex = "0"+rhex ghex = str(hex(green)) ghex = ghex[2:] if len(ghex) == 1: ghex = "0"+ghex bhex = str(hex(blue)) bhex = bhex[2:] if len(bhex) == 1: bhex = "0"+bhex colorhex = "#"+rhex+ghex+bhex return colorhex def redp1(): #rouge+ if etat.ne != 0: etat.ne -= 1 if etat.orr >128: newcol = 255 else: newcol = etat.orr + 127 if newcol == etat.tar: winfonc() else: targcolhex = inttohex(newcol, etat.orv, etat.orb) colorise(targcolhex) def greenp1(): #vert+ if etat.ne != 0: etat.ne -= 1 if etat.orv >128: newcol = 255 else: newcol = etat.orv + 127 if newcol == etat.tav: winfonc() else: targcolhex = inttohex(etat.tar, newcol, etat.tab) colorise(targcolhex) def bluep1(): #bleu+ if etat.ne != 0: etat.ne -= 1 if etat.orb >128: newcol = 255 else: newcol = etat.orb + 127 if newcol == etat.tab: winfonc() else: targcolhex = inttohex(etat.tar, etat.tav, newcol) colorise(targcolhex) def redm1(): #rouge- if etat.ne != 0: etat.ne -= 1 if etat.orr >128: newcol = etat.orr - 127 else: newcol = 0 if newcol == etat.tar: winfonc() else: targcolhex = inttohex(newcol, etat.tav, etat.tab) colorise(targcolhex) def greenm1(): #vert- if etat.ne != 0: etat.ne -= 1 if etat.orv >128: newcol = etat.orv - 127 else: newcol = 0 if newcol == etat.tav: winfonc() else: targcolhex = inttohex(etat.tar, newcol, etat.tab) colorise(targcolhex) def bluem1(): #bleu- if etat.ne != 0: etat.ne -= 1 if etat.orb >128: newcol = etat.orb - 127 else: newcol = 0 if newcol == etat.tab: winfonc() else: targcolhex = inttohex(etat.tar, etat.tav, newcol) colorise(targcolhex) def winfonc(): #bonne reponse essai.configure(text = "Essai : 0") if etat.ne == 2: etat.pt += 3 elif etat.ne == 1: etat.pt += 2 else: etat.pt += 1 etat.ne = 0 soluce.configure(text=etat.diff+" Exact !") score.configure(text="Points : "+str(etat.pt)) nbplay.configure(state=ACTIVE) if etat.np == 0: score.configure(text="Total :"+str(etat.pt),fg="blue") etat.np = 10 nbplay.configure(text="Jeux : 10") def loosefonc(): #3 mauvaises reponses essai.configure(text = "Essai : 0") soluce.configure(text="Solution : "+etat.diff) nbplay.configure(state=ACTIVE) ### Initialisation de la fenêtre principale ### fen1 = Tk() fen1.title("Colortest") can1 = Canvas(fen1,bg ='white',height =260,width =430) can1.grid(row =0,column =0,rowspan =10, columnspan =8) can1.create_text(80,10,text="COULEUR D'ORIGINE") can1.create_text(300,20,text="COULEUR CIBLE") Button(fen1, text ='+',bg="red",relief=GROOVE,width=3,command =redp1).grid(row =11,column =2) Button(fen1, text ='+',bg="green",relief=GROOVE,width=3,command =greenp1).grid(row =12,column =2) Button(fen1, text ='+',bg="blue",relief=GROOVE,width=3,command =bluep1).grid(row =13,column =2) Button(fen1, text ='-',bg="red",relief=GROOVE,width=3,command =redm1).grid(row =11,column =1) Button(fen1, text ='-',bg="green",relief=GROOVE,width=3,command =greenm1).grid(row =12,column =1) Button(fen1, text ='-',bg="blue",relief=GROOVE,width=3,command =bluem1).grid(row =13,column =1) orig = can1.create_rectangle(20,20,220,220,fill ='white') #couleur d'origine testa = can1.create_rectangle(210,40,240,240,width=0,fill='white') #couleur du premier essai testb = can1.create_rectangle(240,40,270,240,width=0,fill='white') #deuxieme essai testc = can1.create_rectangle(270,40,300,240,width=0,fill='white') #troisieme essai target = can1.create_rectangle(300,40,410,240,width=0,fill='white') #couleur cible essai = Label(text ="Essai(s) :3") essai.grid(row=11,column=3,columnspan=2,sticky=W) score = Label(text ="Point(s) :0") score.grid(row=12,column=3,columnspan=2,sticky=W) soluce = Label(text ="Solution :") soluce.grid(row=13,column=3,columnspan=2,sticky=W) nbplay = Button(fen1, text ='Jeux : 10',relief=GROOVE,command =new) nbplay.grid(row =11, column =0,) Button(fen1, text ='Quitter',relief=GROOVE,command=quit).grid(row =13,column=7) etat = Statut() fen1.mainloop()