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 :

Problème de modules


Sujet :

Tkinter Python

  1. #1
    Invité
    Invité(e)
    Par défaut Problème de modules
    Salut à tous, j'ai un problème assez énervant,
    J'ai créer une ihm assez compliqué (pour mon niveau) et je souhaiterais faire tourner un petit canvas qui appelle une fonction nommée Clavier. cette fonction, elle se trouve dans le module Inter. Le problème se situe au niveau du Canevas.bind du premier code (l.46).

    Rien ne vaut un bon vieux code donc voici mes deux modules :
    Module 1 que je lance. (IHM)
    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
    from Tkinter import *
    from Inter import *
     
    # position initiale des champions
    PosX = 10
    PosY = 10
     
    PosX1 = 990
    PosY1 = 610
     
    #Variables g?n?rales
    actuTour=0
     
    #******************************************************************************#
    #*                                                                            *#
    #*        *   *   *            PROGRAMME  PRINCIPAL          *   *   *        *#
    #*                                                                            *#
    #******************************************************************************#
     
    # Creation de la fenetre windows
    Jeu=Tk()
    Jeu.title("CheckMat")
    Jeu.iconbitmap("CheckMate.ico")
    Jeu.geometry("1200x720")
    Jeu.resizable(width=False,height=False)
    Jeu.configure()
     
    # Creation des frames principales
    frame3 = Frame(Jeu, bg="yellow", width=200, height=720)
    frame3.pack(side=LEFT)
     
    frame1 = Frame(Jeu, bg="black",    width=1000, height=620)
    frame1.pack(side=BOTTOM)
    Canevas = Canvas(frame1, width = 1000, height =620, bg ='white')
    photo = PhotoImage(file="TRY.gif")
    Canevas.create_image(0, 0, anchor=NW, image=photo)
    Pion = Canevas.create_oval(PosX-10,PosY-10,PosX+10,PosY+10,width=2,outline='black',fill='red')
    Pionne = Canevas.create_oval(PosX1-10,PosY1-10,PosX1+10,PosY1+10,width=2,outline='black',fill='green')
     
     
    for i in range(0,1000,20):
        Canevas.create_line(i,0,i,620,fill="#000000")
    for j in range(0,620,20):
        Canevas.create_line(0,j,1000,j,fill="#000000")
    Canevas.focus_set()
    Canevas.bind('<Key>',Clavier)
    Canevas.pack()
     
    Jeu.mainloop()
    Module 2 (Inter) :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    from IHM import actuTour
    import time as t
     
    def Clavier(event):
    ...
     
     
    def decompte(count=3):
    ...
     
    def Initialisation():
    ...
    Comment réussir à faire exécuter l'évent Clavier par l'action sur le canevas ? Merci d'avance, je souhaite réellement faire ceci en deux modules séparés. (projet d'isn de fin d'année : Jeu vidéo ).

    En complément, mes codes sources si vous souhaitez tester le tout.

    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
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    from Tkinter import *
    from Inter import *
     
    #******************************************************************************#
    #*                                                                            *#
    #*        *   *   *            VARIABLES INITIALES           *   *   *        *#
    #*                                                                            *#
    #******************************************************************************#
     
    # position initiale des champions
    PosX = 10
    PosY = 10
     
    PosX1 = 990
    PosY1 = 610
     
    #Variables g?n?rales
    actuTour=0
     
    #******************************************************************************#
    #*                                                                            *#
    #*        *   *   *            PROGRAMME  PRINCIPAL          *   *   *        *#
    #*                                                                            *#
    #******************************************************************************#
     
    # Creation de la fenetre windows
    Jeu=Tk()
    Jeu.title("CheckMat")
    Jeu.iconbitmap("CheckMate.ico")
    Jeu.geometry("1200x720")
    Jeu.resizable(width=False,height=False)
    Jeu.configure()
     
    # Creation des frames principales
    frame3 = Frame(Jeu, bg="yellow", width=200, height=720)
    frame3.pack(side=LEFT)
     
    frame1 = Frame(Jeu, bg="black",    width=1000, height=620)
    frame1.pack(side=BOTTOM)
    Canevas = Canvas(frame1, width = 1000, height =620, bg ='white')
    photo = PhotoImage(file="TRY.gif")
    Canevas.create_image(0, 0, anchor=NW, image=photo)
    Pion = Canevas.create_oval(PosX-10,PosY-10,PosX+10,PosY+10,width=2,outline='black',fill='red')
    Pionne = Canevas.create_oval(PosX1-10,PosY1-10,PosX1+10,PosY1+10,width=2,outline='black',fill='green')
     
     
    for i in range(0,1000,20):
        Canevas.create_line(i,0,i,620,fill="#000000")
    for j in range(0,620,20):
        Canevas.create_line(0,j,1000,j,fill="#000000")
    Canevas.focus_set()
    Canevas.bind('<Key>',Clavier)
    Canevas.pack()
     
    frame2 = Frame(Jeu, bg="blue",   width=1000, height=100)
    frame2.pack()
     
    # Creation des frames secondaires
        # Frames de gauches
    framea = Frame(frame3, bg="yellow",width=200, height=70)
    framea.pack (side=TOP)
    photo1 = PhotoImage(file="test1.gif")
    Canfra = Canvas(framea, width=200, height=70, bg="yellow")
    item1 = Canfra.create_image(0,0,anchor=NW, image=photo1)
    print(item1)
    Canfra.pack()
     
    frameb = Frame(frame3, bg="green",width=200, height=90)
    frameb.pack (side=TOP)
     
    framec = Frame(frame3, bg="red",width=200, height=90)
    framec.pack (side=TOP)
     
    framed = Frame(frame3, bg="green",width=200, height=340)
    framed.pack (side=TOP)
     
    framee = Frame(frame3, bg="white",width=200, height=130)
    framee.pack ()
     
    # Frames du haut
        # Frames de la longueur
    framef = Frame(frame2, bg="red",width=90, height=100)
    framef.pack (side=LEFT)
     
    frameg = Frame(frame2, bg="green",width=90, height=100)
    frameg.pack (side=LEFT)
     
    frameh = Frame(frame2, bg="white",width=450, height=100)
    frameh.pack (side=LEFT)
     
    framei = Frame(frame2, bg="green",width=380, height=100)
    framei.pack ()
     
     
        # Frames des sorts/tour
    framej = Frame(frameh, bg="black",width=450, height=65)
    framej.pack (side=TOP)
     
    framek = Frame(frameh, bg="white",width=450, height=35)
    framek.pack (expand=1)
     
    framel = Frame(framej, bg="red",width=90, height=65)
    framel.pack (side=LEFT)
     
    framem = Frame(framej, bg="black",width=90, height=65)
    framem.pack (side=LEFT)
     
    framen = Frame(framej, bg="red",width=90, height=65)
    framen.pack (side=LEFT)
     
    frameo = Frame(framej, bg="black",width=90, height=65)
    frameo.pack (side=LEFT)
     
    framep = Frame(framej, bg="red",width=90, height=65)
    framep.pack ()
     
     
    Jeu.mainloop()
    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
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    from IHM import actuTour
    import time as t
     
    def Clavier(event):
        """ Gestion de l'?v?nement Appui sur une touche du clavier """
        global PosX,PosY,PosX1,PosY1,PosX2,PosY2,PosX3,PosY3,move
        touche = event.keysym
        print(touche)
        if (actuTour == 0) :
            if move > 0 :
    # d?placement vers le haut
                if touche == 'Up':
                    PosY -= 20
                    move=move-1
                    if (PosX==PosX1 and PosY==PosY1) or (PosX==PosX2 and PosY==PosY2) or (PosX==PosX3 and PosY==PosY3) :
                        PosY += 20
                        move=move+1
                        lab1.config(text="pas possible")
                    else :
                        lab1.config(text="Sa roule")
        # d?placement vers le bas
                if touche == 'Down':
                    PosY += 20
                    move=move-1
                    if (PosX==PosX1 and PosY==PosY1) or (PosX==PosX2 and PosY==PosY2) or (PosX==PosX3 and PosY==PosY3) :
                        PosY -= 20
                        move=move+1
                        lab1.config(text="pas possible")
                    else :
                        lab1.config(text="Sa roule")
        # d?placement vers la droite
                if touche == 'Right':
                    PosX += 20
                    move=move-1
                    if (PosX==PosX1 and PosY==PosY1) or (PosX==PosX2 and PosY==PosY2) or (PosX==PosX3 and PosY==PosY3) :
                        PosX -= 20
                        move=move+1
                        lab1.config(text="pas possible")
                    else :
                        lab1.config(text="Sa roule")
        # d?placement vers la gauche
                if touche == 'Left':
                    PosX -= 20
                    move=move-1
                    if (PosX==PosX1 and PosY==PosY1) or (PosX==PosX2 and PosY==PosY2) or (PosX==PosX3 and PosY==PosY3) :
                        PosX += 20
                        move=move+1
                        lab1.config(text="pas possible")
                    else :
                        lab1.config(text="Sa roule")
        # on dessine le pion ? sa nouvelle position
                Canevas.coords(Pion, PosX -10, PosY -10, PosX +10, PosY +10)
                lab2.config(text=move)
            else :
        # d?placement vers le haut
                    if touche == 'Up' or touche == 'Down' or touche == 'Right'  or touche == 'Left'  :
                        lab2.config(text='Impossible')
     
        elif actuTour == 1 :
            if move >0 :
        # d?placement vers le haut
                if touche == 'Up':
                    PosY1 -= 20
                    move=move-1
                    if (PosX1==PosX and PosY1==PosY) or (PosX1==PosX2 and PosY1==PosY2) or (PosX1==PosX3 and PosY1==PosY3) :
                        PosY1 += 20
                        move=move+1
                        lab1.config(text="pas possible")
                    else :
                        lab1.config(text="Sa roule")
        # d?placement vers le bas
                if touche == 'Down':
                    PosY1 += 20
                    move=move-1
                    if (PosX1==PosX and PosY1==PosY) or (PosX1==PosX2 and PosY1==PosY2) or (PosX1==PosX3 and PosY1==PosY3) :
                        PosY1 -= 20
                        move=move+1
                        lab1.config(text="pas possible")
                    else :
                        lab1.config(text="Sa roule")
        # d?placement vers la droite
                if touche == 'Right':
                    PosX1 += 20
                    move=move-1
                    if (PosX1==PosX and PosY1==PosY) or (PosX1==PosX2 and PosY1==PosY2) or (PosX1==PosX3 and PosY1==PosY3) :
                        PosX1 -= 20
                        move=move+1
                        lab1.config(text="pas possible")
                    else :
                        lab1.config(text="Sa roule")
        # d?placement vers la gauche
                if touche == 'Left':
                    PosX1 -= 20
                    move=move-1
                    if (PosX1==PosX and PosY1==PosY) or (PosX1==PosX2 and PosY1==PosY2) or (PosX1==PosX3 and PosY1==PosY3) :
                        PosX1 += 20
                        move=move+1
                        lab1.config(text="pas possible")
                    else :
                        lab1.config(text="Sa roule")
        # on dessine le pion ? sa nouvelle position
                Canevas.coords(Pionne, PosX1 -10, PosY1 -10, PosX1 +10, PosY1 +10)
                lab2.config(text=move)
            else :
        # d?placement vers le haut
                if touche == 'Up' or touche == 'Down' or touche == 'Right'  or touche == 'Left'  :
                    lab2.config(text='Impossible')
        elif actuTour == 2 :
            if move > 0 :
        # d?placement vers le haut
                if touche == 'Up':
                    PosY2 -= 20
                    move=move-1
                    if (PosX2==PosX and PosY2==PosY) or (PosX1==PosX2 and PosY1==PosY2) or (PosX2==PosX3 and PosY2==PosY3) :
                        PosY2 += 20
                        move=move+1
                        lab1.config(text="pas possible")
                    else :
                        lab1.config(text="Sa roule")
        # d?placement vers le bas
                if touche == 'Down':
                    PosY2 += 20
                    move=move-1
                    if (PosX2==PosX and PosY2==PosY) or (PosX1==PosX2 and PosY1==PosY2) or (PosX2==PosX3 and PosY2==PosY3) :
                        PosY2 -= 20
                        move=move+1
                        lab1.config(text="pas possible")
                    else :
                        lab1.config(text="Sa roule")
        # d?placement vers la droite
                if touche == 'Right':
                    PosX2 += 20
                    move=move-1
                    if (PosX2==PosX and PosY2==PosY) or (PosX1==PosX2 and PosY1==PosY2) or (PosX2==PosX3 and PosY2==PosY3) :
                        PosX2 -= 20
                        move=move+1
                        lab1.config(text="pas possible")
                    else :
                        lab1.config(text="Sa roule")
        # d?placement vers la gauche
                if touche == 'Left':
                    PosX2 -= 20
                    move=move-1
                    if (PosX2==PosX and PosY2==PosY) or (PosX1==PosX2 and PosY1==PosY2) or (PosX2==PosX3 and PosY2==PosY3) :
                        PosX2 += 20
                        move=move+1
                        lab1.config(text="pas possible")
                    else :
                        lab1.config(text="Sa roule")
        # on dessine le pion ? sa nouvelle position
                Canevas.coords(Pion1, PosX2 -10, PosY2 -10, PosX2 +10, PosY2 +10)
                lab2.config(text=move)
            else :
        # d?placement vers le haut
                    if touche == 'Up' or touche == 'Down' or touche == 'Right'  or touche == 'Left'  :
                        lab2.config(text='Impossible')
        elif actuTour == 3 :
            if move > 0 :
        # d?placement vers le haut
                if touche == 'Up':
                    PosY3 -= 20
                    move=move-1
                    if (PosX3==PosX and PosY3==PosY) or (PosX3==PosX2 and PosY3==PosY2) or (PosX1==PosX3 and PosY1==PosY3) :
                        PosY3 += 20
                        move=move+1
                        lab1.config(text="pas possible")
                    else :
                        lab1.config(text="Sa roule")
        # d?placement vers le bas
                if touche == 'Down':
                    PosY3 += 20
                    move=move-1
                    if (PosX3==PosX and PosY3==PosY) or (PosX3==PosX2 and PosY3==PosY2) or (PosX1==PosX3 and PosY1==PosY3) :
                        PosY3 -= 20
                        move=move+1
                        lab1.config(text="pas possible")
                    else :
                        lab1.config(text="Sa roule")
        # d?placement vers la droite
                if touche == 'Right':
                    PosX3 += 20
                    move=move-1
                    if (PosX3==PosX and PosY3==PosY) or (PosX3==PosX2 and PosY3==PosY2) or (PosX1==PosX3 and PosY1==PosY3) :
                        PosX3 -= 20
                        move=move+1
                        lab1.config(text="pas possible")
                    else :
                        lab1.config(text="Sa roule")
        # d?placement vers la gauche
                if touche == 'Left':
                    PosX3 -= 20
                    move=move-1
                    if (PosX3==PosX and PosY3==PosY) or (PosX3==PosX2 and PosY3==PosY2) or (PosX1==PosX3 and PosY1==PosY3) :
                        PosX3 += 20
                        move=move+1
                        lab1.config(text="pas possible")
                    else :
                        lab1.config(text="Sa roule")
        # on dessine le pion ? sa nouvelle position
                Canevas.coords(Pion2, PosX3 -10, PosY3 -10, PosX3 +10, PosY3 +10)
                lab2.config(text=move)
            else :
        # d?placement vers le haut
                    if touche == 'Up' or touche == 'Down' or touche == 'Right'  or touche == 'Left'  :
                        lab2.config(text='Impossible')
     
     
    def decompte(count=3):
        global actuTour
        lab.config(text=str(count))
        if count >= 0 :
            Mafenetre.after(1000,decompte, count-1)
        else :
            actuTour=actuTour+1
            Initialisation()
            if actuTour >= 4 :
                actuTour=0
            decompte()
     
    def Initialisation():
        global move,Dep1,Dep2,Dep3,Dep4, x
        if actuTour==0 :
            move = Dep1
        elif actuTour==1 :
            move = Dep2
        elif actuTour==2 :
            move = Dep3
        elif actuTour==3 :
            move = Dep4
        lab.config(text=move)
     
     
    move=3
    #déplacement des joueurs
    Dep1 = 3
    Dep2 = 7
    Dep3 = 2
    Dep4 = 4

    Merci d'avance

  2. #2
    Invité
    Invité(e)
    Par défaut
    J'ai oublié, si vous voulez tester le résultat (ici j'ai tout dans le même module)

    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
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    from Tkinter import *
    import time as t
     
    #******************************************************************************#
    #*                                                                            *#
    #*        *   *   *            VARIABLES INITIALES           *   *   *        *#
    #*                                                                            *#
    #******************************************************************************#
     
    # position initiale des champions
    PosX = 10
    PosY = 10
     
    PosX1 = 990
    PosY1 = 610
     
    PosX2 = 10
    PosY2 = 610
     
    PosX3 = 990
    PosY3 = 10
     
    #Variables g?n?rales
    actuTour=0
     
    def Clavier(event):
        """ Gestion de l'?v?nement Appui sur une touche du clavier """
        global PosX,PosY,PosX1,PosY1,PosX2,PosY2,PosX3,PosY3,move
        touche = event.keysym
        print(touche)
        if (actuTour == 0) :
            if move > 0 :
    # d?placement vers le haut
                if touche == 'Up':
                    PosY -= 20
                    move=move-1
                    if (PosX==PosX1 and PosY==PosY1) or (PosX==PosX2 and PosY==PosY2) or (PosX==PosX3 and PosY==PosY3) :
                        PosY += 20
                        move=move+1
                        lab1.config(text="pas possible")
                    else :
                        lab1.config(text="Sa roule")
        # d?placement vers le bas
                if touche == 'Down':
                    PosY += 20
                    move=move-1
                    if (PosX==PosX1 and PosY==PosY1) or (PosX==PosX2 and PosY==PosY2) or (PosX==PosX3 and PosY==PosY3) :
                        PosY -= 20
                        move=move+1
                        lab1.config(text="pas possible")
                    else :
                        lab1.config(text="Sa roule")
        # d?placement vers la droite
                if touche == 'Right':
                    PosX += 20
                    move=move-1
                    if (PosX==PosX1 and PosY==PosY1) or (PosX==PosX2 and PosY==PosY2) or (PosX==PosX3 and PosY==PosY3) :
                        PosX -= 20
                        move=move+1
                        lab1.config(text="pas possible")
                    else :
                        lab1.config(text="Sa roule")
        # d?placement vers la gauche
                if touche == 'Left':
                    PosX -= 20
                    move=move-1
                    if (PosX==PosX1 and PosY==PosY1) or (PosX==PosX2 and PosY==PosY2) or (PosX==PosX3 and PosY==PosY3) :
                        PosX += 20
                        move=move+1
                        lab1.config(text="pas possible")
                    else :
                        lab1.config(text="Sa roule")
        # on dessine le pion ? sa nouvelle position
                Canevas.coords(Pion, PosX -10, PosY -10, PosX +10, PosY +10)
                lab2.config(text=move)
            else :
        # d?placement vers le haut
                    if touche == 'Up' or touche == 'Down' or touche == 'Right'  or touche == 'Left'  :
                        lab2.config(text='Impossible')
     
        elif actuTour == 1 :
            if move >0 :
        # d?placement vers le haut
                if touche == 'Up':
                    PosY1 -= 20
                    move=move-1
                    if (PosX1==PosX and PosY1==PosY) or (PosX1==PosX2 and PosY1==PosY2) or (PosX1==PosX3 and PosY1==PosY3) :
                        PosY1 += 20
                        move=move+1
                        lab1.config(text="pas possible")
                    else :
                        lab1.config(text="Sa roule")
        # d?placement vers le bas
                if touche == 'Down':
                    PosY1 += 20
                    move=move-1
                    if (PosX1==PosX and PosY1==PosY) or (PosX1==PosX2 and PosY1==PosY2) or (PosX1==PosX3 and PosY1==PosY3) :
                        PosY1 -= 20
                        move=move+1
                        lab1.config(text="pas possible")
                    else :
                        lab1.config(text="Sa roule")
        # d?placement vers la droite
                if touche == 'Right':
                    PosX1 += 20
                    move=move-1
                    if (PosX1==PosX and PosY1==PosY) or (PosX1==PosX2 and PosY1==PosY2) or (PosX1==PosX3 and PosY1==PosY3) :
                        PosX1 -= 20
                        move=move+1
                        lab1.config(text="pas possible")
                    else :
                        lab1.config(text="Sa roule")
        # d?placement vers la gauche
                if touche == 'Left':
                    PosX1 -= 20
                    move=move-1
                    if (PosX1==PosX and PosY1==PosY) or (PosX1==PosX2 and PosY1==PosY2) or (PosX1==PosX3 and PosY1==PosY3) :
                        PosX1 += 20
                        move=move+1
                        lab1.config(text="pas possible")
                    else :
                        lab1.config(text="Sa roule")
        # on dessine le pion ? sa nouvelle position
                Canevas.coords(Pionne, PosX1 -10, PosY1 -10, PosX1 +10, PosY1 +10)
                lab2.config(text=move)
            else :
        # d?placement vers le haut
                if touche == 'Up' or touche == 'Down' or touche == 'Right'  or touche == 'Left'  :
                    lab2.config(text='Impossible')
        elif actuTour == 2 :
            if move > 0 :
        # d?placement vers le haut
                if touche == 'Up':
                    PosY2 -= 20
                    move=move-1
                    if (PosX2==PosX and PosY2==PosY) or (PosX1==PosX2 and PosY1==PosY2) or (PosX2==PosX3 and PosY2==PosY3) :
                        PosY2 += 20
                        move=move+1
                        lab1.config(text="pas possible")
                    else :
                        lab1.config(text="Sa roule")
        # d?placement vers le bas
                if touche == 'Down':
                    PosY2 += 20
                    move=move-1
                    if (PosX2==PosX and PosY2==PosY) or (PosX1==PosX2 and PosY1==PosY2) or (PosX2==PosX3 and PosY2==PosY3) :
                        PosY2 -= 20
                        move=move+1
                        lab1.config(text="pas possible")
                    else :
                        lab1.config(text="Sa roule")
        # d?placement vers la droite
                if touche == 'Right':
                    PosX2 += 20
                    move=move-1
                    if (PosX2==PosX and PosY2==PosY) or (PosX1==PosX2 and PosY1==PosY2) or (PosX2==PosX3 and PosY2==PosY3) :
                        PosX2 -= 20
                        move=move+1
                        lab1.config(text="pas possible")
                    else :
                        lab1.config(text="Sa roule")
        # d?placement vers la gauche
                if touche == 'Left':
                    PosX2 -= 20
                    move=move-1
                    if (PosX2==PosX and PosY2==PosY) or (PosX1==PosX2 and PosY1==PosY2) or (PosX2==PosX3 and PosY2==PosY3) :
                        PosX2 += 20
                        move=move+1
                        lab1.config(text="pas possible")
                    else :
                        lab1.config(text="Sa roule")
        # on dessine le pion ? sa nouvelle position
                Canevas.coords(Pion1, PosX2 -10, PosY2 -10, PosX2 +10, PosY2 +10)
                lab2.config(text=move)
            else :
        # d?placement vers le haut
                    if touche == 'Up' or touche == 'Down' or touche == 'Right'  or touche == 'Left'  :
                        lab2.config(text='Impossible')
        elif actuTour == 3 :
            if move > 0 :
        # d?placement vers le haut
                if touche == 'Up':
                    PosY3 -= 20
                    move=move-1
                    if (PosX3==PosX and PosY3==PosY) or (PosX3==PosX2 and PosY3==PosY2) or (PosX1==PosX3 and PosY1==PosY3) :
                        PosY3 += 20
                        move=move+1
                        lab1.config(text="pas possible")
                    else :
                        lab1.config(text="Sa roule")
        # d?placement vers le bas
                if touche == 'Down':
                    PosY3 += 20
                    move=move-1
                    if (PosX3==PosX and PosY3==PosY) or (PosX3==PosX2 and PosY3==PosY2) or (PosX1==PosX3 and PosY1==PosY3) :
                        PosY3 -= 20
                        move=move+1
                        lab1.config(text="pas possible")
                    else :
                        lab1.config(text="Sa roule")
        # d?placement vers la droite
                if touche == 'Right':
                    PosX3 += 20
                    move=move-1
                    if (PosX3==PosX and PosY3==PosY) or (PosX3==PosX2 and PosY3==PosY2) or (PosX1==PosX3 and PosY1==PosY3) :
                        PosX3 -= 20
                        move=move+1
                        lab1.config(text="pas possible")
                    else :
                        lab1.config(text="Sa roule")
        # d?placement vers la gauche
                if touche == 'Left':
                    PosX3 -= 20
                    move=move-1
                    if (PosX3==PosX and PosY3==PosY) or (PosX3==PosX2 and PosY3==PosY2) or (PosX1==PosX3 and PosY1==PosY3) :
                        PosX3 += 20
                        move=move+1
                        lab1.config(text="pas possible")
                    else :
                        lab1.config(text="Sa roule")
        # on dessine le pion ? sa nouvelle position
                Canevas.coords(Pion2, PosX3 -10, PosY3 -10, PosX3 +10, PosY3 +10)
                lab2.config(text=move)
            else :
        # d?placement vers le haut
                    if touche == 'Up' or touche == 'Down' or touche == 'Right'  or touche == 'Left'  :
                        lab2.config(text='Impossible')
     
     
    def decompte(count=3):
        global actuTour
        lab.config(text=str(count))
        if count >= 0 :
            Jeu.after(1000,decompte, count-1)
        else :
            actuTour=actuTour+1
            Initialisation()
            if actuTour >= 4 :
                actuTour=0
            decompte()
     
    def Initialisation():
        global move,Dep1,Dep2,Dep3,Dep4, x
        if actuTour==0 :
            move = Dep1
        elif actuTour==1 :
            move = Dep2
        elif actuTour==2 :
            move = Dep3
        elif actuTour==3 :
            move = Dep4
        lab.config(text=move)
     
     
    move=3
    #déplacement des joueurs
    Dep1 = 3
    Dep2 = 7
    Dep3 = 2
    Dep4 = 4
    #******************************************************************************#
    #*                                                                            *#
    #*        *   *   *            PROGRAMME  PRINCIPAL          *   *   *        *#
    #*                                                                            *#
    #******************************************************************************#
     
    # Creation de la fenetre windows
    Jeu=Tk()
    Jeu.title("CheckMat")
    Jeu.iconbitmap("CheckMate.ico")
    Jeu.geometry("1200x720")
    Jeu.resizable(width=False,height=False)
    Jeu.configure()
     
    # Creation des frames principales
    frame3 = Frame(Jeu, bg="yellow", width=200, height=720)
    frame3.pack(side=LEFT)
     
    frame1 = Frame(Jeu, bg="black",    width=1000, height=620)
    frame1.pack(side=BOTTOM)
    Canevas = Canvas(frame1, width = 1000, height =620, bg ='white')
    photo = PhotoImage(file="TRY.gif")
    Canevas.create_image(0, 0, anchor=NW, image=photo)
    Pion = Canevas.create_oval(PosX-10,PosY-10,PosX+10,PosY+10,width=2,outline='black',fill='red')
    Pionne = Canevas.create_oval(PosX1-10,PosY1-10,PosX1+10,PosY1+10,width=2,outline='black',fill='green')
    Pion1 = Canevas.create_oval(PosX2-10,PosY2-10,PosX2+10,PosY2+10,width=2,outline='black',fill='blue')
    Pion2 = Canevas.create_oval(PosX3-10,PosY3-10,PosX3+10,PosY3+10,width=2,outline='black',fill='yellow')
    for i in range(0,1000,20):
        Canevas.create_line(i,0,i,620,fill="#000000")
    for j in range(0,620,20):
        Canevas.create_line(0,j,1000,j,fill="#000000")
    Canevas.focus_set()
    Canevas.bind('<Key>',Clavier)
    Canevas.pack()
     
    frame2 = Frame(Jeu, bg="blue",   width=1000, height=100)
    frame2.pack()
     
    # Creation des frames secondaires
        # Frames de gauches
    framea = Frame(frame3, bg="yellow",width=200, height=70)
    framea.pack (side=TOP)
    photo1 = PhotoImage(file="test1.gif")
    Canfra = Canvas(framea, width=200, height=70, bg="yellow")
    item1 = Canfra.create_image(0,0,anchor=NW, image=photo1)
    print(item1)
    Canfra.pack()
     
    frameb = Frame(frame3, bg="green",width=200, height=90)
    frameb.pack (side=TOP)
     
    framec = Frame(frame3, bg="red",width=200, height=90)
    framec.pack (side=TOP)
     
    framed = Frame(frame3, bg="green",width=200, height=340)
    framed.pack (side=TOP)
     
    framee = Frame(frame3, bg="white",width=200, height=130)
    framee.pack ()
     
    # Frames du haut
        # Frames de la longueur
    framef = Frame(frame2, bg="red",width=90, height=100)
    framef.pack (side=LEFT)
     
    frameg = Frame(frame2, bg="green",width=90, height=100)
    frameg.pack (side=LEFT)
     
    frameh = Frame(frame2, bg="white",width=450, height=100)
    frameh.pack (side=LEFT)
     
    framei = Frame(frame2, bg="green",width=380, height=100)
    framei.pack ()
     
     
        # Frames des sorts/tour
    framej = Frame(frameh, bg="black",width=450, height=65)
    framej.pack (side=TOP)
     
    framek = Frame(frameh, bg="white",width=450, height=35)
    framek.pack (expand=1)
     
    framel = Frame(framej, bg="red",width=90, height=65)
    framel.pack (side=LEFT)
     
    framem = Frame(framej, bg="black",width=90, height=65)
    framem.pack (side=LEFT)
     
    framen = Frame(framej, bg="red",width=90, height=65)
    framen.pack (side=LEFT)
     
    frameo = Frame(framej, bg="black",width=90, height=65)
    frameo.pack (side=LEFT)
     
    framep = Frame(framej, bg="red",width=90, height=65)
    framep.pack ()
     
    lab1=Label(frameb, text="")
    lab1.pack()
    btn=Button(framec, text="go", command=decompte)
    btn.pack()
    lab=Label(framed, text="")
    lab.pack()
    lab2=Label(framee, text="")
    lab2.pack()
     
     
     
    Jeu.mainloop()

  3. #3
    Expert éminent

    Homme Profil pro
    Inscrit en
    Octobre 2008
    Messages
    4 300
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Octobre 2008
    Messages : 4 300
    Points : 6 780
    Points
    6 780
    Par défaut
    Salut,

    Je ne comprend pas le but du jeu donc je ne vois pas ce que tu attend et qui n'arrive pas.

    Quoiqu'il en soit, les pions ne semblent se déplacer que selon leur humeur, ce qui ne convient pas je suppose.

    Après quelques modifications ça va beaucoup mieux.

    Remplace tes horribles 200 lignes de la fonction clavier par ceci:
    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
     
    actuTour=0
    positions = [[10, 10], [990, 610], [10, 610], [990, 10]]
    vertical = {'Up': -20, 'Down': 20}
    horizontal = {'Right': 20, 'Left': -20}
    players = (pion, pionne, pion1, pion2)  # A créer d'abord !
     
    def Clavier(event):
        """ Gestion de l'?v?nement Appui sur une touche du clavier """
        touche = event.keysym
        pos = positions[actuTour][:]
        if touche in vertical:
            pos[1] += vertical[touche]
            if pos[1] < 0 or pos[1] > 610:
                # Hors jeu
                return
     
        elif touche in horizontal:
            pos[0] += horizontal[touche]
            if pos[0] < 0 or pos[0] > 990:
                # Hors jeu
                return
     
        if pos in positions:
            # case déjà occupée
            return
     
        positions[actuTour] = pos
        Canevas.coords(players[actuTour], pos[0] -10, pos[1] -10, pos[0] +10, pos[1] +10)
    Mets un encodage en première ligne de ton code.

    Et surtout remplace "Sa roule" par "Ca roule", ça jure.

  4. #4
    Invité
    Invité(e)
    Par défaut
    Salut, désolé pour les fautes dans le code Ici, le jeu n'est pas du tout terminé, le but sera de faire un jeu stratégique au tour par tour, donc dans ce code, je teste juste à faire les déplacement, quand tu dis qu'ils se déplacent comme ils le souhaitent, c'est faux, ils respectent un ordre précis. Il y a un décompte qui tourne en boucle, lorsqu'il arrive à 0, il redémarre, c'est donc au tour du pion suivant. Ceci est la première partie, ensuite, chaque pion a des déplacements limités, donc on peut d'abord bouger le pion rouge puis après le pion vert.
    Ceci dit, j'apprécie ta réponse et je vais rajouter le fait qu'ils ne puissent pas sortir de la carte actuelle

    Ha oui et qu'est-ce qu'un encodage ?

  5. #5
    Expert éminent

    Homme Profil pro
    Inscrit en
    Octobre 2008
    Messages
    4 300
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Octobre 2008
    Messages : 4 300
    Points : 6 780
    Points
    6 780
    Par défaut
    L'encodage est la première ou deuxième ligne d'un script servant à indiquer l'encodage du fichier.

    Sous Linux on trouve généralement ceci:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    # -*- coding: utf-8 -*-
    Sous Windows:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    # -*- coding: latin-1 -*-
    Une PEP décrit ceci.
    http://legacy.python.org/dev/peps/pep-0263/

    Pour ton jeu, quand je l'ai testé les pions ne se déplaçaient pas toujours dans la direction demandée et le pion du haut à gauche n'est jamais apparu.

    Pour la question d'empêcher les pions de sortir du plateau de jeu, c'est déjà inclus dans mon code.

Discussions similaires

  1. Problème chargement module apache
    Par tom79 dans le forum Apache
    Réponses: 1
    Dernier message: 05/03/2008, 12h04
  2. [Delphi 7 et 5] Problème de module Base de données
    Par riadmega dans le forum Débuter
    Réponses: 1
    Dernier message: 25/11/2007, 11h26
  3. problème de module menu jlomenu
    Par nemo666 dans le forum EDI, CMS, Outils, Scripts et API
    Réponses: 11
    Dernier message: 23/04/2007, 22h51
  4. Problème du module login lors du changement de mot de passe
    Par MIC94 dans le forum Wildfly/JBoss
    Réponses: 1
    Dernier message: 31/08/2006, 11h51
  5. [Oracle9i]Problème création module d'écoute
    Par Gidrah dans le forum Oracle
    Réponses: 2
    Dernier message: 25/04/2006, 19h32

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