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
| # -*- coding: utf-8 -*-
from Tkinter import *
from random import randint
def Clavier(event):
""" Gestion de l'événement Appui sur une touche du clavier """
global PosX,PosY
touche = event.char
# déplacement vers le haut
if touche == "z":
PosY -= 20
if PosY<0:
PosY = 0
# déplacement vers le bas
if touche == "s":
PosY += 20
if PosY>320:
PosY = 320
# déplacement vers la droite
if touche == "d":
PosX += 20
if PosX>480:
PosX = 480
# déplacement vers le haut
if touche == "q":
PosX -= 20
if PosX<0:
PosX = 0
# on dessine le pion à sa nouvelle position
Canevas.coords(Pion,PosX -10, PosY -10, PosX +10, PosY +10)
#creation de la fonction qui cache le pion
def cache():
X=0
Y=40
for loopp in range (24):
while X<=480:
B=randint(0,16)*20
while Y<=320:
carre = Canevas.create_rectangle(X-10,Y-10,X+10,Y+10,width=2,fill='green')
carre2=Canevas.create_rectangle(X-10, B-10, X+10, B+10, width=2, fill='black')
Y=Y+20
X=X+20
if X>=40:
Y=0
if X<40:
Y=40
# Création de la fenêtre principale
Mafenetre = Tk()
Mafenetre.title('Pion')
# position initiale du pion
PosX = 0
PosY = 0
# Création d'un widget Canvas (zone graphique)
Largeur = 480
Hauteur = 320
Canevas = Canvas(Mafenetre, width = Largeur, height =Hauteur, bg ='white')
Pion = Canevas.create_oval(PosX-10,PosY-10,PosX+10,PosY+10,width=2,fill='red')
cache()
Canevas.focus_set()
Canevas.bind('<Key>',Clavier)
Canevas.grid(row=0, column = 0, columnspan =3,padx = 5, pady = 5)
# Création d'un widget Button (bouton Quitter)
Button(Mafenetre, text ='Quitter', command = Mafenetre.destroy).grid(row=1, column = 3, padx = 5, pady = 5)
Mafenetre.mainloop() |
Partager