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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
# --- Importer les bibliothèques utilisées ---- #
import tkinter as tk # pour Python 2.x, c'est Tkinter (majuscule initiale)
import random
import time
# --- Variables --- #
w_fen, h_fen = 500, 400 # Dimensions de notre zone graphique.
x = random.randint(0,500) # Position x aléatoire des balles.
y = random.randint(0,400) # Position y aléatoire des balles.
r = 8 # Rayon de la balle.
n = 0
dx = random.randint(-5,5) # Etape 4 : Rebond aléatoire.
dy = random.randint(-5,5) # Etape 4 : Rebond aléatoire.
while n != 1 : # Empecher d'avoir le 0 comme valeur et ainsi empecher un non mouvement possible d'une balle.
if dx == 0 or dy == 0 :
dx = random.randint(-5,5) # Etape 4 : Rebond aléatoire.
dy = random.randint(-5,5) # Etape 4 : Rebond aléatoire.
n = - 1
n = n + 1
n = 1
a = 0
b = 150
c = 10
d = 100
drapeau = False
# --- Fonctions de modification de l'interface graphique --- #
def fin(event=None):
"Fin anticipée (Maj+Ctrl+Fin)."
fen.destroy()
def arret(event=None):
global drapeau
drapeau = True
def reprise(event=None):
global drapeau
drapeau = False
def left (event=None) :# descend
can.move("rec",0,10)
b = b + 10
d = d + 10
print(d)
return d and b
def right (event=None) : # monte
can.move("rec",0,-10)
b = b - 10
d = d - 10
print(d)
return d and b
# --- Fenêtre principale --- #
fen = tk.Tk() # Crée la fenêtre principale de l'application.
fen.title("Boules & Billes...")
tit = tk.Label(fen, text="Boules & Billes :") # Un petit titre...
tit.pack(side=tk.TOP)
can = tk.Canvas(fen) # Puis une surface 'pour dessiner' à l'intérieur.
can.configure(width=w_fen, height=h_fen, bg='white')
can.pack(side=tk.TOP)
# --- COMMANDES --- #
boutQuit=tk.Button(fen, text="Quitter", command=fin).pack(side=tk.BOTTOM)
fen.bind("<Shift-Control-End>", fin)
fen.bind("<Return>",arret)
fen.bind("<space>",reprise)
fen.bind("<Left>",left)
fen.bind("<Right>",right)
# --- FONCTIONS --- #
def cercle(x, y, r, nom = "balle"): #Fonction cercle
a = x - r
b = y - r
c = x + r
d = y + r
return can.create_oval((a,b,c,d), fill="yellow", outline="black", tag = nom)
def rec (nom1 = "rec"):
return can.create_rectangle(a,b,c,d, fill='black', outline='black', width=5, tag = nom1)
# --- PARTIE PRINCIPALE DU CODE --- #
cercle(x,y,r)
rec()
while 1 : # Boucle infinie.
# --- GESTION DU MOUVEMENT --- #
can.move("balle",dx,dy) # Déplacement de la balle selon dx et dy.
fen.update() # Actualisation de la fenetre graphique.
fen.after(10) # Vitesse de la balle.
# --- GESTION DES REBONDS SUR LES BORDS --- #
if 490 < x < 505 or -5 < x < 10 : # Si la balle touche le bord en x ...
dx = -dx # Gestion du rebord en prenant l'inverse de dx pour un rebond coherent.
if 390 < y < 405 or -5 < y < 10 : # Si la balle touche le bord en y ...
dy = -dy # Gestion du rebord en prenant l'inverse de dy pour un rebond coherent.
y = y + dy # Incrémentation de dy sur y.
x = x + dx # Incrémentation de dx sur x.
# --- GESTION DES COLLISIONS --- #
if 0 < x < 10 and b < y < d :
print("test")
# ------------------------------------ #
fen.mainloop() |
Partager