Skip to content

Commit a9dee15

Browse files
authored
Merge pull request #2637 from andoriyaprashant/branch27
Pixel Art Generator Script Added
2 parents b25ce3d + eeda4e1 commit a9dee15

File tree

3 files changed

+304
-0
lines changed

3 files changed

+304
-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: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
import turtle
2+
import tkinter as tk
3+
from tkinter import colorchooser, simpledialog, messagebox
4+
5+
turtle.setup(width=800, height=600)
6+
turtle.speed(0)
7+
turtle.title("Pixel Art Generator")
8+
9+
def draw_pixel(x, y, color, size):
10+
turtle.penup()
11+
turtle.goto(x - size // 2, y - size // 2)
12+
turtle.pendown()
13+
turtle.dot(size, color)
14+
15+
def draw_square(x, y, color, size, fill=False):
16+
turtle.penup()
17+
turtle.goto(x - size // 2, y - size // 2)
18+
turtle.pendown()
19+
if fill:
20+
turtle.fillcolor(color)
21+
turtle.begin_fill()
22+
for _ in range(4):
23+
turtle.forward(size)
24+
turtle.left(90)
25+
if fill:
26+
turtle.end_fill()
27+
28+
def draw_circle(x, y, color, size, fill=False):
29+
turtle.penup()
30+
turtle.goto(x, y - size // 2)
31+
turtle.pendown()
32+
if fill:
33+
turtle.fillcolor(color)
34+
turtle.begin_fill()
35+
turtle.circle(size // 2)
36+
if fill:
37+
turtle.end_fill()
38+
39+
def draw_triangle(x, y, color, size, fill=False):
40+
turtle.penup()
41+
turtle.goto(x, y - size // 2)
42+
turtle.pendown()
43+
if fill:
44+
turtle.fillcolor(color)
45+
turtle.begin_fill()
46+
for _ in range(3):
47+
turtle.forward(size)
48+
turtle.left(120)
49+
if fill:
50+
turtle.end_fill()
51+
52+
def draw_diamond(x, y, color, size, fill=False):
53+
turtle.penup()
54+
turtle.goto(x, y - size // 2)
55+
turtle.pendown()
56+
if fill:
57+
turtle.fillcolor(color)
58+
turtle.begin_fill()
59+
for _ in range(2):
60+
turtle.forward(size)
61+
turtle.left(45)
62+
turtle.forward(size)
63+
turtle.left(135)
64+
if fill:
65+
turtle.end_fill()
66+
67+
def draw_heart(x, y, color, size, fill=False):
68+
turtle.penup()
69+
turtle.goto(x, y - size // 2)
70+
turtle.pendown()
71+
if fill:
72+
turtle.fillcolor(color)
73+
turtle.begin_fill()
74+
turtle.left(50)
75+
turtle.forward(size)
76+
turtle.circle(size // 2, 180)
77+
turtle.right(140)
78+
turtle.circle(size // 2, 180)
79+
turtle.forward(size)
80+
if fill:
81+
turtle.end_fill()
82+
83+
def draw_polygon(x, y, color, size, sides, fill=False):
84+
turtle.penup()
85+
turtle.goto(x, y - size // 2)
86+
turtle.pendown()
87+
if fill:
88+
turtle.fillcolor(color)
89+
turtle.begin_fill()
90+
angle = 360 / sides
91+
for _ in range(sides):
92+
turtle.forward(size)
93+
turtle.left(angle)
94+
if fill:
95+
turtle.end_fill()
96+
97+
def draw_line(x, y, color, size, thickness):
98+
turtle.penup()
99+
turtle.goto(x - size // 2, y)
100+
turtle.pendown()
101+
turtle.pensize(thickness)
102+
turtle.pencolor(color)
103+
turtle.forward(size)
104+
105+
def draw_star(x, y, color, size, points, fill=False):
106+
turtle.penup()
107+
turtle.goto(x, y - size // 2)
108+
turtle.pendown()
109+
if fill:
110+
turtle.fillcolor(color)
111+
turtle.begin_fill()
112+
for _ in range(points):
113+
turtle.forward(size)
114+
turtle.right(144)
115+
if fill:
116+
turtle.end_fill()
117+
118+
def draw_spiral(x, y, color, size, loops):
119+
turtle.penup()
120+
turtle.goto(x, y - size // 2)
121+
turtle.pendown()
122+
turtle.pencolor(color)
123+
for _ in range(loops * 36):
124+
turtle.forward(size / 20)
125+
turtle.right(10)
126+
127+
def set_background_color():
128+
color = colorchooser.askcolor(title="Choose a background color")
129+
if color[1] is not None:
130+
turtle.bgcolor(color[1])
131+
132+
def set_pen_style():
133+
pen_style = simpledialog.askstring("Pen Style", "Enter the pen style (solid/dashed/dotted):")
134+
if pen_style in ['solid', 'dashed', 'dotted']:
135+
turtle.pensize(1)
136+
if pen_style == 'dashed':
137+
turtle.pendown()
138+
turtle.pendown(1, 3)
139+
elif pen_style == 'dotted':
140+
turtle.pendown()
141+
turtle.pendown(1, 1)
142+
143+
def get_color():
144+
color = colorchooser.askcolor(title="Choose a color")
145+
if color[1] is not None:
146+
return color[1]
147+
else:
148+
return None
149+
150+
def get_size():
151+
size = simpledialog.askinteger("Size", "Enter the size (5-50):", minvalue=5, maxvalue=50)
152+
return size
153+
154+
def get_thickness():
155+
thickness = simpledialog.askinteger("Thickness", "Enter the thickness (1-10):", minvalue=1, maxvalue=10)
156+
return thickness
157+
158+
def get_sides():
159+
sides = simpledialog.askinteger("Sides", "Enter the number of sides (3-10):", minvalue=3, maxvalue=10)
160+
return sides
161+
162+
def main():
163+
turtle.speed(0)
164+
turtle.title("Pixel Art Generator")
165+
turtle.setup(800, 600)
166+
167+
canvas = turtle.Screen()
168+
canvas.bgcolor("white")
169+
170+
while True:
171+
pattern = tk.simpledialog.askstring("Pattern", "Choose a pattern (square/circle/triangle/diamond/heart/polygon/line/star/spiral/clear/save/background/pen_style/exit):")
172+
if pattern == 'exit':
173+
break
174+
175+
if pattern == 'clear':
176+
turtle.clear()
177+
canvas.update()
178+
continue
179+
elif pattern == 'save':
180+
file_path = tk.filedialog.asksaveasfilename(defaultextension=".png", filetypes=[("PNG files", "*.png")])
181+
if file_path:
182+
canvas.getcanvas().postscript(file=file_path + ".eps")
183+
canvas.update()
184+
messagebox.showinfo("Saved", f"Pixel art saved as {file_path}.png")
185+
continue
186+
elif pattern == 'background':
187+
set_background_color()
188+
continue
189+
elif pattern == 'pen_style':
190+
set_pen_style()
191+
continue
192+
193+
color = get_color()
194+
if color is None:
195+
break
196+
197+
size = get_size()
198+
if not size:
199+
break
200+
201+
if pattern == 'square':
202+
fill = messagebox.askyesno("Fill", "Fill the square with color?")
203+
draw_func = draw_square
204+
elif pattern == 'circle':
205+
fill = messagebox.askyesno("Fill", "Fill the circle with color?")
206+
draw_func = draw_circle
207+
elif pattern == 'triangle':
208+
fill = messagebox.askyesno("Fill", "Fill the triangle with color?")
209+
draw_func = draw_triangle
210+
elif pattern == 'diamond':
211+
fill = messagebox.askyesno("Fill", "Fill the diamond with color?")
212+
draw_func = draw_diamond
213+
elif pattern == 'heart':
214+
fill = messagebox.askyesno("Fill", "Fill the heart with color?")
215+
draw_func = draw_heart
216+
elif pattern == 'polygon':
217+
sides = get_sides()
218+
if not sides:
219+
break
220+
fill = messagebox.askyesno("Fill", "Fill the polygon with color?")
221+
draw_func = draw_polygon
222+
elif pattern == 'line':
223+
thickness = get_thickness()
224+
if not thickness:
225+
break
226+
draw_func = draw_line
227+
canvas.onclick(lambda x, y: draw_func(x, y, color, size, thickness))
228+
continue
229+
elif pattern == 'star':
230+
points = simpledialog.askinteger("Points", "Enter the number of points (5-20):", minvalue=5, maxvalue=20)
231+
if not points:
232+
break
233+
fill = messagebox.askyesno("Fill", "Fill the star with color?")
234+
draw_func = draw_star
235+
elif pattern == 'spiral':
236+
loops = simpledialog.askinteger("Loops", "Enter the number of loops (1-20):", minvalue=1, maxvalue=20)
237+
if not loops:
238+
break
239+
draw_func = draw_spiral
240+
241+
if pattern != 'line' and pattern != 'star' and pattern != 'spiral':
242+
canvas.onclick(lambda x, y: draw_func(x, y, color, size, fill))
243+
else:
244+
canvas.onclick(lambda x, y: draw_func(x, y, color, size, points if pattern == 'star' else loops))
245+
246+
turtle.done()
247+
248+
if __name__ == "__main__":
249+
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)