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
| import pygame as pg
pg.init()
fenetre = pg.display.set_mode((400, 300))
maison_image = pg.image.load('./maison.png').convert_alpha()
def focus(img, couleur=(255, 255, 0, 192)) :
surf = pg.Surface(
(img.get_width(), img.get_height()),
pg.SRCALPHA,
32
).convert_alpha()
mask = pg.mask.from_surface(img)
for x, y in mask.outline() :
surf.fill(couleur, (x-2, y-2, 5, 5))
return surf
def colorier(img, couleur, alpha=128) :
img = img.copy()
img.fill((couleur) + (alpha,), None, pg.BLEND_RGBA_MULT)
return img
class Maison(pg.sprite.Sprite) :
def __init__(self) :
super().__init__()
self.image = maison_image
self.rect = self.image.get_rect()
class MaisonFocus(pg.sprite.Sprite) :
def __init__(self) :
super().__init__()
self.image = focus(maison_image)
self.rect = self.image.get_rect()
class MaisonSurbrillance(pg.sprite.Sprite) :
def __init__(self) :
super().__init__()
self.image = colorier(maison_image, (128, 128, 0))
self.rect = self.image.get_rect()
maison = Maison()
maison.rect.center = (200, 150)
maisonFocus = MaisonFocus()
maisonFocus.rect.center = maison.rect.center
maisonSurbrillance = MaisonSurbrillance()
maisonSurbrillance.rect.center = maison.rect.center
g = pg.sprite.Group()
g.add(maison)
clock = pg.time.Clock()
tournicoti = True
while tournicoti :
for event in pg.event.get() :
if event.type == pg.QUIT :
tournicoti = False
break
elif event.type == pg.KEYDOWN :
if event.key == pg.K_f :
if g.has(maisonFocus) :
g.remove(maisonFocus)
else :
g.add(maisonFocus)
elif event.key == pg.K_c :
if g.has(maisonSurbrillance) :
g.remove(maisonSurbrillance)
else :
g.add(maisonSurbrillance)
fenetre.fill((32, 170, 178))
g.draw(fenetre)
pg.display.update()
clock.tick(10)
pg.quit() |
Partager