Skip to content

Commit b31ad4d

Browse files
committed
add drawing tool with pygame tutorial
1 parent f928dc6 commit b31ad4d

File tree

4 files changed

+172
-0
lines changed

4 files changed

+172
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -212,5 +212,6 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
212212
- ### GUI Programming
213213
- [How to Make a Text Editor using Tkinter in Python](https://www.thepythoncode.com/article/text-editor-using-tkinter-python). ([code](gui-programming/text-editor))
214214
- [How to Make a Button using PyGame in Python](https://www.thepythoncode.com/article/make-a-button-using-pygame-in-python). ([code](gui-programming/button-in-pygame))
215+
- [How to Make a Drawing Program in Python](https://www.thepythoncode.com/article/make-a-drawing-program-with-python). ([code](gui-programming/drawing-tool-in-pygame))
215216

216217
For any feedback, please consider pulling requests.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [How to Make a Drawing Program in Python](https://www.thepythoncode.com/article/make-a-drawing-program-with-python)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
# Imports
2+
import sys
3+
import pygame
4+
import ctypes
5+
6+
# Increas Dots Per inch so it looks sharper
7+
ctypes.windll.shcore.SetProcessDpiAwareness(True)
8+
9+
# Pygame Configuration
10+
pygame.init()
11+
fps = 300
12+
fpsClock = pygame.time.Clock()
13+
width, height = 640, 480
14+
screen = pygame.display.set_mode((width, height), pygame.RESIZABLE)
15+
16+
font = pygame.font.SysFont('Arial', 20)
17+
18+
# Variables
19+
20+
# Our Buttons will append themself to this list
21+
objects = []
22+
23+
# Initial color
24+
drawColor = [0, 0, 0]
25+
26+
# Initial brush size
27+
brushSize = 30
28+
brushSizeSteps = 3
29+
30+
# Drawing Area Size
31+
canvasSize = [800, 800]
32+
33+
# Button Class
34+
class Button():
35+
def __init__(self, x, y, width, height, buttonText='Button', onclickFunction=None, onePress=False):
36+
self.x = x
37+
self.y = y
38+
self.width = width
39+
self.height = height
40+
self.onclickFunction = onclickFunction
41+
self.onePress = onePress
42+
43+
self.fillColors = {
44+
'normal': '#ffffff',
45+
'hover': '#666666',
46+
'pressed': '#333333',
47+
}
48+
49+
self.buttonSurface = pygame.Surface((self.width, self.height))
50+
self.buttonRect = pygame.Rect(self.x, self.y, self.width, self.height)
51+
52+
self.buttonSurf = font.render(buttonText, True, (20, 20, 20))
53+
54+
self.alreadyPressed = False
55+
56+
objects.append(self)
57+
58+
def process(self):
59+
60+
mousePos = pygame.mouse.get_pos()
61+
62+
self.buttonSurface.fill(self.fillColors['normal'])
63+
if self.buttonRect.collidepoint(mousePos):
64+
self.buttonSurface.fill(self.fillColors['hover'])
65+
66+
if pygame.mouse.get_pressed(num_buttons=3)[0]:
67+
self.buttonSurface.fill(self.fillColors['pressed'])
68+
69+
if self.onePress:
70+
self.onclickFunction()
71+
72+
elif not self.alreadyPressed:
73+
self.onclickFunction()
74+
self.alreadyPressed = True
75+
76+
else:
77+
self.alreadyPressed = False
78+
79+
self.buttonSurface.blit(self.buttonSurf, [
80+
self.buttonRect.width/2 - self.buttonSurf.get_rect().width/2,
81+
self.buttonRect.height/2 - self.buttonSurf.get_rect().height/2
82+
])
83+
screen.blit(self.buttonSurface, self.buttonRect)
84+
85+
86+
# Handler Functions
87+
88+
# Changing the Color
89+
def changeColor(color):
90+
global drawColor
91+
drawColor = color
92+
93+
# Changing the Brush Size
94+
def changebrushSize(dir):
95+
global brushSize
96+
if dir == 'greater':
97+
brushSize += brushSizeSteps
98+
else:
99+
brushSize -= brushSizeSteps
100+
101+
# Save the surface to the Disk
102+
def save():
103+
pygame.image.save(canvas, "canvas.png")
104+
105+
# Button Variables.
106+
buttonWidth = 120
107+
buttonHeight = 35
108+
109+
# Buttons and their respective functions.
110+
buttons = [
111+
['Black', lambda: changeColor([0, 0, 0])],
112+
['White', lambda: changeColor([255, 255, 255])],
113+
['Blue', lambda: changeColor([0, 0, 255])],
114+
['Green', lambda: changeColor([0, 255, 0])],
115+
['Brush Larger', lambda: changebrushSize('greater')],
116+
['Brush Smaller', lambda: changebrushSize('smaller')],
117+
['Save', save],
118+
]
119+
120+
# Making the buttons
121+
for index, buttonName in enumerate(buttons):
122+
Button(index * (buttonWidth + 10) + 10, 10, buttonWidth,
123+
buttonHeight, buttonName[0], buttonName[1])
124+
125+
# Canvas
126+
canvas = pygame.Surface(canvasSize)
127+
canvas.fill((255, 255, 255))
128+
129+
# Game loop.
130+
while True:
131+
screen.fill((30, 30, 30))
132+
for event in pygame.event.get():
133+
if event.type == pygame.QUIT:
134+
pygame.quit()
135+
sys.exit()
136+
137+
# Drawing the Buttons
138+
for object in objects:
139+
object.process()
140+
141+
# Draw the Canvas at the center of the screen
142+
x, y = screen.get_size()
143+
screen.blit(canvas, [x/2 - canvasSize[0]/2, y/2 - canvasSize[1]/2])
144+
145+
# Drawing with the mouse
146+
if pygame.mouse.get_pressed()[0]:
147+
mx, my = pygame.mouse.get_pos()
148+
149+
# Calculate Position on the Canvas
150+
dx = mx - x/2 + canvasSize[0]/2
151+
dy = my - y/2 + canvasSize[1]/2
152+
153+
pygame.draw.circle(
154+
canvas,
155+
drawColor,
156+
[dx, dy],
157+
brushSize,
158+
)
159+
160+
# Reference Dot
161+
pygame.draw.circle(
162+
screen,
163+
drawColor,
164+
[100, 100],
165+
brushSize,
166+
)
167+
168+
pygame.display.flip()
169+
fpsClock.tick(fps)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pygame

0 commit comments

Comments
 (0)