Bonjour a toutes et a tous ,
J'ai pensé que vous pourriez m'aider sur un ou deux points que je ne comprends pas . Je vous donne l'ensemble du code pas trop long , puis je vous pose ma question .
Le main :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import pygame, random, sys
from CLASSES import *
from CONSTANTES import *
 
def AFFICHER_SCORE():
  font = pygame.font.SysFont('Arial', TUILE_TAILLE - 5)
  background = pygame.Surface((LARGEUR, SCORE_HAUTEUR))
  background = background.convert()
  background.fill(BLANC)
  text = font.render(_personnage.chrono.Timer.strftime("%H:%M:%S"), 1, NOIR)
  textpos = text.get_rect(centerx = LARGEUR / 2, centery = SCORE_HAUTEUR / 2)
  background.blit(text, textpos)
  screen.blit(background, (0, 0))
 
pygame.init()
 
screen = pygame.display.set_mode([LARGEUR, HAUTEUR])
pygame.display.set_caption('Le jeu du labyrinthe')
 
XX = 0
YY = 0
with open("Labyrinthe.txt", "r") as fichier:
  for ligne in fichier:
    for sprite in ligne:
      if sprite == 'M':
        _mur = MUR(XX, YY)
        LISTE_MURS.add(_mur)
        LISTE_GLOBALE_SPRITES.add(_mur)
      XX = XX + 1
    XX = 0
    YY = YY + 1
 
_personnage = None
RECHERCHE_PERSONNAGE = True
while RECHERCHE_PERSONNAGE:
    _personnage = PERSONNAGE()
    LISTE_CONFLIT = pygame.sprite.spritecollide(_personnage, LISTE_MURS, False)
    if len(LISTE_CONFLIT) == 0:
        LISTE_GLOBALE_SPRITES.add(_personnage)
        RECHERCHE_PERSONNAGE = False
 
while len(LISTE_OBJETS) < 10:
    _objet = OBJET()
    LISTE_CONFLIT = pygame.sprite.spritecollide(_objet, LISTE_GLOBALE_SPRITES, False)
    if len(LISTE_CONFLIT) == 0:
        LISTE_GLOBALE_SPRITES.add(_objet)
        LISTE_OBJETS.add(_objet)
 
clock = pygame.time.Clock()
 
print("C'est parti...")
 
RUN = True
 
while RUN:
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      RUN = False
    elif event.type == pygame.KEYDOWN:
      if event.key == pygame.K_LEFT:
        _personnage.DIRECTION = 'G'
        break
      elif event.key == pygame.K_RIGHT:
        _personnage.DIRECTION = 'D'
        break
      elif event.key == pygame.K_UP:
        _personnage.DIRECTION = 'H'
        break
      elif event.key == pygame.K_DOWN:
        _personnage.DIRECTION = 'B'
        break
 
  LISTE_GLOBALE_SPRITES.update()
  screen.fill(BLANC)
 
  LISTE_GLOBALE_SPRITES.draw(screen)
  AFFICHER_SCORE()
 
  if _personnage.TERMINE:
    pygame.time.wait(5000)
    TERMINE = False
 
  pygame.display.flip()
  dt = clock.tick(60)
  _personnage.chrono.update(dt)
 
print("Nombre d'objets ramassés : %d" % _personnage.POINTS)
pygame.quit()
CLASSES.py

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import pygame, random, sys
from CONSTANTES import *
from datetime import timedelta, datetime, date, time
 
LISTE_OBJETS = pygame.sprite.Group()
LISTE_MURS = pygame.sprite.Group()
LISTE_GLOBALE_SPRITES = pygame.sprite.Group()
 
class MUR(pygame.sprite.Sprite):
  def __init__(self, x, y):
    pygame.sprite.Sprite.__init__(self)
    self.image = pygame.image.load("MUR.png").convert_alpha()
    self.rect = self.image.get_rect()
    self.rect.y = TUILE_TAILLE * y + SCORE_HAUTEUR
    self.rect.x = TUILE_TAILLE * x
 
class OBJET(pygame.sprite.Sprite):
  def __init__(self):
    pygame.sprite.Sprite.__init__(self)
    self.image = pygame.image.load("OBJET.png").convert_alpha()
    self.rect = self.image.get_rect()
    self.rect.y = random.randint(0, TUILE_NOMBRE - 1) * TUILE_TAILLE + SCORE_HAUTEUR
    self.rect.x = random.randint(0, TUILE_NOMBRE - 1) * TUILE_TAILLE
 
class PERSONNAGE(pygame.sprite.Sprite):
  def __init__(self):
    pygame.sprite.Sprite.__init__(self)
    self.image = pygame.image.load("PERSONNAGE.png").convert_alpha()
    self.rect = self.image.get_rect()
    self.rect.y = random.randint(0, TUILE_NOMBRE - 1) * TUILE_TAILLE + SCORE_HAUTEUR
    self.rect.x = random.randint(0, TUILE_NOMBRE - 1) * TUILE_TAILLE
    self.POINTS = 0
    self.TERMINE = False
    self.DIRECTION = '-'
    self.chrono = Chrono()
 
  def update(self):
    X_COURANT = self.rect.x
    Y_COURANT = self.rect.y
    if self.DIRECTION == 'G':
      self.rect.x -= TUILE_TAILLE
      self.DIRECTION = '-'
    elif self.DIRECTION == 'D':
      self.rect.x += TUILE_TAILLE
      self.DIRECTION = '-'
    elif self.DIRECTION == 'H':
      self.rect.y -= TUILE_TAILLE
      self.DIRECTION = '-'
    elif self.DIRECTION == 'B':
      self.rect.y += TUILE_TAILLE
      self.DIRECTION = '-'
 
    LISTE_COLLISION_MUR = pygame.sprite.spritecollide(self, LISTE_MURS, False)
    if len(LISTE_COLLISION_MUR) > 0:
        self.rect.x = X_COURANT
        self.rect.y = Y_COURANT
 
    LISTE_COLLISION_OBJET = pygame.sprite.spritecollide(self, LISTE_OBJETS, False)
    for objet in LISTE_COLLISION_OBJET:
      objet.kill()
      self.POINTS += POINT_UNITE
      if self.POINTS == 10:
          self.chrono.stop()
 
class Chrono:
  def __init__(self):
   self.Timer = datetime.combine(date.today(), time(0, 0))
   self.STOP = False
 
  def stop(self):
      self.STOP = True
 
  def update(self, dt):
      if self.STOP == False:
        self.Timer += timedelta(milliseconds=dt)
CONSTANTES.py

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
NOIR = (0, 0, 0)
BLANC = (255, 255, 255)
 
TUILE_TAILLE = 30
TUILE_NOMBRE = 25
SCORE_HAUTEUR = 32
 
LARGEUR = TUILE_TAILLE * TUILE_NOMBRE
HAUTEUR = TUILE_TAILLE * TUILE_NOMBRE + SCORE_HAUTEUR
 
POINT_UNITE = 1
Question :
- Dans le fichier main :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
LISTE_GLOBALE_SPRITES.update()
Je ne vois qu'une seule methode update , celle contenue dans la classe PERSONNAGE .
LISTE_GLOBALE_SPRITES a t il le droit d'utiliser la methode " def update(self): " qui se trouve dans la classe PERSONNAGE ?
Meme si on importe la classe PERSONNAGE dans le main , elle n'appartient qu'a cette classe , non ?
Ou peut etre utilise t il une methode update contenue dans la classe Sprite ?