IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Programmation multimédia/Jeux Python Discussion :

[Pygame] [Pygame.sprite] Ralentissement programme


Sujet :

Programmation multimédia/Jeux Python

  1. #1
    Nouveau Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Septembre 2017
    Messages
    1
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 24
    Localisation : France, Manche (Basse Normandie)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Septembre 2017
    Messages : 1
    Points : 1
    Points
    1
    Par défaut [Pygame] [Pygame.sprite] Ralentissement programme
    Bonjour et merci à vous de lire mon message,

    J'ai fait un code en python avec le module pygame :
    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
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    import pygame
    import random
    import sys
    import os
     
    from pygame.locals import *
    from parametre import *
     
    pygame.init()
     
    ghost = 0
     
    cooldownright = -5000
    cooldownleft = -5000
     
    cooldownvitesseleft = -50
    cooldownvitesseright = -50
     
     
    n = 0
     
    #Ouverture de la fenêtre Pygame ( qui s'adaptera automatiquement )
    fenetre = pygame.display.set_mode((1920,1080), RESIZABLE)
     
    fond = pygame.image.load("fond.jpg").convert_alpha()
    fenetre.blit(fond, (0,0))
     
    #Pour les personnages
    perso_01 = "images/personnages/SwordMan/0.2/SwordmanMouv1.png"  
    perso_02 = "images/personnages/SwordMan/0.2/SwordmanReverseMouv1.png"
     
    #Rafraîchissement
    pygame.display.flip()
     
    class Stickman_Left(pygame.sprite.Sprite):
     
        def __init__(self):
     
            super().__init__()
            self.power = random.randint(10, 15)
            self.healt = 100
            self.moveSpeed = 1
     
            self.animMouvLeft = anim_perso_Left
            self.animFightLeft = anim_fight_Left
     
            self.image = pygame.image.load(anim_perso_Left[0]).convert_alpha()
            self.rect = self.image.get_rect()
     
            self.index = 0
            self.image = pygame.image.load(anim_perso_Left[self.index])
     
            self.animationFrame = 12
            self.currentFrame = 0
     
            self.mouvement = 1
     
        def update(self):
            self.currentFrame += 1
            if self.currentFrame >= self.animationFrame:
                self.currentFrame = 0
                self.index = (self.index + 1) % len(anim_perso_Left)
                self.image = pygame.image.load(anim_perso_Left[self.index])
     
        def move_Right(self):
     
            if self.mouvement == 1:
     
                self.rect.x += 3
     
        def move_Left(self):
     
            if self.mouvement == 1:
     
                self.rect.x -= 3
     
        def stop_Move(self):
     
            self.mouvement = 0
     
        def Move(self):
     
            self.mouvement = 1
     
     
     
    class Stickman_Right(pygame.sprite.Sprite):
     
        def __init__(self):
     
            super().__init__()
            self.power = random.randint(10, 15)
            self.healt = 100
            self.moveSpeed = 1
     
            self.animMouvRight = anim_perso_Right
            self.animFightRight = anim_fight_Right
     
            self.image = pygame.image.load(anim_perso_Right[0]).convert_alpha()
            self.rect = self.image.get_rect()
     
            self.index = 0
            self.image = pygame.image.load(anim_perso_Right[self.index])
     
            self.animationFrame = 12
            self.currentFrame = 0
     
            self.mouvement = 1
     
            self.cooldown = 5000
     
        def update(self):
            self.currentFrame += 1
            if self.currentFrame >= self.animationFrame:
                self.currentFrame = 0
                self.index = (self.index + 1) % len(anim_perso_Right)
                self.image = pygame.image.load(anim_perso_Right[self.index])
     
        def move_Right(self):
     
            if self.mouvement == 1:
     
                self.rect.x += 3
     
        def move_Left(self):
     
            if self.mouvement == 1:
     
                self.rect.x -= 3
     
        def stop_Move(self):
     
            self.mouvement = 0
     
        def Move(self):
     
            self.mouvement = 1
     
     
     
     
     
     
    tab=[]
     
    Stickman_Left_list = pygame.sprite.Group()
     
    Stickman_Right_list = pygame.sprite.Group()
     
    all_sprites_list = pygame.sprite.Group()
     
    continuer = 1
     
    pygame.key.set_repeat(400,100)
     
    clock = pygame.time.Clock()
     
    while continuer == 1:
     
        milliseconds += clock.tick(120)
     
        if cooldownvitesseleft + 50 < milliseconds:
     
            for persos in Stickman_Left_list :
     
                persos.move_Right()
     
            cooldownvitesseleft = milliseconds
     
        if cooldownvitesseright + 50 < milliseconds:
     
            for persos in Stickman_Right_list :
     
                persos.move_Left()
     
            cooldownvitesseright = milliseconds
     
        all_sprites_list.update()
        all_sprites_list.draw(fenetre)
     
        pygame.display.flip()
     
        fenetre.blit(fond, (0,0))
     
        fight_Left_list = pygame.sprite.groupcollide(Stickman_Left_list, Stickman_Right_list, 0, 0)
     
        fight_Right_list = pygame.sprite.groupcollide(Stickman_Right_list, Stickman_Left_list, 0, 0)
     
        #coll_group_Left_list = pygame.sprite.groupcollide(Stickman_Left_list, Stickman_Left_list, 0, 0)
     
        #coll_group_Right_list = pygame.sprite.groupcollide(Stickman_Right_list, Stickman_Right_list, 0, 0)
     
        for persos in Stickman_Left_list:
     
            Stickman_Left_list_2 = Stickman_Left_list.copy()
     
            Stickman_Left_list_2.remove(persos)
     
            if pygame.sprite.spritecollideany(persos, Stickman_Left_list_2):
     
                persos.stop_Move()
     
        for persos in Stickman_Right_list:
     
            Stickman_Right_list_2 = Stickman_Right_list.copy()
     
            Stickman_Right_list_2.remove(persos)
     
            if pygame.sprite.spritecollideany(persos, Stickman_Right_list_2):
     
                persos.stop_Move()         
     
     
        for persos in fight_Left_list :
     
            persos.stop_Move()       
     
        for persos in fight_Right_list :
     
            persos.stop_Move()    
     
     
        for event in pygame.event.get():
     
                if event.type == KEYDOWN and event.key == K_s:
     
                    if cooldownleft + 3000 < milliseconds :
     
                        tab.append(ghost)
                        tab[n] = Stickman_Left()
                        tab[n].rect.x = 0
                        tab[n].rect.y = 600   
                        all_sprites_list.add(tab[n])
                        Stickman_Left_list.add(tab[n])
                        n = n + 1
                        cooldownleft = milliseconds
     
                if event.type == KEYDOWN and event.key == K_d:
     
                    if cooldownright + 3000 < milliseconds :
     
                        tab.append(ghost)
                        tab[n] = Stickman_Right()
                        tab[n].rect.x = 1920
                        tab[n].rect.y = 600   
                        all_sprites_list.add(tab[n])
                        Stickman_Right_list.add(tab[n])
                        n = n + 1
                        cooldownright = milliseconds
     
     
     
                if event.type == QUIT:
                      pygame.quit()
                      sys.exit()
    Et cela fait maintenant plusieurs heures que j'essaye de l'optimiser :

    Quand des collisions ont lieux, le jeu ralentit et mon pc portable enclenche les ventilateurs comme si je lui demandais de jouer à un jeu en ultra...

    Et je me demande ce que j'ai fait de mal dans mon code .

    Je joins au message mon dossier ( il faut exécuter Stick.py avec l'IDLE )

    Merci d'avoir lu,

    Skyloq
    Fichiers attachés Fichiers attachés

  2. #2
    Membre confirmé

    Homme Profil pro
    Bidouilleur
    Inscrit en
    Avril 2016
    Messages
    721
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Bidouilleur

    Informations forums :
    Inscription : Avril 2016
    Messages : 721
    Points : 503
    Points
    503
    Billets dans le blog
    1
    Par défaut
    Salut,

    Pour moi pas vraiment de problèmes majeurs, même avec pleins de personnages.

    Le seul truc qui je pense pourrait poser problème est le fait que tu charges chaque fois une image de ton perso dans ton update, tu devrais d'abord tout charger dans ton __init__ et stocker les références à tes images dans un itérable.

    Bon niveau code y'a évidemment plein de trucs à revoir.
    Le temps ronge l'amour comme l'acide.

Discussions similaires

  1. Comprendre et utiliser le module Sprite de Pygame
    Par LittleWhite dans le forum Développement 2D, 3D et Jeux
    Réponses: 0
    Dernier message: 11/11/2013, 21h03
  2. Erreur programme Pygame
    Par Dramdun dans le forum Tkinter
    Réponses: 8
    Dernier message: 24/11/2012, 15h26
  3. sprites et événements souris -Pygame-
    Par nono1602 dans le forum Programmation multimédia/Jeux
    Réponses: 2
    Dernier message: 02/11/2012, 23h59
  4. collision entre groupe de sprites (pygame)
    Par jean-pat dans le forum Programmation multimédia/Jeux
    Réponses: 8
    Dernier message: 17/09/2011, 16h22
  5. [Pygame] Sprite et Rect
    Par maxl1 dans le forum Programmation multimédia/Jeux
    Réponses: 1
    Dernier message: 18/04/2009, 17h36

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo