mirror of
https://github.com/Fare-spec/cours.git
synced 2025-12-09 11:30:38 +00:00
Auto urgent commit.
This commit is contained in:
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)
|
||||
|
||||
Reference in New Issue
Block a user