Tutoriel PygamePrésentation
Pygame est une bibliothèque en Python qui permet de créer des jeux vidéo ou des animations interactives. Avec Pygame, on peut programmer des jeux en 2D, ajouter des images, des
sons, des mouvements, et même des interactions comme appuyer sur des touches ou cliquer avec la souris. 🎮
Cette bibliothèque est parfaite pour les débutants, car elle est simple à utiliser et permet de voir rapidement le résultat de son travail. Ce tutoriel a pour but de donner les bases nécessaires
pour réussir ensuite à créer son propre jeu.
Pourquoi apprendre Pygame ?
Accessible aux débutants : Même si on ne connait pas grand-chose en Python, on peut vite comprendre comment cela fonctionne.
Créatif et amusant : On code tout en laissant libre cours à son imagination pour inventer des jeux ou des animations.
Utile pour apprendre : En programmant avec Pygame, on développe des compétences en logique et en résolution de problèmes, ce qui est très utile dans de nombreux domaines.
Code de base
Pour utiliser le module Pygame il faut l'importer au début du programme.
import pygame as pg
Cependant, si on exécute tel quel le programme dans un navigateur un fenêtre va apparaître mais elle ne pourra être fermée.
Il faut pour cela écrire plusieurs autres ligne :
import pygame as pg
pg.init()
pg.display.set_caption("Pygame")
fin = False
while not fin:
ev = pg.event.wait()
if ev.type == pg.QUIT:
fin = True
pg.quit()
pg.init() permet d'initialiser la fenêtre Pygame.
pg.display.set_caption("Pygame") affiche un titre pour la fenêtre Pygame.
fin = False définit une variable booléenne (ici fausse) qui permettra de déclencher l'arrêt du programme.
La boucle while s'exécute tant que la variable fin est fausse. Ainsi à chaque tour de boucle la variable ev, qui attend de voir qu'elle évènement peut survenir, sera associée à l'évènement QUIT lorsqu'on clique sur la croix de la fenêtre. À ce moment la variable fin devientVraie et on quitte donc la boucle pour exécuter la commande pq.quit() qui permet de fermer la fenêtre Pygame.
Ainsi, c'est dans cette boucle que se dérouleront toutes les actions d'un jeu. On l'appelera la boucle Pygame.
Dessiner dans la fenêtre PygameDimensions de l'écran
Pour définir les dimensions de la zone dans laquelle le jeu va se dérouler on utilise l'instruction pg.display.set_mode((largeur, hauteur)).
Dans l'exemple ci-dessous la fenêtre aura pour dimension 400px par 150px.
import pygame as pg
pg.init()
pg.display.set_caption("Pygame")
ecran = pg.display.set_mode((400, 150))
fin = False
while not fin:
ev = pg.event.wait()
if ev.type == pg.QUIT:
fin = True
pg.quit()Couleur de l'écran
La variable ecran ainsi créée va pouvoir être utilisée pour attribuer des propriétés à la fenêtre de jeu. Par exemple pour donner une couleur de fond :
import pygame as pg
pg.init()
pg.display.set_caption("Pygame")
ecran = pg.display.set_mode((400, 150))
vert = (0,255,200)
fin = False
while not fin:
ecran.fill(vert)
pg.display.update()
ev = pg.event.wait()
if ev.type == pg.QUIT:
fin = True
pg.quit()Segment
Pour tracer un segment on utilise l'instruction pg.draw.lines( display, couleur, fermeture (True ou False) , [(abs1,ord1), (abs2,ord3), etc. ] ) :
import pygame as pg
pg.init()
pg.display.set_caption("Pygame : ligne")
ecran = pg.display.set_mode((400, 150))
vert = (0,255,200)
fin = False
while not fin:
ecran.fill(vert)
pg.draw.lines(ecran, [0,0,0], False, [(0,0),(200,50)], 1)
pg.display.update()
ev = pg.event.wait()
if ev.type == pg.QUIT:
fin = True
pg.quit()
import pygame as pg
pg.init()
pg.display.set_caption("Pygame : lignes")
ecran = pg.display.set_mode((400, 150))
vert = (0,255,200)
fin = False
while not fin:
ecran.fill(vert)
pg.draw.lines(ecran,[255,0,0],True,[(0,130),(200,50),(220,80),(250,60)],5)
pg.display.update()
ev = pg.event.wait()
if ev.type == pg.QUIT:
fin = True
pg.quit()Rectangle
Pour tracer un rectangle on utilise l'instruction pg.draw.rect( display, couleur, [abs, ord, largeur, hauteur], épaisseur ) :
import pygame as pg
pg.init()
pg.display.set_caption("Pygame : rectangle")
ecran = pg.display.set_mode((400, 150))
vert = (0,255,100)
fin = False
while not fin:
ecran.fill(vert)
pg.draw.rect(ecran,[255,150,150],[150,25,100,100])
pg.display.update()
ev = pg.event.wait()
if ev.type == pg.QUIT:
fin = True
pg.quit()Cercle
Pour tracer un cercle on utilise l'instruction pg.draw.circle(display, couleur, (abs,ord), rayon, épaisseur) :
import pygame as pg
pg.init()
pg.display.set_caption("Pygame : cercle")
ecran = pg.display.set_mode((400, 150))
vert = (0,255,100)
fin = False
while not fin:
ecran.fill(vert)
pg.draw.circle(ecran,[0,0,0],[200,75],30,3)
pg.display.update()
ev = pg.event.wait()
if ev.type == pg.QUIT:
fin = True
pg.quit()
import pygame as pg
pg.init()
pg.display.set_caption("Pygame : cercle")
ecran = pg.display.set_mode((400, 150))
vert = (0,255,100)
fin = False
while not fin:
ecran.fill(vert)
pg.draw.circle(ecran,[0,0,0],[200,75],30,0)
pg.display.update()
ev = pg.event.wait()
if ev.type == pg.QUIT:
fin = True
pg.quit()
import pygame as pg
pg.init()
pg.display.set_caption("Pygame : cercle")
ecran = pg.display.set_mode((400, 150))
vert = (0,255,100)
fin = False
while not fin:
ecran.fill(vert)
pg.draw.circle(ecran,[255,0,0],[200,75],30,0)
pg.draw.circle(ecran,[0,0,0],[200,75],30,4)
pg.display.update()
ev = pg.event.wait()
if ev.type == pg.QUIT:
fin = True
pg.quit()Ellipse
Pour tracer une ellipse on utilise l'instruction pg.draw.ellipse(display, couleur, (abs,ord,largeur,hauteur), épaisseur).
import pygame as pg
pg.init()
pg.display.set_caption("Pygame : ellipse")
ecran = pg.display.set_mode((400, 150))
vert = (0,255,100)
fin = False
while not fin:
ecran.fill(vert)
pg.draw.ellipse(ecran,[0,0,0],[150,30,100,80],2)
pg.display.update()
ev = pg.event.wait()
if ev.type == pg.QUIT:
fin = True
pg.quit()
import pygame as pg
pg.init()
pg.display.set_caption("Pygame : ellipse")
ecran = pg.display.set_mode((400, 150))
vert = (0,255,100)
fin = False
while not fin:
ecran.fill(vert)
pg.draw.ellipse(ecran,[0,0,0],[150,30,100,80],0)
pg.display.update()
ev = pg.event.wait()
if ev.type == pg.QUIT:
fin = True
pg.quit()Arc d'ellipse
Pour tracer un arc d'ellipse on utilise l'instruction pg.draw.arc(display, couleur, (abs,ord,largeur,hauteur), angle de départ, angle de fin, épaisseur). Les angles sont exprimés en radians.
import pygame as pg
pg.init()
pg.display.set_caption("Pygame : arc")
ecran = pg.display.set_mode((400, 150))
vert = (0,255,100)
fin = False
while not fin:
ecran.fill(vert)
pg.draw.arc(ecran,[0,0,0],[150,30,100,80],0,3.14159,2)
pg.display.update()
ev = pg.event.wait()
if ev.type == pg.QUIT:
fin = True
pg.quit()Écrire du texte
Pour écrire du texte il faut tout d'abord importer le module pygame.font et ensuite initialiser la prise en charge des polices de caractères avec pg.font.init().
On peut définir alors une police avec dans le code ci dessous l'instruction myfont = pg.font.SysFont('Arial', 30). La police s'appelle myfont et elle utilise Arial (mais on peut saisir celle que l'on souhaite) et la taille des caractères est de 20px.
On définit une surface sur laquelle on écrit avec l'instruction textsurface = myfont.render('GAME OVER', False, (0, 0, 200)) qui crée la surface textsurface sur laquelle on écrit « GAME OVER » avec la couleur (0,0,200) qui correspond à du bleu.
Pour ajouter cette surface à notre fenêtre Pygame on saisit ecran.blit(textsurface,(30,80)) pour la placer à l'abscisse 30 et l'ordonnée 80.
import pygame as pg
import pygame.font
pg.font.init()
myfont = pg.font.SysFont('Arial', 20)
pg.init()
pg.display.set_caption("Texte")
ecran = pg.display.set_mode((200, 200))
textsurface = myfont.render('GAME OVER', False, (0, 0, 200))
ecran.blit(textsurface,(30,80))
pg.display.update()
fin = False
while not fin:
ev = pg.event.wait()
if ev.type == pg.QUIT:
fin = True
pg.quit()Animations
import pygame as pg
pg.init()
pg.display.set_caption("Animation")
clock = pg.time.Clock()
ecran = pg.display.set_mode((400, 150))
x = 10
dx = 5
while True:
clock.tick(20)
ecran.fill(pg.Color("black"))
x += dx
pg.draw.circle(ecran,[255,255,255],[x,75],10,0)
pg.display.update()
for e in pg.event.get():
if e.type == pg.QUIT:
pg.quit()
pg.quit()
import pygame as pg
pg.init()
pg.display.set_caption("Animation")
clock = pg.time.Clock()
ecran = pg.display.set_mode((400, 150))
x = 10
dx = 5
while True:
clock.tick(20)
ecran.fill(pg.Color("black"))
if x > 390:
dx = -5
if x < 10:
dx = 5
x += dx
pg.draw.circle(ecran,[255,255,255],[x,75],10,0)
pg.display.update()
for e in pg.event.get():
if e.type == pg.QUIT:
pg.quit()
pg.quit()
import pygame as pg
pg.init()
pg.display.set_caption("Animation")
clock = pg.time.Clock()
ecran = pg.display.set_mode((400, 150))
y = 10
dy = 5
while True:
clock.tick(20)
ecran.fill(pg.Color("black"))
if y > 140:
dy = -5
if y < 10:
dy = 5
y += dy
pg.draw.circle(ecran,[255,255,255],[200,y],10,0)
pg.display.update()
for e in pg.event.get():
if e.type == pg.QUIT:
pg.quit()
pg.quit()
import pygame as pg
pg.init()
pg.display.set_caption("Animation")
clock = pg.time.Clock()
ecran = pg.display.set_mode((400, 150))
x = 30
dx = 5
y = 10
dy = 5
while True:
clock.tick(40)
ecran.fill(pg.Color("black"))
if x > 390:
dx = -5
if x < 10:
dx = 5
x += dx
if y > 140:
dy = -5
if y < 10:
dy = 5
y += dy
pg.draw.circle(ecran,[150,150,255],[x,y],10,0)
pg.draw.circle(ecran,[255,255,255],[x-4,y-4],2,0)
pg.display.update()
for e in pg.event.get():
if e.type == pg.QUIT:
pg.quit()
pg.quit()Mouvements au clavierDéplacements sans contrainte
import pygame as pg
pg.init()
pg.key.set_repeat(50, 25)
pg.display.set_caption("Clavier")
ecran = pg.display.set_mode((400, 400))
(x, y) = (200, 200)
(dx, dy) = (10, 10)
mvt = {pg.K_LEFT: (-dx, 0),
pg.K_RIGHT: (dx, 0),
pg.K_DOWN: (0, dy),
pg.K_UP: (0, -dy)}
dessin = True
fin = False
while not fin:
if dessin:
ecran.fill(pg.Color("black"))
pg.draw.circle(ecran,[255,255,255],[x,y],30,0)
pg.display.update()
dessin = False
ev = pg.event.wait()
if ev.type == pg.QUIT:
fin = True
if ev.type == pg.KEYDOWN:
if ev.key in mvt:
(DX, DY) = mvt[ev.key]
x += DX
y += DY
dessin = True
pg.quit() Déplacement horizontal + saut
import pygame as pg
pg.init()
pg.display.set_caption("Animation")
clock = pg.time.Clock()
ecran = pg.display.set_mode((400, 150))
x = 10
y = 100
dx = 0
dy = 0
mvt = {pg.K_LEFT: -5, pg.K_RIGHT: 5}
while True:
x += dx
y += dy
clock.tick(40)
ecran.fill([220,220,255])
pg.draw.rect(ecran,[0,200,50],[0,107,400,150])
pg.draw.circle(ecran,[100,110,255],[x,y],10,0)
pg.draw.circle(ecran,[255,255,255],[x-4,y-4],2,0)
pg.draw.ellipse(ecran,[20,150,20],[x-8,110,18,5],0)
pg.display.update()
for e in pg.event.get():
if e.type == pg.QUIT:
pg.quit()
break
if e.type == pg.KEYDOWN:
if e.key in mvt:
dx = mvt[e.key]
if e.key == pg.K_UP:
dy = -10
else:
dx = 0
if dy < 0 and y < 50:
dy = 10
y = 50
if dy > 0 and y>95:
dy = 0
y = 100
pg.quit()Mouvement continu
import pygame as pg
pg.init()
pg.display.set_caption("Animation")
clock = pg.time.Clock()
ecran = pg.display.set_mode((400, 150))
x = 10
y = 100
dx = 0
dy = 0
saut = False
while True:
x += dx
y += dy
clock.tick(40)
ecran.fill([220,220,255])
pg.draw.rect(ecran,[0,200,50],[0,107,400,150])
pg.draw.circle(ecran,[100,110,255],[x,y],10,0)
pg.draw.circle(ecran,[255,255,255],[x-4,y-4],2,0)
pg.draw.ellipse(ecran,[20,150,20],[x-8,110,18,5],0)
pg.display.update()
for e in pg.event.get():
if e.type == pg.QUIT:
pg.quit()
break
if e.type == pg.KEYDOWN:
if e.key == pg.K_LEFT:
dx = -5
if e.key == pg.K_RIGHT:
dx = 5
if e.key == pg.K_UP and not saut:
dy = -10
saut = True
if e.type == pg.KEYUP:
if e.key == pg.K_LEFT or e.key == pg.K_RIGHT:
dx = 0
if saut:
if dy < 0 and y < 50:
dy = 10
y = 50
if dy > 0 and y>95:
dy = 0
y = 100
saut = False
pg.quit()Détection des bordures
import pygame as pg
pg.init()
pg.display.set_caption("Animation")
clock = pg.time.Clock()
ecran = pg.display.set_mode((400, 150))
x = 10
y = 100
dx = 0
dy = 0
saut = False
gauche = False
droite = False
while True:
y += dy
if gauche and dx<0:
x += dx
if droite and dx>0:
x += dx
clock.tick(40)
ecran.fill([220,220,255])
pg.draw.rect(ecran,[0,200,50],[0,107,400,150])
pg.draw.circle(ecran,[100,110,255],[x,y],10,0)
pg.draw.circle(ecran,[255,255,255],[x-4,y-4],2,0)
pg.draw.ellipse(ecran,[20,150,20],[x-8,110,18,5],0)
pg.display.update()
for e in pg.event.get():
if e.type == pg.QUIT:
pg.quit()
break
if e.type == pg.KEYDOWN:
if e.key == pg.K_LEFT:
dx = -5
if e.key == pg.K_RIGHT:
dx = 5
if e.key == pg.K_UP and not saut:
dy = -10
saut = True
if e.type == pg.KEYUP:
if e.key == pg.K_LEFT or e.key == pg.K_RIGHT:
dx = 0
if saut:
if dy < 0 and y < 50:
dy = 10
y = 50
if dy > 0 and y>95:
dy = 0
y = 100
saut = False
if x < 11:
gauche = False
else:
gauche = True
if x >= 390:
droite = False
else:
droite = True
pg.quit()Ajout d'un ennemi
import pygame as pg
from random import*
pg.init()
pg.display.set_caption("Animation")
clock = pg.time.Clock()
ecran = pg.display.set_mode((400, 150))
x = 10
y = 100
dx = 0
dy = 0
saut = False
gauche = False
droite = False
mechant = False
xm = 500
ym = 100
dxm = 0
dym = 0
while True:
y += dy
if gauche and dx<0:
x += dx
if droite and dx>0:
x += dx
xm += dxm
ym += dym
clock.tick(40)
ecran.fill([220,220,255])
pg.draw.rect(ecran,[0,200,50],[0,107,400,150])
pg.draw.circle(ecran,[100,110,255],[x,y],10,0)
pg.draw.circle(ecran,[255,255,255],[x-4,y-4],2,0)
pg.draw.ellipse(ecran,[20,150,20],[x-8,110,18,5],0)
pg.draw.circle(ecran,[255,0,0],[xm,ym],10,0)
pg.draw.circle(ecran,[255,255,255],[xm-4,ym-4],2,0)
pg.display.update()
for e in pg.event.get():
if e.type == pg.QUIT:
pg.quit()
break
if e.type == pg.KEYDOWN:
if e.key == pg.K_LEFT:
dx = -5
if e.key == pg.K_RIGHT:
dx = 5
if e.key == pg.K_UP and not saut:
dy = -10
saut = True
if e.type == pg.KEYUP:
if e.key == pg.K_LEFT or e.key == pg.K_RIGHT:
dx = 0
if saut:
if dy < 0 and y < 50:
dy = 10
y = 50
if dy > 0 and y>95:
dy = 0
y = 100
saut = False
if x < 11:
gauche = False
else:
gauche = True
if x >= 390:
droite = False
else:
droite = True
proba = random()
if proba > 0.95 and not mechant:
mechant = True
if mechant:
dxm = -10
if xm < -200:
xm = 500
mechant = False
dxm = 0
pg.quit()Collision avec l'ennemi
import pygame as pg
from random import*
pg.init()
pg.display.set_caption("Animation")
clock = pg.time.Clock()
ecran = pg.display.set_mode((400, 150))
x = 10
y = 100
dx = 0
dy = 0
saut = False
gauche = False
droite = False
mechant = False
xm = 500
ym = 100
dxm = 0
dym = 0
collision = False
while True:
if abs(x-xm)<20 and abs(y-ym)<20:
collision = True
y += dy
if gauche and dx<0:
x += dx
if droite and dx>0:
x += dx
xm += dxm
ym += dym
clock.tick(40)
ecran.fill([220,220,255])
pg.draw.rect(ecran,[0,200,50],[0,107,400,150])
pg.draw.circle(ecran,[100,110,255],[x,y],10,0)
pg.draw.circle(ecran,[255,255,255],[x-4,y-4],2,0)
pg.draw.ellipse(ecran,[20,150,20],[x-8,110,18,5],0)
pg.draw.circle(ecran,[255,0,0],[xm,ym],10,0)
pg.draw.circle(ecran,[255,255,255],[xm-4,ym-4],2,0)
pg.draw.ellipse(ecran,[20,150,20],[xm-8,110,18,5],0)
pg.display.update()
for e in pg.event.get():
if e.type == pg.QUIT:
pg.quit()
break
if e.type == pg.KEYDOWN:
if e.key == pg.K_LEFT:
dx = -5
if e.key == pg.K_RIGHT:
dx = 5
if e.key == pg.K_UP and not saut:
dy = -10
saut = True
if e.type == pg.KEYUP:
if e.key == pg.K_LEFT or e.key == pg.K_RIGHT:
dx = 0
if saut:
if dy < 0 and y < 50:
dy = 10
y = 50
if dy > 0 and y>95:
dy = 0
y = 100
saut = False
if x < 11:
gauche = False
else:
gauche = True
if x >= 390:
droite = False
else:
droite = True
proba = random()
if proba > 0.95 and not mechant:
mechant = True
if mechant:
dxm = -10
if xm < -200:
xm = 500
mechant = False
dxm = 0
if collision:
dxm = 0
pg.quit()Gestion des vies
import pygame as pg
from random import*
pg.init()
pg.font.init()
pg.display.set_caption("Animation")
clock = pg.time.Clock()
ecran = pg.display.set_mode((400, 150))
x = 10
y = 100
dx = 0
dy = 0
saut = False
gauche = False
droite = False
mechant = False
xm = 500
ym = 100
dxm = 0
dym = 0
vies = 5
collision = False
while True:
if vies == -1:
pg.quit()
if abs(x-xm)<20 and abs(y-ym)<20:
collision = True
y += dy
if gauche and dx<0:
x += dx
if droite and dx>0:
x += dx
xm += dxm
ym += dym
clock.tick(40)
ecran.fill([220,220,255])
pg.draw.rect(ecran,[200,100,100],[5,5,vies*10,10])
pg.draw.rect(ecran,[0,200,50],[0,107,400,150])
pg.draw.circle(ecran,[100,110,255],[x,y],10,0)
pg.draw.circle(ecran,[255,255,255],[x-4,y-4],2,0)
pg.draw.ellipse(ecran,[20,150,20],[x-8,110,18,5],0)
pg.draw.circle(ecran,[255,0,0],[xm,ym],10,0)
pg.draw.circle(ecran,[255,255,255],[xm-4,ym-4],2,0)
pg.draw.ellipse(ecran,[20,150,20],[xm-8,110,18,5],0)
pg.display.update()
for e in pg.event.get():
if e.type == pg.QUIT:
pg.quit()
break
if e.type == pg.KEYDOWN:
if e.key == pg.K_LEFT:
dx = -5
if e.key == pg.K_RIGHT:
dx = 5
if e.key == pg.K_UP and not saut:
dy = -10
saut = True
if e.type == pg.KEYUP:
if e.key == pg.K_LEFT or e.key == pg.K_RIGHT:
dx = 0
if saut:
if dy < 0 and y < 50:
dy = 10
y = 50
if dy > 0 and y>95:
dy = 0
y = 100
saut = False
if x < 11:
gauche = False
else:
gauche = True
if x >= 390:
droite = False
else:
droite = True
proba = random()
if proba > 0.95 and not mechant:
mechant = True
if mechant:
dxm = -10
if xm < -200:
xm = 500
mechant = False
dxm = 0
if collision:
xm = 500
mechant = False
dxm = 0
vies -= 1
collision = False
pg.quit()Gestion du « game over » et du score
import pygame as pg
import pygame.font
from random import*
pg.font.init()
myfont = pg.font.SysFont('Arial', 30)
pg.init()
pg.display.set_caption("Animation")
clock = pg.time.Clock()
ecran = pg.display.set_mode((400, 150))
x = 10
y = 100
dx = 0
dy = 0
saut = False
gauche = False
droite = False
mechant = False
xm = 500
ym = 100
dxm = 0
dym = 0
vies = 5
collision = False
score = 0
jeu = True
while jeu:
score += 1
if vies == -1:
jeu = False
if abs(x-xm)<20 and abs(y-ym)<20:
collision = True
y += dy
if gauche and dx<0:
x += dx
if droite and dx>0:
x += dx
xm += dxm
ym += dym
clock.tick(40)
ecran.fill([220,220,255])
if jeu:
pg.draw.rect(ecran,[200,100,100],[5,5,vies*10,10])
else:
myfont1 = pg.font.SysFont('Arial', 30)
textsurface1 = myfont1.render('GAME OVER', False, (0, 0, 0))
ecran.blit(textsurface1,(100,20))
myfont2 = pg.font.SysFont('Arial', 20)
textsurface2 = myfont2.render('SCORE : '+str(score), False, (0, 0, 0))
ecran.blit(textsurface2,(135,70))
pg.draw.rect(ecran,[0,200,50],[0,107,400,150])
pg.draw.circle(ecran,[100,110,255],[x,y],10,0)
pg.draw.circle(ecran,[255,255,255],[x-4,y-4],2,0)
pg.draw.ellipse(ecran,[20,150,20],[x-8,110,18,5],0)
pg.draw.circle(ecran,[255,0,0],[xm,ym],10,0)
pg.draw.circle(ecran,[255,255,255],[xm-4,ym-4],2,0)
pg.draw.ellipse(ecran,[20,150,20],[xm-8,110,18,5],0)
pg.display.update()
for e in pg.event.get():
if e.type == pg.QUIT:
pg.quit()
break
if e.type == pg.KEYDOWN:
if e.key == pg.K_LEFT:
dx = -5
if e.key == pg.K_RIGHT:
dx = 5
if e.key == pg.K_UP and not saut:
dy = -10
saut = True
if e.type == pg.KEYUP:
if e.key == pg.K_LEFT or e.key == pg.K_RIGHT:
dx = 0
if saut:
if dy < 0 and y < 50:
dy = 10
y = 50
if dy > 0 and y>95:
dy = 0
y = 100
saut = False
if x < 11:
gauche = False
else:
gauche = True
if x >= 390:
droite = False
else:
droite = True
proba = random()
if proba > 0.95 and not mechant:
mechant = True
if mechant:
dxm = -10
if xm < -200:
xm = 500
mechant = False
dxm = 0
if collision:
xm = 500
mechant = False
dxm = 0
vies -= 1
collision = False
while True:
clock.tick(40)
for e in pg.event.get():
if e.type == pg.QUIT:
pg.quit()
breakAmélioration du mouvement des ennemis
import pygame as pg
import pygame.font
from random import*
pg.font.init()
myfont = pg.font.SysFont('Arial', 30)
pg.init()
pg.display.set_caption("Animation")
clock = pg.time.Clock()
ecran = pg.display.set_mode((400, 150))
x = 200
y = 100
dx = 0
dy = 0
saut = False
gauche = False
droite = False
mechant = False
xm = 500
ym = 100
dxm = 0
dym = 0
vies = 5
collision = False
score = 0
jeu = True
while jeu:
score += 1
if vies == -1:
jeu = False
if abs(x-xm)<20 and abs(y-ym)<20:
collision = True
y += dy
if gauche and dx<0:
x += dx
if droite and dx>0:
x += dx
xm += dxm
ym += dym
clock.tick(40)
ecran.fill([220,220,255])
if jeu:
pg.draw.rect(ecran,[200,100,100],[5,5,vies*10,10])
else:
myfont1 = pg.font.SysFont('Arial', 30)
textsurface1 = myfont1.render('GAME OVER', False, (0, 0, 0))
ecran.blit(textsurface1,(100,20))
myfont2 = pg.font.SysFont('Arial', 20)
textsurface2 = myfont2.render('SCORE : '+str(score), False, (0, 0, 0))
ecran.blit(textsurface2,(135,70))
pg.draw.rect(ecran,[0,200,50],[0,107,400,150])
pg.draw.circle(ecran,[100,110,255],[x,y],10,0)
pg.draw.circle(ecran,[255,255,255],[x-4,y-4],2,0)
pg.draw.ellipse(ecran,[20,150,20],[x-8,110,18,5],0)
pg.draw.circle(ecran,[255,0,0],[xm,ym],10,0)
pg.draw.circle(ecran,[255,255,255],[xm-4,ym-4],2,0)
pg.draw.ellipse(ecran,[20,150,20],[xm-8,110,18,5],0)
pg.display.update()
for e in pg.event.get():
if e.type == pg.QUIT:
pg.quit()
break
if e.type == pg.KEYDOWN:
if e.key == pg.K_LEFT:
dx = -5
if e.key == pg.K_RIGHT:
dx = 5
if e.key == pg.K_UP and not saut:
dy = -10
saut = True
if e.type == pg.KEYUP:
if e.key == pg.K_LEFT or e.key == pg.K_RIGHT:
dx = 0
if saut:
if dy < 0 and y < 50:
dy = 10
y = 50
if dy > 0 and y>95:
dy = 0
y = 100
saut = False
if x < 11:
gauche = False
else:
gauche = True
if x >= 390:
droite = False
else:
droite = True
proba = random()
if proba > 0.95 and not mechant:
mechant = True
signe = 2*randint(0,1)-1
if signe == 1:
xm = 500
dxm = -10
else:
xm = -100
dxm = 10
if xm < -200:
xm = 500
mechant = False
dxm = 0
if xm > 600:
xm = -100
mechant = False
dxm = 0
if collision:
xm = 500
mechant = False
dxm = 0
vies -= 1
collision = False
while True:
clock.tick(40)
for e in pg.event.get():
if e.type == pg.QUIT:
pg.quit()
breakAmélioration du mouvement des ennemis n°2
import pygame as pg
import pygame.font
from random import*
pg.font.init()
myfont = pg.font.SysFont('Arial', 30)
pg.init()
pg.display.set_caption("Animation")
clock = pg.time.Clock()
ecran = pg.display.set_mode((400, 150))
x = 200
y = 100
dx = 0
dy = 0
saut = False
gauche = False
droite = False
mechant = False
xm = 500
ym = 100
dxm = 0
dym = 0
vies = 5
collision = False
score = 0
jeu = True
while jeu:
score += 1
if vies == -1:
jeu = False
if abs(x-xm)<20 and abs(y-ym)<20:
collision = True
y += dy
if gauche and dx<0:
x += dx
if droite and dx>0:
x += dx
xm += dxm
ym += dym
clock.tick(40)
ecran.fill([220,220,255])
if jeu:
pg.draw.rect(ecran,[200,100,100],[5,5,vies*10,10])
else:
myfont1 = pg.font.SysFont('Arial', 30)
textsurface1 = myfont1.render('GAME OVER', False, (0, 0, 0))
ecran.blit(textsurface1,(100,20))
myfont2 = pg.font.SysFont('Arial', 20)
textsurface2 = myfont2.render('SCORE : '+str(score), False, (0, 0, 0))
ecran.blit(textsurface2,(135,70))
pg.draw.rect(ecran,[0,200,50],[0,107,400,150])
pg.draw.circle(ecran,[100,110,255],[x,y],10,0)
pg.draw.circle(ecran,[255,255,255],[x-4,y-4],2,0)
pg.draw.ellipse(ecran,[20,150,20],[x-8,110,18,5],0)
pg.draw.circle(ecran,[255,0,0],[xm,ym],10,0)
pg.draw.circle(ecran,[255,255,255],[xm-4,ym-4],2,0)
pg.draw.ellipse(ecran,[20,150,20],[xm-8,110,18,5],0)
pg.display.update()
for e in pg.event.get():
if e.type == pg.QUIT:
pg.quit()
break
if e.type == pg.KEYDOWN:
if e.key == pg.K_LEFT:
dx = -5
if e.key == pg.K_RIGHT:
dx = 5
if e.key == pg.K_UP and not saut:
dy = -10
saut = True
if e.type == pg.KEYUP:
if e.key == pg.K_LEFT or e.key == pg.K_RIGHT:
dx = 0
if saut:
if dy < 0 and y < 50:
dy = 10
y = 50
if dy > 0 and y>95:
dy = 0
y = 100
saut = False
if x < 11:
gauche = False
else:
gauche = True
if x >= 390:
droite = False
else:
droite = True
proba = random()
if proba > 0.95 and not mechant:
mechant = True
signe = 2*randint(0,1)-1
if signe == 1:
xm = 500
dxm = -randint(5,15)
else:
xm = -100
dxm = randint(5,15)
if xm < -200:
xm = 500
mechant = False
dxm = 0
if xm > 600:
xm = -100
mechant = False
dxm = 0
if collision:
xm = 500
mechant = False
dxm = 0
vies -= 1
collision = False
while True:
clock.tick(40)
for e in pg.event.get():
if e.type == pg.QUIT:
pg.quit()
breakAmélioration du mouvement des ennemis n°3
import pygame as pg
import pygame.font
from random import*
pg.font.init()
myfont = pg.font.SysFont('Arial', 30)
pg.init()
pg.display.set_caption("Animation")
clock = pg.time.Clock()
ecran = pg.display.set_mode((400, 150))
x = 200
y = 100
dx = 0
dy = 0
saut = False
gauche = False
droite = False
mechant = False
xm = 500
ym = 100
dxm = 0
dym = 0
vies = 5
collision = False
score = 0
jeu = True
while jeu:
score += 1
if vies == -1:
jeu = False
if abs(x-xm)<20 and abs(y-ym)<20:
collision = True
y += dy
if gauche and dx<0:
x += dx
if droite and dx>0:
x += dx
xm += dxm
ym += dym
clock.tick(40)
ecran.fill([220,220,255])
if jeu:
pg.draw.rect(ecran,[200,100,100],[5,5,vies*10,10])
else:
myfont1 = pg.font.SysFont('Arial', 30)
textsurface1 = myfont1.render('GAME OVER', False, (0, 0, 0))
ecran.blit(textsurface1,(100,20))
myfont2 = pg.font.SysFont('Arial', 20)
textsurface2 = myfont2.render('SCORE : '+str(score), False, (0, 0, 0))
ecran.blit(textsurface2,(135,70))
pg.draw.rect(ecran,[0,200,50],[0,107,400,150])
pg.draw.circle(ecran,[100,110,255],[x,y],10,0)
pg.draw.circle(ecran,[255,255,255],[x-4,y-4],2,0)
pg.draw.ellipse(ecran,[20,150,20],[x-8,110,18,5],0)
pg.draw.circle(ecran,[255,0,0],[xm,ym],10,0)
pg.draw.circle(ecran,[255,255,255],[xm-4,ym-4],2,0)
pg.draw.ellipse(ecran,[20,150,20],[xm-8,110,18,5],0)
pg.display.update()
for e in pg.event.get():
if e.type == pg.QUIT:
pg.quit()
break
if e.type == pg.KEYDOWN:
if e.key == pg.K_LEFT:
dx = -5
if e.key == pg.K_RIGHT:
dx = 5
if e.key == pg.K_UP and not saut:
dy = -10
saut = True
if e.type == pg.KEYUP:
if e.key == pg.K_LEFT or e.key == pg.K_RIGHT:
dx = 0
if saut:
if dy < 0 and y < 50:
dy = 10
y = 50
if dy > 0 and y>95:
dy = 0
y = 100
saut = False
if x < 11:
gauche = False
else:
gauche = True
if x >= 390:
droite = False
else:
droite = True
proba = random()
if proba > 0.95 and not mechant:
mechant = True
signe = 2*randint(0,1)-1
dym = randint(0,5)
ym = 100
if signe == 1:
xm = 500
dxm = -randint(5,15)
else:
xm = -100
dxm = randint(5,15)
if xm < -200:
xm = 500
mechant = False
dxm = 0
if xm > 600:
xm = -100
mechant = False
dxm = 0
if ym < 50:
dym = abs(dym)
if ym > 120:
dym = -abs(dym)
if collision:
xm = 500
mechant = False
dxm = 0
vies -= 1
collision = False
while True:
clock.tick(40)
for e in pg.event.get():
if e.type == pg.QUIT:
pg.quit()
breakUn exemple de jeu un peu plus complexe
from random import*
import pygame as pg
import pygame.font
pg.font.init()
myfont = pg.font.SysFont('Arial', 30)
pg.init()
clock = pg.time.Clock()
pg.key.set_repeat(10, 5)
pg.display.set_caption("Labyrinthe")
ecran = pg.display.set_mode((640, 360))
global niveau, xp1, yp1, mvtp1, objp1, perso, jeu, finjeu
bg = pg.image.load("https://www.sarmate.xyz//Cours/Informatique/pygame/exemples/labyrinthe/lave.png").convert_alpha()
bg = pg.transform.scale(bg , (640,360) )
perso1 = pg.image.load("https://www.sarmate.xyz//Cours/Informatique/pygame/exemples/labyrinthe/e1.png").convert_alpha()
perso1 = pg.transform.scale(perso1 , (20,20) )
perso2 = pg.image.load("https://www.sarmate.xyz//Cours/Informatique/pygame/exemples/labyrinthe/e2.png").convert_alpha()
perso2 = pg.transform.scale(perso2 , (20,20) )
perso3 = pg.image.load("https://www.sarmate.xyz//Cours/Informatique/pygame/exemples/labyrinthe/e3.png").convert_alpha()
perso3 = pg.transform.scale(perso3 , (20,20) )
perso4 = pg.image.load("https://www.sarmate.xyz//Cours/Informatique/pygame/exemples/labyrinthe/e4.png").convert_alpha()
perso4 = pg.transform.scale(perso4 , (20,20) )
path = pg.image.load("https://www.sarmate.xyz//Cours/Informatique/pygame/exemples/labyrinthe/path.png").convert_alpha()
path = pg.transform.scale(path , (20,20) )
fin = pg.image.load("https://www.sarmate.xyz//Cours/Informatique/pygame/exemples/labyrinthe/fin.png").convert_alpha()
fin = pg.transform.scale(fin , (20,20) )
niveaux =[
[
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,0],
[0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,1,1,1,1,0,1,0,1,0],
[0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,0,1,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1,1,1,1,1,1,1,1,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
]
]
initniveau = [(100,40),(480,320)]
def afficheEcran():
ecran.blit(bg, (0,0) )
for i in range(0,18):
for j in range(0,32):
if niveau[i][j] == 1:
ecran.blit(path, (20*j,20*i) )
if niveau[i][j] == 2:
ecran.blit(fin, (20*j,20*i) )
ecran.blit(perso, (xp1,yp1) )
pg.display.update()
n = randint(0, len(niveaux)-1)
niveau = niveaux[n]
(xp1,yp1) = initniveau[n]
mouvementp1 = False
mvtp1 = (0,0)
objp1 = (xp1,yp1)
perso = perso1
jeu = True
finjeu = False
def initialise():
global niveau, xp1, yp1, mvtp1, objp1, perso, jeu, finjeu
n = randint(0, len(niveaux)-1)
niveau = niveaux[n]
(xp1,yp1) = initniveau[n]
mouvementp1 = False
mvtp1 = (0,0)
objp1 = (xp1,yp1)
perso = perso1
jeu = True
finjeu = False
initialise()
while True:
while jeu:
clock.tick(80)
afficheEcran()
if niveau[yp1/20][xp1/20] == 2 and not mouvementp1:
jeu = False
finjeu = True
for e in pg.event.get():
if e.type == pg.QUIT:
pg.quit()
if e.type == pg.KEYDOWN:
if e.key == pg.K_LEFT and not mouvementp1:
if niveau[yp1/20][xp1/20-1] != 0:
mouvementp1 = True
mvtp1 = (-4,0)
objp1 = (xp1-20,yp1)
perso = perso2
if e.key == pg.K_RIGHT and not mouvementp1:
if niveau[yp1/20][xp1/20+1] != 0:
mouvementp1 = True
mvtp1 = (4,0)
objp1 = (xp1+20,yp1)
perso = perso3
if e.key == pg.K_UP and not mouvementp1:
if niveau[yp1/20-1][xp1/20] != 0:
mouvementp1 = True
mvtp1 = (0,-4)
objp1 = (xp1,yp1-20)
perso = perso4
if e.key == pg.K_DOWN and not mouvementp1:
if niveau[yp1/20+1][xp1/20] != 0:
mouvementp1 = True
mvtp1 = (0,4)
objp1 = (xp1,yp1+20)
perso = perso1
if mouvementp1:
(xp1,yp1) = (xp1+mvtp1[0],yp1+mvtp1[1])
if (xp1,yp1) == objp1:
mouvementp1 = False
while finjeu:
clock.tick(40)
textsurface = myfont.render('VICTOIRE', False, (255, 255, 255))
ecran.blit(textsurface,(250,150))
pg.display.update()
for e in pg.event.get():
if e.type == pg.QUIT:
pg.quit()
if e.type == pg.KEYDOWN:
initialise()