Imitation du jeu de dédé

Description

Voici un petit code avec une interface graphique Tk qui permet d'imiter le jeu Dédé de la Française des jeux. Ce code est assez simple. Il en existe plusieurs version (2) : une avec Pmw et une autre sans utiliser la bibliothèque Pmw

Source / Exemple :


# -*- coding:Utf-8 -*-

#################
#  Aéra group   #
#               #
# Imitation du  #
#    jeu Dédé   #
#  Version 1.1  #
#################

    #---------------------- Créé le dimanche 20 août 2006 ----------------------#
        #-------------- Dernière mise à jour : le 20 août 2006 --------------#

  #======================== PROGRAMME PYTHON : ========================#

# ===== 1. Importation ===== #

from Tkinter import *
import Pmw
from random import *
from tkMessageBox import askyesno
import tkMessageBox

# ===== 2. Fonctions préliminaires ===== #

def nouv():
    global bn, ar, ars, tk, cou, coup
    ans=askyesno('Nouvelle partie',"Voulez-vous réellement recommencer une nouvelle partie ? \nVotre progression actuelle sera perdu")
    if ans:
        can.delete(ALL)
        can1.delete(ALL)
        bn=0
        ar=2
        ars=0
        tk=1
        cou=0
        coup=0
        b1.configure(state=NORMAL)
        b2.configure(state=NORMAL)
        b3.configure(state=NORMAL)
        b4.configure(state=DISABLED)
        l.configure(text='Nombre de ticket acheté : ' + str(tk))
        l1.configure(text='Argent perdu : ' + str(ars) + ' ?')
        l2.configure(text='Coup(s) gagnant(s) : ' + str(cou))
        l3.configure(text='Coup(s) perdant(s) : ' + str(coup))
        l4.configure(text='Argent disponible : ' + str(ar) + ' ?',fg='black')
        ch.configure(text='')
        ch1.configure(text='')

    

def dessin1():
    can.delete(ALL)
    can.create_rectangle(10,10,190,190,fill='grey90')
    can.create_oval(80,80,120,120,fill='black')

def dessin2():
    can.delete(ALL)
    can.create_rectangle(10,10,190,190,fill='grey90')
    can.create_oval(30,30,70,70,fill='black')
    can.create_oval(130,130,170,170,fill='black')

def dessin3():
    can.delete(ALL)
    can.create_rectangle(10,10,190,190,fill='grey90')
    can.create_oval(80,80,120,120,fill='black')
    can.create_oval(30,30,70,70,fill='black')
    can.create_oval(130,130,170,170,fill='black')

def dessin4():
    can.delete(ALL)
    can.create_rectangle(10,10,190,190,fill='grey90')
    can.create_oval(30,30,70,70,fill='black')
    can.create_oval(130,130,170,170,fill='black')
    can.create_oval(130,30,170,70,fill='black')
    can.create_oval(30,130,70,170,fill='black')
    

def dessin5():
    can.delete(ALL)
    can.create_rectangle(10,10,190,190,fill='grey90')
    can.create_oval(80,80,120,120,fill='black')
    can.create_oval(30,30,70,70,fill='black')
    can.create_oval(130,130,170,170,fill='black')
    can.create_oval(130,30,170,70,fill='black')
    can.create_oval(30,130,70,170,fill='black')

def dessin6():
    can.delete(ALL)
    can.create_rectangle(10,10,190,190,fill='grey90')
    can.create_oval(30,30,70,70,fill='black')
    can.create_oval(130,130,170,170,fill='black')
    can.create_oval(130,30,170,70,fill='black')
    can.create_oval(30,130,70,170,fill='black')
    can.create_oval(30,80,70,120,fill='black')
    can.create_oval(130,80,170,120,fill='black')

def des1():
    can1.delete(ALL)
    can1.create_rectangle(10,10,190,190,fill='grey90')
    can1.create_oval(80,80,120,120,fill='black')

def des2():
    can1.delete(ALL)
    can1.create_rectangle(10,10,190,190,fill='grey90')
    can1.create_oval(30,30,70,70,fill='black')
    can1.create_oval(130,130,170,170,fill='black')

def des3():
    can1.delete(ALL)
    can1.create_rectangle(10,10,190,190,fill='grey90')
    can1.create_oval(80,80,120,120,fill='black')
    can1.create_oval(30,30,70,70,fill='black')
    can1.create_oval(130,130,170,170,fill='black')

def des4():
    can1.delete(ALL)
    can1.create_rectangle(10,10,190,190,fill='grey90')
    can1.create_oval(30,30,70,70,fill='black')
    can1.create_oval(130,130,170,170,fill='black')
    can1.create_oval(130,30,170,70,fill='black')
    can1.create_oval(30,130,70,170,fill='black')
    

def des5():
    can1.delete(ALL)
    can1.create_rectangle(10,10,190,190,fill='grey90')
    can1.create_oval(80,80,120,120,fill='black')
    can1.create_oval(30,30,70,70,fill='black')
    can1.create_oval(130,130,170,170,fill='black')
    can1.create_oval(130,30,170,70,fill='black')
    can1.create_oval(30,130,70,170,fill='black')

def des6():
    can1.delete(ALL)
    can1.create_rectangle(10,10,190,190,fill='grey90')
    can1.create_oval(30,30,70,70,fill='black')
    can1.create_oval(130,130,170,170,fill='black')
    can1.create_oval(130,30,170,70,fill='black')
    can1.create_oval(30,130,70,170,fill='black')
    can1.create_oval(30,80,70,120,fill='black')
    can1.create_oval(130,80,170,120,fill='black')
    

def detec():
    global ars,ar, coup, cou
    g=[2,2,40,80,100,2,2,500]
    a=randrange(6)+1
    b=randrange(6)+1
    det=randrange(8)
    ga=g[det]
    if a==1:
        dessin1()
    if a==2:
        dessin2()
    if a==3:
        dessin3()
    if a==4:
        dessin4()
    if a==5:
        dessin5()
    if a==6:
        dessin6()
    if b==1:
        des1()
    if b==2:
        des2()
    if b==3:
        des3()
    if b==4:
        des4()
    if b==5:
        des5()
    if b==6:
        des6()
    if a+b==7:
        ch1.configure(text='Vous avez gagnié : ' + str(ga) + ' ?')
        cou=cou+1
        ar=ar+ga
        l2.configure(text='Coup(s) gagnant(s) : ' + str(cou))
        if ar<=0:
            l4.configure(text='Argent disponible : ' + str(ar) + ' ?',fg='red')
        else:
            l4.configure(text='Argent disponible : ' + str(ar) + ' ?',fg='black')
    else :
        ch1.configure(text='Vous avez perdu : ' + str(ga) + ' ?')
        ars=ga+ars
        coup=coup+1
        l1.configure(text='Argent perdu : ' + str(ars) + ' ?')
        l3.configure(text='Coup(s) perdant(s) : ' + str(coup))
    ch.configure(text='Le total des dés est : ' + str(a+b) + ' (' + str(a) + ' + ' + str(b) + ')')
    

def lan1():
    global bn
    b1.configure(state=DISABLED)
    bn=bn+1
    if bn==3:
        b4.configure(state=NORMAL)
    detec()
def lan2():
    global bn
    b2.configure(state=DISABLED)
    bn=bn+1
    if bn==3:
        b4.configure(state=NORMAL)
    detec()
def lan3():
    global bn
    b3.configure(state=DISABLED)
    bn=bn+1
    if bn==3:
       b4.configure(state=NORMAL) 
    detec()

def acha():
    global bn,tk, ar
    b1.configure(state=NORMAL)
    b2.configure(state=NORMAL)
    b3.configure(state=NORMAL)
    b4.configure(state=DISABLED)
    bn=0
    tk=tk+1
    ar=ar-2
    l.configure(text='Nombre de ticket achetés : ' + str(tk))
    if ar<=0:
        l4.configure(text='Argent disponible : ' + str(ar) + ' ?',fg='red')
    else:
        l4.configure(text='Argent disponible : ' + str(ar) + ' ?',fg='black')

def quiter():
    ansk=askyesno('Quitter',"Voulez-vous réellement quitter ?")
    if ansk:
        tkMessageBox.showinfo('Salut', 'À une prochaine fois peut-être')
        fen.destroy()

def helpe():
    tkMessageBox.showinfo('Aide',"Régle du jeu :\n\nQuand vous gratez une case (ça s'appel un lancer)\nil y a deux dés qui apparessent. Si leur somme fait\n7, vous avez gagnié, sinon vous avez perdu ! Pour ce qui ne savent\npas compter, il ne doivent pas s'inquietter, l'ordinateur compte pour eux !\nPrix d'un billet : 2 ?.\n\nCf un vrai biller de Dédé pour plus de précision")

def savoir():
    tkMessageBox.showinfo("À propos de ...", "Logiciel ''Imitation de Dédé''\n\n_______________________\n\n\nLogiciel créé avec Python 2.4 par Aéra group.\nVersion 1.1 (Avec Pmw)") 

# ===== 3. Programme principale ===== #

    

fen = Tk()
fen.title('Jeu de Dédé')

bn=0
tk=1
ar=2
ars=0
cou=0
coup=0

fen.protocol("WM_DELETE_WINDOW", quiter)

menu = Menu(fen)
fichier=Menu(menu,tearoff=0)
aid=Menu(menu,tearoff=0)
menu.add_cascade(label='Fichier',menu=fichier)
fichier.add_command(label='Nouveau',command=nouv)
fichier.add_separator()
fichier.add_command(label='Quitter',command=quiter)
menu.add_cascade(label='Aide',menu=aid)
aid.add_command(label='Aide',command=helpe)
aid.add_separator()
aid.add_command(label='À propos de ...',command=savoir)
fen.config(menu = menu)

grou = Pmw.Group(fen, tag_text = 'Gratage')
grou.grid(row=3,column=1)
grou1 =  Pmw.Group(fen, tag_text = 'Potre-feuille')
grou1.grid(row=3,column=2)

can = Canvas(fen,width = 200, height = 200)
can.grid(row=0, column=1,padx = 25, pady = 10)
can1 = Canvas(fen,width = 200, height = 200)
can1.grid(row=0, column=2,padx = 25, pady = 10)

b1 = Button(grou.interior(),text = 'Gratter le lancer 1',command = lan1)
b1.grid(padx = 5, pady = 5)
b2 = Button(grou.interior(),text = 'Gratter le lancer 2',command = lan2)
b2.grid(padx = 5, pady = 5)
b3 = Button(grou.interior(),text = 'Gratter le lancer 3',command = lan3)
b3.grid(padx = 5, pady = 5)

ch = Label(fen)
ch.grid(row=1,columnspan=4)
ch1 = Label(fen)
ch1.grid(row=2,columnspan=4)
b4 = Button(fen,text = 'Racheter un ticket',command = acha)
b4.grid(row=4,columnspan=4,padx = 10, pady = 10)
b4.configure(state=DISABLED)

l=Label(grou1.interior(),text='Nombre de ticket acheté : ' + str(tk))
l.grid()
l1=Label(grou1.interior(),text='Argent perdu : ' + str(ars) + ' ?')
l1.grid()
l2=Label(grou1.interior(),text='Coup(s) gagnant(s) : ' + str(cou))
l2.grid()
l3=Label(grou1.interior(),text='Coup(s) perdant(s) : ' + str(coup))
l3.grid()
l4=Label(grou1.interior(),text='Argent disponible : ' + str(ar) + ' ?')
l4.grid()

# ===== 4. Démarage ===== #

fen.mainloop()

###############################################################
###############################################################
###############################################################
###############################################################
###############################################################
###############################################################
###             V  E  S  I  O  N     n  °   2     #############
###############################################################
###############################################################
###############################################################
###############################################################
###############################################################
###############################################################

# -*- coding:Utf-8 -*-

#################
#  Aéra group   #
#               #
# Imitation du  #
#    jeu Dédé   #
#  Version 1.2  #
#################

    #---------------------- Créé le dimanche 20 août 2006 ----------------------#
        #-------------- Dernière mise à jour : le 20 août 2006 --------------#

  #======================== PROGRAMME PYTHON : ========================#

# ===== 1. Importation ===== #

from Tkinter import *
from random import *
from tkMessageBox import askyesno
import tkMessageBox

# ===== 2. Fonctions préliminaires ===== #

def nouv():
    global bn, ar, ars, tk, cou, coup
    ans=askyesno('Nouvelle partie',"Voulez-vous réellement recommencer une nouvelle partie ? \nVotre progression actuelle sera perdu")
    if ans:
        can.delete(ALL)
        can1.delete(ALL)
        bn=0
        ar=2
        ars=0
        tk=1
        cou=0
        coup=0
        b1.configure(state=NORMAL)
        b2.configure(state=NORMAL)
        b3.configure(state=NORMAL)
        b4.configure(state=DISABLED)
        l.configure(text='Nombre de ticket acheté : ' + str(tk))
        l1.configure(text='Argent perdu : ' + str(ars) + ' ?')
        l2.configure(text='Coup(s) gagnant(s) : ' + str(cou))
        l3.configure(text='Coup(s) perdant(s) : ' + str(coup))
        l4.configure(text='Argent disponible : ' + str(ar) + ' ?',fg='black')
        ch.configure(text='')
        ch1.configure(text='')

    

def dessin1():
    can.delete(ALL)
    can.create_rectangle(10,10,190,190,fill='grey90')
    can.create_oval(80,80,120,120,fill='black')

def dessin2():
    can.delete(ALL)
    can.create_rectangle(10,10,190,190,fill='grey90')
    can.create_oval(30,30,70,70,fill='black')
    can.create_oval(130,130,170,170,fill='black')

def dessin3():
    can.delete(ALL)
    can.create_rectangle(10,10,190,190,fill='grey90')
    can.create_oval(80,80,120,120,fill='black')
    can.create_oval(30,30,70,70,fill='black')
    can.create_oval(130,130,170,170,fill='black')

def dessin4():
    can.delete(ALL)
    can.create_rectangle(10,10,190,190,fill='grey90')
    can.create_oval(30,30,70,70,fill='black')
    can.create_oval(130,130,170,170,fill='black')
    can.create_oval(130,30,170,70,fill='black')
    can.create_oval(30,130,70,170,fill='black')
    

def dessin5():
    can.delete(ALL)
    can.create_rectangle(10,10,190,190,fill='grey90')
    can.create_oval(80,80,120,120,fill='black')
    can.create_oval(30,30,70,70,fill='black')
    can.create_oval(130,130,170,170,fill='black')
    can.create_oval(130,30,170,70,fill='black')
    can.create_oval(30,130,70,170,fill='black')

def dessin6():
    can.delete(ALL)
    can.create_rectangle(10,10,190,190,fill='grey90')
    can.create_oval(30,30,70,70,fill='black')
    can.create_oval(130,130,170,170,fill='black')
    can.create_oval(130,30,170,70,fill='black')
    can.create_oval(30,130,70,170,fill='black')
    can.create_oval(30,80,70,120,fill='black')
    can.create_oval(130,80,170,120,fill='black')

def des1():
    can1.delete(ALL)
    can1.create_rectangle(10,10,190,190,fill='grey90')
    can1.create_oval(80,80,120,120,fill='black')

def des2():
    can1.delete(ALL)
    can1.create_rectangle(10,10,190,190,fill='grey90')
    can1.create_oval(30,30,70,70,fill='black')
    can1.create_oval(130,130,170,170,fill='black')

def des3():
    can1.delete(ALL)
    can1.create_rectangle(10,10,190,190,fill='grey90')
    can1.create_oval(80,80,120,120,fill='black')
    can1.create_oval(30,30,70,70,fill='black')
    can1.create_oval(130,130,170,170,fill='black')

def des4():
    can1.delete(ALL)
    can1.create_rectangle(10,10,190,190,fill='grey90')
    can1.create_oval(30,30,70,70,fill='black')
    can1.create_oval(130,130,170,170,fill='black')
    can1.create_oval(130,30,170,70,fill='black')
    can1.create_oval(30,130,70,170,fill='black')
    

def des5():
    can1.delete(ALL)
    can1.create_rectangle(10,10,190,190,fill='grey90')
    can1.create_oval(80,80,120,120,fill='black')
    can1.create_oval(30,30,70,70,fill='black')
    can1.create_oval(130,130,170,170,fill='black')
    can1.create_oval(130,30,170,70,fill='black')
    can1.create_oval(30,130,70,170,fill='black')

def des6():
    can1.delete(ALL)
    can1.create_rectangle(10,10,190,190,fill='grey90')
    can1.create_oval(30,30,70,70,fill='black')
    can1.create_oval(130,130,170,170,fill='black')
    can1.create_oval(130,30,170,70,fill='black')
    can1.create_oval(30,130,70,170,fill='black')
    can1.create_oval(30,80,70,120,fill='black')
    can1.create_oval(130,80,170,120,fill='black')
    

def detec():
    global ars,ar, coup, cou
    g=[2,2,40,80,100,2,2,500]
    a=randrange(6)+1
    b=randrange(6)+1
    det=randrange(8)
    ga=g[det]
    if a==1:
        dessin1()
    if a==2:
        dessin2()
    if a==3:
        dessin3()
    if a==4:
        dessin4()
    if a==5:
        dessin5()
    if a==6:
        dessin6()
    if b==1:
        des1()
    if b==2:
        des2()
    if b==3:
        des3()
    if b==4:
        des4()
    if b==5:
        des5()
    if b==6:
        des6()
    if a+b==7:
        ch1.configure(text='Vous avez gagnié : ' + str(ga) + ' ?')
        cou=cou+1
        ar=ar+ga
        l2.configure(text='Coup(s) gagnant(s) : ' + str(cou))
        if ar<=0:
            l4.configure(text='Argent disponible : ' + str(ar) + ' ?',fg='red')
        else:
            l4.configure(text='Argent disponible : ' + str(ar) + ' ?',fg='black')
    else :
        ch1.configure(text='Vous avez perdu : ' + str(ga) + ' ?')
        ars=ga+ars
        coup=coup+1
        l1.configure(text='Argent perdu : ' + str(ars) + ' ?')
        l3.configure(text='Coup(s) perdant(s) : ' + str(coup))
    ch.configure(text='Le total des dés est : ' + str(a+b) + ' (' + str(a) + ' + ' + str(b) + ')')
    

def lan1():
    global bn
    b1.configure(state=DISABLED)
    bn=bn+1
    if bn==3:
        b4.configure(state=NORMAL)
    detec()
def lan2():
    global bn
    b2.configure(state=DISABLED)
    bn=bn+1
    if bn==3:
        b4.configure(state=NORMAL)
    detec()
def lan3():
    global bn
    b3.configure(state=DISABLED)
    bn=bn+1
    if bn==3:
       b4.configure(state=NORMAL) 
    detec()

def acha():
    global bn,tk, ar
    b1.configure(state=NORMAL)
    b2.configure(state=NORMAL)
    b3.configure(state=NORMAL)
    b4.configure(state=DISABLED)
    bn=0
    tk=tk+1
    ar=ar-2
    l.configure(text='Nombre de ticket achetés : ' + str(tk))
    if ar<=0:
        l4.configure(text='Argent disponible : ' + str(ar) + ' ?',fg='red')
    else:
        l4.configure(text='Argent disponible : ' + str(ar) + ' ?',fg='black')

def quiter():
    ansk=askyesno('Quitter',"Voulez-vous réellement quitter ?")
    if ansk:
        tkMessageBox.showinfo('Salut', 'À une prochaine fois peut-être')
        fen.destroy()

def helpe():
    tkMessageBox.showinfo('Aide',"Régle du jeu :\n\nQuand vous gratez une case (ça s'appel un lancer)\nil y a deux dés qui apparessent. Si leur somme fait\n7, vous avez gagnié, sinon vous avez perdu ! Pour ce qui ne savent\npas compter, il ne doivent pas s'inquietter, l'ordinateur compte pour eux !\nPrix d'un billet : 2 ?.\n\nCf un vrai biller de Dédé pour plus de précision")

def savoir():
    tkMessageBox.showinfo("À propos de ...", "Logiciel ''Imitation de Dédé''\n\n_______________________\n\n\nLogiciel créé avec Python 2.4 par Aéra group.\nVersion 1.2 (Sans Pmw)") 

# ===== 3. Programme principale ===== #

    

fen = Tk()
fen.title('Jeu de Dédé')

bn=0
tk=1
ar=2
ars=0
cou=0
coup=0

fen.protocol("WM_DELETE_WINDOW", quiter)

menu = Menu(fen)
fichier=Menu(menu,tearoff=0)
aid=Menu(menu,tearoff=0)
menu.add_cascade(label='Fichier',menu=fichier)
fichier.add_command(label='Nouveau',command=nouv)
fichier.add_separator()
fichier.add_command(label='Quitter',command=quiter)
menu.add_cascade(label='Aide',menu=aid)
aid.add_command(label='Aide',command=helpe)
aid.add_separator()
aid.add_command(label='À propos de ...',command=savoir)
fen.config(menu = menu)

grou = Frame(fen,bd =2, relief =RAISED)
grou.grid(row=3,column=1)
grou1 =  Frame(fen,bd =2, relief =RAISED)
grou1.grid(row=3,column=2)

can = Canvas(fen,width = 200, height = 200)
can.grid(row=0, column=1,padx = 25, pady = 10)
can1 = Canvas(fen,width = 200, height = 200)
can1.grid(row=0, column=2,padx = 25, pady = 10)

b1 = Button(grou,text = 'Gratter le lancer 1',command = lan1)
b1.grid(padx = 5, pady = 5)
b2 = Button(grou,text = 'Gratter le lancer 2',command = lan2)
b2.grid(padx = 5, pady = 5)
b3 = Button(grou,text = 'Gratter le lancer 3',command = lan3)
b3.grid(padx = 5, pady = 5)

ch = Label(fen)
ch.grid(row=1,columnspan=4)
ch1 = Label(fen)
ch1.grid(row=2,columnspan=4)
b4 = Button(fen,text = 'Racheter un ticket',command = acha)
b4.grid(row=4,columnspan=4,padx = 10, pady = 10)
b4.configure(state=DISABLED)

l=Label(grou1,text='Nombre de ticket acheté : ' + str(tk))
l.grid()
l1=Label(grou1,text='Argent perdu : ' + str(ars) + ' ?')
l1.grid()
l2=Label(grou1,text='Coup(s) gagnant(s) : ' + str(cou))
l2.grid()
l3=Label(grou1,text='Coup(s) perdant(s) : ' + str(coup))
l3.grid()
l4=Label(grou1,text='Argent disponible : ' + str(ar) + ' ?')
l4.grid()

# ===== 4. Démarage ===== #

fen.mainloop()

Conclusion :


Voilà. À noté que d'abitude, je fais une version avec WxPython. Ici, exeptionellement je n'en ai pas fait, faute de temps. Peu être dans un certain temps, j'en crérais une , mais rien n'ai sûr.

NOTE AUX PERSONNES QUI SOUHAITE TÉLÉCHARGER LA BLIBLIOTHÈQUE GRAPHIQUE PMW :

Il est possible de télécharger la bibliotèque
sur le site http://prdownloads.sourceforge.net/pmw/Pmw.1.2.tar.gz?download. Vous obtiendrez un fichier
compresser que vous décompresserez dans "C:\Python24\Lib\site-packages" (si "C:\Python24" est votre répertoire
d'instalation de Python 2.4 (ou autre version bien entendu))

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.