-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaint.py
More file actions
138 lines (112 loc) · 5.29 KB
/
paint.py
File metadata and controls
138 lines (112 loc) · 5.29 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
import pygame
import pygame.gfxdraw
import copy
from collections import deque
import shape
import button
pygame.init()
screen = pygame.display.set_mode([1280, 720])
stroke = {
'points' : [],
'brush_size' : 3,
'brush_color' : (0,0,0)
}
strokes = [copy.deepcopy(stroke)]
undo_stack = deque()
# credit to Davis Yoshida (Stack Overflow)
def map_range(value, start1, stop1, start2, stop2):
return (value - start1) / (stop1 - start1) * (stop2 - start2) + start2
def draw_line_with_circles(point1, point2, radius, color):
dist = point1.distance_to(point2)
for i in range(0, int(dist)):
pos = pygame.Vector2(map_range(i, 0, dist, point1.x, point2.x), map_range(i, 0, dist, point1.y, point2.y))
pygame.gfxdraw.aacircle(screen, int(pos.x), int(pos.y), radius, color)
pygame.gfxdraw.filled_circle(screen, int(pos.x), int(pos.y), radius, color)
def display_strokes(strokes):
for curr_stroke in strokes:
for point1, point2 in zip(curr_stroke['points'], curr_stroke['points'][1:]):
draw_line_with_circles(point1, point2, curr_stroke['brush_size'], curr_stroke['brush_color'])
# Buttons
start = 10
increment = 35
plus_button = button.Button(pygame.Vector2(start, 10), 3, shape.Plus(), (0,0,0), pygame.Vector2(30,30))
minus_button = button.Button(pygame.Vector2(start + increment, 10), 3, shape.Minus(), (0,0,0), pygame.Vector2(30, 30))
black_button = button.Button(pygame.Vector2(start + increment * 2, 10), 15, shape.Circle(), (0,0,0), pygame.Vector2(30, 30))
red_button = button.Button(pygame.Vector2(start + increment * 3, 10), 15, shape.Circle(), (255,0,0), pygame.Vector2(30, 30))
green_button = button.Button(pygame.Vector2(start + increment * 4, 10), 15, shape.Circle(), (0,255,0), pygame.Vector2(30, 30))
blue_button = button.Button(pygame.Vector2(start + increment * 5, 10), 15, shape.Circle(), (0,0,255), pygame.Vector2(30, 30))
white_button = button.Button(pygame.Vector2(start + increment * 6, 10), 15, shape.Circle(), (255,255,255), pygame.Vector2(30, 30))
undo_button = button.Button(pygame.Vector2(pygame.display.get_surface().get_width() - 85, 10), 20, shape.Arrow_Backward(), (0,0,0), pygame.Vector2(30,30))
redo_button = button.Button(pygame.Vector2(pygame.display.get_surface().get_width() - 50, 10), 20, shape.Arrow_Forward(), (0,0,0), pygame.Vector2(30,30))
#setup
background_color = (255, 255, 255)
button_shelf_color = (200, 200, 200)
screen.fill((255, 255, 255))
#loop
running = True
while running:
# event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
if plus_button.mouse_over():
stroke['brush_size'] += 1
if minus_button.mouse_over():
stroke['brush_size'] -= 1
if black_button.mouse_over():
stroke['brush_color'] = (0,0,0)
if red_button.mouse_over():
stroke['brush_color'] = (255,0,0)
if green_button.mouse_over():
stroke['brush_color'] = (0,255,0)
if blue_button.mouse_over():
stroke['brush_color'] = (0,0,255)
if white_button.mouse_over():
stroke['brush_color'] = (255,255,255)
if undo_button.mouse_over():
if len(strokes) >= 2:
undo_stack.append(strokes[-2])
del strokes[-2]
screen.fill(background_color)
display_strokes(strokes)
if redo_button.mouse_over():
if(len(undo_stack) >= 1):
strokes.insert(-2, undo_stack.pop())
screen.fill(background_color)
display_strokes(strokes)
# easy to use variables
width = pygame.display.get_surface().get_width()
height = pygame.display.get_surface().get_height()
mouse_pos = pygame.Vector2(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1])
# background - turned off to allow for dynamic rendering, improving performance
# adding positions to brush if mouse held down
mousePos = pygame.Vector2(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1])
if pygame.mouse.get_pressed()[0] and mouse_pos.y > 50:
stroke['points'].append(mouse_pos)
strokes[-1] = copy.deepcopy(stroke)
undo_stack.clear()
elif len(strokes[-1]['points']) >= 1:
strokes.append(copy.deepcopy(stroke))
strokes[-1]['points'] = []
stroke['points'] = []
if(len(stroke['points']) >= 2):
draw_line_with_circles(stroke['points'][-2], stroke['points'][-1], stroke['brush_size'], stroke['brush_color'])
#display button tray
pygame.draw.rect(screen, button_shelf_color, pygame.Rect(0, 0, width, 50))
plus_button.display(screen)
minus_button.display(screen)
black_button.display(screen)
red_button.display(screen)
green_button.display(screen)
green_button.display(screen)
blue_button.display(screen)
white_button.display(screen)
undo_button.display(screen)
redo_button.display(screen)
#display mouse brush
if mouse_pos.y < 50 - stroke['brush_size']:
pygame.draw.circle(screen, stroke['brush_color'], mousePos, stroke['brush_size'])
# Flip the display
pygame.display.flip()
pygame.quit()