7 examples of 'how to draw a square in python' in Python

Every line of 'how to draw a square in python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
46def square(mark, number):
47 "Draw white square with black outline and number."
48 up()
49 goto(mark.x, mark.y)
50 down()
51
52 color('black', 'white')
53 begin_fill()
54 for count in range(4):
55 forward(99)
56 left(90)
57 end_fill()
58
59 if number is None:
60 return
61 elif number < 10:
62 forward(20)
63
64 write(number, font=('Arial', 60, 'normal'))
85def draw_textured_square(w=None, h=None):
86 """
87 Draws a texture square of 2 x 2 size centered at 0, 0
88
89 Make sure to call glEnable(GL_TEXTURE_RECTANGLE_ARB) first.
90
91 :param w: width of the image in pixels
92 :param h: height of the image in pixels
93 """
94 if w is None or h is None:
95 glBegin(GL_QUADS)
96 glTexCoord2f(0.0, 0.0)
97 glVertex2f(-1.0, -1.0) # Bottom Left Of The Texture and Quad
98 glTexCoord2f(1.0, 0.0)
99 glVertex2f(1.0, -1.0) # Bottom Right Of The Texture and Quad
100 glTexCoord2f(1.0, 1.0)
101 glVertex2f(1.0, 1.0) # Top Right Of The Texture and Quad
102 glTexCoord2f(0.0, 1.0)
103 glVertex2f(-1.0, 1.0) # Top Left Of The Texture and Quad
104 glEnd()
105 else:
106 glBegin(GL_QUADS)
107 glTexCoord2f(0.0, 0.0)
108 glVertex2f(-1.0, -1.0) # Bottom Left
109 glTexCoord2f(w, 0.0)
110 glVertex2f(1.0, -1.0) # Bottom Right
111 glTexCoord2f(w, h)
112 glVertex2f(1.0, 1.0) # Top Right
113 glTexCoord2f(0.0, h)
114 glVertex2f(-1.0, 1.0) # Top Left
115 glEnd()
51def square(x, y):
52 "Draw square using path at (x, y)."
53 path.up()
54 path.goto(x, y)
55 path.down()
56 path.begin_fill()
57
58 for count in range(4):
59 path.forward(20)
60 path.left(90)
61
62 path.end_fill()
63def drawSquare(self, x, y, fig, fclr):
64 axis = fig.gca()
65 axis.add_patch(patches.RegularPolygon((x, y), 4, self.radius, orientation=(np.pi/4), fc=fclr))
31def draw(self, screen):
32 # 棋盤底色
33 pygame.draw.rect(screen, (185, 122, 87),
34 [self.start_x - self.edge_size, self.start_y - self.edge_size,
35 (self.grid_count - 1) * self.grid_size + self.edge_size * 2,
36 (self.grid_count - 1) * self.grid_size + self.edge_size * 2], 0)
37
38 for r in range(self.grid_count):
39 y = self.start_y + r * self.grid_size
40 pygame.draw.line(screen, (0, 0, 0), [self.start_x, y],
41 [self.start_x + self.grid_size * (self.grid_count - 1), y], 2)
42
43 for c in range(self.grid_count):
44 x = self.start_x + c * self.grid_size
45 pygame.draw.line(screen, (0, 0, 0), [x, self.start_y],
46 [x, self.start_y + self.grid_size * (self.grid_count - 1)], 2)
47
48 for r in range(self.grid_count):
49 for c in range(self.grid_count):
50 piece = self.grid[r][c]
51 if piece != '.':
52 if piece == 'b':
53 color = (0, 0, 0)
54 else:
55 color = (255, 255, 255)
56
57 x = self.start_x + c * self.grid_size
58 y = self.start_y + r * self.grid_size
59 pygame.draw.circle(screen, color, [x, y], self.grid_size // 2)
460def draw_square(self,width,height,radius):
461 img = cairo.ImageSurface(cairo.FORMAT_ARGB32, width,height)
462 cr = cairo.Context(img)
463 m = radius/sqrt(2.0)
464
465 # Slightly darker border for the colour area
466 sw = max(0.75*self.stroke_width, 3.0)
467 cr.set_source_rgb(*NEUTRAL_DARK_GREY)
468 cr.rectangle(self.x0-m-sw, self.y0-m-sw, 2*(m+sw), 2*(m+sw))
469 cr.fill()
470
471 h,s,v = self.hsv
472 ds = 2*m*CSTEP
473 v = 0.0
474 x1 = self.x0-m
475 x2 = self.x0+m
476 y = self.y0-m
477 while v < 1.0:
478 g = cairo.LinearGradient(x1,y,x2,y)
479 g.add_color_stop_rgb(0.0, *hsv_to_rgb(h,0.0,1.0-v))
480 g.add_color_stop_rgb(1.0, *hsv_to_rgb(h,1.0,1.0-v))
481 cr.set_source(g)
482 cr.rectangle(x1,y, 2*m, ds)
483 cr.fill_preserve()
484 cr.stroke()
485 y += ds
486 v += CSTEP
487 h,s,v = self.hsv
488 x = self.x0-m + s*2*m
489 y = self.y0-m + (1-v)*2*m
490 cr.set_source_rgb(*hsv_to_rgb(1-h,1-s,1-v))
491 cr.arc(x,y, 3.0, 0.0, 2*pi)
492 cr.stroke()
493
494 return img
203def draw_squares(self):
204 self.fill_xs_ys()
205 #
206
207 text_background_buffer = int(self.square_size/6)
208 xs_size = len(self.xs)
209 ys_size = len(self.ys)
210 box_number = 0
211 colors_index = 0
212 font = ImageFont.truetype("arialbd.ttf", 15)
213 draw = ImageDraw.Draw(self.img, 'RGBA')
214
215 for ly in range(0, ys_size - 1):
216 for lx in range(0, xs_size - 1):
217 txt = str(box_number)
218 tw, th = draw.textsize(txt, font)
219 text_x = int((self.xs[lx] + self.xs[lx + 1] - tw)/2) + 1
220 text_y = int((self.ys[ly] + self.ys[ly + 1] - th)/2) - 1
221 draw.rectangle(
222 [
223 self.xs[lx] + text_background_buffer,
224 self.ys[ly] + text_background_buffer,
225 self.xs[lx + 1] - text_background_buffer,
226 self.ys[ly + 1] - text_background_buffer
227 ],
228 fill=self.colors[colors_index],
229 outline=False)
230
231 draw.text((text_x + 1, text_y + 1), txt, (0, 0, 0), font=font)
232 draw.text((text_x - 1, text_y + 1), txt, (0, 0, 0), font=font)
233 draw.text((text_x + 1, text_y - 1), txt, (0, 0, 0), font=font)
234 draw.text((text_x - 1, text_y - 1), txt, (0, 0, 0), font=font)
235 draw.text((text_x, text_y), txt, (255, 255, 255), font=font)
236 # index the position
237 self.position_index[len(self.position_index) - 1].append(
238 (int((self.xs[lx] + self.xs[lx + 1])/2),
239 int((self.ys[ly] + self.ys[ly + 1])/2)))
240
241 # update for next iteration
242 box_number += 1
243 if box_number == 100:
244 # next color
245 box_number = 0
246 colors_index += 1
247 colors_index %= len(self.colors) # cycle colors
248 self.position_index.append([])
249
250 del draw

Related snippets