Bonjour, je suis débutante en tkinter. Je n'arrive pas à faire avancer mon carré ( perso ) , il avance et recule , donc reste dans la même place . j'explique un peu le but de mon jeux. Le 'carrée' va devenir un personnage qui doit se déplacer pour attraper les 'pommes' mais ne doit pas toucher les rectacles ( qui ne sont pas présent dans ce code ) Voici mon code :
merci

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
from tkinter import *
from random import randrange
def nouveau_jeu():
    a=0
def rectangle():
    d=0
    b=2
    c=10
    while d<3:
       can1.create_rectangle(10+c, 10+c, 100+c, 100+c)
       d=d+1
       c=c+100
def pomme(): #ce quelle va manger, pomme aléatoire
    a=0
    x=randrange(840)
    y=randrange(480)
    i=0
    i2=0
    while i<rectangle:
        i2=1
def perso(): #personnage
    global dx, dy, x, y, nana, direction
    nana=can1.create_rectangle(20,20,50,50)
 
def avant(event):
    global dx, dy, nana, direction
    can1.delete(nana)
    direction=1
    dx=0
    dy=-10
    nana=can1.create_rectangle(20+10, 20, 50+10, 50+10)
def gauche(event):
    global dx, dy, nana
    can1.delete(nana)
    direction=2
    dx=-10
    dy=0
    nana=can1.create_rectangle(20+dx, 20+dy, 50+dx, 50+dy)
def droite(event):
    global dx, dy, nana
    can1.delete(nana)
    direction=3
    dx=10
    dy=0
    nana=can1.create_rectangle(20+dx, 20+dy, 50+dx, 50+dy)
    direction=0
def bas(event):
    global dx, dy, nana
    can1.delete(nana)
    dx=0
    dy=10
    nana=can1.create_rectangle(20+dx, 20+dy, 50+dx, 50+dy)
 
fen1=Tk()
fen1.title("SUPER NANA ! :D")
can1= Canvas (fen1, bg='blue', height=480, width=840)
can1.grid(row=0, column=0, rowspan=10)
can1.bind_all("<Up>", avant)
can1.bind_all("<Right>", droite)
can1.bind_all("<Down>", bas)
can1.bind_all("<Left>", gauche)
Button(fen1, text="Nouveau Jeu", font=("ComicSansMS"), command=perso).grid(row =4, column=1, sticky=N, padx=5)
Button(fen1, text="Quitter", font=("ComicSansMS"),command=fen1.destroy).grid(row =6, column=1, sticky=N)
x=randrange(840)
y=randrange(480)
dx=0
dy=0
direction=0
score=0
fen1.mainloop()