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 :

jeu tron python


Sujet :

Tkinter Python

  1. #1
    Nouveau Candidat au Club
    Homme Profil pro
    Lycéen
    Inscrit en
    Mai 2020
    Messages
    1
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Lycéen

    Informations forums :
    Inscription : Mai 2020
    Messages : 1
    Points : 1
    Points
    1
    Par défaut jeu tron python
    bonjour je suis en classe de première et j'ai un mini projet à terminer et j'ai choisi avec des potes de faire le jeu tron à l aide de tkinter mais nous somme blockés, nous n arrivons pas à faire programme pour qu une ligne puisse suivre la moto et impacter le 2 second joueur
    voici le programme que nous avons commencé à faire.

    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
    from tkinter import*
    import tkinter as tk
    # procédure générale de déplacement :
    def avance(gd, hb):
         global x1, y1
         x1, y1 = x1 +gd, y1 +hb
         can1.coords(oval1, x1,y1, x1+30,y1+30)
    def avance2(gd, hb):
         global x2, y2
         x2, y2 = x2 +gd, y2 +hb
         can1.coords(oval2, x2,y2, x2+30,y2+30)
    #comparaison des coordoner
    def triSelection_2(a) :
        n = len(a)
        for i in range(0,n-1,2) : 
            k = i
            for j in range(i+1,n-1,2) :
                if a[k] > a[j] : k = j
            a[k],a[i] = a[i],a[k]
            a[k+1],a[i+1]=a[i+1],a[k+1]
    def comp_1(y,z):
         y=co1
         z=co2
         indice=[]
         element=co1[-2]
         triSelection_2(co1)
         triSelection_2(co2)
         prer=[]
         a = 0
         b = len(co2)-1
         m = (a+b)//2
         while a < b :
             if co2[m] == element :
                 return m
             elif co2[m] > element :
                 b = m-1
             else :
                 a = m+1
             m = (a+b)//2
             indice.append(a)
             return a
         for i in indice:
              prer.append(co2[i+1])
         element=co2[-1]
         a=0
         while a < b :
             if prer[m] == element :
                 return m
             elif prer[m] > element :
                 b = m-1
             else :
                 a = m+1
             m = (a+b)//2
             return a
             if a!=0:
                  print("game over")
         print(a)
     
    def comp_2(co1,co2):
         indice=[]
         element=co1[-2]
         triSelection_2(co1)
         triSelection_2(co2)
         prer=[]
         a = 0
         b = len(co2)-1
         m = (a+b)//2
         while a < b :
             if co2[m] == element :
                 return m
             elif co2[m] > element :
                 b = m-1
             else :
                 a = m+1
             m = (a+b)//2
             indice.append(a)
             return a
         for i in indice:
              prer.append(co1[i+1])
         element=co1[-1]
         a=0
         while a < b :
             if prer[m] == element :
                 return m
             elif prer[m] > element :
                 b = m-1
             else :
                 a = m+1
             m = (a+b)//2
             return a
             if a!=0:
                  print("game over")
         print(a)
    # gestionnaires d'événements :
    def depl_gauche(event):
         avance(-10, 0)
         co1.append(x1)
         co1.append(y1)
         comp_1(co1,co2)
         comp_2(co1,co2)
    def depl_droite(event):
         avance(10, 0)
         co1.append(x1)
         co1.append(y1)
         comp_1(co1,co2)
         comp_2(co1,co2)
    def depl_haut(event):
         avance(0, -10)
         co1.append(x1)
         co1.append(y1)
         comp_1(co1,co2)
         comp_2(co1,co2)
    def depl_bas(event):
         avance(0, 10)
         co1.append(x1)
         co1.append(y1)
         comp_1(co1,co2)
         comp_2(co1,co2)
    def depl_gauche2(event):
         avance2(-10, 0)
         co2.append(x2)
         co2.append(y2)
         comp1(co1,co2)
         comp2(co1,co2)
    def depl_droite2(event):
         avance2(10, 0)
         co2.append(x2)
         co2.append(y2)
         comp_1(co1,co2)
         comp_2(co1,co2)
    def depl_gauche2(event):
         avance2(-10, 0)
         co2.append(x2)
         co2.append(y2)
         comp_1(co1,co2)
         comp_2(co1,co2)
    def depl_haut2(event):
         avance2(0, -10)
         co2.append(x2)
         co2.append(y2)
         comp_1(co1,co2)
         comp_2(co1,co2)
    def depl_gauche2(event):
         avance2(-10, 0)
         co2.append(x2)
         co2.append(y2)
         comp_1(co1,co2)
         comp_2(co1,co2)
    def depl_bas2(event):
         avance2(0, 10)
         co2.append(x2)
         co2.append(y2)
         comp_1(co1,co2)
         comp_2(co1,co2)
    def depl_gauche2(event):
         avance2(-10, 0)
         co2.append(x2)
         co2.append(y2)
         comp_1(co1,co2)
         comp_2(co1,co2)
    #------ Programme principal -------
    # les variables suivantes seront utilisées de manière globale :
    x1, y1 = 10, 10 # coordonnées initiales
    x2, y2 = 20,20
    co1=[]
    co2=[]
    co1.append(x1)
    co1.append(y1)
    co2.append(x2)
    co2.append(y2)
    # Création du widget principal ("maître") :
    fen1 = Tk()
    fen1.title("Exercice d'animation avec tkinter")
     
    # création des widgets "esclaves" :
    app = tk.Tk()
    can1 = Canvas(fen1,bg='dark grey',height=600,width=600)
    oval1 = can1.create_oval(x1,y1,x1+3,y1+3,width=1,fill='red')
    can1.pack(side=LEFT)
    Button(fen1,text='Quitter',command=fen1.quit).pack(side=BOTTOM)
    app.bind("<KeyPress-d>",depl_gauche)
    app.bind("<KeyPress-x>",depl_droite)
    app.bind("<KeyPress-z>",depl_haut)
    app.bind("<KeyPress-s>",depl_bas)
    oval2 = can1.create_oval(x2,y2,x2+3,y2+3,width=1,fill='red')
    app.bind("<KeyPress-l>",depl_gauche2)
    app.bind("<KeyPress-m>",depl_droite2)
    app.bind("<KeyPress-p>",depl_haut2)
    app.bind("<KeyPress-:>",depl_bas2)
    # démarrage du réceptionnaire d’évènements (boucle principale) :
    fen1.mainloop()
    merci d avance de votre aide

  2. #2
    Expert éminent sénior
    Homme Profil pro
    Architecte technique retraité
    Inscrit en
    Juin 2008
    Messages
    21 313
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Manche (Basse Normandie)

    Informations professionnelles :
    Activité : Architecte technique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2008
    Messages : 21 313
    Points : 36 819
    Points
    36 819
    Par défaut
    Salut,

    Créer un disque, le faire bouger, et tracer une ligne qui suit les déplacements, ... peut être utilisé pour réaliser un jeu de Tron (ou n'importe quoi d'autre).
    On peut écrire çà comme çà:
    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
    import tkinter as tk
     
    motion = {
        'q': (-10, 0),
        's': (10, 0),
        'z': (0, -10),
        'w': (0, 10),
        }
     
    def on_key(event):
     
        if not event.char in motion.keys():
            return
        dx, dy = motion[event.char]
        canvas.move('ball', dx, dy)
     
        x0, y0, x1, y1 = canvas.bbox('ball')
        c0 = (x0+x1)/2
        c1 = (y0+y1)/2
        line.extend([c0, c1])
        canvas.coords('line', *line)
     
    canvas = tk.Canvas(width=600, height=600)
    canvas.pack()
     
    canvas.create_oval(5, 5, 15, 15, fill='red', tag='ball')
    line = [10, 10, 10, 10]
    canvas.create_line(*line, tag='line')
    canvas.bind('<Key>', on_key)
    canvas.focus_set()
    tk.mainloop()
    à essayer de comprendre et à adapter suivant vos besoins.

    - W

Discussions similaires

  1. interface graphique jeu TRON 3D
    Par aedh04 dans le forum 3D
    Réponses: 1
    Dernier message: 19/02/2011, 00h03
  2. Simple Jeu en python
    Par moithibault dans le forum Programmation multimédia/Jeux
    Réponses: 29
    Dernier message: 30/06/2009, 19h37
  3. Problème jeu hunter python
    Par florichoute dans le forum Tkinter
    Réponses: 1
    Dernier message: 20/05/2009, 14h00
  4. Jeu en Python
    Par Milo69 dans le forum Programmation multimédia/Jeux
    Réponses: 2
    Dernier message: 31/10/2006, 12h16

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