def smile(x, y, color): radius = 30 _point = sd.get_point(x=x, y=y) sd.circle(center_position=_point, radius=radius, width=3, color=sd.COLOR_BLACK) sd.circle(center_position=_point, radius=radius - 2, width=0, color=color) left_point = sd.get_point(x=x - radius // 3, y=y + radius // 3) right_point = sd.get_point(x=x - radius // 5, y=y + 2 * radius // 3) sd.ellipse(left_bottom=left_point, right_top=right_point, color=sd.COLOR_BLACK) left_point = sd.get_point(x=x + radius // 5, y=y + radius // 3) right_point = sd.get_point(x=x + radius // 3, y=y + 2 * radius // 3) sd.ellipse(left_bottom=left_point, right_top=right_point, color=sd.COLOR_BLACK) mouth = [ sd.Point(x=x - radius // 2, y=y - radius // 3), sd.Point(x=x - radius // 4, y=y - radius // 2), sd.Point(x=x + radius // 4, y=y - radius // 2), sd.Point(x=x + radius // 2, y=y - radius // 3), ] sd.lines(point_list=mouth, width=3, color=sd.COLOR_BLACK)
def draw_roof(x0, y0, height_wall, width_wall, COLOR): points = [ sd.Point(x=x0 - width_wall / 4, y=y0 + height_wall), sd.Point(x=x0 + width_wall / 2, y=y0 + 1.8 * height_wall), sd.Point(x=x0 + 5 * width_wall / 4, y=y0 + height_wall) ] sd.polygon(point_list=points, color=COLOR, width=0)
def smile(x, y, colors): sd.circle(sd.Point(x + 11, y + 11), 6, colors, 1) sd.circle(sd.Point(x - 9, y + 11), 6, colors, 1) sd.circle(sd.Point(x, y), 30, colors, 1) sd.line(sd.Point(x - 6, y - 9), sd.Point(x + 7, y - 9), colors, 1) sd.line(sd.Point(x - 6, y - 9), sd.Point(x - 9, y - 6), colors, 1) sd.line(sd.Point(x + 7, y - 9), sd.Point(x + 10, y - 6), colors, 1)
def draw_house(_x=300, _y=10): brick_height = 10 brick_length = 30 brick_seam = 3 house_length = brick_length * 9 + brick_seam * 8 house_height = (brick_height + brick_seam) * 18 brick_color = (136, 69, 53) seam_color = (104, 108, 94) bottom_point = sd.get_point(_x, _y) top_point = sd.get_point(x=_x + house_length, y=_y + house_height) sd.rectangle(left_bottom=bottom_point, right_top=top_point, color=brick_color) triangle = [ sd.Point(_x - 50, _y + house_height), sd.Point(_x + house_length + 50, _y + house_height), sd.Point(_x + house_length // 2, _y + house_length), ] sd.polygon(point_list=triangle, width=0, color=seam_color) for y in range(house_height // (brick_height + brick_seam)): if y % 2 == 0: start_x = _x + brick_length else: start_x = _x + brick_length // 2 if 0 < y <= house_height // (brick_height + brick_seam): line_point1 = sd.get_point(x=_x, y=_y + y * (brick_height + brick_seam)) line_point2 = sd.get_point(x=_x + house_length - 1, y=_y + y * (brick_height + brick_seam)) sd.line(start_point=line_point1, end_point=line_point2, color=seam_color, width=brick_seam) for x in range(start_x, _x + house_length, brick_length + brick_seam): line_point1 = sd.get_point(x=x, y=_y + y * (brick_height + brick_seam)) line_point2 = sd.get_point(x=x, y=_y + (y + 1) * (brick_height + brick_seam)) sd.line(start_point=line_point1, end_point=line_point2, color=seam_color, width=brick_seam) bottom_point = sd.get_point(x=_x + (brick_length + brick_seam) * 3 - 2, y=_y + (brick_height + brick_seam) * 6 + 2) top_point = sd.get_point(x=_x + (brick_length + brick_seam) * 6, y=_y + (brick_height + brick_seam) * 12) sd.rectangle(left_bottom=bottom_point, right_top=top_point, color=(157, 161, 170)) sd.rectangle(left_bottom=bottom_point, right_top=top_point, color=(138, 102, 66), width=4)
def draw_wall(): k = 0 for i in range(0, 400, 50): k = 50 - k for j in range(k + 400, 800, 100): left_bottom = sd.Point(j, i) right_top = sd.Point(j + 100, i + 50) sd.rectangle(left_bottom=left_bottom, right_top=right_top, color=sd.COLOR_DARK_RED, width=2)
def smile(x, y, color): radius = 50 s.circle(center_position=s.Point(x, y), radius=radius, color=color, width=0) s.circle(center_position=s.Point(x + 15, y + 20), radius=7, color=s.COLOR_CYAN, width=0) s.circle(center_position=s.Point(x - 15, y + 20), radius=7, color=s.COLOR_CYAN, width=0) s.line(start_point=s.Point(x - 20, y - 10), end_point=s.Point(x + 20, y - 10), color=s.COLOR_RED, width=4) s.line(start_point=s.Point(x - 20, y - 12), end_point=s.Point(x + -30, y + 5), color=s.COLOR_RED, width=4) s.line(start_point=s.Point(x + 20, y - 12), end_point=s.Point(x + 30, y + 5), color=s.COLOR_RED, width=4)
def ulibka(x, y, color): x3 = x - 50 y3 = y - 10 x4 = x - 25 y4 = y - 25 x5 = x + 25 y5 = y - 25 x6 = x + 50 y6 = y - 10 point3 = sd.Point(x3, y3) point4 = sd.Point(x4, y4) point5 = sd.Point(x5, y5) point6 = sd.Point(x6, y6) point_list = (point3, point4, point5, point6) sd.lines(point_list, color, False, 2)
def falling_snowflake(): snowflakes_length = [sd.random_number(10, 101) for _ in range(20)] snowflakes_point = { i: [sd.random_number(0, 500), 500, i] for i in range(20) } old_points_arr = [[snowflakes_point[key][0], snowflakes_point[key][1]] for key, val in snowflakes_point.items()] while True: sd.start_drawing() for i in range(20): if len(old_points_arr) < 20: sd.snowflake(center=old_points_arr[i], length=snowflakes_length[i], color=sd.background_color) x, y, delay = snowflakes_point[i] if delay < 1: point = sd.get_point(x, y) sd.snowflake(center=sd.Point(old_points_arr[i][0], old_points_arr[i][1]), length=snowflakes_length[i], color=sd.background_color) sd.snowflake(center=point, length=snowflakes_length[i]) if y < 20: snowflakes_point[i][1] = 500 continue snowflakes_point[i][1] -= 9 snowflakes_point[i][0] += sd.random_number(-10, 10) old_points_arr[i] = [x, y] snowflakes_point[i][2] -= .2 sd.finish_drawing() sd.sleep(0.1) if sd.user_want_exit(): break
def smile(x, y, color): point = sd.Point( x, y, ) sd.circle(point, 100, color, 5) glaz(x, y, color) glaz_2(x, y, color) ulibka(x, y, color)
def draw_sun(_x, _y, _color=sd.COLOR_YELLOW, _step=30): # """Функция рисуюет смайл""" for i in range(0, 360, _step): sd.vector(start=sd.get_point(_x, _y), angle=i, length=80, color=_color) center_position = sd.Point(_x, _y) radius = 50 sd.circle(center_position=center_position, radius=radius, color=sd.COLOR_YELLOW, width=0)
def rainbow(): rainbow_colors = [ sd.COLOR_RED, sd.COLOR_ORANGE, sd.COLOR_YELLOW, sd.COLOR_GREEN, sd.COLOR_CYAN, sd.COLOR_BLUE, sd.COLOR_PURPLE ] radius = 750 for _ in rainbow_colors: color = rainbow_colors[sd.random_number(0, 6)] sd.circle(sd.Point(350, 50), radius, color, 20) radius += 20
def paint(): home.house() home.window_and_roof() branch.branch(sd.Point(900, 50), 90, 80) branch.branch(sd.Point(800, 50), 90, 30) branch.branch(sd.Point(1000, 50), 90, 30) flakes = [sun.Snowflake_Sun() for _ in range(10)] vectors = [ sun.Snowflake_Sun().vector(angles=angles) for angles in range(0, 361, 30) ] while True: sd.start_drawing() smile.smile(550, 150, sd.COLOR_BLACK) sun.sun_anim(vectors) sun.snowflake(flakes) smile.smile1(550, 150, sd.COLOR_BLACK) rainbow.rainbow() sd.finish_drawing() sd.sleep(0.05) if sd.user_want_exit(): break
def draw_rainbow(_i=0): center_position = sd.Point(1500, 50) radius = 400 if _i == 1: k = -1 else: k = 1 for color in rainbow_colors[k]: color = rainbow_colors[sd.random_number(0, 6)] radius += 20 sd.circle(center_position=center_position, radius=radius, color=color, width=20)
def third_screen(): for i in range(10): point = sd.random_point() color = sd.random_color() dx = sd.random_number(30, 100) dy = sd.random_number(30, 100) right_top = sd.Point(x=point.x + dx, y=point.y + dy) sd.ellipse(left_bottom=point, right_top=right_top, color=color) v3 = sd.Vector(start_point=sd.Point(0, 0), direction=90, length=50) for direction in range(0, 181, 20): v = sd.Vector(start_point=sd.Point(x=300, y=300), direction=direction, length=100) v.draw(width=3) v2 = sd.Vector(start_point=v.end_point, direction=direction + 30, length=50) v2.draw(color=sd.COLOR_GREEN, width=2) v2.add(v3) v2.draw(color=sd.COLOR_ORANGE) sd.snowflake(center=sd.Point(), length=60, factor_b=0.2, factor_c=100) if TAKE_SNAPSHOTS: sd.take_snapshot(path=SNAPSHOTS_PATH)
def second_screen(): sd.start_drawing() sd.vector(start=sd.Point(x=230, y=260), angle=70, length=200, color=sd.COLOR_PURPLE, width=2) for i in range(10): point = sd.random_point() color = sd.random_color() radius = sd.random_number(20, 60) sd.circle(center_position=point, radius=radius, color=color, width=0) sd.finish_drawing() if TAKE_SNAPSHOTS: sd.take_snapshot(file_name='second_screen.png', path=SNAPSHOTS_PATH)
def calc_p1(self, t, b): if t == 0: t = 1e-6 if t == 1: t -= 1e-6 nt = 1 - t nt2 = nt * nt t2 = t * t p = sd.Point() p.x = (b.x - nt2 * self.points[0].x - t2 * self.points[2].x) / (2 * t * nt) p.y = (b.y - nt2 * self.points[0].y - t2 * self.points[2].y) / (2 * t * nt) self.points[1] = p
def window_and_roof(): points_for_roof = [ sd.Point(350, 250), sd.Point(750, 250), sd.Point(550, 330) ] sd.lines(points_for_roof, width=0, closed=True) sd.rectangle(sd.Point(500, 110), sd.Point(600, 210), width=0) sd.polygon(points_for_roof, color=sd.COLOR_RED, width=0) sd.square(sd.Point(500, 110), 100, color=sd.COLOR_BLACK, width=4)
def snowfall(): for k in range(2): y = 500 for i in range(10): sd.clear_screen() y -= 30 for x in [100, 200, 300, 400, 500]: radius = sd.random_number(30, 50) point = sd.Point(x=x, y=y) sd.snowflake(center=point, length=radius) mouse_point, mouse_buttons = sd.get_mouse_state() print("mouse_state is {} + {}".format(mouse_point, mouse_buttons)) if sd.user_want_exit(sleep_time=0.1): break if sd.user_want_exit(0): break
def smile(_x, _y, color): """Функция рисуюет смайл""" center_position = sd.Point(_x, _y) center_position_left_eye = sd.Point(_x - 15, _y + 15) center_position_right_eye = sd.Point(_x + 15, _y + 15) first_dot_smile = sd.Point(_x - 25, _y - 10) second_dot_smile = sd.Point(_x - 5, _y - 20) third_dot_smile = sd.Point(_x + 5, _y - 20) four_dot_smile = sd.Point(_x + 25, _y - 10) point_list = [first_dot_smile, second_dot_smile, third_dot_smile, four_dot_smile] radius = 50 radius_eye = 5 sd.circle(center_position=center_position, radius=radius, color=color, width=4) sd.circle(center_position=center_position_left_eye, radius=radius_eye, color=color, width=3) sd.circle(center_position=center_position_right_eye, radius=radius_eye, color=color, width=3) sd.lines(point_list=point_list, color=color, width=3)
def house(): sd.rectangle(sd.Point(0, 0), sd.Point(1200, 50), color=sd.COLOR_WHITE, width=0) # снежное поле sd.rectangle(sd.Point(400, 50), sd.Point(700, 250), width=1) delta = 25 for y in range(50, 250, 20): delta += 25 for x in range(375 + delta, 675, 50): if x > 375: sd.rectangle(sd.Point(x, y), sd.Point(x + 50, y + 20), width=1) delta -= 50
def getTBSegment(segment, Pc, T, R, D): kk = (1 - T) * 0.5 t = kk + T k_extr = 0 b = sd.Point() for cycle in range(100): segment.calc(t, b) d_ = length_line(b, Pc) - R if abs(d_) <= D: return (t, b) if kk == k_extr: k = -0.5 if d_ > 0 else 0.5 else: k = -1 if d_ > 0 else 1 k_extr = kk kk *= k t += kk kk = abs(kk) return (t, b)
# В итоге должно получиться: # - одна общая функция со множеством параметров, # - все функции отрисовки треугольника/квадрата/етс берут 3 параметра и внутри себя ВЫЗЫВАЮТ общую функцию. # # Не забудте в этой общей функции придумать, как устранить разрыв в начальной/конечной точках рисуемой фигуры # (если он есть. подсказка - на последней итерации можно использовать линию от первой точки) # Часть 2-бис. # А теперь - сколько надо работы что бы внести изменения в код? Выгода на лицо :) # Поэтому среди программистов есть принцип D.R.Y. https://clck.ru/GEsA9 # Будьте ленивыми, не используйте копи-пасту! def geometry(point, angles=3, length=100): point_1 = point for angle in range(0, angles - 1, 1): v = sd.get_vector(point, angle=angle * (360 // angles), length=length) point = v.end_point v.draw() sd.line(point_1, point) geometry(sd.Point(100, 100), 3) geometry(sd.Point(400, 100), 4) geometry(sd.Point(100, 300), 5) geometry(sd.Point(400, 300), 6) sd.pause() # зачет!
common_part(*args,3) def square(*args): common_part(*args,4) def pentagon(*args): common_part(*args,5) def hexagon(*args): common_part(*args,6) triangle(sd.Point(100, 100), 30, 100) square(sd.Point(370, 100), 30, 100) pentagon(sd.Point(150, 400), 30, 100) hexagon(sd.Point(450, 350), 30, 100) # Часть 2 (делается после зачета первой части) # # Надо сформировать функцию, параметризированную в местах где была "небольшая правка". # Это называется "Выделить общую часть алгоритма в отдельную функцию" # Потом надо изменить функции рисования конкретных фигур - вызывать общую функцию вместо "почти" одинакового кода. # # В итоге должно получиться: # - одна общая функция со множеством параметров, # - все функции отрисовки треугольника/квадрата/етс берут 3 параметра и внутри себя ВЫЗЫВАЮТ общую функцию. #
# -*- coding: utf-8 -*- import random import simple_draw as sd sd.resolution = (1200, 600) # Нарисовать пузырек - три вложенных окружностей с шагом 5 пикселей point = sd.Point(100, 200) radius = 50 for _ in range(3): radius += 5 sd.circle(center_position=point, radius=radius) # Написать функцию рисования пузырька, принммающую 3 (или более) параметра: точка рисования, шаг и цвет def bubble( point_, radius_, color_, ): """Функция рисует пузырь""" for _ in range(3): radius_ += 5 sd.circle(center_position=point_, radius=radius_, color=color_) # Нарисовать 10 пузырьков в ряд radius = 50 color = sd.COLOR_RED for x in range(100, 1100, 100):
def draw(self, color): if self.y > self.length: sd.snowflake(center=sd.Point(self.x, self.y), length=self.length, color=color)
def pentagon(*args): common_part(*args, 5) def hexagon(*args): common_part(*args, 6) a = int( input("""Какой цвет вы хотите выбрать? 0 - Красный 1 - Оранжевый 2 - Желтый 3 - Зеленый 4 - Голубой 5 - Синий 6 - Пурпурный Выберите желаемый цвет > """)) while True: if -1 < a < 7: break else: print("Вы ввели некоректное число") a = int(input('Выберите желаемый цвет >')) continue triangle(sd.Point(100, 100), 30, 100, ALL_COLOR[a]) square(sd.Point(370, 100), 30, 100, ALL_COLOR[a]) pentagon(sd.Point(150, 400), 30, 100, ALL_COLOR[a]) hexagon(sd.Point(450, 350), 30, 100, ALL_COLOR[a]) sd.pause()
# -*- coding: utf-8 -*- import simple_draw as sd import random as rand sd.resolution = (1200, 600) # Нарисовать пузырек - три вложенных окружностей с шагом 5 пикселей point = sd.Point(x=80, y=80) for i in range(1, 4): r = 50 + i * 5 sd.circle(center_position=point, radius=r, color=sd.COLOR_PURPLE, width=1) # Написать функцию рисования пузырька, принммающую 2 (или более) параметра: точка рисовании и шаг def draw_bubbles(center, rad, step, clr, width): for ii in range(1, 4): sd.circle(center_position=center, radius=rad + ii * step, color=clr, width=width) point = sd.Point(x=230, y=80) color = sd.COLOR_DARK_RED draw_bubbles(point, 50, 10, color, 3) # Нарисовать 10 пузырьков в ряд color = sd.COLOR_DARK_ORANGE for i in range(10):
def first_screen(): points = [ sd.Point(x=230, y=450), sd.Point(x=240, y=460), sd.Point(x=230, y=470), sd.Point(x=240, y=480), ] sd.polygon(point_list=points) points2 = [sd.Point(p.x + 20, p.y + 20) for p in points] sd.lines(point_list=points2, color=sd.COLOR_DARK_ORANGE, width=2) sd.line(start_point=sd.Point(x=20, y=20), end_point=sd.Point(x=40, y=300), width=2) sd.square(left_bottom=sd.Point( 400, 300, ), side=100, width=2) sd.rectangle( left_bottom=sd.Point(x=200, y=200), right_top=sd.Point(x=300, y=300), color=sd.COLOR_DARK_GREEN, width=2, ) sd.rectangle( left_bottom=sd.Point(x=400, y=300), right_top=sd.Point(x=300, y=400), color=sd.COLOR_DARK_GREEN, width=2, ) if TAKE_SNAPSHOTS: sd.take_snapshot(path=SNAPSHOTS_PATH)
pygame.draw.line( sc, COLOR_LINE, [10, sc.get_height() - 10], [sc.get_width() - 10, sc.get_height() - 10]) pygame.draw.line(sc, COLOR_LINE, [10, 10], [10, sc.get_height() - 10]) pygame.display.update() # Обозначение линий координат и их разметка font = pygame.font.SysFont('Consolas', 18) PaintSimbol("0", 17, 17) PaintSimbol("D", 17, sc.get_height() - 17) PaintSimbol("t", sc.get_width() - 17, 17) PaintMetki() pygame.display.update() for i in range(0, 100): MassivTochek.append(sd.Point(i, (150 + 120 * math.sin(math.pi / 12 * i)))) while 1: for i in pygame.event.get(): if i.type == pygame.QUIT: pygame.quit() # # Рисуем синус зелёным for i in range(len(MassivTochek)): text = font.render(".", 1, (COLOR_TEXT)) y = sc.get_height() - MassivTochek[i].y + 5 place = text.get_rect(center=(i * 10 + 5, y)) sc.blit(text, place) pygame.display.update() # # Рисуем косинус желтым
def glaz_2(x, y, color): x2 = x + 40 y2 = y + 40 point2 = sd.Point(x2, y2) sd.circle(point2, 25, color, 2)