mirror of
https://github.com/Fare-spec/cours.git
synced 2025-12-07 10:50:36 +00:00
fin du cours
This commit is contained in:
BIN
learning_pygame/bouncing_ball_project.zip
Normal file
BIN
learning_pygame/bouncing_ball_project.zip
Normal file
Binary file not shown.
42
learning_pygame/bouncing_ball_project/ball.py
Normal file
42
learning_pygame/bouncing_ball_project/ball.py
Normal file
@@ -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)
|
||||
|
||||
32
learning_pygame/bouncing_ball_project/balle.py
Normal file
32
learning_pygame/bouncing_ball_project/balle.py
Normal file
@@ -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)
|
||||
48
learning_pygame/bouncing_ball_project/bouncing_ball_game.py
Normal file
48
learning_pygame/bouncing_ball_project/bouncing_ball_game.py
Normal file
@@ -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)
|
||||
5
learning_pygame/bouncing_ball_project/constantes.py
Normal file
5
learning_pygame/bouncing_ball_project/constantes.py
Normal file
@@ -0,0 +1,5 @@
|
||||
LARGEUR_ECRAN = 600
|
||||
HAUTEUR_ECRAN = 800
|
||||
|
||||
BLANC = (255,255,255)
|
||||
|
||||
39
learning_pygame/bouncing_ball_project/raquette.py
Normal file
39
learning_pygame/bouncing_ball_project/raquette.py
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user