Skip to content

Commit e5d8c91

Browse files
Pixel Art Generator Script Added
1 parent c0b68a7 commit e5d8c91

File tree

3 files changed

+300
-0
lines changed

3 files changed

+300
-0
lines changed

Pixel Art Generator/README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Pixel Art Generator
2+
3+
## Description
4+
5+
The Pixel Art Generator is a Python script that allows you to create pixel art using turtle graphics. It provides various drawing patterns, colors, and sizes to create beautiful pixel art right on the canvas.
6+
7+
## Features
8+
9+
- Drawing patterns:
10+
- Square
11+
- Circle
12+
- Triangle
13+
- Diamond
14+
- Heart
15+
- Polygon (with customizable sides)
16+
- Line
17+
- Star (with customizable points)
18+
- Spiral (with customizable loops)
19+
- Fill shapes with colors (optional)
20+
- Adjust the size of the shapes (5-50 pixels)
21+
- Adjust the thickness of the pen (1-10 pixels)
22+
- Set different pen styles (solid, dashed, dotted)
23+
- Change the background color of the canvas
24+
- Clear the canvas to start a new drawing
25+
- Save the created pixel art as a PNG image file
26+
27+
## How to Use
28+
29+
1. Run the script using Python.
30+
2. A turtle graphics window will appear.
31+
3. Choose a pattern from the options:
32+
- "square", "circle", "triangle", "diamond", "heart", "polygon", "line", "star", "spiral"
33+
4. Depending on the pattern selected, you may need to provide additional information (e.g., size, sides, points, loops).
34+
5. Choose a color from the color chooser dialog.
35+
6. Click on the canvas to draw the selected shape with the chosen color.
36+
7. Use "Fill" option to fill the shape with color (if applicable).
37+
8. Adjust the pen style, background color, or save the pixel art as needed.
38+
9. To clear the canvas and start over, choose "clear" option.
39+
10. To save your artwork, choose "save" option and provide a file path.
40+
41+
## Dependencies
42+
43+
- Python
44+
- tkinter (standard library)
45+
- turtle (standard library)
46+
47+
## Author
48+
49+
This script was developed by andoriyaprashant
50+
51+
Feel free to use, modify, and distribute this script for personal and educational purposes. If you make any improvements or add new features, consider contributing back to the community!
52+
53+
Happy pixel art drawing!
Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
import turtle
2+
import tkinter as tk
3+
from tkinter import colorchooser, simpledialog, messagebox
4+
5+
def draw_pixel(x, y, color, size):
6+
turtle.penup()
7+
turtle.goto(x - size // 2, y - size // 2)
8+
turtle.pendown()
9+
turtle.dot(size, color)
10+
11+
def draw_square(x, y, color, size, fill=False):
12+
turtle.penup()
13+
turtle.goto(x - size // 2, y - size // 2)
14+
turtle.pendown()
15+
if fill:
16+
turtle.fillcolor(color)
17+
turtle.begin_fill()
18+
for _ in range(4):
19+
turtle.forward(size)
20+
turtle.left(90)
21+
if fill:
22+
turtle.end_fill()
23+
24+
def draw_circle(x, y, color, size, fill=False):
25+
turtle.penup()
26+
turtle.goto(x, y - size // 2)
27+
turtle.pendown()
28+
if fill:
29+
turtle.fillcolor(color)
30+
turtle.begin_fill()
31+
turtle.circle(size // 2)
32+
if fill:
33+
turtle.end_fill()
34+
35+
def draw_triangle(x, y, color, size, fill=False):
36+
turtle.penup()
37+
turtle.goto(x, y - size // 2)
38+
turtle.pendown()
39+
if fill:
40+
turtle.fillcolor(color)
41+
turtle.begin_fill()
42+
for _ in range(3):
43+
turtle.forward(size)
44+
turtle.left(120)
45+
if fill:
46+
turtle.end_fill()
47+
48+
def draw_diamond(x, y, color, size, fill=False):
49+
turtle.penup()
50+
turtle.goto(x, y - size // 2)
51+
turtle.pendown()
52+
if fill:
53+
turtle.fillcolor(color)
54+
turtle.begin_fill()
55+
for _ in range(2):
56+
turtle.forward(size)
57+
turtle.left(45)
58+
turtle.forward(size)
59+
turtle.left(135)
60+
if fill:
61+
turtle.end_fill()
62+
63+
def draw_heart(x, y, color, size, fill=False):
64+
turtle.penup()
65+
turtle.goto(x, y - size // 2)
66+
turtle.pendown()
67+
if fill:
68+
turtle.fillcolor(color)
69+
turtle.begin_fill()
70+
turtle.left(50)
71+
turtle.forward(size)
72+
turtle.circle(size // 2, 180)
73+
turtle.right(140)
74+
turtle.circle(size // 2, 180)
75+
turtle.forward(size)
76+
if fill:
77+
turtle.end_fill()
78+
79+
def draw_polygon(x, y, color, size, sides, fill=False):
80+
turtle.penup()
81+
turtle.goto(x, y - size // 2)
82+
turtle.pendown()
83+
if fill:
84+
turtle.fillcolor(color)
85+
turtle.begin_fill()
86+
angle = 360 / sides
87+
for _ in range(sides):
88+
turtle.forward(size)
89+
turtle.left(angle)
90+
if fill:
91+
turtle.end_fill()
92+
93+
def draw_line(x, y, color, size, thickness):
94+
turtle.penup()
95+
turtle.goto(x - size // 2, y)
96+
turtle.pendown()
97+
turtle.pensize(thickness)
98+
turtle.pencolor(color)
99+
turtle.forward(size)
100+
101+
def draw_star(x, y, color, size, points, fill=False):
102+
turtle.penup()
103+
turtle.goto(x, y - size // 2)
104+
turtle.pendown()
105+
if fill:
106+
turtle.fillcolor(color)
107+
turtle.begin_fill()
108+
for _ in range(points):
109+
turtle.forward(size)
110+
turtle.right(144)
111+
if fill:
112+
turtle.end_fill()
113+
114+
def draw_spiral(x, y, color, size, loops):
115+
turtle.penup()
116+
turtle.goto(x, y - size // 2)
117+
turtle.pendown()
118+
turtle.pencolor(color)
119+
for _ in range(loops * 36):
120+
turtle.forward(size / 20)
121+
turtle.right(10)
122+
123+
def set_background_color():
124+
color = colorchooser.askcolor(title="Choose a background color")
125+
if color[1] is not None:
126+
turtle.bgcolor(color[1])
127+
128+
def set_pen_style():
129+
pen_style = simpledialog.askstring("Pen Style", "Enter the pen style (solid/dashed/dotted):")
130+
if pen_style in ['solid', 'dashed', 'dotted']:
131+
turtle.pensize(1)
132+
if pen_style == 'dashed':
133+
turtle.pendown()
134+
turtle.pendown(1, 3)
135+
elif pen_style == 'dotted':
136+
turtle.pendown()
137+
turtle.pendown(1, 1)
138+
139+
def get_color():
140+
color = colorchooser.askcolor(title="Choose a color")
141+
if color[1] is not None:
142+
return color[1]
143+
else:
144+
return None
145+
146+
def get_size():
147+
size = simpledialog.askinteger("Size", "Enter the size (5-50):", minvalue=5, maxvalue=50)
148+
return size
149+
150+
def get_thickness():
151+
thickness = simpledialog.askinteger("Thickness", "Enter the thickness (1-10):", minvalue=1, maxvalue=10)
152+
return thickness
153+
154+
def get_sides():
155+
sides = simpledialog.askinteger("Sides", "Enter the number of sides (3-10):", minvalue=3, maxvalue=10)
156+
return sides
157+
158+
def main():
159+
turtle.speed(0)
160+
turtle.title("Pixel Art Generator")
161+
turtle.setup(800, 600)
162+
163+
canvas = turtle.Screen()
164+
canvas.bgcolor("white")
165+
166+
while True:
167+
pattern = tk.simpledialog.askstring("Pattern", "Choose a pattern (square/circle/triangle/diamond/heart/polygon/line/star/spiral/clear/save/background/pen_style/exit):")
168+
if pattern == 'exit':
169+
break
170+
171+
if pattern == 'clear':
172+
turtle.clear()
173+
canvas.update()
174+
continue
175+
elif pattern == 'save':
176+
file_path = tk.filedialog.asksaveasfilename(defaultextension=".png", filetypes=[("PNG files", "*.png")])
177+
if file_path:
178+
canvas.getcanvas().postscript(file=file_path + ".eps")
179+
canvas.update()
180+
messagebox.showinfo("Saved", f"Pixel art saved as {file_path}.png")
181+
continue
182+
elif pattern == 'background':
183+
set_background_color()
184+
continue
185+
elif pattern == 'pen_style':
186+
set_pen_style()
187+
continue
188+
189+
color = get_color()
190+
if color is None:
191+
break
192+
193+
size = get_size()
194+
if not size:
195+
break
196+
197+
if pattern == 'square':
198+
fill = messagebox.askyesno("Fill", "Fill the square with color?")
199+
draw_func = draw_square
200+
elif pattern == 'circle':
201+
fill = messagebox.askyesno("Fill", "Fill the circle with color?")
202+
draw_func = draw_circle
203+
elif pattern == 'triangle':
204+
fill = messagebox.askyesno("Fill", "Fill the triangle with color?")
205+
draw_func = draw_triangle
206+
elif pattern == 'diamond':
207+
fill = messagebox.askyesno("Fill", "Fill the diamond with color?")
208+
draw_func = draw_diamond
209+
elif pattern == 'heart':
210+
fill = messagebox.askyesno("Fill", "Fill the heart with color?")
211+
draw_func = draw_heart
212+
elif pattern == 'polygon':
213+
sides = get_sides()
214+
if not sides:
215+
break
216+
fill = messagebox.askyesno("Fill", "Fill the polygon with color?")
217+
draw_func = draw_polygon
218+
elif pattern == 'line':
219+
thickness = get_thickness()
220+
if not thickness:
221+
break
222+
draw_func = draw_line
223+
canvas.onclick(lambda x, y: draw_func(x, y, color, size, thickness))
224+
continue
225+
elif pattern == 'star':
226+
points = simpledialog.askinteger("Points", "Enter the number of points (5-20):", minvalue=5, maxvalue=20)
227+
if not points:
228+
break
229+
fill = messagebox.askyesno("Fill", "Fill the star with color?")
230+
draw_func = draw_star
231+
elif pattern == 'spiral':
232+
loops = simpledialog.askinteger("Loops", "Enter the number of loops (1-20):", minvalue=1, maxvalue=20)
233+
if not loops:
234+
break
235+
draw_func = draw_spiral
236+
237+
if pattern != 'line' and pattern != 'star' and pattern != 'spiral':
238+
canvas.onclick(lambda x, y: draw_func(x, y, color, size, fill))
239+
else:
240+
canvas.onclick(lambda x, y: draw_func(x, y, color, size, points if pattern == 'star' else loops))
241+
242+
turtle.done()
243+
244+
if __name__ == "__main__":
245+
main()

Pixel Art Generator/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
turtle
2+
tkinter

0 commit comments

Comments
 (0)