-
Notifications
You must be signed in to change notification settings - Fork 0
/
PaintAPP.py
92 lines (81 loc) · 3.29 KB
/
PaintAPP.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
import pygame
def init():
global screen
pygame.init()
# set screen size
screen = pygame.display.set_mode((280, 280))
# title name
pygame.display.set_caption("MyPaintApp")
# background color= 'white'
screen.fill([0, 0, 0])
main_loop()
# draw circle from start to the end by given radius (like a line that made by several circles)
def round_line(surface, color, start, end, radius=1):
dx = end[0] - start[0]
dy = end[1] - start[1]
distance = max(abs(dx), abs(dy))
for i in range(distance):
x = int(start[0] + float(i) / distance * dx)
y = int(start[1] + float(i) / distance * dy)
pygame.display.update(pygame.draw.circle(surface, color, (x, y), radius))
# let program to draw
draw_on = False
# a value for showing last position that mouse had been moving
last_pos = None
# line color
color = (255, 255, 255)
# thickness of line
radius = 8
# a value that let program to continue (False means that "MyPaintApp" closed without saving
# and True means "MyPaintApp" saved 'new' picture and continue the running
resume = False
# return resume
def return_resume():
global resume
return resume
def main_loop():
global draw_on, last_pos, color, radius, resume
# Simple exception handling
try:
while True:
for event in pygame.event.get():
# if program closed by 'close button'
if event.type == pygame.QUIT:
resume = False
raise StopIteration
# if you press keyboard buttons
if event.type == pygame.KEYDOWN:
# and that button is 'Enter' (or return0)
if event.key == pygame.K_RETURN:
# save image as 'image.png'
pygame.image.save(screen, "image.png")
# and resume because you save 'new' image
resume = True
# and exit
raise StopIteration
# if you press mouse button and that button is right click
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 3:
# restart program (for erase background)
init()
# if you press mouse button and that button is left click
if event.type == pygame.MOUSEBUTTONDOWN:
# let program to draw
draw_on = True
# and draw circle
pygame.draw.circle(screen, color, event.pos, radius)
# if you put your finger off from button (button goes up)
if event.type == pygame.MOUSEBUTTONUP:
# don't let program to draw
draw_on = False
# if mouse move
if event.type == pygame.MOUSEMOTION:
if draw_on:
# update screen detail and draw
pygame.display.update(pygame.draw.circle(screen, color, event.pos, radius))
round_line(screen, color, event.pos, last_pos, radius)
# save new last position
last_pos = event.pos
pygame.display.flip()
except StopIteration:
# if something raise exit
pygame.quit()