From 6f3d5dd36204b8a11c062bbe0e2fa37d5af99443 Mon Sep 17 00:00:00 2001 From: Spectre Date: Tue, 21 Jan 2025 15:39:35 +0100 Subject: [PATCH] fin du cours --- learning_pygame/bouncing_ball_project.zip | Bin 0 -> 4347 bytes learning_pygame/bouncing_ball_project/ball.py | 42 +++++++++++++++ .../bouncing_ball_project/balle.py | 32 ++++++++++++ .../bouncing_ball_game.py | 48 ++++++++++++++++++ .../bouncing_ball_project/constantes.py | 5 ++ .../bouncing_ball_project/raquette.py | 39 ++++++++++++++ 6 files changed, 166 insertions(+) create mode 100644 learning_pygame/bouncing_ball_project.zip create mode 100644 learning_pygame/bouncing_ball_project/ball.py create mode 100644 learning_pygame/bouncing_ball_project/balle.py create mode 100644 learning_pygame/bouncing_ball_project/bouncing_ball_game.py create mode 100644 learning_pygame/bouncing_ball_project/constantes.py create mode 100644 learning_pygame/bouncing_ball_project/raquette.py diff --git a/learning_pygame/bouncing_ball_project.zip b/learning_pygame/bouncing_ball_project.zip new file mode 100644 index 0000000000000000000000000000000000000000..d0b275528abb9bc362ed984fd3434ca5e9b93cf0 GIT binary patch literal 4347 zcmcgwTaVjB6i#n|4TzWWKH5Aap;zcCf;xqMu~_gab1iiM{=m_w2LMvyF7ii)W8e8#0!I1 z^i?c+_>ssUQL$YX9qC~<`h_pWKm{s^l)by&zSBBvyRFUk`krWt8;ypr@2($q@|~YG z8j#-%$G#dz_Qvk|-X^A2S5{Z+!u(US_x88j)ZM5H`dhPY?W{vCRclt6U6Z~SOY)E% z9EN=;C1=gb$WWxrjJ&I=7s&tyQmuV29{FxWAxpk6|?ieZLu@!9sf)+B$QBP@e&{F;)Mfdg(@3%G% zJDvT#t^F_e$fgA)yr>{d1phu$!%ph~NeUVCQ(qscNzTlE1{<5=iaf{RN_OYl>dsS@Kr>0}X)$DW93 zci4N;mnh(@N(K85!Sy7GyzV%O#hQrZ1VvOAPe);l&qRNoo_Gnh@WK3hDmg}J&~&4^ zF4CZAXKbsl2EuizHeFX#;e|gCH9_H|pN=-u6xSN2hYdEx^+v;)_ObP*_%z?guAt9Z zHIn2N3gJ5Bv|Z!WyjeDKrOEC%q-m;y)@cUPWg-XIS1qJ}Yrk(XprMAc@#XyZP0`?_ z)5F@mbV6#B%Yuh_jaTy0w*-(;80|d1SbMtobOL)`%%~sMJAZzTOrrNgycu{StqRzN z7y_qX3DZS%CUC|(m%M-d^3xyfi=`61@559iACFa%%;)bQ3WvE|w2l)MF@PMg8;Hz-jKCXk;sXmP zP-G0KVD=APZIz6pK#%T)0nQ$)H5e#fV>gWYdgNZ%_Xape{EAGmNCS`xgbxf;fv-+* zQr6jqCwdi%5=u7qx;WP0=?S)7$CBd>{cua`IQ?EUMC=7&fXJs%IUfrh%D0F`pZ5To z|EE~UhvAsW*W1Q|LWnY7X7Dj-&*3>ENan;Np}IadbWSmEbo!Be;zX*K=p(UCY^}i% zfuE=-4y!{&gsLbIsO1%qy1XlOlpR==4e)i{0Lg{p1LULPx9J$1rTq3vGAqswCm~MKWG9EF%GEl@vtIhq>B1lncn%s*~ zETsdQq1wznPhZTC&(j}?DDx2v;GH25Z_hprJAPB{DddUjO$+_COtdVv=_XL5+hO;i zN@80Fu&HSYsM0iW%_$(D*ESq)p12B878{zWjv^2HH$qk00?%vtDovti5!qzc%Bu7{ zLH8cRQcc%lu9`B`$6nHHDSzA1-3SG*F7uL;!MH}-PSSMS^Knz7dm2wzHjCh4XsK$s zloaVk%DI|Fa&t}Kv6*|p!Opq!moF^7!~^5_dH#$(YjBnUW}@CZ1wNP;d=-9Yf`8i!(EMQM5_FbIm+)ulT&Z;W H5w-pUGVBX} literal 0 HcmV?d00001 diff --git a/learning_pygame/bouncing_ball_project/ball.py b/learning_pygame/bouncing_ball_project/ball.py new file mode 100644 index 0000000..ed5f031 --- /dev/null +++ b/learning_pygame/bouncing_ball_project/ball.py @@ -0,0 +1,42 @@ +import sys, pygame +import balle +##########Definitions des constantes +# Taille de la fenetre +LARGEUR_ECRAN = 600 +HAUTEUR_ECRAN = 800 +# Couleur +BLANC = (255, 255, 255) +NOIR = (0, 0, 0) + +pygame.init() #initialisation des modules de pygame + +# Creation de la fenetre +ecran = pygame.display.set_mode((LARGEUR_ECRAN, HAUTEUR_ECRAN)) +ecran.fill(BLANC) + +pygame.display.set_caption('Balle rebondissante') + +clock = pygame.time.Clock() + +b1 = balle.Balle() + +bouge = False + +while True: #Demarrage de la boucle infinie + for event in pygame.event.get(): + if event.type == pygame.QUIT: #Evt de sortie de boucle + sys.exit() + elif event.type == pygame.MOUSEBUTTONDOWN: + bouge = not bouge + elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE: + bouge = not bouge + + + ecran.fill(BLANC) + if bouge: + b1.deplace() + b1.affiche(ecran) + + pygame.display.update() #rafraichissement + clock.tick(60) + diff --git a/learning_pygame/bouncing_ball_project/balle.py b/learning_pygame/bouncing_ball_project/balle.py new file mode 100644 index 0000000..e2c2087 --- /dev/null +++ b/learning_pygame/bouncing_ball_project/balle.py @@ -0,0 +1,32 @@ +import pygame +import pygame + +class Balle: + """ + Definie une balle qui se deplace dans la fenetre ecran + Attributs : rayon , xpos , ypos , xvit , yvit + Methodes : deplace , affiche + """ + + def __init__(self): + self.rayon = 10 + self.xpos = 300.0 + self.ypos = 400.0 + self.xvit = 4.5 + self.yvit = 3.0 + + def deplace(self): + self.xpos += self.xvit + self.ypos += self.yvit + if self.xpos + self.rayon > 600 or self.xpos - self.rayon < 0: + self.xvit = -self.xvit + if self.ypos + self.rayon > 800 or self.ypos - self.rayon < 0: + self.yvit = -self.yvit + + def affiche(self, ecran): + """ + Dessine la balle sur l'écran + Paramètres : + ecran : pygame.Surface - L'écran où dessiner la balle + """ + pygame.draw.circle(ecran, (255, 0, 0), (int(self.xpos), int(self.ypos)), self.rayon) diff --git a/learning_pygame/bouncing_ball_project/bouncing_ball_game.py b/learning_pygame/bouncing_ball_project/bouncing_ball_game.py new file mode 100644 index 0000000..fffe058 --- /dev/null +++ b/learning_pygame/bouncing_ball_project/bouncing_ball_game.py @@ -0,0 +1,48 @@ +import sys +import pygame +from balle import Balle +from raquette import Raquette +from constantes import * + +pygame.init() + +# Création de la fenêtre +ecran = pygame.display.set_mode((LARGEUR_ECRAN, HAUTEUR_ECRAN)) +ecran.fill(BLANC) +pygame.display.set_caption('Balle rebondissante avec raquette') + +clock = pygame.time.Clock() + +# Initialisation des objets +balle = Balle() +raquette = Raquette() + +bouge = False + +while True: # Boucle principale + for event in pygame.event.get(): + if event.type == pygame.QUIT: + sys.exit() + elif event.type == pygame.KEYDOWN: + if event.key == pygame.K_SPACE: + bouge = not bouge + elif event.key == pygame.K_LEFT: + raquette.deplaceGauche() + elif event.key == pygame.K_RIGHT: + raquette.deplaceDroite() + + ecran.fill(BLANC) + + if bouge: + balle.deplace() + + # Collision balle-raquette + if (balle.ypos + balle.rayon >= HAUTEUR_ECRAN - 20 and + raquette.xpos <= balle.xpos <= raquette.xpos + raquette.largeur): + balle.yvit = -balle.yvit + + balle.affiche(ecran) + raquette.affiche(ecran) + + pygame.display.update() + clock.tick(60) diff --git a/learning_pygame/bouncing_ball_project/constantes.py b/learning_pygame/bouncing_ball_project/constantes.py new file mode 100644 index 0000000..a4e8f30 --- /dev/null +++ b/learning_pygame/bouncing_ball_project/constantes.py @@ -0,0 +1,5 @@ +LARGEUR_ECRAN = 600 +HAUTEUR_ECRAN = 800 + +BLANC = (255,255,255) + diff --git a/learning_pygame/bouncing_ball_project/raquette.py b/learning_pygame/bouncing_ball_project/raquette.py new file mode 100644 index 0000000..fdd2607 --- /dev/null +++ b/learning_pygame/bouncing_ball_project/raquette.py @@ -0,0 +1,39 @@ +import pygame +from constantes import * + +class Raquette: + """ + Définit une raquette qui se déplace horizontalement + dans le bas de la fenêtre écran. + Attributs : largeur (int, par défaut 100), + xpos (int, par défaut LARGEUR_ECRAN//2 - largeur//2), + vit (int, par défaut 6) + L'épaisseur de la raquette est de 10. + Méthodes : deplaceGauche, deplaceDroite, affiche + """ + + def __init__(self): + self.largeur = 100 + self.xpos = LARGEUR_ECRAN // 2 - self.largeur // 2 + self.vit = 6 + + def deplaceGauche(self): + """ + Déplace la raquette vers la gauche si possible. + """ + self.xpos = max(0, self.xpos - self.vit) + + def deplaceDroite(self): + """ + Déplace la raquette vers la droite si possible. + """ + self.xpos = min(LARGEUR_ECRAN - self.largeur, self.xpos + self.vit) + + def affiche(self, ecran): + """ + Dessine la raquette sur l'écran. + """ + pygame.draw.rect(ecran, (0, 0, 255), (int(self.xpos), HAUTEUR_ECRAN - 20, self.largeur, 10)) + +if __name__ == '__main__': + pass