from tkinter import *
carte = [
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 1, 0, 1, 1, 0, 1, 0, 1],
[0, 0, 0, 0, 1, 2, 0, 1, 0, 1],
[0, 1, 0, 1, 1, 1, 1, 1, 0, 2],
[0, 1, 0, 0, 0, 0, 0, 1, 1, 1],
[0, 1, 1, 1, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 0],
[1, 1, 0, 1, 0, 0, 0, 0, 1, 0],
[2, 1, 0, 1, 0, 1, 1, 0, 1, 1],
[0, 0, 0, 1, 0, 2, 1, 0, 0, "f"]]
xjoueur = 0
yjoueur = 0
tx = 0
ty = 0
score = 0
fen = Tk()
canva = Canvas(fen, width=200, height=200, bg="ivory")
label = Label(fen, text="Score: " + str(score))
label.pack(side = TOP)
canva.pack()
def affichercarte():
canva.delete("all")
global xjoueur, yjoueur, rect
rect = canva.create_rectangle(xjoueur*20, yjoueur*20, xjoueur*20+20, yjoueur*20+20,fill="red")
y = 0
for ligne in carte:
x = 0
for colone in ligne:
if carte[y][x] == 1:
m = canva.create_rectangle(x*20, y*20, x*20+20, y*20+20, fill="black")
canva.pack()
if carte[y][x] == 2:
m = canva.create_rectangle(x*20+5, y*20+5, x*20+20-5, y*20+20-5, fill="yellow")
if carte[y][x] == "f":
canva.create_rectangle(x*20, y*20, x*20+20, y*20+20, fill="green")
x+= 1
y += 1
def mouvement(direction):
global xjoueur,yjoueur,tx,ty
tx = xjoueur
ty = yjoueur
if direction == "z" and yjoueur>0:
ty -= 1
if direction == "s" and yjoueur<9:
ty += 1
if direction == "d" and xjoueur<9:
tx += 1
if direction == "q" and xjoueur>0:
tx -= 1
if carte[ty][tx] != 1:
return True
def clavier(touche):
global yjoueur, xjoueur, ty, tx, score
if mouvement(touche.keysym):
xjoueur = tx
yjoueur = ty
if carte[yjoueur][xjoueur] == 2:
score += 1
carte[yjoueur][xjoueur] = 0
affichercarte()
label.config(text="Score: " + str(score))
canva.coords(rect, xjoueur*20, yjoueur*20, xjoueur*20+20, yjoueur*20+20)
affichercarte()
canva.focus_set()
canva.bind("<Key>", clavier)
canva.pack()
fen.mainloop()