From 6e382231863c443f6a19e4275bee59a8a49407ae Mon Sep 17 00:00:00 2001 From: Eveldee Date: Wed, 8 May 2019 21:31:12 +0200 Subject: [PATCH] Add fall --- game/fall.py | 131 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 128 insertions(+), 3 deletions(-) diff --git a/game/fall.py b/game/fall.py index 8052b6c..5f3ee09 100644 --- a/game/fall.py +++ b/game/fall.py @@ -1,9 +1,134 @@ 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_jg = None +image_jd = None +DROITE = 'Droite' +GAUCHE = 'Gauche' +image_j = DROITE # Main -def main(canvas: Canvas): - pass +def main(c: Canvas): + global canvas, image_triangle, image_jg, image_jd, joueur, triangle, timer + + canvas = c + + # Images + # - Triangle - + # Origin: 8x7 + # Real: 32x28 + # - Personnage - + # Origin: 10x21 + # Real: 40x84 + image_triangle = PhotoImage(file = 'res/Triangle.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): - pass + 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) + +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 + +