Bonjour, je m'appelle Vincent, pseudo VinsS.

Ubuntero depuis Dapper et conquis par cette passionnante aventure.

Je dispose de temps libre pour contribuer aux efforts de la communauté. C'est donc avec plaisir que je rejoins l'équipe User d'Ubuntu-fr.

vins@swing.be

Bruxelles le 24/07/07

Un petit jeu en python : testcolor.py

#! /usr/bin/env python
# -*- coding: utf-8 -*-
#       colortest.py
#       
#       Author Vincent Vande Vyvre <vins@swing.be>
#       
#       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,levp, nivdif = 10, 3, 0, 0, 0, 0, 0, 0, 0, "", 100, 1
        #nombre de jeu, nombre d'essais, total des points,couleurs d'origine RVB
        #couleurs cible, couleur differente (string), niveau de jeu (increment)

class Level1:
        """Niveau de jeu 1

        évalue les couleurs aléatoires et les paramètres de jeu"""

        def new(self):
                #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")
			levbut1.configure(state=DISABLED)
			levbut2.configure(state=DISABLED)
                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 = self.inttohex(rr, rv, rb)

                #designation aleatoire de la couleur modifiée
                while etat.diff == "":
                        coldif, lev = (randrange(6)), etat.levp         #choix d'une des 3 couleurs                         
                        if coldif == 0:
                                if rr > lev:
                                        rr -= lev
                                        etat.diff = "-rouge"            #necessaire pour afficher la solution
                                        etat.tar = rr
                        elif coldif == 1:
                                if rr < 255-lev:
                                        rr += lev
                                        etat.diff = "+rouge"
                                        etat.tar = rr
                        elif coldif == 2:
                                if rv > lev:
                                        rv -= lev
                                        etat.diff = "-vert"
                                        etat.tav = rv
                        elif coldif == 3:
                                if rv < 255-lev:
                                        rv += lev
                                        etat.diff = "+vert"
                                        etat.tav = rv
                        elif coldif == 4:
                                if rb > lev:
                                        rb -= lev
                                        etat.diff = "-bleu"
                                        etat.tab = rb
                        else:
                                if rb < 255-lev:
                                        rb += lev
                                        etat.diff = "+bleu"
                                        etat.tab = rb
                targcolhex = self.inttohex(rr, rv, rb)                       
                etat.np -= 1
                nbp = "Jouer : "+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 inttohex(self, 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

class Level2:
        """Niveau de jeu 2

        évalue les couleurs aléatoires et les paramètres de jeu"""

        def new(self):
                #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")
			levbut1.configure(state=DISABLED)
			levbut2.configure(state=DISABLED)
                essai.configure(text="Essais : 3")
                etat.diff = ""
                #creation des 3 valeurs aleatoires
                rr = randrange(0, 65536)
                etat.orr, etat.tar = rr, rr     #rouge  
                rv = randrange(0, 65536)
                etat.orv, etat.tav = rv, rv     #vert
                rb = randrange(0, 65536)
                etat.orb, etat.tab = rb, rb     #bleu
                origcolhex = self.inttohex(rr, rv, rb)
                #print origcolhex

                #designation aleatoire de la couleur modifiée
                while etat.diff == "":
                        coldif, lev = (randrange(6)), etat.levp       	#choix d'une des 3 couleurs                         
                        if coldif == 0:
                                if rr > lev:
                                        rr -= lev
                                        etat.diff = "-rouge"            #necessaire pour afficher la solution
                                        etat.tar = rr
                        elif coldif == 1:
                                if rr < 65535-lev:
                                        rr += lev
                                        etat.diff = "+rouge"
                                        etat.tar = rr
                        elif coldif == 2:
                                if rv > lev:
                                        rv -= lev
                                        etat.diff = "-vert"
                                        etat.tav = rv
                        elif coldif == 3:
                                if rv < 65535-lev:
                                        rv += lev
                                        etat.diff = "+vert"
                                        etat.tav = rv
                        elif coldif == 4:
                                if rb > lev:
                                        rb -= lev
                                        etat.diff = "-bleu"
                                        etat.tab = rb
                        else:
                                if rb < 65535-lev:
                                        rb += lev
                                        etat.diff = "+bleu"
                                        etat.tab = rb
                targcolhex = self.inttohex(rr, rv, rb)                       
                etat.np -= 1
                nbp = "Jouer : "+str(etat.np)
                #print targcolhex, etat.diff
                #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 inttohex(self, red, green, blue):
                #conversion au format str "#RRRRVVVVBBBB"
                rhex = str(hex(red))
                rhex = rhex[2:]
                while len(rhex) < 4:
                        rhex = "0"+rhex
                ghex = str(hex(green))  
                ghex = ghex[2:]
                while len(ghex) < 4:
                        ghex = "0"+ghex
                bhex = str(hex(blue))
                bhex = bhex[2:]
                while len(bhex) < 4:
                        bhex = "0"+bhex
                colorhex = "#"+rhex+ghex+bhex
                return colorhex

def new():
        if etat.nivdif == 1:
                easy.new()
        else:
                hard.new()
        
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 redp1():
        #rouge+
        if etat.nivdif == 2:
                redp2()
        else :
                if etat.ne != 0:
                        lev = etat.levp
                        etat.ne -= 1
                        if etat.orr > 255-lev:
                                newcol = 255
                        else:
                                newcol = etat.orr + lev
                        if newcol == etat.tar:
                                winfonc()
                        else:
                                targcolhex = easy.inttohex(newcol, etat.orv, etat.orb)               
                                colorise(targcolhex)

def redp2():
        if etat.ne != 0:
                lev = etat.levp
                etat.ne -= 1
                if etat.orr > 65535-lev:
                        newcol = 65535
                else:
                        newcol = etat.orr + lev
                if newcol == etat.tar:
                        winfonc()
                else:
                        targcolhex = hard.inttohex(newcol, etat.orv, etat.orb)               
                        colorise(targcolhex)
                
def greenp1():
        #vert+
        if etat.nivdif == 2:
                greenp2()
        else :
                if etat.ne != 0:
                        lev = etat.levp
                        etat.ne -= 1
                        if etat.orv > 255-lev:
                                newcol = 255
                        else:
                                newcol = etat.orv + lev
                        if newcol == etat.tav:
                         winfonc()
                        else:
                                targcolhex = easy.inttohex(etat.tar, newcol, etat.tab)               
                                colorise(targcolhex)

def greenp2():
        if etat.ne != 0:
                lev = etat.levp
                etat.ne -= 1
                if etat.orv > 65535-lev:
                        newcol = 65535
                else:
                        newcol = etat.orv + lev
                if newcol == etat.tav:
                        winfonc()
                else:
                        targcolhex = hard.inttohex(etat.orr, newcol, etat.orb)               
                        colorise(targcolhex)

def bluep1():
        #bleu+
        if etat.nivdif == 2:
                bluep2()
        else :
                if etat.ne != 0:
                        lev = etat.levp
                        etat.ne -= 1
                        if etat.orb > 255-lev:
                         newcol = 255
                        else:
                                newcol = etat.orb + lev
                        if newcol == etat.tab:
                                winfonc()
                        else:
                                targcolhex = easy.inttohex(etat.tar, etat.tav, newcol)               
                                colorise(targcolhex)

def bluep2():
        if etat.ne != 0:
                lev = etat.levp
                etat.ne -= 1
                if etat.orb > 65535-lev:
                        newcol = 65535
                else:
                        newcol = etat.orb + lev
                if newcol == etat.tab:
                        winfonc()
                else:
                        targcolhex = hard.inttohex(etat.orr, etat.orv, newcol)               
                        colorise(targcolhex)

def redm1():
        #rouge-
        if etat.nivdif == 2:
                redm2()
        else :
                if etat.ne != 0:
                        lev = etat.levp
                        etat.ne -= 1
                        if etat.orr > lev-1:
                                newcol = etat.orr - lev
                        else:
                                newcol = 0
                        if newcol == etat.tar:
                                winfonc()
                        else:
                                targcolhex = easy.inttohex(newcol, etat.tav, etat.tab)               
                                colorise(targcolhex)

def redm2():
        if etat.ne != 0:
                        lev = etat.levp
                        etat.ne -= 1
                        if etat.orr > lev-1:
                                newcol = etat.orr - lev
                        else:
                                newcol = 0
                        if newcol == etat.tar:
                                winfonc()
                        else:
                                targcolhex = hard.inttohex(newcol, etat.tav, etat.tab)               
                                colorise(targcolhex)

def greenm1():
        #vert-
        if etat.nivdif == 2:
                greenm2()
        else :
                if etat.ne != 0:
                        lev = etat.levp
                        etat.ne -= 1
                        if etat.orv > lev-1:
                                newcol = etat.orv - lev
                        else:
                                newcol = 0
                        if newcol == etat.tav:
                                winfonc()
                        else:
                                targcolhex = easy.inttohex(etat.tar, newcol, etat.tab)               
                                colorise(targcolhex)

def greenm2():
        if etat.ne != 0:
                        lev = etat.levp
                        etat.ne -= 1
                        if etat.orv > lev-1:
                                newcol = etat.orv - lev
                        else:
                                newcol = 0
                        if newcol == etat.tav:
                                winfonc()
                        else:
                                targcolhex = hard.inttohex(etat.tav, newcol, etat.tab)               
                                colorise(targcolhex)

def bluem1():
        #bleu-
        if etat.nivdif == 2:
                bluem2()
        else :
                if etat.ne != 0:
                        lev = etat.levp
                        etat.ne -= 1
                        if etat.orb > lev-1:
                                newcol = etat.orb - lev
                        else:
                                newcol = 0
                        if newcol == etat.tab:
                                winfonc()
                        else:
                                targcolhex = easy.inttohex(etat.tar, etat.tav, newcol)               
                                colorise(targcolhex)

def bluem2():
        if etat.ne != 0:
                        lev = etat.levp
                        etat.ne -= 1
                        if etat.orb > lev-1:
                                newcol = etat.orb - lev
                        else:
                                newcol = 0
                        if newcol == etat.tab:
                                winfonc()
                        else:
                                targcolhex = hard.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, etat.pt, etat.ne = 10, 0, 3
                nbplay.configure(text="Jouer : 10")
		levbut1.configure(state=NORMAL)
		levbut2.configure(state=NORMAL)

def loosefonc():
        #3 mauvaises reponses
        essai.configure(text = "Essai : 0")
        soluce.configure(text="Solution : "+etat.diff)
        nbplay.configure(state=ACTIVE)
        if etat.np == 0:
                score.configure(text="Total :"+str(etat.pt),fg="blue")
                etat.np, etat.pt, etat.ne = 10, 0, 3
                nbplay.configure(text="Jouer : 10")
		levbut1.configure(state=NORMAL)
		levbut2.configure(state=NORMAL)

def easylev():
        if etat.np==10 and etat.ne==3:
                #print "niveau 1"
                etat.levp = 100		#une valeur + petite augmente la difficulé
                etat.nivdif = 1

def hightlev():
        if etat.np==10 and etat.ne==3:
                #print "niveau 2"
                etat.levp = 6000	#une valeur + petite augmente la difficulé
                etat.nivdif = 2

def aide ():
	try:
 		fenhelp.destroy()		#ne semble pas être fonctionnel
   	except:
             	pass
	fenhelp = Tk()
	fenhelp.title("Aide - Colortest")
 	message1 = Label(fenhelp, text='Instructions de jeu:', font='arial 16')
    	message1.pack()
	message1bis = Label(fenhelp, text="Le but du jeu consiste à recréer la couleur cible en\n partant de la couleur d'origine et en lui appliquant une\n des six modifications données par les six boutons colorés.\n\nUne seule modification, ajout ou retrait rouge, vert ou bleu\n permet d'aboutir à la couleur cible.\n\nLe niveau 2 présente deux couleurs fort proches et,\n donc, un niveau de difficulté supérieur.")
	message1bis.pack()
     	bouton_quit = Button(fenhelp, text= 'Quitter', relief= GROOVE, command=fenhelp.destroy)
   	bouton_quit.pack(side='bottom', pady=5)
        
### 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(120,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 ='Jouer : 10',relief=GROOVE,command =new)
nbplay.grid(row =11, column =0,)
butvar = IntVar()							#identification des radio-boutons
levbut1 = Radiobutton(fen1,text="Niveau 1",variable=butvar,value=0,command=easylev)
levbut2 = Radiobutton(fen1,text="niveau 2",variable=butvar,value=1,command=hightlev)
levbut1.grid(row=12,column=0)
levbut2.grid(row=13,column=0)
Button(fen1,text="Aide",relief=GROOVE,width=6,command=aide).grid(row=12,column=7)
Button(fen1, text ='Quitter',relief=GROOVE,width=6,command=quit).grid(row =13,column=7)

etat = Statut()
easy = Level1()
hard = Level2()
levbut1.invoke()							#niveau 1

fen1.mainloop()
  • utilisateurs/vinss.txt
  • Dernière modification: Le 28/04/2018, 11:26
  • (modification externe)