Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
utilisateurs:vinss [Le 24/07/2007, 13:33] – créée VinsS | utilisateurs:vinss [Le 18/02/2009, 05:33] (Version actuelle) – VinsS | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
+ | Bonjour, je m' | ||
+ | |||
+ | 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' | ||
+ | |||
+ | vins@swing.be | ||
+ | |||
+ | Bruxelles le 24/07/07 | ||
+ | |||
+ | |||
+ | Un petit jeu en python : | ||
+ | | ||
+ | |||
+ | < | ||
+ | #! / | ||
+ | # -*- coding: utf-8 -*- | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | # 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 | ||
+ | # | ||
+ | # 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 | ||
+ | # | ||
+ | # MA 02110-1301, USA. | ||
+ | |||
+ | |||
+ | from Tkinter import * | ||
+ | from random import * | ||
+ | |||
+ | class Statut: | ||
+ | """ | ||
+ | |||
+ | renvoie l' | ||
+ | |||
+ | np, ne, pt, orr, orv, orb, tar, tav, tab, diff,levp, nivdif = 10, 3, 0, 0, 0, 0, 0, 0, 0, "", | ||
+ | #nombre de jeu, nombre d' | ||
+ | #couleurs cible, couleur differente (string), niveau de jeu (increment) | ||
+ | |||
+ | class Level1: | ||
+ | """ | ||
+ | |||
+ | é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=" | ||
+ | levbut1.configure(state=DISABLED) | ||
+ | levbut2.configure(state=DISABLED) | ||
+ | essai.configure(text=" | ||
+ | etat.diff = "" | ||
+ | #creation des 3 valeurs aleatoires | ||
+ | rr = randrange(0, | ||
+ | etat.orr, etat.tar = rr, rr # | ||
+ | rv = randrange(0, | ||
+ | etat.orv, etat.tav = rv, rv #vert | ||
+ | rb = randrange(0, | ||
+ | etat.orb, etat.tab = rb, rb #bleu | ||
+ | origcolhex = self.inttohex(rr, | ||
+ | |||
+ | # | ||
+ | while etat.diff == "": | ||
+ | coldif, lev = (randrange(6)), | ||
+ | if coldif == 0: | ||
+ | if rr > lev: | ||
+ | rr -= lev | ||
+ | etat.diff = " | ||
+ | etat.tar = rr | ||
+ | elif coldif == 1: | ||
+ | if rr < 255-lev: | ||
+ | rr += lev | ||
+ | etat.diff = " | ||
+ | etat.tar = rr | ||
+ | elif coldif == 2: | ||
+ | if rv > lev: | ||
+ | rv -= lev | ||
+ | etat.diff = " | ||
+ | etat.tav = rv | ||
+ | elif coldif == 3: | ||
+ | if rv < 255-lev: | ||
+ | rv += lev | ||
+ | etat.diff = " | ||
+ | etat.tav = rv | ||
+ | elif coldif == 4: | ||
+ | if rb > lev: | ||
+ | rb -= lev | ||
+ | etat.diff = " | ||
+ | etat.tab = rb | ||
+ | else: | ||
+ | if rb < 255-lev: | ||
+ | rb += lev | ||
+ | etat.diff = " | ||
+ | etat.tab = rb | ||
+ | targcolhex = self.inttohex(rr, | ||
+ | etat.np -= 1 | ||
+ | nbp = "Jouer : " | ||
+ | #affichage initial | ||
+ | can1.itemconfigure(orig, | ||
+ | can1.itemconfigure(testa, | ||
+ | can1.itemconfigure(testb, | ||
+ | can1.itemconfigure(testc, | ||
+ | can1.itemconfigure(target, | ||
+ | nbplay.configure(text=nbp) | ||
+ | soluce.configure(text=" | ||
+ | |||
+ | def inttohex(self, | ||
+ | #conversion au format str "# | ||
+ | rhex = str(hex(red)) | ||
+ | rhex = rhex[2:] | ||
+ | if len(rhex) == 1: | ||
+ | rhex = " | ||
+ | ghex = str(hex(green)) | ||
+ | ghex = ghex[2:] | ||
+ | if len(ghex) == 1: | ||
+ | ghex = " | ||
+ | bhex = str(hex(blue)) | ||
+ | bhex = bhex[2:] | ||
+ | if len(bhex) == 1: | ||
+ | bhex = " | ||
+ | colorhex = "#" | ||
+ | return colorhex | ||
+ | |||
+ | class Level2: | ||
+ | """ | ||
+ | |||
+ | é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=" | ||
+ | levbut1.configure(state=DISABLED) | ||
+ | levbut2.configure(state=DISABLED) | ||
+ | essai.configure(text=" | ||
+ | etat.diff = "" | ||
+ | #creation des 3 valeurs aleatoires | ||
+ | rr = randrange(0, | ||
+ | etat.orr, etat.tar = rr, rr # | ||
+ | rv = randrange(0, | ||
+ | etat.orv, etat.tav = rv, rv #vert | ||
+ | rb = randrange(0, | ||
+ | etat.orb, etat.tab = rb, rb #bleu | ||
+ | origcolhex = self.inttohex(rr, | ||
+ | #print origcolhex | ||
+ | |||
+ | # | ||
+ | while etat.diff == "": | ||
+ | coldif, lev = (randrange(6)), | ||
+ | if coldif == 0: | ||
+ | if rr > lev: | ||
+ | rr -= lev | ||
+ | etat.diff = " | ||
+ | etat.tar = rr | ||
+ | elif coldif == 1: | ||
+ | if rr < 65535-lev: | ||
+ | rr += lev | ||
+ | etat.diff = " | ||
+ | etat.tar = rr | ||
+ | elif coldif == 2: | ||
+ | if rv > lev: | ||
+ | rv -= lev | ||
+ | etat.diff = " | ||
+ | etat.tav = rv | ||
+ | elif coldif == 3: | ||
+ | if rv < 65535-lev: | ||
+ | rv += lev | ||
+ | etat.diff = " | ||
+ | etat.tav = rv | ||
+ | elif coldif == 4: | ||
+ | if rb > lev: | ||
+ | rb -= lev | ||
+ | etat.diff = " | ||
+ | etat.tab = rb | ||
+ | else: | ||
+ | if rb < 65535-lev: | ||
+ | rb += lev | ||
+ | etat.diff = " | ||
+ | etat.tab = rb | ||
+ | targcolhex = self.inttohex(rr, | ||
+ | etat.np -= 1 | ||
+ | nbp = "Jouer : " | ||
+ | #print targcolhex, etat.diff | ||
+ | #affichage initial | ||
+ | can1.itemconfigure(orig, | ||
+ | can1.itemconfigure(testa, | ||
+ | can1.itemconfigure(testb, | ||
+ | can1.itemconfigure(testc, | ||
+ | can1.itemconfigure(target, | ||
+ | nbplay.configure(text=nbp) | ||
+ | soluce.configure(text=" | ||
+ | |||
+ | def inttohex(self, | ||
+ | #conversion au format str "# | ||
+ | rhex = str(hex(red)) | ||
+ | rhex = rhex[2:] | ||
+ | while len(rhex) < 4: | ||
+ | rhex = " | ||
+ | ghex = str(hex(green)) | ||
+ | ghex = ghex[2:] | ||
+ | while len(ghex) < 4: | ||
+ | ghex = " | ||
+ | bhex = str(hex(blue)) | ||
+ | bhex = bhex[2:] | ||
+ | while len(bhex) < 4: | ||
+ | bhex = " | ||
+ | colorhex = "#" | ||
+ | return colorhex | ||
+ | |||
+ | def new(): | ||
+ | if etat.nivdif == 1: | ||
+ | easy.new() | ||
+ | else: | ||
+ | hard.new() | ||
+ | | ||
+ | def colorise(targcolhex): | ||
+ | #affichage des essais de couleur | ||
+ | essai.configure(text=" | ||
+ | if etat.ne == 2: | ||
+ | can1.itemconfigure(testa, | ||
+ | elif etat.ne == 1: | ||
+ | can1.itemconfigure(testb, | ||
+ | else : | ||
+ | can1.itemconfigure(testc, | ||
+ | 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, | ||
+ | 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, | ||
+ | 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: | ||
+ | | ||
+ | else: | ||
+ | targcolhex = easy.inttohex(etat.tar, | ||
+ | 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, | ||
+ | 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: | ||
+ | | ||
+ | else: | ||
+ | newcol = etat.orb + lev | ||
+ | if newcol == etat.tab: | ||
+ | winfonc() | ||
+ | else: | ||
+ | targcolhex = easy.inttohex(etat.tar, | ||
+ | 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, | ||
+ | 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, | ||
+ | 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, | ||
+ | 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, | ||
+ | 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, | ||
+ | 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, | ||
+ | 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, | ||
+ | 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+" | ||
+ | score.configure(text=" | ||
+ | nbplay.configure(state=ACTIVE) | ||
+ | if etat.np == 0: | ||
+ | score.configure(text=" | ||
+ | etat.np, etat.pt, etat.ne = 10, 0, 3 | ||
+ | nbplay.configure(text=" | ||
+ | levbut1.configure(state=NORMAL) | ||
+ | levbut2.configure(state=NORMAL) | ||
+ | |||
+ | def loosefonc(): | ||
+ | #3 mauvaises reponses | ||
+ | essai.configure(text = "Essai : 0") | ||
+ | soluce.configure(text=" | ||
+ | nbplay.configure(state=ACTIVE) | ||
+ | if etat.np == 0: | ||
+ | score.configure(text=" | ||
+ | etat.np, etat.pt, etat.ne = 10, 0, 3 | ||
+ | nbplay.configure(text=" | ||
+ | levbut1.configure(state=NORMAL) | ||
+ | levbut2.configure(state=NORMAL) | ||
+ | |||
+ | def easylev(): | ||
+ | if etat.np==10 and etat.ne==3: | ||
+ | #print " | ||
+ | etat.levp = 100 #une valeur + petite augmente la difficulé | ||
+ | etat.nivdif = 1 | ||
+ | |||
+ | def hightlev(): | ||
+ | if etat.np==10 and etat.ne==3: | ||
+ | #print " | ||
+ | etat.levp = 6000 #une valeur + petite augmente la difficulé | ||
+ | etat.nivdif = 2 | ||
+ | |||
+ | def aide (): | ||
+ | try: | ||
+ | | ||
+ | | ||
+ | pass | ||
+ | fenhelp = Tk() | ||
+ | fenhelp.title(" | ||
+ | | ||
+ | message1.pack() | ||
+ | message1bis = Label(fenhelp, | ||
+ | message1bis.pack() | ||
+ | | ||
+ | | ||
+ | | ||
+ | ### Initialisation de la fenêtre principale ### | ||
+ | fen1 = Tk() | ||
+ | fen1.title(" | ||
+ | |||
+ | can1 = Canvas(fen1, | ||
+ | can1.grid(row =0,column =0,rowspan =10, columnspan =8) | ||
+ | can1.create_text(120, | ||
+ | can1.create_text(300, | ||
+ | Button(fen1, | ||
+ | Button(fen1, | ||
+ | Button(fen1, | ||
+ | Button(fen1, | ||
+ | Button(fen1, | ||
+ | Button(fen1, | ||
+ | orig = can1.create_rectangle(20, | ||
+ | testa = can1.create_rectangle(210, | ||
+ | testb = can1.create_rectangle(240, | ||
+ | testc = can1.create_rectangle(270, | ||
+ | target = can1.create_rectangle(300, | ||
+ | essai = Label(text =" | ||
+ | essai.grid(row=11, | ||
+ | score = Label(text =" | ||
+ | score.grid(row=12, | ||
+ | soluce = Label(text =" | ||
+ | soluce.grid(row=13, | ||
+ | nbplay = Button(fen1, | ||
+ | nbplay.grid(row =11, column =0,) | ||
+ | butvar = IntVar() # | ||
+ | levbut1 = Radiobutton(fen1, | ||
+ | levbut2 = Radiobutton(fen1, | ||
+ | levbut1.grid(row=12, | ||
+ | levbut2.grid(row=13, | ||
+ | Button(fen1, | ||
+ | Button(fen1, | ||
+ | |||
+ | etat = Statut() | ||
+ | easy = Level1() | ||
+ | hard = Level2() | ||
+ | levbut1.invoke() # | ||
+ | |||
+ | fen1.mainloop() | ||
+ | </ | ||
+ | |||