|
| 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() |
0 commit comments