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 80 81 82
| import tkinter as tk
import random
racine = tk.Tk()
racine.title("Jeu")
COULEUR_FOND = "rosybrown"
COULEUR_QUADR = "grey"
COULEUR_MUR = "black"
LARGEUR = 800
HAUTEUR = 800
COTE = 50
NB_COL = LARGEUR // COTE
NB_LINE = HAUTEUR // COTE
tableau = None
objets = []
couleurs = ["red"]
def quadrilage():
"""Affiche un quadrilage sur le canvas."""
x0, x1 = 0, LARGEUR
y = 0
while y <= HAUTEUR:
canvas.create_line(x0, y, x1, y, fill=COULEUR_QUADR)
y += COTE
y0, y1 = 0, LARGEUR
x = 0
while x <= LARGEUR:
canvas.create_line(x, y0, x, y1, fill=COULEUR_QUADR)
x += COTE
def pion_rouge():
global objets
x = random.randint(40, 700 + 1)
y = random.randint(40, 700 + 1)
objets.append ( canvas.create_oval (( x,y), (x+40, y+40), fill="red"))
def pion_vert():
global objets
x = random.randint(40, 700 + 1)
y = random.randint(40, 700 + 1)
objets.append(canvas.create_oval (( x,y), (x+40, y+40), fill="green" ))
def pion_jaune():
global objets
x = random.randint(40, 700 + 1)
y = random.randint(40, 700 + 1)
objets.append(canvas.create_oval((x,y), (x+40, y+40), fill = "yellow"))
def pion_bleu():
global objets
x = random.randint(40, 700 + 1)
y = random.randint(40, 700 + 1)
objets.append(canvas.create_oval((x,y), (x+40, y+40), fill = "blue"))
def undo():
global objets
canvas.delete(objets[-1:])
objets = objets[:-1]
bouton_pion_rouge = tk.Button(racine, text="Pion Rouge", command=pion_rouge)
bouton_pion_vert = tk.Button(racine, text="Pion Vert", command=pion_vert)
bouton_pion_jaune = tk.Button(racine, text="Pion Jaune", command=pion_jaune)
bouton_pion_bleu = tk.Button(racine, text="Pion Bleu", command=pion_bleu)
bouton_undo = tk.Button(racine, text="Undo", command=undo)
canvas = tk.Canvas(racine, width=LARGEUR, height=HAUTEUR, bg=COULEUR_FOND)
quadrilage()
bouton_pion_rouge.grid(column=0, row=1)
bouton_pion_vert.grid(column=0, row=2)
bouton_pion_jaune.grid(column=0, row=3)
bouton_pion_bleu.grid(column=0, row=4)
bouton_undo.grid(column=0, row=20)
canvas.grid(column=1, row=1, columnspan=3, rowspan=30)
racine.mainloop() |
Partager