codeFlowType:
codeWrite
codeFlowLang:
Python
Создайте окно с черным фоном и размером screen_width = 640 screen_height = 480. Нарисуйте фигуры: − треугольник; − пятиугольник; − горизонтальная линия; − вертикальная линия; − звезда; − окружность с квадратом внутри.
import pygame
pygame.init()
screen_width = 640
screen_height = 480
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Shapes")
clock = pygame.time.Clock()
background_color = (0, 0, 0)
shape_color = (255, 255, 255)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill(background_color)
# Draw shapes
# Triangle
pygame.draw.polygon(screen, shape_color, [(320, 100), (200, 300), (440, 300)])
# Pentagon
pygame.draw.polygon(screen, shape_color, [(120, 200), (160, 180), (200, 200), (200, 250), (140, 250)])
# Horizontal line
pygame.draw.line(screen, shape_color, (80, 400), (560, 400), 3)
# Vertical line
pygame.draw.line(screen, shape_color, (320, 60), (320, 400), 3)
# Star
pygame.draw.polygon(screen, shape_color, [(320, 30), (340, 80), (400, 70), (360, 110), (380, 160),
(320, 130), (260, 160), (280, 110), (240, 70), (300, 80)])
# Circle with square inside
pygame.draw.circle(screen, shape_color, (500, 300), 100)
pygame.draw.rect(screen, shape_color, (450, 250, 100, 100), 3)
pygame.display.flip()
clock.tick(60)
pygame.quit()
Результат: