当前位置:网站首页>[p5.js] actual copy - chess board
[p5.js] actual copy - chess board
2022-07-28 17:49:00 【Zhigan doctrine circle Maojun】

Analyze the law of motion
It can be found by careful observation that , The black squares and white squares are scaled in turn , While the black square returns to its maximum size , The white square began to shrink ,vice versa
Analyze the turning point
1. The expansion and contraction of black squares <-> The expansion and contraction of white squares
2. Zoom in <-> narrow
Code implementation
After finding the turning point , The idea of the code is very clear . First, distinguish two boards —— Blackboard and whiteboard , Place white squares and black squares respectively , That is, define a converted signal variable .
stay draw Method to define a self increasing variable , This variable is related to the transformation of the size of the square . When a color grid shrinks and enlarges to the maximum , Transform a Boolean value , Start scaling the squares of two colors .
let startScale; // Initial size of the square
let endScale; // The limit of square reduction
let returned; // Whether to reply to the initial size
let turnwhite; // Whether to change from black square to white square
function setup() {
createCanvas(320, 320);
startScale = 40;
endScale = 20;
returned = false;
turnwhite = false;
}
function draw() {
if(turnwhite == false) { // White board, black square
if(returned == false){ // narrow
startScale-=0.2;
if(startScale < endScale) {
returned = !returned;
}
}
if(returned == true) { // Zoom in
startScale+=0.2;
if(startScale > 40) {
returned = !returned;
turnwhite = !turnwhite;
}
}
whiteboard(startScale);
}
if(turnwhite == true) { // Blackboard white square
if(returned == false){ // narrow
startScale-=0.2;
if(startScale < endScale) {
returned = !returned;
}
}
if(returned == true) { // Zoom in
startScale+=0.2;
if(startScale > 40) {
returned = !returned;
turnwhite = !turnwhite;
}
}
blackboard(startScale);
}
}
function blackboard(startScale){
background(0, 50); // blackboard
fill(255); // White squares
rectMode(CENTER); // Set the grid anchor in the center
for (let x = 20; x <= 280; x = x + 80) {
for (let y = 20; y <= 280; y = y + 80) { // Odd line
rect(x, y, startScale, startScale);
}
}
for (let x = 60; x <= 320; x = x + 80) {
for (let y = 60; y <= 320; y = y + 80) { // Even number line
rect(x, y, startScale, startScale);
}
}
}
function whiteboard(startScale){
background(255, 50); // Whiteboard
fill(0); // Black Square
rectMode(CENTER);
for (let x = 60; x <= 320; x = x + 80) {
for (let y = 20; y <= 280; y = y + 80) { // Odd line
rect(x, y, startScale, startScale);
}
}
for (let x = 20; x <= 280; x = x + 80) {
for (let y = 60; y <= 320; y = y + 80) { // Even number line
rect(x, y, startScale, startScale);
}
}
}
边栏推荐
猜你喜欢
随机推荐
封装、继承、多态
QT编写串口助手
1.1-注释
abstract、static、final
软件测试的培训机构靠谱吗
2.2-数据类型
【C语言笔记分享】字符函数和字符串函数(建议收藏)
leetcode系统性刷题(二)------贪心、回溯、递归
关于非递归和递归分别实现求第n个斐波那契数
阿里云天池大赛赛题解析(深度学习篇)--阅读笔记1--赛题一
IDEA报错Error running ‘Application‘ Command line is too long解决方案
Arya professional web automated test platform
零基础学习软件测试有什么条件?
USB virtual serial port (CDC) limit speed test
Solve package is not available (for R ve [package 'xxx' is not available (for R version x.y.z) "warning?]
mmdetection3d(3)---网络输出
How to install PS filter plug-in
【机器学习笔记】Regularization : Ridge Regression(岭回归)
.net MVC的理解
PS fast making full screen watermark








