Probleme sur python

Fermé
wolfnight28 Messages postés 1 Date d'inscription mercredi 15 mars 2017 Statut Membre Dernière intervention 15 mars 2017 - 15 mars 2017 à 18:50
_Ritchi_ Messages postés 21215 Date d'inscription samedi 17 mars 2007 Statut Contributeur Dernière intervention 7 mai 2024 - 16 mars 2017 à 08:18



Bonjour a tous je suis un eleve de terminale S et je suis en option ISN et j ai pour bit de creer un jeux de mon choix sur python. J 'ai choisi un jeu simple : un tableau en mouvement sur un fond et un pistolet a clou le but est de clouer le tableau rien de plus simple. Pour l 'instant j ai commencer et j ai fait en sorte que le clic de la souris creer une image d un clou a l endroit cliqué sur un fichier python et sur un deuxieme j ai mis une image en mouvement. Maintenant je dois les relier pour en faire qu un fichier avec une image en mouvement et un clou qui apparait a l'endroit ou l'on clique. Le probleme est que, a la fin, si je met l'initialisation avant le fen1.mainloop l image ne bouge pas et lorsque j inverse les deux l 'image bouge mais le clou n apparait pas au clic. Comment faire en sorte de combiner ces deux options ? voici une capture d ecran de mon programme.

1 réponse

_Ritchi_ Messages postés 21215 Date d'inscription samedi 17 mars 2007 Statut Contributeur Dernière intervention 7 mai 2024 6 058
16 mars 2017 à 08:18
Bonjour,

Voici ma proposition en Python3.6
Note: n'ayant pastes images, j'en ai mis d'autres: à toi de remettre les tiennes.

# -*- coding: utf-8 -*-

from tkinter import *

def initialisation():
    global photoL, imageL, personnage1,  photoH, imageH, personnage2, photoD,  imageD

    photoD = PhotoImage(file="MonDecor.gif")
    imageD = can1.create_image(406,305, image=photoD)
    photoH = PhotoImage(file="MonClou.gif")
    imageH = can1.create_image(50,50,anchor=CENTER, image=photoH)
    photoL = PhotoImage(file="MonTableau.gif")
    imageL = can1.create_image(60,50,anchor=CENTER, image=photoL)
    can1.pack()

    personnage1 = "Clou"
    personnage2 = "Tableau"

def depl_clou(x,y):
    print("depl_clou")
    can1.coords(imageH, x, y)


def config_clou():
    global personnage1
    print("config_clou")
    personnage1="Clou"

def clic(event):
    global personnage1
    print("clic")
    if personnage1 == "Clou":
        depl_clou(event.x, event.y)

def move():
    global x1, y1, dx, dy, flag
    print("move")

    if x1 > 210:
        x1, dx, dy = 210, 0, 15
    if y1 > 210:
        y1, dx, dy = 210, -15, 0
    if x1 < 10:
        x1, dx, dy = 10, 0, -15
    if y1 < 10:
        y1, dx, dy = 10, 15, 0

    x1, y1 = x1+dx, y1+dy
    can1.coords(imageL, x1, y1)
    if flag >0:
        print("flag>0")
        fen1.after(50,move)

def stop_it():
    global flag
    print("stop_it")
    flag=0

def start_it():
    global flag
    print("start_it")
    if flag == 0:
        flag=1
        move()

print("init")
x1, y1 = 10, 10
dx, dy = 15, 0
flag = 0

fen1 = Tk()
can1 = Canvas(fen1, bg='dark grey', height=610, width=812)
can1.pack(side=LEFT, padx=5, pady=5)
initialisation()

# oval1 = can1.create_image(20,50,anchor=CENTER,image=photoL)
bou1 = Button(fen1, text="Quitter", width=8, command=fen1.quit)
bou1.pack()
bou2 = Button(fen1, text="Démarrer", width=8, command=start_it)
bou2.pack()
bou3 = Button(fen1, text="Arrêter", width=8, command=stop_it)
bou3.pack()
can1.bind("<Button-1>", clic)


fen1.mainloop()


Qu'ai-je changé ?
J'ai déplacé la ligne "x1, y1 = x1+dx, y1+dy", supprimé ce qui concernait "oval1" car c'est en fait "imageL", déplacé "initialisation()", remplacé dans clic() "global personnage" par "global personnage1" mais je ne vois pas l'intérêt de ce global ni du test "if personnage1 == "Clou":", ...

Ritchi
0