diff --git a/learning_pygame/bouncing_ball/ball.py b/learning_pygame/bouncing_ball/ball.py new file mode 100644 index 0000000..ed5f031 --- /dev/null +++ b/learning_pygame/bouncing_ball/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/balle.py b/learning_pygame/bouncing_ball/balle.py new file mode 100644 index 0000000..6533eab --- /dev/null +++ b/learning_pygame/bouncing_ball/balle.py @@ -0,0 +1,23 @@ +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 + elif self.ypos + self.rayon > 800 or self.ypos - self.rayon < 0: + self.yvit = - self.yvit