mirror of
https://github.com/Fare-spec/cours.git
synced 2025-12-08 03:00:37 +00:00
Auto urgent commit.
This commit is contained in:
41
pygame/V2/balle/balle.py
Executable file
41
pygame/V2/balle/balle.py
Executable file
@@ -0,0 +1,41 @@
|
||||
import pygame
|
||||
from constantes import *
|
||||
import raquette
|
||||
from math import *
|
||||
|
||||
class Balle(object):
|
||||
|
||||
def __init__(self):
|
||||
self.rayon = 10
|
||||
self.xpos = LARGEUR_ECRAN/2
|
||||
self.ypos = HAUTEUR_ECRAN/2
|
||||
self.xvit =4.5
|
||||
self.yvit = 3.0
|
||||
|
||||
|
||||
def deplace(self,raquette):
|
||||
self.xpos += self.xvit
|
||||
self.ypos += self.yvit
|
||||
self.rebonds(raquette)
|
||||
|
||||
|
||||
def affiche(self, ecran):
|
||||
pygame.draw.circle(ecran,(0,0,0) , (int(self.xpos),int(self.ypos)) , self.rayon)
|
||||
|
||||
def rebonds(self, raquette):
|
||||
if self.xpos + self.rayon > LARGEUR_ECRAN or self.xpos - self.rayon < 0:
|
||||
self.xvit = - self.xvit
|
||||
if self.ypos + self.rayon > HAUTEUR_ECRAN or self.ypos - self.rayon < 0:
|
||||
self.yvit = - self.yvit
|
||||
|
||||
if self.ypos >= HAUTEUR_ECRAN-20:
|
||||
if self.xpos >= raquette.xpos:
|
||||
if self.xpos <= raquette.xpos + raquette.largeur :
|
||||
self.yvit = - self.yvit
|
||||
if self.xpos >= raquette.xpos + raquette.largeur - raquette.largeur/10:
|
||||
self.xvit = 4.0
|
||||
self.yvit = 6.0
|
||||
|
||||
if self.xpos <= raquette.xpos + raquette.largeur/20:
|
||||
self.xvit = -4.0
|
||||
self.yvit = 6.0
|
||||
43
pygame/V2/balle/balle_rebondissante.py
Executable file
43
pygame/V2/balle/balle_rebondissante.py
Executable file
@@ -0,0 +1,43 @@
|
||||
import sys, pygame
|
||||
import balle
|
||||
from constantes import *
|
||||
|
||||
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 and event.button == 1:
|
||||
bouge = not bouge
|
||||
elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
|
||||
bouge = not bouge
|
||||
ecran.fill(BLANC)
|
||||
|
||||
b1.deplace()
|
||||
b1.affiche(ecran)
|
||||
pygame.display.update()
|
||||
|
||||
|
||||
ecran.fill(BLANC)
|
||||
if bouge:
|
||||
b1.deplace()
|
||||
b1.affiche(ecran)
|
||||
|
||||
pygame.display.update() #rafraichissement
|
||||
clock.tick(60)
|
||||
|
||||
if __name__ == '__main__':
|
||||
b1 = balle.Balle()
|
||||
45
pygame/V2/balle/casse_brique.py
Executable file
45
pygame/V2/balle/casse_brique.py
Executable file
@@ -0,0 +1,45 @@
|
||||
import sys, pygame
|
||||
import balle
|
||||
import raquette
|
||||
from constantes import *
|
||||
|
||||
pygame.init()
|
||||
|
||||
# Creation de la fenetre
|
||||
ecran = pygame.display.set_mode((LARGEUR_ECRAN,HAUTEUR_ECRAN))
|
||||
|
||||
# Titre de la fenetre
|
||||
pygame.display.set_caption('Casse_Brique')
|
||||
|
||||
clock = pygame.time.Clock()
|
||||
|
||||
b1 = balle.Balle()
|
||||
r1 = raquette.Raquette()
|
||||
bouge = False
|
||||
bouge_gauche_raquette = False
|
||||
bouge_droite_raquette = False
|
||||
|
||||
while True :
|
||||
for event in pygame.event.get() :
|
||||
if event.type == pygame.QUIT :
|
||||
sys.exit()
|
||||
elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE :
|
||||
bouge = not bouge
|
||||
|
||||
if pygame.key.get_pressed()[pygame.K_q]:
|
||||
r1.deplaceGauche()
|
||||
if pygame.key.get_pressed()[pygame.K_d]:
|
||||
r1.deplaceDroite()
|
||||
|
||||
ecran.fill(BLANC)
|
||||
|
||||
if bouge :
|
||||
b1.deplace(r1)
|
||||
|
||||
|
||||
|
||||
b1.deplace(r1)
|
||||
b1.affiche(ecran)
|
||||
r1.affiche(ecran)
|
||||
pygame.display.update()
|
||||
clock.tick(60)
|
||||
7
pygame/V2/balle/constantes.py
Executable file
7
pygame/V2/balle/constantes.py
Executable file
@@ -0,0 +1,7 @@
|
||||
##########Definitions des constantes
|
||||
# Taille de la fenetre
|
||||
LARGEUR_ECRAN = 600
|
||||
HAUTEUR_ECRAN = 800
|
||||
# Couleur
|
||||
BLANC = (255, 255, 255)
|
||||
NOIR = (0, 0, 0)
|
||||
56
pygame/V2/balle/raquette.py
Executable file
56
pygame/V2/balle/raquette.py
Executable file
@@ -0,0 +1,56 @@
|
||||
import pygame
|
||||
from constantes import *
|
||||
|
||||
class Raquette:
|
||||
"""
|
||||
Definie une raquette qui se deplace horizontalement
|
||||
dans le bas de la fenetre ecran
|
||||
Attributs : largeur (int defaut 100),
|
||||
xpos (int defaut LARGEUR_ECRAN//2 - self.largeur//2),
|
||||
vit (int defaut 6)
|
||||
L'épaisseur de la raquette est de 10
|
||||
Methodes : deplaceGauche, deplaceDroite, affiche
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self.largeur = 100
|
||||
self.xpos = LARGEUR_ECRAN//2 - self.largeur//2
|
||||
self.vit = 6
|
||||
|
||||
def deplaceGauche(self):
|
||||
"""
|
||||
Deplace la raquette de vit vers la gauche
|
||||
Parametres :
|
||||
self : Raquette
|
||||
Return :
|
||||
None
|
||||
Effet de bord :
|
||||
Modifie l'attribut xpos en lui enlevant,
|
||||
si c'est possible la valeur de vit (et met xpos à 0 sinon)
|
||||
"""
|
||||
self.xpos -= self.vit
|
||||
if self.xpos < 0:
|
||||
self.xpos = 0
|
||||
|
||||
|
||||
def deplaceDroite(self):
|
||||
"""
|
||||
Deplace la raquette de vit vers la droite
|
||||
Parametres :
|
||||
self : Raquette
|
||||
Return :
|
||||
None
|
||||
Effet de bord :
|
||||
Modifie l'attribut xpos en lui ajoutant,
|
||||
si c'est possible la valeur de vit (et met xpos à
|
||||
600-self.largeur sinon sinon)
|
||||
"""
|
||||
self.xpos += self.vit
|
||||
if self.xpos > LARGEUR_ECRAN - self.largeur:
|
||||
self.xpos = LARGEUR_ECRAN - self.largeur
|
||||
|
||||
def affiche(self, ecran):
|
||||
pygame.draw.rect(ecran, (0,0,255), (int(self.xpos), HAUTEUR_ECRAN-20, self.largeur, 10))
|
||||
|
||||
if __name__ == '__main__':
|
||||
pass
|
||||
42
pygame/bouncing_ball/ball.py
Normal file
42
pygame/bouncing_ball/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)
|
||||
|
||||
23
pygame/bouncing_ball/balle.py
Normal file
23
pygame/bouncing_ball/balle.py
Normal file
@@ -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
|
||||
5
pygame/bouncing_ball/constantes.py
Normal file
5
pygame/bouncing_ball/constantes.py
Normal file
@@ -0,0 +1,5 @@
|
||||
LARGEUR_ECRAN = 600
|
||||
HAUTEUR_ECRAN = 800
|
||||
|
||||
BLANC = (255,255,255)
|
||||
|
||||
53
pygame/bouncing_ball/f
Normal file
53
pygame/bouncing_ball/f
Normal file
@@ -0,0 +1,53 @@
|
||||
[38;5;238m───────┬──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────[0m
|
||||
[38;5;238m│ [0mFile: [1mraquette.py[0m
|
||||
[38;5;238m───────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────[0m
|
||||
[38;5;238m 1[0m [38;5;238m│[0m [38;2;249;38;114mimport[0m[38;2;248;248;242m [0m[38;2;248;248;242msys[0m
|
||||
[38;5;238m 2[0m [38;5;238m│[0m [38;2;249;38;114mimport[0m[38;2;248;248;242m [0m[38;2;248;248;242mpygame[0m
|
||||
[38;5;238m 3[0m [38;5;238m│[0m [38;2;249;38;114mfrom[0m[38;2;248;248;242m [0m[38;2;248;248;242mballe[0m[38;2;248;248;242m [0m[38;2;249;38;114mimport[0m[38;2;248;248;242m [0m[38;2;248;248;242mBalle[0m
|
||||
[38;5;238m 4[0m [38;5;238m│[0m [38;2;249;38;114mfrom[0m[38;2;248;248;242m [0m[38;2;248;248;242mraquette[0m[38;2;248;248;242m [0m[38;2;249;38;114mimport[0m[38;2;248;248;242m [0m[38;2;248;248;242mRaquette[0m
|
||||
[38;5;238m 5[0m [38;5;238m│[0m [38;2;249;38;114mfrom[0m[38;2;248;248;242m [0m[38;2;248;248;242mconstantes[0m[38;2;248;248;242m [0m[38;2;249;38;114mimport[0m[38;2;248;248;242m [0m[38;2;190;132;255m*[0m
|
||||
[38;5;238m 6[0m [38;5;238m│[0m
|
||||
[38;5;238m 7[0m [38;5;238m│[0m [38;2;248;248;242mpygame[0m[38;2;248;248;242m.[0m[38;2;248;248;242minit[0m[38;2;248;248;242m([0m[38;2;248;248;242m)[0m
|
||||
[38;5;238m 8[0m [38;5;238m│[0m
|
||||
[38;5;238m 9[0m [38;5;238m│[0m [38;2;117;113;94m#[0m[38;2;117;113;94m Création de la fenêtre[0m
|
||||
[38;5;238m 10[0m [38;5;238m│[0m [38;2;248;248;242mecran[0m[38;2;248;248;242m [0m[38;2;249;38;114m=[0m[38;2;248;248;242m [0m[38;2;248;248;242mpygame[0m[38;2;248;248;242m.[0m[38;2;248;248;242mdisplay[0m[38;2;248;248;242m.[0m[38;2;248;248;242mset_mode[0m[38;2;248;248;242m([0m[38;2;248;248;242m([0m[38;2;255;255;255mLARGEUR_ECRAN[0m[38;2;248;248;242m,[0m[38;2;248;248;242m [0m[38;2;255;255;255mHAUTEUR_ECRAN[0m[38;2;248;248;242m)[0m[38;2;248;248;242m)[0m
|
||||
[38;5;238m 11[0m [38;5;238m│[0m [38;2;248;248;242mecran[0m[38;2;248;248;242m.[0m[38;2;248;248;242mfill[0m[38;2;248;248;242m([0m[38;2;255;255;255mBLANC[0m[38;2;248;248;242m)[0m
|
||||
[38;5;238m 12[0m [38;5;238m│[0m [38;2;248;248;242mpygame[0m[38;2;248;248;242m.[0m[38;2;248;248;242mdisplay[0m[38;2;248;248;242m.[0m[38;2;248;248;242mset_caption[0m[38;2;248;248;242m([0m[38;2;230;219;116m'[0m[38;2;230;219;116mBalle rebondissante avec raquette[0m[38;2;230;219;116m'[0m[38;2;248;248;242m)[0m
|
||||
[38;5;238m 13[0m [38;5;238m│[0m
|
||||
[38;5;238m 14[0m [38;5;238m│[0m [38;2;248;248;242mclock[0m[38;2;248;248;242m [0m[38;2;249;38;114m=[0m[38;2;248;248;242m [0m[38;2;248;248;242mpygame[0m[38;2;248;248;242m.[0m[38;2;248;248;242mtime[0m[38;2;248;248;242m.[0m[38;2;248;248;242mClock[0m[38;2;248;248;242m([0m[38;2;248;248;242m)[0m
|
||||
[38;5;238m 15[0m [38;5;238m│[0m
|
||||
[38;5;238m 16[0m [38;5;238m│[0m [38;2;117;113;94m#[0m[38;2;117;113;94m Initialisation des objets[0m
|
||||
[38;5;238m 17[0m [38;5;238m│[0m [38;2;248;248;242mballe[0m[38;2;248;248;242m [0m[38;2;249;38;114m=[0m[38;2;248;248;242m [0m[38;2;248;248;242mBalle[0m[38;2;248;248;242m([0m[38;2;248;248;242m)[0m
|
||||
[38;5;238m 18[0m [38;5;238m│[0m [38;2;248;248;242mraquette[0m[38;2;248;248;242m [0m[38;2;249;38;114m=[0m[38;2;248;248;242m [0m[38;2;248;248;242mRaquette[0m[38;2;248;248;242m([0m[38;2;248;248;242m)[0m
|
||||
[38;5;238m 19[0m [38;5;238m│[0m
|
||||
[38;5;238m 20[0m [38;5;238m│[0m [38;2;248;248;242mbouge[0m[38;2;248;248;242m [0m[38;2;249;38;114m=[0m[38;2;248;248;242m [0m[38;2;190;132;255mFalse[0m
|
||||
[38;5;238m 21[0m [38;5;238m│[0m
|
||||
[38;5;238m 22[0m [38;5;238m│[0m [38;2;249;38;114mwhile[0m[38;2;248;248;242m [0m[38;2;190;132;255mTrue[0m[38;2;248;248;242m:[0m[38;2;248;248;242m [0m[38;2;117;113;94m#[0m[38;2;117;113;94m Boucle principale[0m
|
||||
[38;5;238m 23[0m [38;5;238m│[0m [38;2;248;248;242m [0m[38;2;249;38;114mfor[0m[38;2;248;248;242m [0m[38;2;248;248;242mevent[0m[38;2;248;248;242m [0m[38;2;249;38;114min[0m[38;2;248;248;242m [0m[38;2;248;248;242mpygame[0m[38;2;248;248;242m.[0m[38;2;248;248;242mevent[0m[38;2;248;248;242m.[0m[38;2;248;248;242mget[0m[38;2;248;248;242m([0m[38;2;248;248;242m)[0m[38;2;248;248;242m:[0m
|
||||
[38;5;238m 24[0m [38;5;238m│[0m [38;2;248;248;242m [0m[38;2;249;38;114mif[0m[38;2;248;248;242m [0m[38;2;248;248;242mevent[0m[38;2;248;248;242m.[0m[38;2;248;248;242mtype[0m[38;2;248;248;242m [0m[38;2;249;38;114m==[0m[38;2;248;248;242m [0m[38;2;248;248;242mpygame[0m[38;2;248;248;242m.[0m[38;2;255;255;255mQUIT[0m[38;2;248;248;242m:[0m
|
||||
[38;5;238m 25[0m [38;5;238m│[0m [38;2;248;248;242m [0m[38;2;248;248;242msys[0m[38;2;248;248;242m.[0m[38;2;248;248;242mexit[0m[38;2;248;248;242m([0m[38;2;248;248;242m)[0m
|
||||
[38;5;238m 26[0m [38;5;238m│[0m [38;2;248;248;242m [0m[38;2;249;38;114melif[0m[38;2;248;248;242m [0m[38;2;248;248;242mevent[0m[38;2;248;248;242m.[0m[38;2;248;248;242mtype[0m[38;2;248;248;242m [0m[38;2;249;38;114m==[0m[38;2;248;248;242m [0m[38;2;248;248;242mpygame[0m[38;2;248;248;242m.[0m[38;2;255;255;255mKEYDOWN[0m[38;2;248;248;242m:[0m
|
||||
[38;5;238m 27[0m [38;5;238m│[0m [38;2;248;248;242m [0m[38;2;249;38;114mif[0m[38;2;248;248;242m [0m[38;2;248;248;242mevent[0m[38;2;248;248;242m.[0m[38;2;248;248;242mkey[0m[38;2;248;248;242m [0m[38;2;249;38;114m==[0m[38;2;248;248;242m [0m[38;2;248;248;242mpygame[0m[38;2;248;248;242m.[0m[38;2;255;255;255mK_SPACE[0m[38;2;248;248;242m:[0m
|
||||
[38;5;238m 28[0m [38;5;238m│[0m [38;2;248;248;242m [0m[38;2;248;248;242mbouge[0m[38;2;248;248;242m [0m[38;2;249;38;114m=[0m[38;2;248;248;242m [0m[38;2;249;38;114mnot[0m[38;2;248;248;242m [0m[38;2;248;248;242mbouge[0m
|
||||
[38;5;238m 29[0m [38;5;238m│[0m [38;2;248;248;242m [0m[38;2;249;38;114melif[0m[38;2;248;248;242m [0m[38;2;248;248;242mevent[0m[38;2;248;248;242m.[0m[38;2;248;248;242mkey[0m[38;2;248;248;242m [0m[38;2;249;38;114m==[0m[38;2;248;248;242m [0m[38;2;248;248;242mpygame[0m[38;2;248;248;242m.[0m[38;2;255;255;255mK_LEFT[0m[38;2;248;248;242m:[0m
|
||||
[38;5;238m 30[0m [38;5;238m│[0m [38;2;248;248;242m [0m[38;2;248;248;242mraquette[0m[38;2;248;248;242m.[0m[38;2;248;248;242mdeplaceGauche[0m[38;2;248;248;242m([0m[38;2;248;248;242m)[0m
|
||||
[38;5;238m 31[0m [38;5;238m│[0m [38;2;248;248;242m [0m[38;2;249;38;114melif[0m[38;2;248;248;242m [0m[38;2;248;248;242mevent[0m[38;2;248;248;242m.[0m[38;2;248;248;242mkey[0m[38;2;248;248;242m [0m[38;2;249;38;114m==[0m[38;2;248;248;242m [0m[38;2;248;248;242mpygame[0m[38;2;248;248;242m.[0m[38;2;255;255;255mK_RIGHT[0m[38;2;248;248;242m:[0m
|
||||
[38;5;238m 32[0m [38;5;238m│[0m [38;2;248;248;242m [0m[38;2;248;248;242mraquette[0m[38;2;248;248;242m.[0m[38;2;248;248;242mdeplaceDroite[0m[38;2;248;248;242m([0m[38;2;248;248;242m)[0m
|
||||
[38;5;238m 33[0m [38;5;238m│[0m
|
||||
[38;5;238m 34[0m [38;5;238m│[0m [38;2;248;248;242m [0m[38;2;248;248;242mecran[0m[38;2;248;248;242m.[0m[38;2;248;248;242mfill[0m[38;2;248;248;242m([0m[38;2;255;255;255mBLANC[0m[38;2;248;248;242m)[0m
|
||||
[38;5;238m 35[0m [38;5;238m│[0m
|
||||
[38;5;238m 36[0m [38;5;238m│[0m [38;2;248;248;242m [0m[38;2;249;38;114mif[0m[38;2;248;248;242m [0m[38;2;248;248;242mbouge[0m[38;2;248;248;242m:[0m
|
||||
[38;5;238m 37[0m [38;5;238m│[0m [38;2;248;248;242m [0m[38;2;248;248;242mballe[0m[38;2;248;248;242m.[0m[38;2;248;248;242mdeplace[0m[38;2;248;248;242m([0m[38;2;248;248;242m)[0m
|
||||
[38;5;238m 38[0m [38;5;238m│[0m
|
||||
[38;5;238m 39[0m [38;5;238m│[0m [38;2;248;248;242m [0m[38;2;117;113;94m#[0m[38;2;117;113;94m Collision balle-raquette[0m
|
||||
[38;5;238m 40[0m [38;5;238m│[0m [38;2;248;248;242m [0m[38;2;249;38;114mif[0m[38;2;248;248;242m [0m[38;2;248;248;242m([0m[38;2;248;248;242mballe[0m[38;2;248;248;242m.[0m[38;2;248;248;242mypos[0m[38;2;248;248;242m [0m[38;2;249;38;114m+[0m[38;2;248;248;242m [0m[38;2;248;248;242mballe[0m[38;2;248;248;242m.[0m[38;2;248;248;242mrayon[0m[38;2;248;248;242m [0m[38;2;249;38;114m>=[0m[38;2;248;248;242m [0m[38;2;255;255;255mHAUTEUR_ECRAN[0m[38;2;248;248;242m [0m[38;2;249;38;114m-[0m[38;2;248;248;242m [0m[38;2;190;132;255m20[0m[38;2;248;248;242m [0m[38;2;249;38;114mand[0m
|
||||
[38;5;238m 41[0m [38;5;238m│[0m [38;2;248;248;242m [0m[38;2;248;248;242mraquette[0m[38;2;248;248;242m.[0m[38;2;248;248;242mxpos[0m[38;2;248;248;242m [0m[38;2;249;38;114m<=[0m[38;2;248;248;242m [0m[38;2;248;248;242mballe[0m[38;2;248;248;242m.[0m[38;2;248;248;242mxpos[0m[38;2;248;248;242m [0m[38;2;249;38;114m<=[0m[38;2;248;248;242m [0m[38;2;248;248;242mraquette[0m[38;2;248;248;242m.[0m[38;2;248;248;242mxpos[0m[38;2;248;248;242m [0m[38;2;249;38;114m+[0m[38;2;248;248;242m [0m[38;2;248;248;242mraquette[0m[38;2;248;248;242m.[0m[38;2;248;248;242mlargeur[0m[38;2;248;248;242m)[0m[38;2;248;248;242m:[0m
|
||||
[38;5;238m 42[0m [38;5;238m│[0m [38;2;248;248;242m [0m[38;2;248;248;242mballe[0m[38;2;248;248;242m.[0m[38;2;248;248;242myvit[0m[38;2;248;248;242m [0m[38;2;249;38;114m=[0m[38;2;248;248;242m [0m[38;2;249;38;114m-[0m[38;2;248;248;242mballe[0m[38;2;248;248;242m.[0m[38;2;248;248;242myvit[0m
|
||||
[38;5;238m 43[0m [38;5;238m│[0m
|
||||
[38;5;238m 44[0m [38;5;238m│[0m [38;2;248;248;242m [0m[38;2;248;248;242mballe[0m[38;2;248;248;242m.[0m[38;2;248;248;242maffiche[0m[38;2;248;248;242m([0m[38;2;248;248;242mecran[0m[38;2;248;248;242m)[0m
|
||||
[38;5;238m 45[0m [38;5;238m│[0m [38;2;248;248;242m [0m[38;2;248;248;242mraquette[0m[38;2;248;248;242m.[0m[38;2;248;248;242maffiche[0m[38;2;248;248;242m([0m[38;2;248;248;242mecran[0m[38;2;248;248;242m)[0m
|
||||
[38;5;238m 46[0m [38;5;238m│[0m
|
||||
[38;5;238m 47[0m [38;5;238m│[0m [38;2;248;248;242m [0m[38;2;248;248;242mpygame[0m[38;2;248;248;242m.[0m[38;2;248;248;242mdisplay[0m[38;2;248;248;242m.[0m[38;2;248;248;242mupdate[0m[38;2;248;248;242m([0m[38;2;248;248;242m)[0m
|
||||
[38;5;238m 48[0m [38;5;238m│[0m [38;2;248;248;242m [0m[38;2;248;248;242mclock[0m[38;2;248;248;242m.[0m[38;2;248;248;242mtick[0m[38;2;248;248;242m([0m[38;2;190;132;255m60[0m[38;2;248;248;242m)[0m
|
||||
[38;5;238m 49[0m [38;5;238m│[0m
|
||||
[38;5;238m───────┴──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────[0m
|
||||
49
pygame/bouncing_ball/raquette.py
Normal file
49
pygame/bouncing_ball/raquette.py
Normal file
@@ -0,0 +1,49 @@
|
||||
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)
|
||||
|
||||
BIN
pygame/bouncing_ball_project.zip
Normal file
BIN
pygame/bouncing_ball_project.zip
Normal file
Binary file not shown.
42
pygame/bouncing_ball_project/ball.py
Normal file
42
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
pygame/bouncing_ball_project/balle.py
Normal file
32
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
pygame/bouncing_ball_project/bouncing_ball_game.py
Normal file
48
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
pygame/bouncing_ball_project/constantes.py
Normal file
5
pygame/bouncing_ball_project/constantes.py
Normal file
@@ -0,0 +1,5 @@
|
||||
LARGEUR_ECRAN = 600
|
||||
HAUTEUR_ECRAN = 800
|
||||
|
||||
BLANC = (255,255,255)
|
||||
|
||||
39
pygame/bouncing_ball_project/raquette.py
Normal file
39
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