-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisplay_player.py
More file actions
40 lines (37 loc) · 1.6 KB
/
Copy pathdisplay_player.py
File metadata and controls
40 lines (37 loc) · 1.6 KB
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
# ATTENTION utilisation de PILLOW pour regler ERREUR WINDOWS BMP FILE
# sur mon odi pygame ne supportais pas les PNG
# d'ou l'installation / utilisation de la bibliothèque PIL (PILLOW)
import pygame
from PIL import Image
chemin_map = "../ex04/map2.txt"
taille_bloc = 80
pygame.init()
screen = pygame.display.set_mode((400, 400))
try:
pil_img = Image.open("../ex05/main.png").convert("RGBA") #RGBA = nomenclature PIL (red, green, blue, transparent)
image = pygame.image.fromstring(pil_img.tobytes(), pil_img.size, "RGBA")
#pil_img.tobytes() = convertit l'image PIL en une chaîne d'octets
#pil_img.size = récupère les dimensions de l'image pour afficher les pixels correctement
image = image.convert_alpha()
image = pygame.transform.scale(image, (taille_bloc, taille_bloc))
with open(chemin_map, "r") as f:
ma_map = [l.strip() for l in f]
except FileNotFoundError:
print("ERREUR")
exit()
running = True
while running:
screen.fill((255, 255, 255))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
for y, ligne in enumerate(ma_map):
for x, case in enumerate(ligne):
if case == "1":
pygame.draw.rect(screen, (125, 125, 125), (x * taille_bloc, y * taille_bloc, taille_bloc, taille_bloc))
if case == "0" or case == "P":
pygame.draw.rect(screen, (200, 200, 200), (x * taille_bloc, y * taille_bloc, taille_bloc, taille_bloc))
if case == "P":
screen.blit(image, (x * taille_bloc, y * taille_bloc))
pygame.display.flip()
pygame.quit()