Лабораторные работы по Python, ЛФИ, 1 семестр
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
5.2 KiB

import pygame
from pygame.draw import rect, circle, ellipse, polygon, aalines, lines
from pygame.gfxdraw import aapolygon
import typing
def shift_points(points: typing.List[typing.Any], dx: int, dy: int) -> typing.List[typing.Any]:
return [(x + dx, y + dy, *wh) for (x, y, *wh) in points]
def draw_background(s):
mountains = [
(0, 346), (95, 114), (165, 275), (272, 148), (474, 450),
(618, 139), (667, 195), (793, 41)
]
mountains_bottom = [
(0, 593), (39, 579), (78, 573), (102, 573), (177, 562),
(417, 562), (433, 565), (441, 576), (441, 597),
(444, 599), (444, 656), (466, 665), (793, 665)
]
polygon(s, (179, 179, 179), mountains + [(W, H), (0, H)])
lines(s, (0, 0, 0), False, mountains)
polygon(s, (170, 222, 135), mountains_bottom + [(W, H), (0, H)])
aalines(s, (0, 0, 0), False, points=mountains_bottom)
def draw_animal(s, delta_x=0, delta_y=0):
leg = [
(109, 725, 25, 60),
(108, 783, 26, 53),
(115, 834, 26, 18),
]
rectangles = [
(245, 564, 65, 40),
(238, 600, 50, 130),
(100, 700, 180, 75),
*leg, *shift_points(leg, 36, 34),
*shift_points(leg, 103, -5), *shift_points(leg, 128, 33)
]
for ellipse_rect in shift_points(rectangles, delta_x, delta_y):
ellipse(s, (255, 255, 255), ellipse_rect)
ellipse(s, (229, 128, 255), (260 + delta_x, 567 + delta_y, 30, 26))
ellipse(s, (0, 0, 0), (275 + delta_x, 574 + delta_y, 11, 10))
polygon(s, (255, 255, 255), shift_points([
(245, 580), (238, 573), (236, 569), (232, 563),
(231, 559), (229, 555), (230, 550), (232, 556),
(235, 562), (240, 566), (244, 569), (248, 570), (253, 572)
], delta_x, delta_y))
polygon(s, (255, 255, 255), shift_points([
(253, 571), (250, 568), (246, 560), (244, 555), (243, 552),
(243, 547), (246, 555), (250, 561), (253, 563), (259, 567)
], delta_x, delta_y))
def draw_flower(s, delta_x: int, delta_y: int):
ellipses = [
(663, 799, 24, 11), (652, 803, 24, 11),
(679, 802, 24, 11),
(668, 809, 24, 11, (255, 255, 0)),
(686, 808, 24, 11), (647, 813, 24, 11),
(660, 816, 24, 11), (679, 816, 24, 11)
]
ellipses = shift_points(ellipses, delta_x - 682, delta_y - 814)
for ellipse_rect in ellipses:
color = (255, 255, 255)
stroke = (0, 0, 0)
if len(ellipse_rect) == 5:
color = ellipse_rect[4]
stroke = None
ellipse(s, color, ellipse_rect[:4])
if stroke is not None:
ellipse(s, stroke, ellipse_rect[:4], 1)
def draw_rotated_flower(s, angle: float, delta_x: int, delta_y: int):
surface = pygame.Surface((64, 41), pygame.SRCALPHA)
draw_flower(surface, 35, 15)
rotated_surf = pygame.transform.rotate(surface, angle)
s.blit(rotated_surf, rotated_surf.get_rect(center=(delta_x + 32, delta_y + 20)))
def draw_flowers(s, delta_x: int, delta_y: int):
circle(s, (113, 200, 55), (150 + delta_x, 150 + delta_y), 150)
# (515, 775)
# List[(X, Y, angle)]
flowers = [
(165, 40, 0), (75, 63, -30), (97, 121, -10),
(230, 97, 30), (160, 184, 15), (221, 177, 70)
]
for x, y, angle in flowers:
draw_rotated_flower(s, -angle, x + delta_x - 20, y + delta_y)
def draw_animal_scaled(s, delta_x: int, delta_y: int, scale_x: float, scale_y: float):
surface = pygame.Surface((214, 350), pygame.SRCALPHA)
draw_animal(surface, -100, -540)
surface = pygame.transform.scale(surface, (int(214 * abs(scale_x)), int(350 * abs(scale_y))))
surface = pygame.transform.flip(surface, scale_x < 0, scale_y < 0)
s.blit(
surface,
surface.get_rect(center=(delta_x + int(214 * abs(scale_x) / 2), delta_y + int(350 * abs(scale_y) / 2)))
)
def draw_flowers_scaled(s, delta_x: int, delta_y: int, scale_x: float, scale_y: typing.Optional[float] = None):
if scale_y is None:
scale_y = scale_x
surface = pygame.Surface((300, 300), pygame.SRCALPHA)
draw_flowers(surface, 0, 0)
surface = pygame.transform.scale(surface, (int(300 * scale_x), int(300 * scale_y)))
s.blit(
surface,
surface.get_rect(center=(delta_x + int(150 * scale_x), delta_y + int(150 * scale_y)))
)
pygame.init()
FPS = 30
W, H = 794, 1123
screen = pygame.display.set_mode((W, H))
screen.fill((175, 221, 233))
draw_background(screen)
draw_flowers_scaled(screen, 510, 892, 0.45)
draw_flowers_scaled(screen, 751, 1035, 0.28)
draw_flowers_scaled(screen, 664, 730, 0.5)
draw_flowers_scaled(screen, 561, 666, 0.36)
draw_flowers_scaled(screen, 694, 604, 0.32)
draw_flowers_scaled(screen, -20, 576, 0.3)
draw_animal_scaled(screen, -150, 730, 2, 2)
draw_animal_scaled(screen, 165, 586, 0.4, 0.4)
draw_animal_scaled(screen, 318, 639, -0.4, 0.4)
draw_animal_scaled(screen, 279, 465, 0.4, 0.4)
draw_animal_scaled(screen, 664, 595, -0.95, 0.95)
pygame.display.update()
clock = pygame.time.Clock()
finished = False
while not finished:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
finished = True
pygame.quit()