Files
lab_3_Ilya/bunny.py

91 lines
2.6 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import pygame
from pygame.draw import ellipse, circle
pygame.init()
FPS = 30
SCREEN_SIZE = (400, 400)
WHITE = 'white'
screen = pygame.display.set_mode(SCREEN_SIZE)
def draw_body(surface, x, y, width, height, color):
"""Рисует тело зайца."""
ellipse(surface, color, (x - width // 2, y - height // 2, width, height))
def draw_head(surface, x, y, size, color):
"""Рисует голову зайца."""
circle(surface, color, (x, y), size // 2)
def draw_ear(surface, x, y, width, height, color):
"""Рисует ухо зайца."""
ellipse(surface, color, (x - width // 2, y - height // 2, width, height))
def draw_leg(surface, x, y, width, height, color):
"""Рисует ногу зайца."""
ellipse(surface, color, (x - width // 2, y - height // 2, width, height))
def draw_hare(surface, x, y, width, height, color):
"""
Рисует зайца на экране.
Args:
surface: объект pygame.Surface
x, y: координаты центра изображения
width, height: ширина и высота изображения
color: цвет в формате, подходящем для pygame.Color
"""
# Параметры тела
body_width = width // 2
body_height = height // 2
body_y = y + body_height // 2
draw_body(surface, x, body_y, body_width, body_height, color)
# Параметры головы
head_size = height // 4
head_y = y - head_size // 2
draw_head(surface, x, head_y, head_size, color)
# Параметры ушей
ear_height = height // 3
ear_y = y - height // 2 + ear_height // 2
ear_width = width // 8
ear_positions = (x - head_size // 4, x + head_size // 4)
for ear_x in ear_positions:
draw_ear(surface, ear_x, ear_y, ear_width, ear_height, color)
# Параметры ног
leg_height = height // 16
leg_y = y + height // 2 - leg_height // 2
leg_width = width // 4
leg_positions = (x - width // 4, x + width // 4)
for leg_x in leg_positions:
draw_leg(surface, leg_x, leg_y, leg_width, leg_height, color)
def main():
"""Основная функция программы."""
draw_hare(screen, 60, 60, 50, 50, WHITE)
pygame.display.update()
clock = pygame.time.Clock()
running = True
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
if __name__ == "__main__":
main()