From 3a9ecba96aa66943755cd2072f2fb7447e6c4a6f Mon Sep 17 00:00:00 2001 From: papush! Date: Sat, 16 Nov 2019 18:09:01 +0100 Subject: [PATCH] ajout du rendu de la carte MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Je tiens à ce qu’il soit noté que le code de ce projet est le plus sale qu’il m’ait été donné de voir depuis un bon moment. --- src/graphic.py | 7 +++--- src/pacmap.py | 58 ++++++++++++++++++++++++++++---------------------- 2 files changed, 37 insertions(+), 28 deletions(-) diff --git a/src/graphic.py b/src/graphic.py index 2aa147b..e8b092b 100755 --- a/src/graphic.py +++ b/src/graphic.py @@ -39,8 +39,9 @@ class Screen: """refresh/redraw all""" pac_x, pac_y = self.pacman.position pac_res = self.pacman.resolution - self.pacman_sprite.rect.x = int(pac_x / 28 / pac_res * self.screen.get_width()) - self.pacman_sprite.rect.y = int(pac_y / 31 / pac_res * self.screen.get_height()) + pacmap.draw(self.screen) + self.pacman_sprite.rect.x = int(pac_x / 28 / pac_res * self.screen.get_width()) - 10 + self.pacman_sprite.rect.y = int(pac_y / 31 / pac_res * self.screen.get_height()) - 10 # print(self.pacman_sprite.rect.x, self.pacman_sprite.rect.y, self.pacman_sprite.rect.size) self.entity_group.draw(self.screen) @@ -68,4 +69,4 @@ if __name__ == '__main__': pacman = m_pacman.Pacman((1,1)) pacmap = m_pacmap.Map(maze_img_file="../pacmap_maze1.png") phys_motor = PhysicMotor(pacmap, pacman) - screen = Screen((560, 620), pacmap, phys_motor, pacman) \ No newline at end of file + screen = Screen((560, 620), pacmap, phys_motor, pacman) diff --git a/src/pacmap.py b/src/pacmap.py index 2eff264..a5869bd 100755 --- a/src/pacmap.py +++ b/src/pacmap.py @@ -2,6 +2,7 @@ from enum import IntEnum from copy import deepcopy from PIL import Image +import pygame import os class DotTile(IntEnum): @@ -54,10 +55,11 @@ class Map: 1: small pac-dot 2: big pac-dot """ + self._surf = pygame.Surface((Map.width * 20, Map.height * 20)) if maze_img_file and os.path.isfile(maze_img_file): print('coucou') try: - self.phys_map = decode_map(maze_img_file) + self.phys_map = self.decode_map(maze_img_file) except Exception as e: raise e else: @@ -182,6 +184,35 @@ class Map: cpt |= dictionnary[dir] self.intersect_map[-1].append(cpt) + def draw(self, surf): + surf.blit(self._surf, (0, 0)) + + def decode_map(self, img_file): + img = Image.open(img_file) + dictionnary = { + (0 , 0, 0): PhysTile.GRD, + (255, 0, 255): PhysTile.GCF, + (0 , 0, 255): PhysTile.WAL, + (0 , 255, 0): PhysTile.GSD, + (0 , 255, 255): PhysTile.GWL, + (255, 0, 0): PhysTile.TPT, + (255, 255, 0): PhysTile.FIT, + } + data = list(img.getdata()) + matrix = [] + for row in range(img.height): + matrix.append([]) + for col in range(img.width): + try: + color = data[col + row*img.width][:3] + tile = dictionnary[color] # avoid alpha component + self._surf.fill(color, pygame.Rect(col*20, row*20, 20, 20)) + except: + raise ValueError("Pixel " + str(col) + "," + str(row) + " is invalid") + matrix[-1].append(tile) + return matrix + + def explore(matrix, x=-1, y=-1): """explore the given matrix and change it (GRD and TPT become FIT)""" @@ -205,32 +236,9 @@ def connex(matrix, x=-1, y=-1): return False return True -def decode_map(img_file): - img = Image.open(img_file) - dictionnary = { - (0 , 0, 0): PhysTile.GRD, - (255, 0, 255): PhysTile.GCF, - (0 , 0, 255): PhysTile.WAL, - (0 , 255, 0): PhysTile.GSD, - (0 , 255, 255): PhysTile.GWL, - (255, 0, 0): PhysTile.TPT, - (255, 255, 0): PhysTile.FIT, - } - data = list(img.getdata()) - matrix = [] - for row in range(img.height): - matrix.append([]) - for col in range(img.width): - try: - tile = dictionnary[data[col + row*img.width][:3]] # avoid alpha component - except: - raise ValueError("Pixel " + str(col) + "," + str(row) + " is invalid") - matrix[-1].append(tile) - return matrix - def get_one_in_digit(number): cpt = 0 while number: cpt += number & 1 number //= 2 - return cpt \ No newline at end of file + return cpt