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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
| import pygame, sys, random
from pygame.locals import *
import pygame as pg
from pygame import gfxdraw
pygame.init()
fen=pygame.display.set_mode((1280,720))
pygame.display.set_caption("Casse MATH")
FPS = 30
BLANC=(255,255,255)
NOIR=(0,0,0)
COULEUR_ECRAN = (0, 0, 0, 255)
JAUNE=(255,255,0)
pygame.init()
pygame.key.set_repeat(400,10)
plan=[
#----0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0],
[0,1,1,1,1,1,1,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0],
[0,1,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0],
[1,1,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1,1,1,1,1,1],
[0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
]
chemin=pygame.image.load("path.jpg")
terre=pygame.image.load("Grass.jpg")
groupe_affichage = pg.sprite.Group()
attributs_tour = dict(
dimension=(80, 80),
position=(100, 600),
couleur=pg.Color('red'),
)
attributs_tour2 = dict(
dimension=(80, 80),
position=(500, 600),
couleur=pg.Color('orange'),
)
attributs_tour3 = dict(
dimension=(80, 80),
position=(900, 600),
couleur=pg.Color('green'),
)
image_cercle = pg.Surface(attributs_tour['dimension'])
image_cercle.fill((70, 70, 70, 255))
gfxdraw.filled_circle(image_cercle, 40, 40, 35, attributs_tour['couleur'])
cercle = pg.sprite.Sprite(groupe_affichage)
cercle.image = image_cercle
cercle.rect = image_cercle.get_rect()
cercle.rect.topleft = attributs_tour['position']
image_cercle2 = pg.Surface(attributs_tour2['dimension'])
image_cercle2.fill((70, 70, 70, 255))
gfxdraw.filled_circle(image_cercle2, 40, 40, 35, attributs_tour2['couleur'])
cercle = pg.sprite.Sprite(groupe_affichage)
cercle.image = image_cercle2
cercle.rect = image_cercle2.get_rect()
cercle.rect.topleft = attributs_tour2['position']
image_cercle3 = pg.Surface(attributs_tour3['dimension'])
image_cercle3.fill((70, 70, 70, 255))
gfxdraw.filled_circle(image_cercle2, 40, 40, 35, attributs_tour3['couleur'])
cercle = pg.sprite.Sprite(groupe_affichage)
cercle.image = image_cercle3
cercle.rect = image_cercle3.get_rect()
cercle.rect.topleft = attributs_tour3['position']
attributs_focus = dict(dimension=(84, 84), 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 dessineChemin(fen,x,y):
return(fen.blit(chemin,(x,y)))
rectChem=[]
for x in range(24):
for y in range(8):
if plan[y][x]==1:
rectChem.append(fen.blit(chemin,(53*x,67*y)))
rectterre=[]
for x in range(24):
for y in range(8):
if plan[y][x]==0:
rectterre.append(fen.blit(terre,(53*x,67*y)))
def dessineFond(fen,rectChem):
for br in rectChem:
fen.blit(chemin,br)
def dessineTerre(fen,rectterre):
for br in rectterre:
fen.blit(terre,br)
fen.fill(NOIR)
dessineFond(fen,rectChem)
dessineTerre(fen,rectterre)
pygame.display.update()
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]) // 80
dy = (y - attributs_grille['position'][1]) // 80
x = attributs_grille['position'][0] + dx * 80
y = attributs_grille['position'][1] + dy * 80
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')
x, y = event.pos
if dupliquer_item(x, y, 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)
groupe_affichage.draw(fen)
pg.display.update()
pg.quit() |