#! /usr/bin/python3
# -*- coding: utf-8 -*-
#
# canvas = toile
# balle = canvas.create_oval( x0, y0, x1, y1)
# (x0,y0) = coin haut-gauche ; (x1,y1) = coin bas-droit
import tkinter as tk
import time

WIDTH = 400
HEIGHT = 400
nombre_g = 0
nombre_d = 0
vx = 2
vy = 3
deltat = 5 # ms

# variables globales
fenetre = 0
toile = 0
balle = 0
raquette1 = 0
raquette2 = 0
affichage_nombre_g = 0
affichage_nombre_d = 0
texte = 0
    
def balle_move():
    global balle, raquette1, raquette2
    global vx, vy, nombre_g, nombre_d
    # recuperation des coordonnees de la balle
    x1, y1, x2, y2 = toile.bbox(balle)
    # test de choc sur les parois :
    if ( x1 < 0 and vx < 0 ) or ( x2 > WIDTH and vx > 0 ) :
        vx = - vx
        # décompte des choc sur les parois verticales
        if x1 < 0 :
            nombre_g = nombre_g + 1
        if x2 > WIDTH :
            nombre_d = nombre_d + 1
    if ( y1 < 0 and vy < 0 ) or ( y2 > HEIGHT and vy > 0 ) :
        vy = - vy

    gx1, gy1, gx2, gy2 = toile.bbox(raquette1)
    choc = False
    choc_gx = x1 < gx2
    choc_gy = ( not ( y1 < gy1 and y2 < gy1 ) ) and \
             ( not ( y1 > gy2 and y2 > gy2 ) )
    if choc_gx and choc_gy and vx < 0 :
        vx = - vx

    dx1, dy1, dx2, dy2 = toile.bbox(raquette2)
    choc = False
    choc_dx = x2 > dx1
    choc_dy = ( not ( y1 < dy1 and y2 < dy1 ) ) and \
              ( not ( y1 > dy2 and y2 > dy2 ) )
    if choc_dx and choc_dy and vy > 0 :
        vx = - vx
    
    toile.move(balle, vx, vy)

    return

def animation():
    # appel "récursif" de la fonction animation() tous les deltat
    global toile
    global nombre_g, nombre_d
    balle_move()
    score = '{0:4d}        {1:4d}'.format(nombre_g, nombre_d)
    texte.insert("1.0", score)
    # not recursion with animation()
    # but add animation() to the list of mainloop()
    fenetre.after(deltat, animation)
    return

def clavier(event) :
    global raquette1, raquette2
    # print("pressed", event.char)
    if event.char == "2" :
        toile.move(raquette1, 0, 10)
    elif event.char == "8" :
        toile.move(raquette1, 0, -10)
    return

def animation_rayane() :
    global balle, raquette1, raquette2
    global fenetre, toile, texte
    fenetre = tk.Tk()

    # score
    texte = tk.Text(fenetre, height=1, width=16)
    texte.pack(side=tk.TOP)
    score = '{0:4d}        {1:4d}'.format(nombre_g, nombre_d)
    texte.insert("1.0", score)
    
    # toile sur laquelle on dessine :
    toile = tk.Canvas(fenetre, width = WIDTH, height = HEIGHT, background='yellow')
    toile.pack(side=tk.TOP)
    bouton = tk.Button(fenetre, text ="Fermer", command = fenetre.destroy)
    bouton.pack(side=tk.BOTTOM)
    
    fenetre.bind("<Key>", clavier)

    balle = toile.create_oval( 20, 20, 60, 60, outline = 'white', fill = 'blue' )
    # create_rectangle(x0,y0, x1,y1)
    raquette1 = toile.create_rectangle(0, HEIGHT/2-40, 20, HEIGHT/2+40, fill="white")
    raquette2 = toile.create_rectangle(WIDTH-20, HEIGHT/2-40, WIDTH, HEIGHT/2+40, fill="white")

    # appel de animation(), après un délai de deltat
    fenetre.after(deltat, animation)

    # jusqu'ici, rien n'apparaît à l'écran.
    # les instructions sont enregistrées pour être exécutées à la fin du programme.
    print("Juste avant fenetre.mainloop()")
    # time.sleep(5)
    # execution de l'affichage et de l'animation :
    #  sans fenetre.mainloop(), on a le même résultat, mais beaucoup plus lent.
    # équivaut à atexit()
    fenetre.mainloop()
    print("Après fenetre.mainloop()")
    # time.sleep(5)
    return

def rayane_2018_04_24() :
    # score=score+1
    # UnboundLocalError: local variable 'score' referenced before assignment
    score = 0

    #création de la fenetre
    fenetre = Tk()
    fenetre.geometry("640x480")
    # bouton de sortie
    # destroy au lieu de quit
    bouton=Button(fenetre, text="Fermer", command=fenetre.destroy)
    bouton.pack()

    #création du canevas
    can = Canvas(fenetre,width=640, height = 480,bg='black')
    can.pack()

    #création de la balle
    balle=can.create_oval(300,210,330,240,fill="white")

    #création des raquette
    raquette1= can.create_rectangle(40, 180, 60,300,fill="white")
    raquette2= can.create_rectangle(600,180,580,300, fill="white")

    #
    score=score+1
    # TextGame.set("Score : "+ str(score))
    # NameError: name 'TextGame' is not defined
    # TextGame.set("Score : "+ str(score))
    texte = "Score : "+ str(score)
    
    fenetre.mainloop()
    return

animation_rayane()

#Importation de la bibliotheque
# from tkinter import *
# rayane_2018_04_24()
