Browse Source

Done task 2 in lab 3

master
Lev 3 years ago
parent
commit
b36a742600
  1. 117
      lab3/task_2.py

117
lab3/task_2.py

@ -0,0 +1,117 @@
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, 567, 30, 26))
ellipse(s, (0, 0, 0), (275, 574, 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)
pygame.init()
FPS = 30
W, H = 794, 1123
screen = pygame.display.set_mode((W, H))
screen.fill((175, 221, 233))
draw_background(screen)
draw_animal(screen)
# draw_flower(screen, 682, 814)
draw_flowers(screen, 515, 775)
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()
Loading…
Cancel
Save