Files
lab3/rabbit_1.py

105 lines
3.5 KiB
Python

import pygame
from pygame.draw import ellipse, circle
pygame.init()
FPS = 30
screen = pygame.display.set_mode((400, 400))
class Hare:
"""Класс для представления зайца"""
def __init__(self, x, y, width, height, color):
"""
Инициализация зайца
x, y - координаты центра изображения
width, height - ширина и высота изображения
color - цвет зайца
"""
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
def draw_body(self, surface):
"""Рисует тело зайца"""
body_width = self.width // 2
body_height = self.height // 2
body_y = self.y + body_height // 2
ellipse(surface, self.color,
(self.x - body_width // 2, body_y - body_height // 2,
body_width, body_height))
def draw_head(self, surface):
"""Рисует голову зайца"""
head_size = self.height // 4
head_y = self.y - head_size // 2
circle(surface, self.color, (self.x, head_y), head_size // 2)
return head_size
def draw_ears(self, surface, head_size):
"""Рисует уши зайца"""
ear_height = self.height // 3
ear_y = self.y - self.height // 2 + ear_height // 2
ear_width = self.width // 8
ear_offset = head_size // 4
ellipse(surface, self.color,
(self.x - ear_offset - ear_width // 2, ear_y - ear_height // 2,
ear_width, ear_height))
ellipse(surface, self.color,
(self.x + ear_offset - ear_width // 2, ear_y - ear_height // 2,
ear_width, ear_height))
def draw_legs(self, surface):
"""Рисует ноги зайца"""
leg_height = self.height // 16
leg_y = self.y + self.height // 2 - leg_height // 2
leg_width = self.width // 4
leg_offset = self.width // 4
ellipse(surface, self.color,
(self.x - leg_offset - leg_width // 2, leg_y - leg_height // 2,
leg_width, leg_height))
ellipse(surface, self.color,
(self.x + leg_offset - leg_width // 2, leg_y - leg_height // 2,
leg_width, leg_height))
def draw_eyes(self, surface, head_size):
"""Рисует глаза зайца"""
eye_size = head_size // 8
eye_y = self.y - head_size // 2 - head_size // 8
eye_offset = head_size // 6
eye_color = pygame.Color('black')
circle(surface, eye_color, (self.x - eye_offset, eye_y), eye_size)
circle(surface, eye_color, (self.x + eye_offset, eye_y), eye_size)
def draw(self, surface):
"""Полная отрисовка зайца"""
self.draw_body(surface)
head_size = self.draw_head(surface)
self.draw_ears(surface, head_size)
self.draw_legs(surface)
self.draw_eyes(surface, head_size)
# Заливаем фон
screen.fill(pygame.Color('white'))
# Создаем и рисуем зайца
hare = Hare(200, 200, 200, 300, pygame.Color('gray'))
hare.draw(screen)
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()