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

Python Discussion :

programme qui reste sur 1 fenetre noire (pas d'erreur)


Sujet :

Python

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Homme Profil pro
    conducteur autocar
    Inscrit en
    Janvier 2020
    Messages
    67
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : conducteur autocar
    Secteur : Transports

    Informations forums :
    Inscription : Janvier 2020
    Messages : 67
    Par défaut programme qui reste sur 1 fenetre noire (pas d'erreur)
    Bonjour à vous,

    j'ai pris 1 code de generateur de dongeons sur stack et je l'ai un peu remanié.
    j'ai 2 fichiers,
    le fichier principal (Main.py) qui doit lancer la fenetre
    le fichier qui dessine 1 dongeon (dMap.py)

    Qd, ds 1 console, je lance Main.py, qques fois, j'ai la fenetre qui apparait ms qui reste noire, pas d'affichage d'aucune sorte.
    Le programme plante j'essaye de fermer la fenetre et au bout de qques secondes, j'ai un message me disant que Main.py ne repond pas, je peux attendre ou forcer à quitter.
    Je force à quitter ms je n'ai aucun message d'erreur ds la console.

    Peut etre est-ce dû au fait que je suis (si je ne dis pas de betise) en environnement virtuel et/ou que j'utilise pygame

    Je vous laisse le code des 2 fichiers
    Main.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
     
     
    import pygame
    from dMap import dMap
     
    # Initialize Pygame
    pygame.init()
     
    # Set up the display
    width = 600
    height = 600
    screen = pygame.display.set_mode((width, height))
     
    startx = 20   # map width
    starty = 10   # map height
    themap = dMap()
    themap.makeMap(startx, starty, 110, 50, 60)
     
    # Draw the map
    for y in range(starty):
        for x in range(startx):
            if themap.mapArr[y][x] == 0:
                color = (255, 255, 255)  # floor
            elif themap.mapArr[y][x] == 1:
                color = (0, 0, 0)  # wall
            elif themap.mapArr[y][x] == 2:
                color = (128, 128, 128)  # corridor
            elif themap.mapArr[y][x] == 3 or themap.mapArr[y][x] == 4 or themap.mapArr[y][x] == 5:
                color = (255, 0, 0)  # door
            pygame.draw.rect(screen, color, (x*30, y*30, 30, 30))
     
    # Update the display
    pygame.display.update()
     
    # Keep the window open until it is closed manually
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
    dMap.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
    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
     
    # Class to produce random map layouts
     
    from random import randrange
     
     
    class dMap:
     
        def __init__(self):
            self.roomList = []
            self.cList = []
     
        def makeMap(self, xsize, ysize, fail, b1, mrooms):
            """
            Generate random layout of rooms, corridors and other features
            """
            # makeMap can be modified to accept arguments for values of failed,
            # and percentile of features.
            # Create first room
            self.size_x = xsize
            self.size_y = ysize
            # initialize map to all walls
            self.mapArr = []
            for y in range(ysize):
                tmp = []
                for x in range(xsize):
                    tmp.append(1)
                self.mapArr.append(tmp)
     
            w, l, t = self.makeRoom()
            while len(self.roomList) == 0:
                # Ligne originale
                # y = randrange(ysize - 1 - l) + 1
                y = randrange(max(1, ysize - 1 - l)) + 1
                # ligne originale
                # x = randrange(xsize - 1 - w) + 1
                x = randrange(max(1, xsize - 1 - w)) + 1
                p = self.placeRoom(l, w, x, y, xsize, ysize, 6, 0)
            failed = 0
            # The lower the value that failed < , the smaller the dungeon
            while failed < fail:
                chooseRoom = randrange(len(self.roomList))
                ex, ey, ex2, ey2, et = self.makeExit(chooseRoom)
                feature = randrange(100)
                # Begin feature choosing (more features to be added here)
                if feature < b1:
                    w, l, t = self.makeCorridor()
                else:
                    w, l, t = self.makeRoom()
                roomDone = self.placeRoom(l, w, ex2, ey2, xsize, ysize, t, et)
                # If placement failed increase possibility map is full
                if roomDone == 0:
                    failed += 1
                # Possiblilty of linking rooms
                elif roomDone == 2:
                    if self.mapArr[ey2][ex2] == 0:
                        if randrange(100) < 7:
                            self.makePortal(ex, ey)
                        failed += 1
                # Otherwise, link up the 2 rooms
                else:
                    self.makePortal(ex, ey)
                    failed = 0
                    if t < 5:
                        tc = [len(self.roomList)-1, ex2, ey2, t]
                        self.cList.append(tc)
                        self.joinCorridor(len(self.roomList)-1, ex2, ey2, t, 50)
                if len(self.roomList) == mrooms:
                    failed = fail
            self.finalJoins()
     
        def makeRoom(self):
            """
            Randomly produce room size
            """
            rtype = 5
            rwide = randrange(8) + 3
            rlong = randrange(8) + 3
            return rwide, rlong, rtype
     
        def makeCorridor(self):
            """
            Randomly produce corridor length and heading
            """
            clength = randrange(18) + 3
            heading = randrange(4)
            # north
            if heading == 0:
                wd = 1
                lg = - clength
            # east
            elif heading == 1:
                wd = clength
                lg = 1
            # south
            elif heading == 2:
                wd = 1
                lg = clength
            # west
            elif heading == 3:
                wd = - clength
                lg = 1
            return wd, lg, heading
     
        def placeRoom(self, ll, ww, xposs, yposs, xsize, ysize, rty, ext):
            """
            Place feature if enough space and return canPlace as true or false
            """
            # Determine room position and size
            xpos = max(1, xposs)
            ypos = max(1, yposs)
            ww = abs(ww)
            ll = abs(ll)
            if rty == 5:
                if ext == 0 or ext == 2:
                    offset = randrange(ww)
                    xpos -= offset
                else:
                    offset = randrange(ll)
                    ypos -= offset
            # Check if there is enough space for the room
            if ww + xpos + 1 > xsize or ll + ypos + 1 > ysize:
                return False
            # Check if the space is not already occupied
            for j in range(ll):
                for k in range(ww):
                    if self.mapArr[ypos+j][xpos+k] != 1:
                        return False
            # Add the room to the list of rooms
            temp = [ll, ww, xpos, ypos]
            self.roomList.append(temp)
            # Build walls and floor
            for j in range(ll + 2):
                for k in range(ww + 2):
                    self.mapArr[ypos-1+j][xpos-1+k] = 2
            for j in range(ll):
                for k in range(ww):
                    self.mapArr[ypos+j][xpos+k] = 0
            return True
     
        def makeExit(self, rn):
            """
            Pick random wall and random point along that wall
            """
            room = self.roomList[rn]
            while True:
                rw = randrange(4)
                # north wall
                if rw == 0:
                    rx = randrange(room[1]) + room[2]
                    ry = room[3] - 1
                    rx2 = rx
                    ry2 = ry - 1
                # esat wall
                elif rw == 1:
                    ry = randrange(room[0]) + room[3]
                    rx = room[2] + room[1]
                    rx2 = rx + 1
                    ry2 = ry
                # south wall
                elif rw == 2:
                    rx = randrange(room[1]) + room[2]
                    ry = room[3] + room[0]
                    rx2 = rx
                    ry2 = ry + 1
                # west wall
                elif rw == 3:
                    ry = randrange(room[0]) + room[3]
                    rx = room[2] - 1
                    rx2 = rx - 1
                    ry2 = ry
                # If space is a wall, exit
                if self.mapArr[ry][rx] == 2:
                    break
            return rx, ry, rx2, ry2, rw
     
        def makePortal(self, px, py):
            """
            Create doors in walls
            """
            ptype = randrange(100)
            # secret door
            if ptype > 90:
                self.mapArr[py][px] = 5
                return
            # closed door
            elif ptype > 75:
                self.mapArr[py][px] = 4
                return
            # open door
            elif ptype > 40:
                self.mapArr[py][px] = 3
                return
            # hole in the wall
            else:
                self.mapArr[py][px] = 0
     
        def joinCorridor(self, cno, xp, yp, ed, psb):
            """
            Check corridor endpoint and make an exit if it links to another room
            """
            cArea = self.roomList[cno]
            # Find the corridor endpoint
            endx = xp - (cArea[1] - 1) if xp != cArea[2] or yp != cArea[3] else xp + (cArea[1] - 1)
            endy = yp - (cArea[0] - 1) if xp != cArea[2] or yp != cArea[3] else yp + (cArea[0] - 1)
     
            checkExit = []
            # north corridor
            if ed == 0:
                checkExit.append((
                    [endx - 2, endy, endx - 1, endy],
                    [endx, endy - 2, endx, endy - 1],
                    [endx + 2, endy, endx + 1, endy]))
            # east corridor
            elif ed == 1:
                checkExit.append((
                    [endx, endy - 2, endx, endy - 1],
                    [endx + 2, endy, endx + 1, endy],
                    [endx, endy + 2, endx, endy + 1]))
            # south corridor
            elif ed == 2:
                checkExit.append((
                    [endx + 2, endy, endx + 1, endy],
                    [endx, endy + 2, endx, endy + 1],
                    [endx - 2, endy, endx - 1, endy]))
            # west corridor
            elif ed == 3:
                checkExit.append((
                    [endx - 2, endy, endx - 1, endy],
                    [endx, endy - 2, endx, endy - 1],
                    [endx, endy + 2, endx, endy + 1]))
     
            # Loop through possible exits
            for exits in checkExit:
                for coords in exits:
                    xxx, yyy, xxx1, yyy1 = coords
                    # Check if (yyy, xxx) is within the bounds of mapArr
                    if yyy >= 0 and yyy < len(self.mapArr) and xxx >= 0 and xxx < len(self.mapArr[yyy]):
                        # If joins to a room
                        if self.mapArr[yyy][xxx] == 0:
                            # Possibility of linking rooms
                            if randrange(100) < psb:
                                self.makePortal(xxx1, yyy1)
                                return
     
        def finalJoins(self):
            """
            Final stage, loops through all the corridors to see if any can be
            joined to other rooms
            """
            for x in self.cList:
                self.joinCorridor(x[0], x[1], x[2], x[3], 10)
    ce qu'indique la console
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    >>>  $ python3 Main.py 
    pygame 2.3.0 (SDL 2.24.2, Python 3.9.2)
    Hello from the pygame community. https://www.pygame.org/contribute.html
    Processus arrêté
    (05)
    1 lanterne ou une ampoule pr aider 1 amateur éclairé à la bougie?
    En vous remerciant
    grub

  2. #2
    Membre confirmé
    Homme Profil pro
    conducteur autocar
    Inscrit en
    Janvier 2020
    Messages
    67
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : conducteur autocar
    Secteur : Transports

    Informations forums :
    Inscription : Janvier 2020
    Messages : 67
    Par défaut
    j'ai verifié ceci aussi:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
    libsdl2-dev/stable,now 2.0.14+dfsg2-3+deb11u1 amd64  [installé]                                                                                       
    root@debian:~# apt list libsdl2-mixer-dev                                                                                                             
    En train de lister... Fait                                                                                                                            
    libsdl2-mixer-dev/stable,now 2.0.4+dfsg1-3 amd64  [installé]                                                                                          
    root@debian:~# apt list libsdl2-image-dev                                                                                                             
    En train de lister... Fait                                                                                                                            
    libsdl2-image-dev/stable,now 2.0.5+dfsg1-2 amd64  [installé]                                                                                          
    root@debian:~# apt list libsdl2-ttf-dev                                                                                                               
    En train de lister... Fait                                                                                                                            
    libsdl2-ttf-dev/stable,now 2.0.15+dfsg1-1 amd64  [installé]                                                                                           
    root@debian:~# apt list libsdl2-gfx-dev                                                                                                               
    En train de lister... Fait                                                                                                                            
    libsdl2-gfx-dev/stable,now 1.0.4+dfsg-3.1 amd64  [installé]
    nouveau Main.py qui ne change rien:
    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
     
     
    import pygame
    from dMap import dMap
     
     
    def main():
        # Initialize Pygame
        pygame.init()
     
        # Set up the display
        width = 600
        height = 600
        screen = pygame.display.set_mode((width, height))
     
        startx = 20   # map width
        starty = 10   # map height
        themap = dMap()
        themap.makeMap(startx, starty, 110, 50, 60)
     
        # Draw the map
        for y in range(starty):
            for x in range(startx):
                if themap.mapArr[y][x] == 0:
                    color = (200, 200, 200)  # floor
                elif themap.mapArr[y][x] == 1:
                    color = (0, 0, 0)  # wall
                elif themap.mapArr[y][x] == 2:
                    color = (128, 128, 128)  # corridor
                elif themap.mapArr[y][x] == 3 or themap.mapArr[y][x] == 4 or themap.mapArr[y][x] == 5:
                    color = (255, 0, 0)  # door
                pygame.draw.rect(screen, color, (x*30, y*30, 30, 30))
     
        # Update the display
        pygame.display.update()
     
        # Keep the window open until it is closed manually
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    quit()
     
     
    if __name__ == "__main__":
        main()

  3. #3
    Expert éminent
    Homme Profil pro
    Architecte technique retraité
    Inscrit en
    Juin 2008
    Messages
    21 741
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Manche (Basse Normandie)

    Informations professionnelles :
    Activité : Architecte technique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2008
    Messages : 21 741
    Par défaut
    Salut,

    Si on vire dMap de main, ce code devrait fonctionner.
    => le problème est dans le code que vous avez récupéré et/ou les modifications que vous y avez apporté.

    C'est là que j'arrête car ce forum n'est pas une hotline où on répare les codes qui ne marchent pas mais un endroit où vous venez dans l'espoir de devenir meilleur programmeur. Pour çà, il faut avoir une idée de votre niveau et présenter un travail que fait par vous même, histoire d'avoir une idée de ce que vous n'avez pas compris.

    Là on ne sait pas du tout où vous en êtes et à par faire, à votre place, la mise au point du code difficile de vous aider.

    - W
    Architectures post-modernes.
    Python sur DVP c'est aussi des FAQs, des cours et tutoriels

Discussions similaires

  1. [Toutes versions] Conception de graphes : programme qui compile sur Office 2016 et pas sur Office 2007
    Par Invité dans le forum Macros et VBA Excel
    Réponses: 5
    Dernier message: 25/02/2016, 13h57
  2. Réponses: 8
    Dernier message: 19/05/2008, 09h00
  3. Réponses: 0
    Dernier message: 16/05/2008, 10h22
  4. Macro qui marche sur un poste et pas sur les autres
    Par INeedHelp dans le forum Macros et VBA Excel
    Réponses: 8
    Dernier message: 15/05/2008, 08h58
  5. [Debutant] Un thread qui dessine sur une fenetre ???
    Par Spartan03 dans le forum OpenGL
    Réponses: 6
    Dernier message: 05/04/2006, 20h19

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