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
|
import os
import pygame
from display import Scaling, INTERNAL_WIDTH, INTERNAL_HEIGHT
DEBUG = os.getenv("DEBUG")
class FieldSprite(pygame.sprite.Sprite):
def __init__(self, area: tuple ):
super().__init__()
self.area = area
self.update()
def update(self):
self.rect = Scaling.area_to_rect(self.area)
self.image = pygame.Surface(self.rect.size)
self.image.fill("green")
class Field():
initial_area = (400_000, 280_000)
def __init__(self):
self.sprites = pygame.sprite.Group()
self.sprites.add(FieldSprite( (0, 0, *__class__.initial_area) ))
self.area_full = __class__.initial_area[0] * __class__.initial_area[1]
self.area_current = self.area_full
self.percentage = 100
def coordinates_inside_area(self, area, x, y) -> bool:
""" Test if coordinates are inside area """
if x < area[0]:
return False
if y < area[1]:
return False
if x >= area[0]+area[2]:
return False
if y >= area[1]+area[3]:
return False
return True
def update_area(self):
""" calculates remaining area and remaining percentage """
self.area_current = sum( s.area[2]*s.area[3] for s in self.sprites )
self.percentage = 100 * self.area_current // self.area_full
if DEBUG:
print(
f"FIELD: {self.area_full}/{self.area_current}, "
f"{self.percentage}")
def slice(
self,
pos: tuple,
direction: bool,
thickness: int) -> pygame.Rect:
""" Slice one area into two areas """
# Find the overlapping area
for field in self.sprites:
if self.coordinates_inside_area(field.area, pos[0], pos[1]):
break
else:
return None
# remove current area
ax, ay, aw, ah = field.area
field.remove(self.sprites)
# create new areas if there is any space
if direction:
x1 = ax
x2 = pos[0] - thickness
x3 = pos[0] + thickness
x4 = ax + aw
if x2 > x1:
self.sprites.add(FieldSprite( (x1, ay, x2-x1, ah) ))
if x4 > x3:
self.sprites.add(FieldSprite( (x3, ay, x4-x3, ah) ))
self.update_area()
return Scaling.area_to_rect((x2, ay, x3-x2, ah))
y1 = ay
y2 = pos[1] - thickness
y3 = pos[1] + thickness
y4 = ay + ah
if y2 > y1:
self.sprites.add(FieldSprite( (ax, y1, aw, y2-y1) ))
if y4 > y3:
self.sprites.add(FieldSprite( (ax, y3, aw, y4-y3) ))
self.update_area()
return Scaling.area_to_rect((ax, y2, aw, y3-y2))
def update(self, dt):
""" Update sprites basis of dt. dt = milliseconds from last update """
pass
|