ajout du rendu de la carte

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.
This commit is contained in:
papush! 2019-11-16 18:09:01 +01:00
parent fe90a5a145
commit 3a9ecba96a
2 changed files with 37 additions and 28 deletions

View File

@ -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)

View File

@ -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,29 +236,6 @@ 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: