from tkinter import * from random import randint import color # Var canvas = None joueur = None triangles = [] triangle = None timer = 0 fin = 0 # Touches gauche = False droite = False # Images image_triangle = None image_triangle_Highlight = None image_jg = None image_jd = None DROITE = 'Droite' GAUCHE = 'Gauche' image_j = DROITE # Main def main(c: Canvas): global canvas, image_triangle, image_jg, image_jd, joueur, triangle, timer, image_triangle_Highlight canvas = c # Images # - Triangle - # Origin: 8x7 # Real: 32x28 # - Personnage - # Origin: 10x21 # Real: 40x84 image_triangle = PhotoImage(file = 'res/Triangle.png').zoom(4) image_triangle_Highlight = PhotoImage(file = 'res/Triangle_Highlight.png').zoom(4) image_jg = PhotoImage(file = 'res/Personnage_Gauche.png').zoom(4) image_jd = PhotoImage(file = 'res/Personnage_Droit.png').zoom(4) # Joueur joueur = canvas.create_image(200, 258, image = image_jd) # Triangle timer = randint(60, 300) # Bindings win = canvas.master win.bind('', lambda event: gauche_event(True)) win.bind('', lambda event: droite_event(True)) win.bind('', lambda event: gauche_event(False)) win.bind('', lambda event: droite_event(False)) # Loop def loop(deltatime: float, difficulty: float, end): deplacement_joueur(difficulty) deplacement_triangle(difficulty) create_triangles(difficulty) collision() if fin == 1: end() return def create_triangles(difficulty: float): global timer if timer <= 0: triangles.append(canvas.create_image(randint(20, 380), 14, image = image_triangle)) timer = randint(120, 300) else: timer -= difficulty def deplacement_joueur(difficulty: float): global image_j # Obtention des coordonnées du joueur x1, _ = canvas.coords(joueur) if droite and x1 <= 375: canvas.move(joueur, 2 * difficulty, 0) if image_j == GAUCHE: canvas.itemconfigure(joueur, image = image_jd) image_j = DROITE if gauche and x1 >= 25: canvas.move(joueur, -2 * difficulty, 0) if image_j == DROITE: canvas.itemconfigure(joueur, image = image_jg) image_j = GAUCHE def deplacement_triangle(difficulty: float): for triangle in triangles: # Obtention des coordonnées du triangle x1, y1 = canvas.coords(triangle) if y1 <= 282: canvas.move(triangle, 0, 2 * difficulty) else: canvas.delete(triangle) triangles.remove(triangle) if y1 >= 250: clignotement(triangle) canvas.after(100, lambda t = triangle: clignotement(t)) def clignotement(triangle): canvas.itemconfigure(triangle, image = image_triangle_Highlight) canvas.after(50, lambda t = triangle: canvas.itemconfigure(t, image = image_triangle)) def collision(): global fin for triangle in triangles: # Obtention des coordonnées des objets xt, yt = canvas.coords(triangle) xj, yj = canvas.coords(joueur) if yt + 16 >= yj - 42: if xj - 20 <= xt +- 2 and xt +- 2 <= xj + 20: fin = 1 if yt + 8 >= yj - 42: if xj - 20 <= xt + 6 and xt + 6 <= xj + 20 or xj - 20 <= xt - 6 and xt - 6 <= xj + 20: fin = 1 if yt >= yj - 42: if xj - 20 <= xt + 10 and xt + 10 <= xj + 20 or xj - 20 <= xt - 10 and xt - 10 <= xj + 20: fin = 1 if yt - 8 >= yj - 42: if xj - 20 <= xt + 14 and xt + 14 <= xj + 20 or xj - 20 <= xt - 14 and xt - 14 <= xj + 20: fin = 1 def gauche_event(pressed: bool): global gauche gauche = pressed def droite_event(pressed: bool): global droite droite = pressed