美文网首页Learning Processingprocessing
[Processing] Lesson Two(4~5) 精华

[Processing] Lesson Two(4~5) 精华

作者: 科大侠 | 来源:发表于2016-01-17 21:07 被阅读222次

(用markdown记笔记的时候代码格式复制不进来,谁可以告诉我怎么贴代码格式可以更贴近真实一些,拜谢!)

2.变量

2.1 变量种类(type)

int:整数
float:小数
boolean:对或错

注意

每个变量都要有种类(type),这样电脑才知道给这个变量分配多少存储空间;

每个变量都要有名称。

2.2 变量名称

系统变量(已存在)

width:窗口宽度(像素);
height:窗口高度(像素);
frameCount:运行过的帧数;

自定义变量

如果较长最好取成"circleX","widthA"之类,不要用"Circlex"这种,因为系统变量很多事首字母大小写的。

练习:圆圈变大变色
Paste_Image.png

float circleX1=25;
float circleY1=25;
float circleX2=75;
float circleY2=25;
float circleX3=25;
float circleY3=75;
float circleX4=75;
float circleY4=75;
float circleSize=0;
float change=0.5;

void setup(){
size(100,100);
}

void draw(){
background(255);
fill(circleX1);
ellipse(circleX1,circleY1,circleSize,circleSize);

fill(circleX2);
ellipse(circleX2,circleY2,circleSize,circleSize);

fill(circleX3);
ellipse(circleX3,circleY3,circleSize,circleSize);

fill(circleX4);
ellipse(circleX4,circleY4,circleSize,circleSize);

circleSize=circleSize+change;
}

2.3 随机变量

random():注意random默认设置是小数变量。
random(255) 意思是 random(0,255);
例:
float w = random(1,100);
int x = int(random(1,100));要给整数设置随机变量范围时需要用 "int" 对其加以限制。

羞耻的练习

一直向上但在x轴随机位置闪烁的程序生物Pia(截图是动画问题)
思考:如何让Pia在x轴上闪烁的不那么频繁


Paste_Image.png

float PiaX;
float PiaY;

float r;
float g;
float b;

void setup(){
size(200,200);
PiaY=0;
}

void draw(){
background(255);
PiaX = random(width);

//Pia's body
fill(150);
triangle(PiaX,200+PiaY,PiaX-10,160+PiaY,PiaX+10,160+PiaY);

//Pia's ear
fill(255);
line(PiaX-20,150+PiaY,100,150+PiaY);
ellipse(PiaX-20,150+PiaY,10,10);
ellipse(PiaX+20,150+PiaY,10,10);

//Pia's head
ellipse(PiaX,150+PiaY,20,20);

//Pia's eyes
r = random(255);
g = random(255);
b = random(255);
fill(r,g,b);
rectMode(CENTER);
rect(PiaX-5,145+PiaY,5,5);
rect(PiaX+5,145+PiaY,5,5);
PiaY = PiaY-1;
}

2.4 条件判定

15比20大——false;
5等于5——true;

用途

对变量使用条件判定来让系统可以选择符合判定的不同路径运行

2.5 Conditionals:if, else, else if

(翻译不准确还是用原文标题吧)

例子

Processing 中,我们有这样逻辑的语句:
If the mouse is on the left side of the screen, draw a rectangle on the left side of the screen.

正式一些,程序可以写成
if (mouseX < width/2){
fill(255);
rect(0,0,width/2,height/2);
}

Paste_Image.png
通俗化用法

只有 "if" 的:
if (boolean expression) {
// code to execute if boolean expression is true
}

加上 "else" 的情况(2种情况)
if (boolean expression) {
// code to execute if boolean expression is true
} else {
// code to execute if boolean expression is false
}

加上 "else if" 的情况(3种情况以上)
if (boolean expression #1) {
// code to execute if boolean expression #1 is true
} else if (boolean expression #2) {
// code to execute if boolean expression #2 is true
} else if (boolean expression #n) {
// code to execute if boolean expression #n is true
} else {
// code to execute if none of the above
// boolean expressions are true
}

2.6 逻辑操作

(不多叙述,或与非都比较熟悉了)
AND : &&
OR:||
NOT:!

2.7 反弹球编程练习

Tips

speed = speed * -1;(碰到窗口边时,速度反向等大)
speed = speed * 0.95;(碰到窗口边时,速度反向略小,模拟真实环境)
button = ! button; (用作switch,条件判定后false = true / true = false)

2.8 设置变量为不同状态,方便条件判定

通用例

int state = 0;

if (state == 0){
if(//条件){
state = 1;
}
}
现在state就从0变成了1。

练习 Bouncing gravity ball

Paste_Image.png

//Bouncing ball
float x=0;
float y=0;
float speedY=0;
float gravity=0.3;
float speedX=3;
boolean button=false;
int r=0;
int g=255;
int b=0;
int change=1;

void setup(){
size(1200,400);
}

void draw(){
background(255);

//set button
if (button){
//simulate reality
rect(x,y,10,10);
x=x+speedX;
y=y+speedY;
speedY=speedY+gravity;
if (x>width || x<0){
speedX=speedX-1;
}
if (y>height){
speedY=speedY
-0.95;
}
}

if (mousePressed){
button=!button;
}

//change color
fill(r,g,b);
if(mouseX<width/2){
r=r+change;
}else if(mouseX>width/2){
r=r-change;
}

}

相关文章

网友评论

    本文标题:[Processing] Lesson Two(4~5) 精华

    本文链接:https://www.haomeiwen.com/subject/erhkkttx.html