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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
| import pygame as pg
from pygame import gfxdraw
LARGEUR_ECRAN = 600
HAUTEUR_ECRAN = 400
COULEUR_ECRAN = (0, 0, 0, 255)
FPS = 30
ecran = pg.display.set_mode((LARGEUR_ECRAN, HAUTEUR_ECRAN))
groupe_affichage = pg.sprite.Group()
attributs_grille = dict(
dimension=(400, 200), # grille de 10 × 5 avec dimension de 40px
position=(25, 100),
)
image_grille = pg.Surface(attributs_grille['dimension'])
image_grille.fill(pg.Color('lightBlue'))
grille = pg.sprite.Sprite(groupe_affichage)
grille.image = image_grille
grille.rect = image_grille.get_rect()
grille.rect.topleft = attributs_grille['position']
attributs_cercle = dict(
dimension=(40, 40),
position=(20, 20),
couleur=pg.Color('red'),
)
image_cercle = pg.Surface(attributs_cercle['dimension'])
image_cercle.fill((33, 33, 33, 255))
gfxdraw.filled_circle(image_cercle, 20, 20, 15, attributs_cercle['couleur'])
cercle = pg.sprite.Sprite(groupe_affichage)
cercle.image = image_cercle
cercle.rect = image_cercle.get_rect()
cercle.rect.topleft = attributs_cercle['position']
attributs_focus = dict(dimension=(44, 44), couleur=pg.Color('yellow'))
image_focus = pg.Surface(attributs_focus['dimension']).convert_alpha()
image_focus.fill((0, 0, 0, 0))
focus = pg.sprite.Sprite()
focus.image = image_focus
focus.rect = image_focus.get_rect()
gfxdraw.rectangle(image_focus, focus.rect, attributs_focus['couleur'])
def selectionner_item(item):
global item_en_selection
item_en_selection = item
focus.add(groupe_affichage)
focus.rect.center = item.rect.center
def deselectionner_item():
global item_en_selection
item_en_selection = None
focus.remove(groupe_affichage)
def dupliquer_item(x, y, item_a_dupliquer):
dx = (x - attributs_grille['position'][0]) // 40
dy = (y - attributs_grille['position'][1]) // 40
x = attributs_grille['position'][0] + dx * 40
y = attributs_grille['position'][1] + dy * 40
if (x, y) in items_places:
return False
item = pg.sprite.Sprite(groupe_affichage)
item.image = item_a_dupliquer.image.copy()
item.rect = item.image.get_rect()
item.rect.topleft = x, y
items_places[(x, y)] = item
return True
items_places = {}
item_en_selection = None
clock = pg.time.Clock()
tournicoton = True
while tournicoton:
clock.tick(FPS)
for event in pg.event.get():
if event.type == pg.QUIT:
tournicoton = False
break
elif event.type == pg.MOUSEBUTTONDOWN:
if item_en_selection:
if focus.rect.collidepoint(event.pos):
print('désélection du cercle')
deselectionner_item()
elif grille.rect.collidepoint(event.pos):
print('clic sur la grille')
if dupliquer_item(*event.pos, item_en_selection):
print('cercle placé sur la grille')
deselectionner_item()
else:
print('position déjà occupée')
else:
if cercle.rect.collidepoint(event.pos):
print('sélection du cercle')
selectionner_item(cercle)
ecran.fill(COULEUR_ECRAN)
groupe_affichage.draw(ecran)
pg.display.update()
pg.quit() |
Partager