当前位置:网站首页>【p5.js】实战临摹——国际象棋盘
【p5.js】实战临摹——国际象棋盘
2022-07-28 16:34:00 【至肝主义圈毛君】

分析运动规律
通过仔细观察可以发现,黑色方格与白色方格轮流放缩,在黑色方格回复到最大尺寸的同时,白色方格开始缩小,vice versa
分析转折点
1. 黑方格的放缩 <-> 白方格的放缩
2. 放大 <-> 缩小
代码实现
发现转折点后,代码的思路就很清晰了。首先区分两块板——黑板和白板,分别放置白方格和黑方格,即定义一个转换的信号变量。
在draw方法中定义一个自增变量,该变量与方格的大小变换相关。当一种颜色方格缩小又放大到最大时,转变一个布尔值,使两一种颜色方格开始缩放。
let startScale; // 方格初始大小
let endScale; // 方格缩小极限
let returned; // 是否回复初始大小
let turnwhite; // 是否从黑方格转白方格
function setup() {
createCanvas(320, 320);
startScale = 40;
endScale = 20;
returned = false;
turnwhite = false;
}
function draw() {
if(turnwhite == false) { // 白板黑方格
if(returned == false){ // 缩小
startScale-=0.2;
if(startScale < endScale) {
returned = !returned;
}
}
if(returned == true) { // 放大
startScale+=0.2;
if(startScale > 40) {
returned = !returned;
turnwhite = !turnwhite;
}
}
whiteboard(startScale);
}
if(turnwhite == true) { // 黑板白方格
if(returned == false){ // 缩小
startScale-=0.2;
if(startScale < endScale) {
returned = !returned;
}
}
if(returned == true) { // 放大
startScale+=0.2;
if(startScale > 40) {
returned = !returned;
turnwhite = !turnwhite;
}
}
blackboard(startScale);
}
}
function blackboard(startScale){
background(0, 50); // 黑板
fill(255); // 白方格
rectMode(CENTER); // 设置方格锚点在中心
for (let x = 20; x <= 280; x = x + 80) {
for (let y = 20; y <= 280; y = y + 80) { // 奇数行
rect(x, y, startScale, startScale);
}
}
for (let x = 60; x <= 320; x = x + 80) {
for (let y = 60; y <= 320; y = y + 80) { // 偶数行
rect(x, y, startScale, startScale);
}
}
}
function whiteboard(startScale){
background(255, 50); // 白板
fill(0); // 黑方格
rectMode(CENTER);
for (let x = 60; x <= 320; x = x + 80) {
for (let y = 20; y <= 280; y = y + 80) { // 奇数行
rect(x, y, startScale, startScale);
}
}
for (let x = 20; x <= 280; x = x + 80) {
for (let y = 60; y <= 320; y = y + 80) { // 偶数行
rect(x, y, startScale, startScale);
}
}
}
边栏推荐
- 2021 National Undergraduate data statistics and Analysis Competition
- Can‘t use an undefined value as an ARRAY reference at probe2symbol
- 软件测试就业前景如何?
- Technical aspects passed easily, HR: those with only three years of experience in large factories are not worth 20K
- 软件测试培训两个月靠谱吗?
- 小白必看的软件测试发展路线
- Visual Object Class介绍PASCAL VOC数据集
- hgu95av2.在线安装失败
- Distinguish between the export of ES6 and the module.exports of nodejs
- 编译原理学习笔记3(自上而下语法分析)
猜你喜欢

MySQL optimization summary

Factor in R

Visual Object Class介绍PASCAL VOC数据集

R language drawing / drawing / drawing 2

In depth sharing of Ali (ant financial) technical interview process, with preliminary preparation and learning direction
@RequestMapping详解

hgu95av2.在线安装失败

MySQL高级-MVCC(超详细整理)

谈谈你知道的发布上线(一)

关于非递归和递归分别实现求第n个斐波那契数
随机推荐
生信人的20个R语言习题
R language drawing / drawing / drawing 2
在airtest中使用ocr反向识别文本内容
新手通过自学转行软件测试难度大吗?
禅道项目管理软件,敏捷开发团队不可或缺的工具
零基础学习软件测试有什么条件?
Ggplot2 map
Punctual atomic serial port protocol
[阅读笔记] For Paper:R-CNN系列的三篇论文总结
【C语言进阶】——剖析入微数据在内存中的存储 【下】(浮点数存储)
Division optimization of JS decimal calculation on the Internet
软件测试和软件开发应该怎么选择?
Arya-专业web自动化测试平台
PCA 报错Error in eigen(crossprod(t(X), t(X)), symmetric = TRUE) : ‘x‘里有无穷值或遗漏值
解决Package is not available (for R ve【PACKAGE ‘XXX’ IS NOT AVAILABLE (FOR R VERSION X.Y.Z)” WARNING?】
R语言画图/绘图/作图2
es6 Promise
Interviewer: the actual record of algorithm question brushing.pdf I can't even answer it
关于非递归和递归分别实现求第n个斐波那契数
软件测试前景如何?该如何进行学习呢?