-
Notifications
You must be signed in to change notification settings - Fork 0
/
paint.py
82 lines (65 loc) · 2.27 KB
/
paint.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
from utils import *
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Drawing Program")
def init_grid(rows, cols, color):
grid = []
for i in range(rows):
grid.append([])
for _ in range(cols):
grid[i].append(color)
return grid
def draw_grid(win, grid):
for i, row in enumerate(grid):
for j, pixel in enumerate(row):
pygame.draw.rect(win, pixel, (j * PIXEL_SIZE, i * PIXEL_SIZE, PIXEL_SIZE, PIXEL_SIZE))
if DRAW_GRID_LINES:
for i in range(ROWS + 1):
pygame.draw.line(win, BLACK, (0, i * PIXEL_SIZE), (WIDTH, i * PIXEL_SIZE))
for i in range(COLS + 1):
pygame.draw.line(win, BLACK, (i * PIXEL_SIZE, 0), (i * PIXEL_SIZE, HEIGHT - TOOLBAR_HEIGHT))
def draw(win, grid, buttons):
win.fill(BG_COLOR)
draw_grid(win, grid)
for button in buttons:
button.draw(win)
pygame.display.update()
def get_row_col_from_pos(pos):
x, y = pos
row = y // PIXEL_SIZE
col = x // PIXEL_SIZE
if row >= ROWS:
raise IndexError
return row, col
run = True
clock = pygame.time.Clock()
grid = init_grid(ROWS, COLS, BG_COLOR)
drawing_color = BLACK
button_y = HEIGHT - TOOLBAR_HEIGHT/2 - 25
buttons = [
Button(10, button_y, 50, 50, BLACK),
Button(70, button_y, 50, 50, RED),
Button(130, button_y, 50, 50, GREEN),
Button(190, button_y, 50, 50, BLUE),
Button(250, button_y, 50, 50, WHITE, "Erase", BLACK),
Button(310, button_y, 50, 50, WHITE, "Clear", BLACK)
]
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if pygame.mouse.get_pressed()[0]:
pos = pygame.mouse.get_pos()
try:
row, col = get_row_col_from_pos(pos)
grid[row][col] = drawing_color
except IndexError:
for button in buttons:
if not button.clicked(pos):
continue
drawing_color = button.color
if button.text == "Clear":
grid = init_grid(ROWS, COLS, BG_COLOR)
drawing_color = BLACK
draw(WIN, grid, buttons)
pygame.quit()