IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Tkinter Python Discussion :

OXO avec Tkinter


Sujet :

Tkinter Python

  1. #1
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Janvier 2010
    Messages
    64
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Janvier 2010
    Messages : 64
    Points : 26
    Points
    26
    Par défaut OXO avec Tkinter
    Bonjour tout le monde !

    Me revoilà avec un petit programme à réaliser et j'ai à nouveau besoin de vôtre aide. Cette fois-ci je dois recréer le jeu OXO avec Python et Tkinter.

    Voilà où j'en suis pour l'instant :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
     
    from tkinter import *
    from random import randrange
     
    def damier():
        'Dessine la zone de jeu pour le OXO'
        # Effacer d'abord tout dessin préexistant :
        can.delete(ALL)
        can.create_rectangle(10, 10, 640, 640, outline='black')
        can.create_rectangle(220, 10, 430, 220, outline='black', fill='dark grey')
        can.create_rectangle(10, 220, 220, 430, outline='black', fill='dark grey')
        can.create_rectangle(430, 220, 640, 430, outline='black', fill='dark grey')
        can.create_rectangle(220, 430, 430, 640, outline='black', fill='dark grey')
     
    def placerX(event):
        'Place un X.'
     
        can.create_line(event.x, event.y, event.x + 100, event.y + 100, fill='red')
        can.create_line(event.x + 100, event.y, event.x, event.y + 100, fill='red')
     
    def placerO(event):
        'Place un O.'
        pointX = event.x
        pointY = event.y
     
        if pointX < listeCoord[0][0] :
            if pointY < listeCoord[0][1] :
                can.create_oval(listeCoord[0][0] - 180, listeCoord[0][1] -180, listeCoord[0][0] - 30, listeCoord[0][1] - 30, outline='blue')
        elif pointX < listeCoord[1][0] :
            if pointY < listeCoord[1][1] :
                can.create_oval(listeCoord[1][0] - 180, listeCoord[1][1] -180, listeCoord[1][0] - 30, listeCoord[1][1] - 30, outline='blue')
        elif pointX < listeCoord[2][0] :
            if pointY < listeCoord[2][1] :
                can.create_oval(listeCoord[2][0] - 180, listeCoord[2][1] -180, listeCoord[2][0] - 30, listeCoord[2][1] - 30, outline='blue')
        elif pointX < listeCoord[3][0] :
            if pointY < listeCoord[3][1] :
                can.create_oval(listeCoord[3][0] - 180, listeCoord[3][1] -180, listeCoord[3][0] - 30, listeCoord[3][1] - 30, outline='blue')
        elif pointX < listeCoord[4][0] :
            if pointY < listeCoord[4][1] :
                can.create_oval(listeCoord[4][0] - 180, listeCoord[4][1] -180, listeCoord[4][0] - 30, listeCoord[4][1] - 30, outline='blue')
        elif pointX < listeCoord[5][0] :
            if pointY < listeCoord[5][1] :
                can.create_oval(listeCoord[5][0] - 180, listeCoord[5][1] -180, listeCoord[5][0] - 30, listeCoord[5][1] - 30, outline='blue')
        elif pointX < listeCoord[6][0] :
            if pointY < listeCoord[6][1] :
                can.create_oval(listeCoord[6][0] - 180, listeCoord[6][1] -180, listeCoord[6][0] - 30, listeCoord[6][1] - 30, outline='blue')
        elif pointX < listeCoord[7][0] :
            if pointY < listeCoord[7][1] :
                can.create_oval(listeCoord[7][0] - 180, listeCoord[7][1] -180, listeCoord[7][0] - 30, listeCoord[7][1] - 30, outline='blue')
     
        print(pointX, pointY, listeCoord[0][0], listeCoord[0][1])
     
    listeCoord = [[220,220],[430,220],[640,220],[220,430],[430,430],[640,430],[220,640],[430,640],[640,640]]
     
    ##### Programme principal : ############
     
    fen = Tk()
    can = Canvas(fen, width =650, height =650, bg ='grey')
    can.bind("<Button-1>", placerX)
    can.bind("<Button-3>", placerO)
    can.pack()
    damier()
    fen.mainloop()
    Je suis un peu callé et ne sais pas trop comment continuer... Quelques pistes ?

    Merci à vous !
    Tony K.

  2. #2
    Expert confirmé Avatar de PauseKawa
    Homme Profil pro
    Technicien Help Desk, maintenance, réseau, système et +
    Inscrit en
    Juin 2006
    Messages
    2 725
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Technicien Help Desk, maintenance, réseau, système et +
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2006
    Messages : 2 725
    Points : 4 005
    Points
    4 005
    Par défaut
    Bonsoir,

    Vous avez déjà toutes les infos dans le canvas, autant les exploiter.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    from tkinter import *
    from random import randrange
     
    def damier():
        'Dessine la zone de jeu pour le OXO'
        # Effacer d'abord tout dessin préexistant :
        can.delete(ALL)
        Fill = 'dark grey'
        for y in range(3):
            for x in range(3):
                can.create_rectangle(10+(x*220), 10+(y*220), 230+(x*220), 230+(y*220), outline='black', fill=Fill)
                if Fill == 'dark grey':
                    Fill = 'grey'
                else:
                    Fill = 'dark grey'
     
    def placerX(event):
        'Place un X.'
        x1, y1, x2, y2 = can.coords(can.find_closest(event.x, event.y))
        can.create_line(x1, y1, x2, y2, fill='red')
        can.create_line(x1, y2, x2, y1, fill='red')
     
    def placerO(event):
        'Place un O.'
        can.create_oval(can.coords(can.find_closest(event.x, event.y)), outline='blue')
     
    ##### Programme principal : ############
     
    fen = Tk()
    can = Canvas(fen, width =650, height =650, bg ='grey')
    can.bind("<Button-1>", placerX)
    can.bind("<Button-3>", placerO)
    can.pack()
    damier()
    fen.mainloop()
    @+

  3. #3
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Janvier 2010
    Messages
    64
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Janvier 2010
    Messages : 64
    Points : 26
    Points
    26
    Par défaut
    Merci PauseKawa.. C'est court comme programme et fait ce que ça doit.. J'ai juste encore un peu de mal à comprendre le code...

    Si maintenant je voudrais bloquer la case dans laquelle il y a déjà une croix ou un rond je fais comment ? Et pour compter les points ? Dès que OXO est aligné sur la grille le jeu affiche 1 point pour celui qui a joué en dernier et puis il redémarre.. c'est possible avec ton code ?

    Merci à toi.

    PS: Je suis vraiment pas fort doué en programmation et j'ai ce cours 4h par semaine.. quand je programme j'essaie toujours de faire avec un code simple

  4. #4
    Expert confirmé Avatar de PauseKawa
    Homme Profil pro
    Technicien Help Desk, maintenance, réseau, système et +
    Inscrit en
    Juin 2006
    Messages
    2 725
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Technicien Help Desk, maintenance, réseau, système et +
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2006
    Messages : 2 725
    Points : 4 005
    Points
    4 005
    Par défaut
    Bonjour,

    Pour ce qui est de la partie logique il me semble que les tags soit le plus simple. Exemple:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    from tkinter import *
     
    def placerX(event):
        'Place un X.'
        case = can.find_closest(event.x, event.y)
        if "O" in can.gettags(case):
            return
        can.itemconfig(case, tags=("X"))
        x1, y1, x2, y2 = can.coords(case)
        can.create_line(x1, y1, x2, y2, fill='red', tags="dessin")
        can.create_line(x1, y2, x2, y1, fill='red', tags="dessin")
     
    def placerO(event):
        'Place un O.'
        case = can.find_closest(event.x, event.y)
        if "X" in can.gettags(case):
            return
     
        can.itemconfig(case, tags=("O"))
        case = can.find_closest(event.x, event.y)
        can.create_oval(can.coords(case), outline='blue', tags="dessin")
     
    def Reset():
        for tag in ('X', 'O'):
            for item in can.find_withtag(tag):
                can.dtag(item, tag)
        for item in can.find_withtag("dessin"):
            can.delete(item)
     
    ##### Programme principal : ############
     
    fen = Tk()
    can = Canvas(fen, width=650, height=650, bg='grey')
    can.bind("<Button-1>", placerX)
    can.bind("<Button-3>", placerO)
    can.pack()
    Fill = 'dark grey'
    for y in range(3):
        for x in range(3):
            can.create_rectangle(10+(x*220), 10+(y*220), 230+(x*220), 230+(y*220), outline='black', fill=Fill)
            if Fill == 'dark grey':
                Fill = 'grey'
            else:
                Fill = 'dark grey'
    Button(fen, text="Reset", command=Reset).pack()
    fen.mainloop()
    Pourquoi je n'efface pas les rectangles dans reset ?
    Les items d'un Canvas sont identifiers par un id de type int unique.
    Les rectangles sont donc numérotés (puisque c'est les premiers items) de 1 à 9, utile pour les identifiers.
    On a donc
    1 2 3
    4 5 6
    7 8 9
    Lors de l'ajout d'un tag ("X" ou "O" dans le code plus haut) il est donc facile de vérifier les cellules.
    Par exemple pour vérifier si une conbinaison est gagnante avec un dico:
    win = {1: ((2, 3), (5, 9), (4, 7)), ... 9: ((3, 6), (1, 5), (7, 8))}

    A vous de jouer

    @+

Discussions similaires

  1. Canvas avec Tkinter
    Par darksh3ll dans le forum Tkinter
    Réponses: 1
    Dernier message: 03/08/2007, 15h42
  2. Réponses: 2
    Dernier message: 27/05/2007, 02h38
  3. IDLE plante avec Tkinter
    Par von_magnus dans le forum EDI/RAD
    Réponses: 2
    Dernier message: 06/07/2006, 07h20
  4. [Tkinter] Plusieurs fenêtre avec Tkinter
    Par cyrpaut dans le forum Tkinter
    Réponses: 2
    Dernier message: 04/01/2006, 22h24
  5. [Tkinter] Un petit souçis d'event avec Tkinter
    Par fire.stone dans le forum Tkinter
    Réponses: 4
    Dernier message: 29/10/2005, 20h56

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo