-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
172 lines (156 loc) · 5.52 KB
/
run.py
File metadata and controls
172 lines (156 loc) · 5.52 KB
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
import pygame
from pygame.locals import *
import sys
from elements import *
import tkinter
from tkinter import Tk, messagebox
#init modules
pygame.init()
pygame.display.set_caption("Python-Paint")
tkroot = Tk()
#colors
colors = {
"white": (255, 255, 255),
"black": (0,0,0),
"red": (255, 0, 0),
"blue": (0, 255, 0),
"green": (0, 0, 255),
"pink": (255, 153, 238),
"purple": (187, 51, 255),
"teal": (128, 255, 255),
"yellow": (255, 255, 0)
}
pallates = [
ColorPallate(400, 600, colors["black"]),
ColorPallate(450, 600, colors["red"]),
ColorPallate(500, 600, colors["blue"]),
ColorPallate(550, 600, colors["green"]),
ColorPallate(400, 650, colors["pink"]),
ColorPallate(450, 650, colors["purple"]),
ColorPallate(500, 650, colors["teal"]),
ColorPallate(550, 650, colors["yellow"])
]
#initalize screen
size = (1000, 700)
cellsize = (int(size[0]/5), int(550/5))
screen = pygame.display.set_mode(size)
screen.fill(colors["white"])
#initalize fonts/UI text
UIfont = pygame.font.SysFont("arial", 30)
SizeText = UIfont.render("Brush Size:", False, (0, 0, 0))
ColorText = UIfont.render("Colors:", False, (0, 0, 0))
SaveText = UIfont.render("Save/load:", False, (0, 0, 0))
#init UI elements
border = Border(0, 550)
size1button = Button("1", UIfont, 120, 600, resize)
size2button = Button("2", UIfont, 160, 600, resize)
size3button = Button("3", UIfont, 200, 600, resize)
clearbutton = Button("Clear", UIfont, 140, 650, clear_grid)
savebutton = Button("Save", UIfont, 735, 615, save_grid)
loadbutton = Button("Load", UIfont, 835, 615, load_grid)
#init cell grid & Brush
brush = Brush()
grid = []
xptr = 0
yptr = 0
for i in range(cellsize[1]):
row = []
for j in range(cellsize[0]):
row.append(Cell(xptr, yptr))
xptr += 5
yptr += 5
xptr = 0
grid.append(row)
#draw UI elements
border.Draw(screen)
for pallate in pallates:
pallate.Draw(screen)
size1button.Draw(screen)
size2button.Draw(screen)
size3button.Draw(screen)
clearbutton.Draw(screen)
savebutton.Draw(screen)
loadbutton.Draw(screen)
#draw text
screen.blit(SizeText, (100, 560))
screen.blit(ColorText, (450, 560))
screen.blit(SaveText, (750, 560))
#draw cell grid
for row in grid:
for cell in row:
cell.Draw(screen)
pygame.display.update()
update_rect = Rect((0, 0), (1000, 550))
run = True
while run:
brush.position = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
#left click actions
if pygame.mouse.get_pressed()[0]:
#click on canvas
if brush.position[1] < 550:
#selecting target cell
for i in range(len(grid)):
for j in range(len(grid[i])):
if grid[i][j].Click(brush.position):
#painting
circle = Draw_Circle([i, j], brush.size)
for position in circle:
try:
grid[position[0]][position[1]].image.fill(brush.color)
grid[position[0]][position[1]].color = brush.color
except:
pass
#click on UI elements
else:
#brush size buttons
if size1button.Click(brush.position):
size1button.onclick(brush, 1)
if size2button.Click(brush.position):
size2button.onclick(brush, 2)
if size3button.Click(brush.position):
size3button.onclick(brush, 4)
if clearbutton.Click(brush.position):
clearbutton.onclick(grid)
#color pallate buttons
for pallate in pallates:
if pallate.Click(brush.position):
pallate.OnClick(brush)
#save/load buttons
if savebutton.Click(brush.position):
savebutton.onclick(grid, tkroot)
if loadbutton.Click(brush.position):
colorgrid = loadbutton.onclick(grid, tkroot)
if colorgrid:
for i in range(len(grid)):
for j in range(len(grid[i])):
grid[i][j].image.fill(colorgrid[i][j])
grid[i][j].color = colorgrid[i][j]
else:
tkroot.withdraw()
messagebox.showerror("Error", "Invalid Save File")
#eraser control
if pygame.mouse.get_pressed()[2]:
if brush.position[1] < 550:
#selecting target cell
for i in range(len(grid)):
for j in range(len(grid[i])):
if grid[i][j].Click(brush.position):
#painting
circle = Draw_Circle([i, j], brush.size)
for position in circle:
try:
grid[position[0]][position[1]].image.fill((255,255,255))
grid[position[0]][position[1]].color = (255,255,255)
except:
pass
#draw cell grid
for row in grid:
for cell in row:
cell.Draw(screen)
#update display
pygame.display.update(update_rect)
pygame.quit()
sys.exit()