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 :

Scrolling avec pygame


Sujet :

Programmation multimédia/Jeux Python

  1. #1
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2015
    Messages
    6
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Côte d'Or (Bourgogne)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2015
    Messages : 6
    Points : 7
    Points
    7
    Par défaut Scrolling avec pygame
    salut, j'aimerais crée un petit jeu de plateforme en utilisant le tiles mapping que j'ai plus trouver en C.

    j'ai donc adapté le code pour l'utiliser avec pygame mais cela fonctionne a moitié... La fenêtre s'ouvre, les tiles s'affichent correctement mais quand j'utilise les flèches pour bouger, les tiles tout en bas et tout a gauche font vraiment n’importe quoi (voir les images a la suite) . c'est assez compliqué d'expliquer donc je met les fichiers sur mega pour que vous puissiez tester vous même. source ici

    les images pour montrer le problème (j'ai mis les trait noir avec paint pour montrer ou se situe le problème) :
    Nom : debut.PNG
Affichages : 1156
Taille : 13,1 KoNom : nimp.PNG
Affichages : 889
Taille : 15,8 Ko

    J'ai déjà longuement cherché une réponse mais je n'ai absolument rien trouvé.

    Si vous avez la flemme de telecharger la source je met le code source et l'image dans ce post.

    L'image pour les tiles : Nom : tiles.png
Affichages : 804
Taille : 8,5 Ko

    le code du fichier level.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
    import pygame
    class Level():
        def __init__(self) :
            self.move_speed = 5
            self.tile_width = 24
            self.tile_height = 16
            self.nb_tilesx_screen = 15
            self.nb_tilesy_screen = 13
            self.nb_tilesx_world = 75
            self.nb_tilesy_world = 17
            self.screen_width = self.tile_width*self.nb_tilesx_screen
            self.screen_height = self.tile_height*self.nb_tilesy_screen
            self.xscroll = 0
            self.yscroll = (self.nb_tilesy_world-self.nb_tilesy_screen)*self.tile_height
            self.level = []
            self.tiles = pygame.image.load('pictures\\tiles.png')
            self.tileset = {'0' : [self.tiles.subsurface((0,0,24,16)),'empty','sky'],
                            '1' : [self.tiles.subsurface((24,0,24,16)),'full','treat'],
                            '2' : [self.tiles.subsurface((48,0,24,16)),'full','brick'],
                            '3' : [self.tiles.subsurface((72,0,24,16)),'full','tubetop1'],
                            '4' : [self.tiles.subsurface((96,0,24,16)),'full','tubetop2'],
                            '5' : [self.tiles.subsurface((120,0,24,16)),'full','tubebody1'],
                            '6' : [self.tiles.subsurface((144,0,24,16)),'full','tubebody2'],
                            '7' : [self.tiles.subsurface((168,0,24,16)),'full','block']}
     
     
        def load_map(self,level):
            with open(level,'r') as level :
                m = []
                item = ''
                for line in level :
                    line_level = []
                    for col in line :
                        if col != '\n' and col != ' ' :
                            item += col
                        else :
                            line_level.append(item)
                            item = ''
                    m.append(line_level)
                self.level = m
     
     
     
        def display_map(self,screen):
            xmin = (self.xscroll/self.tile_width)-1
            xmax = (self.xscroll+self.screen_width)/self.tile_width
            ymin = (self.yscroll/self.tile_height)-1
            ymax = (self.yscroll+self.screen_height)/self.tile_height
     
            for i in range(ymin,ymax) :
                for j in range(xmin,xmax) :
                    x = j*self.tile_width-self.xscroll
                    y = i*self.tile_height-self.yscroll
                    item = self.level[i][j]
                    screen.blit(self.tileset[item][0],(x,y))
     
     
            pygame.display.flip()
     
     
        def screen_size(self):
            return (self.tile_width*self.nb_tilesx_screen,self.tile_height*self.nb_tilesy_screen)
     
        def move(self,direction):
            xscroll = self.xscroll
            yscroll = self.yscroll
            if direction == 'right' :
                xscroll += self.move_speed
                if xscroll > self.nb_tilesx_world*self.tile_width-self.screen_width-1 :
                    self.xscroll = self.nb_tilesx_world*self.tile_width-self.screen_width-1
                else :
                    self.xscroll = xscroll
     
            elif direction == 'left' :
                xscroll -= self.move_speed
                if xscroll < 0:
                    self.xscroll = 0
                else :
                    self.xscroll = xscroll
     
            elif direction == 'up' :
                yscroll -= self.move_speed
                if yscroll < 0 :
                    self.yscroll = 0
                else :
                    self.yscroll = yscroll
     
            elif direction == 'down' :
                yscroll += self.move_speed
                if yscroll > self.nb_tilesy_world*self.tile_height-self.screen_height-1 :
                    self.yscroll = self.nb_tilesy_world*self.tile_height-self.screen_height-1
                else :
                    self.yscroll = yscroll
    Le code principal :

    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
    import pygame
    from level import *
     
    pygame.init()
    horloge = pygame.time.Clock()
    level = Level()
    level.load_map('level.txt')
    screen = pygame.display.set_mode(level.screen_size())
    main = 1
    direction = ''
    while main :
        horloge.tick(30)
        for event in pygame.event.get() :
            if event.type == pygame.QUIT :
                main = 0
            if event.type == pygame.KEYDOWN :
                if event.key == pygame.K_RIGHT :
                    direction = 'right'
                elif event.key == pygame.K_LEFT :
                    direction = 'left'
                elif event.key == pygame.K_UP :
                    direction = 'up'
                elif event.key == pygame.K_DOWN :
                    direction = 'down'
     
            if event.type == pygame.KEYUP :
                if event.key == pygame.K_RIGHT and direction == 'right' :
                    direction = ''
                elif event.key == pygame.K_LEFT and direction == 'left' :
                    direction == ''
                elif event.key == pygame.K_UP and direction == 'up' :
                    direction == ''
                elif event.key == pygame.K_DOWN and direction == 'down' :
                    direction == ''
     
        level.move(direction)
        level.display_map(screen)
    pygame.quit()
    et enfin le fichier texte qui contient la structure du niveau (a coller dans un fichier nommé level.txt) :

    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 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 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 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 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 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 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 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 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 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 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 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 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 0 0 0 0 0 0 0 0
    0 0 0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 3 4 0 0 0 2 2 2 2 0 0 2 2
    0 0 5 6 0 0 0 0 0 0 0 0 0 0 0 0 0 5 6 0 0 0 0 0 0 0 0 0 0 0 0 0 5 6 0 0 0 0 0 0 0 0 0 0 0 0 0 5 6 0 0 0 0 0 0 0 0 0 0 0 0 0 5 6 0 0 0 0 0 0 0 0 0 0 0
    0 0 5 6 0 0 0 0 0 0 0 0 0 0 0 0 0 5 6 0 0 0 0 0 0 0 0 0 0 0 0 0 5 6 0 0 0 0 0 0 0 0 0 0 0 0 0 5 6 0 0 0 0 0 0 0 0 0 0 0 0 0 5 6 0 0 0 0 0 0 0 0 0 0 0
    0 0 5 6 0 0 0 0 0 0 0 0 0 0 0 0 0 5 6 0 0 0 0 0 0 0 0 0 0 0 0 0 5 6 0 0 0 0 0 0 0 0 0 0 0 0 0 5 6 0 0 0 0 0 0 0 0 0 0 0 0 0 5 6 0 0 0 0 0 0 0 0 0 0 0
    7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7

    Merci de m'apporter votre aide !

  2. #2
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2015
    Messages
    6
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Côte d'Or (Bourgogne)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2015
    Messages : 6
    Points : 7
    Points
    7
    Par défaut
    Petit up apres actualisation du post

  3. #3
    Responsable 2D/3D/Jeux


    Avatar de LittleWhite
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Mai 2008
    Messages
    26 860
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Mai 2008
    Messages : 26 860
    Points : 219 064
    Points
    219 064
    Billets dans le blog
    120
    Par défaut
    Bonjour,

    Pour les chemins, n'utilisez pas '\\' mais '/'. (Sinon, ça marche pas sous Linux)
    Aussi, ligne 32 du fichier level.py :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    if col != '\n' and col != ' ' and col != '\r':
    Vous aviez oublié le '\r'. Mais bon, faites en sorte de stripper la ligne, à la place.

    Aussi, comme j'ai eu plusieurs souci, j'ai fait ceci :
    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
        def display_map(self,screen):
            xmin = (self.xscroll/self.tile_width)-1
            xmax = (self.xscroll+self.screen_width)/self.tile_width
            ymin = (self.yscroll/self.tile_height)-1
            ymax = (self.yscroll+self.screen_height)/self.tile_height
            """if xmin < 0 :
                xmin = 0
            if xmax > (self.xscroll+self.screen_width)/self.tile_width :
                xmax = ((self.xscroll+self.screen_width)/self.tile_width)
            if ymin < 0 :
                ymin = 0
            if ymax > (self.yscroll+self.screen_height)/self.tile_height :
                ymax = ((self.yscroll+self.screen_height)/self.tile_height)"""
            for i in range(ymin,ymax) :
                for j in range(xmin,xmax) :
                    x = j*self.tile_width-self.xscroll
                    y = i*self.tile_height-self.yscroll
                    item = self.level[i][j]
                    if item >= '0' and item <= '9':
                            screen.blit(self.tileset[item][0],(x,y))
     
     
            pygame.display.flip()
    Car trop de données "mauvaises" dans le tableau du niveau.

    Sinon, j'ai réussi à lancer et ça marche correctement (pas les mêmes bogues que ce que vous avez en capture). Peut être grâce à mes modifications ?

    Votre code manque de commentaires (qui explique votre logique/réflexion/comment vous l'implémentez) et non qui traduise le code en français).
    Vous souhaitez participer à la rubrique 2D/3D/Jeux ? Contactez-moi

    Ma page sur DVP
    Mon Portfolio

    Qui connaît l'erreur, connaît la solution.

  4. #4
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2015
    Messages
    6
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Côte d'Or (Bourgogne)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2015
    Messages : 6
    Points : 7
    Points
    7
    Par défaut
    bonjour, merci de votre réponse !
    Je testerais ces modifications des que possible !

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Scroll avec mollette d'une zone de texte
    Par newbiemac dans le forum Général JavaScript
    Réponses: 2
    Dernier message: 09/05/2007, 21h31
  2. [FLASH 8] Probleme de scrolling avec clip
    Par Mr-Hunter dans le forum Flash
    Réponses: 1
    Dernier message: 28/11/2005, 22h04
  3. [FLASH 8] Scroll avec juste 2 fleches
    Par Gemelos dans le forum Flash
    Réponses: 10
    Dernier message: 24/11/2005, 14h36
  4. [javascript] Scroll avec une image
    Par Salih-du-91 dans le forum Général JavaScript
    Réponses: 4
    Dernier message: 05/10/2005, 07h46

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