From 7316b9d3d9d5ca6cce664238f6f8b49540798f53 Mon Sep 17 00:00:00 2001 From: Eveldee Date: Sun, 24 Mar 2019 18:53:50 +0100 Subject: [PATCH] Add pong --- game/pong.py | 89 ++++++++++++++++++++++++++++++++++++++++++++++++-- main.py | 2 +- res/Barre.png | Bin 0 -> 218 bytes 3 files changed, 87 insertions(+), 4 deletions(-) create mode 100644 res/Barre.png diff --git a/game/pong.py b/game/pong.py index 8052b6c..4d3426a 100644 --- a/game/pong.py +++ b/game/pong.py @@ -1,9 +1,92 @@ from tkinter import * +import color + +# Var +ACCENT_COLOR = color.PURPLE +SECONDARY_COLOR = color.BROWN +canvas = None +# Touches +haut = False +bas = False +# Trait +trait = None +T_WIDTH = 10 +T_HEIGHT = 90 +PAST_Y = 5 +# Balle +balle = None +cos = 0.0 +sin = 0.0 +B_RADIUS = 10 +PASB = 3.0 + +image = None # Main -def main(canvas: Canvas): - pass +def main(c: Canvas): + global canvas, trait, balle, cos, sin, image + canvas = c + image = PhotoImage(file ="res/Barre.png") + + x_t = 360 + y_t = 150 + x_b = 200 + y_b = 150 + trait = canvas.create_image(x_t, y_t, image = image) + balle = canvas.create_oval(x_b - B_RADIUS, y_b - B_RADIUS, x_b + B_RADIUS, y_b + B_RADIUS, outline = SECONDARY_COLOR, fill = SECONDARY_COLOR) + + cos = -0.5 + sin = 0.8660 + + win = canvas.master + win.bind('', lambda event: haut_event(True)) + win.bind('', lambda event: bas_event(True)) + win.bind('', lambda event: haut_event(False)) + win.bind('', lambda event: bas_event(False)) # Loop def loop(deltatime: float, difficulty: float, end): - pass + collision() + deplacementballe(difficulty) + deplacementtrait(difficulty) + + _, _, x2, _ = canvas.coords(balle) + if x2 >= 396: + end() + return + +# DĂ©placement de la balle +def deplacementballe(difficulty: float): + canvas.move(balle, cos * PASB * difficulty, -sin * PASB * difficulty) + +def collision(): + global cos, sin + + xt1, yt1 = canvas.coords(trait) + xt1, yt1, xt2, yt2 = xt1 - 5, yt1 - 45, xt1 + 5, yt1 + 45 + x1, y1, x2, y2 = canvas.coords(balle) + if x1 <= 5: + cos = -cos + if y1 <= 5 or y2 >= 295: + sin = -sin + if x2 + 5 >= xt1 and x1 - 5 <= xt2 and y1 - 5 <= yt2 and y2 + 5 >= yt1: + cos = -abs(cos) + canvas.itemconfigure(balle, outline = ACCENT_COLOR, fill = ACCENT_COLOR) + canvas.after(200, lambda: canvas.itemconfigure(balle, outline=SECONDARY_COLOR, fill=SECONDARY_COLOR)) + +def deplacementtrait(difficulty: float): + _, y1 = canvas.coords(trait) + y1, y2 = y1 - 45, y1 + 45 + if haut and y1 >= 10: + canvas.move(trait, 0, -PAST_Y * difficulty) + if bas and y2 <= 290: + canvas.move(trait, 0, PAST_Y * difficulty) + +def haut_event(pressed: bool): + global haut + haut = pressed + +def bas_event(pressed: bool): + global bas + bas = pressed + diff --git a/main.py b/main.py index 2470a20..a900c23 100644 --- a/main.py +++ b/main.py @@ -104,7 +104,7 @@ def getMessage(score): return 'Bien, la maitrise vient avec le temps' elif score < 1000: return 'Bien joué, vous approchez de la difficulté maximum' - elif score < 5000: + elif score < 2000: return "Splendide, vous n\'avez plus qu'à attendre l'éternité" else: return "Ce jeu n'a plus aucun secrets pour vous !" diff --git a/res/Barre.png b/res/Barre.png new file mode 100644 index 0000000000000000000000000000000000000000..a24e2b074341d52d9afe4cf81b92544ad3509661 GIT binary patch literal 218 zcmeAS@N?(olHy`uVBq!ia0vp^Aa)c78<1?=bKDC^aTa()7Bet#3xP1>rMq>1fPy`q zE{-7{oo}yiu+{LQ?2 zF{cAkRr4HppVZ7|j5jTu|K@5dy8#pH@x80tF6nOn;}n*-e(i@D*AC%rB2vE&EFh8YSBuTJ(&1iFjC)78&q Iol`;+0QGoLKmY&$ literal 0 HcmV?d00001