-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tools.py
116 lines (91 loc) · 3.6 KB
/
Tools.py
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
import pygame as pg
import pygame.mouse
from pygame import Vector2
from Settings import *
from main import *
from DrawTiles import *
tools = pg.sprite.Group()
class Text(pg.sprite.Sprite):
def __init__(self, pos, size):
super().__init__()
self.pos = pos
self.text_font = pg.font.SysFont(None, int(size/1.5))# noqa
self.image = self.text_font.render("1", True, (0, 0, 0))
self.rect = self.image.get_rect(center=pos)
def update(self, number):
self.rect = self.image.get_rect(center=self.pos)
self.image = self.text_font.render(str(number), True, (0, 0, 0))
class Color(pg.sprite.Sprite):
def __init__(self, pos, size, color):
super().__init__()
self.pos = pos
self.size = size
self.color = color
self.image = pg.Surface((size.x, size.y))
self.image.fill(self.color)
self.rect = self.image.get_rect(center=self.pos)
def choose_color(self, mouse_pos):
if self.rect.collidepoint(mouse_pos):
if pg.mouse.get_pressed()[0]:
return True
class Tool(pg.sprite.Sprite):
def __init__(self, pos, size, tool_type, image):
super().__init__()
self.pos = pos
self.size = size
self.image = pygame.image.load(image).convert()
self.image = pg.transform.scale(self.image, size)
self.rect = self.image.get_rect(center=self.pos)
self.tool_type = tool_type
self.can_click_to_change = True
def tool_use(self, mouse_pos, surf, tiles):
if self.rect.collidepoint(mouse_pos):
if pg.mouse.get_pressed()[0]:
match self.tool_type:
case 0:
self.change_pen("pen", tiles)
case 1:
self.change_pen("rubber", tiles)
case 2:
self.change_pen("bucket", tiles)
case 3:
self.change_pensize(True, tiles)
case 4:
self.change_pensize(False, tiles)
case 5:
self.change_pen("trash", tiles)
case 6:
self.save(surf)
@staticmethod
def change_pen(pen, tiles):
tiles.pen_type = pen
@staticmethod
def save(surf):
name = input("Name: ")
pg.image.save(surf, name + ".png")
def change_pensize(self, change, tiles):
if self.can_click_to_change:
if change:
tiles.pen_size += 1
elif tiles.pen_size > 1:
tiles.pen_size -= 1
self.can_click_to_change = False
class Setup:
def __init__(self):
self.num_of_colors = len(COLOR_PALLET)
self.num_of_tools = len(UI_IMAGES)
self.cords_of_colors = []
self.cords_of_tools = []
self.size = Vector2((WIDTH+50)/self.num_of_colors, HEIGHT/self.num_of_tools)
self.cords()
def cords(self):
for i in range(self.num_of_colors):
self.cords_of_colors.append((self.size.x * i + self.size.x/2, HEIGHT + 25))
for i in range(self.num_of_tools):
self.cords_of_tools.append(Vector2(WIDTH + 25, self.size.y * i + self.size.y/2))
for i, cords in enumerate(self.cords_of_colors):
tools.add(Color(cords, Vector2(25, 25), COLOR_PALLET[i]))# noqa
for i, cord in enumerate(self.cords_of_tools):
tools.add(Tool(cord, Vector2(35, 35), i, UI_IMAGES[i]))# noqa
if i == 3:
tools.add(Text(Vector2(cord.x, cord.y + self.size.y/2), self.size.x))# noqa