Рисуем шахматную доску на P5 с Python
В этой заметке мы нарисуем шахматную доску и расставим на ней фигуры. Для работы будем использовать фреймворк на P5 и язык программирования Python.
Копия видеоролика есть в телеграм канале Блог учителя Информатики 🚀
from p5py import *
run()
size(800, 600)
background(255)
# your code goes here
def place_figure(figure, row, col, size=42):
textSize(size)
textAlign(CENTER)
fill(0)
x = col * cell_size + offset_x - cell_size / 2
y = row * cell_size + offset_y - cell_size * 0.25
text(figure, x, y)
cell_size = 50
rows, cols = 8, 8
offset_x, offset_y = 60, 60
for row in range(rows):
for col in range(cols):
if (row + col) % 2 == 0:
fill('Bisque')
else:
fill('SaddleBrown')
x = col * cell_size + offset_x
y = row * cell_size + offset_y
rect(x, y, cell_size, cell_size)
# Добавляем цифры
textSize(18)
textAlign(CENTER)
fill(0)
for row in range(rows):
x = offset_x - 10
y = row * cell_size + offset_y + cell_size * 0.65
text(rows - row, x, y)
# Добавляем буквы
letters = 'ABCDEFGH'
for col in range(cols):
x = col * cell_size + offset_x + cell_size / 2
y = rows * cell_size + offset_y + cell_size * 0.35
text(letters[col], x, y)
place_figure('♔', 1, 1)
place_figure('♖', 3, 2)
place_figure('♝', 4, 6)
place_figure('♞', 5, 4)Python












