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
| #!/usr/bin/env python3
# -*- coding: utf-8 -*-
import random # utile ? pour quoi ?
from tkinter import *
def clavier(event):
""" Gestion de l'événement Appui sur une touche du clavier """
global posX, posY
touche = event.keysym
# déplacement vers le haut
if touche in ('z', 'Up'):
posY -= 10
# déplacement vers le bas
if touche in ('s', 'Down'):
posY += 10
# déplacement vers la droite
if touche in ('d', 'Right'):
posX += 10
# déplacement vers la gauche
if touche in ('q', 'Left'):
posX -= 10
# Repositionnement du pion
canvas.coords(pion, posX-5, posY-5, posX+5, posY+5)
# end def
def dessiner_decor (chemin_fichier):
""" dessin du labyrinthe """
global largeur_canevas, hauteur_canevas
global largeur_carre, hauteur_carre
largeur_canevas = canvas.winfo_reqwidth() - 1
hauteur_canevas = canvas.winfo_reqheight() - 1
with open(chemin_fichier) as fichier:
data = fichier.readlines()
lignes = len(data)
hauteur_carre = 10
for ligne in range(lignes):
ligne_data = data[ligne].strip("\r\n")
colonnes = len(ligne_data)
largeur_carre = 10
for colonne in range(colonnes):
if ligne_data[colonne] == "B":
x = colonne * largeur_carre
y = ligne * hauteur_carre
canvas.create_rectangle(
x, y, x+largeur_carre, y+hauteur_carre,
fill="black", outline="black",
)
# end if - data "B"
# end for - colonne
# end for - ligne
# end with - fichier
# end def
# Création de la fenêtre
fenetre = Tk()
# Création d'un widget Canvas
canvas = Canvas(fenetre, width=520, height=200, highlightthickness=0, bg="white")
canvas.pack(padx=5, pady=5)
dessiner_decor("Laby.txt")
Button(fenetre, text="Quitter", command=fenetre.destroy).pack(padx=5, pady=5)
# Position du pion au départ
posX = 15
posY = 185
pion = canvas.create_rectangle(posX-5, posY-5, posX+5, posY+5, width=1, outline='black', fill='medium purple')
# événements clavier
fenetre.bind_all('<Key>', clavier)
# boucle principale
fenetre.mainloop() |