-
楼主作为一个纯粹的工科男,
p>
却阴差阳错到了机关坐办公室,
沦落为天天写材料为生,
内心实在是千万个草泥马在狂奔。
为了保持工科男的本性,
也为了不被这些无聊的材料压成
神经病,决定给自己找点乐趣。去年先是接触了
一下
arduino
,编程完成了一个遥控小车,
利用
appinventor
编了个手机遥控软件,
基本实现了在手机屏幕上画图形,小车就在地上按
画的路径行走。开始心挺大,想进一步
学习做个小四轴,
自平衡小车,激光雕刻机等,但
是由于现在每天下班第一任务是陪孩子玩,
加之学习硬件还是比较烧钱,<
/p>
结果就荒废了。
上
个月无意中发现了
p>
processing
,又看了一些大神的作品,觉得很有意思,而
且学习软件比学
习硬件时间上比较灵活(比如每天中午可以学习半小时)
,所以决定学习一下,丰富一下自
己的业余生活。为了避免再次半途而废特开此
贴记录过程(不过还是很难说,哈哈)
。
今天先补上前段时间零星学习的内容:
学习用教材:
《爱上
processing
< br>》
、
《代码本色》
。
一、已学习的常用简单命令
1.
设置屏幕尺寸:
size
(宽,高)
;
2.
画点:
point
(
x
,
y
)
;
3.
划线:
line
(
x1,y1,x2,y2
)
;
4.
画
三角形:
triangle
(
x1,y
1,x2,y2,x3,y3
)
;
5.
四边形:
quad
(
x1,y1,x2,y2,x3,y3,x4,y
4
)
;
6.
角度换算为弧度:
radians
(角度)
;
7.
长方形:
rect
(
x
,
y
,宽,高)
;
8.
椭圆
:
ellipse(x,y,
宽,高
)
;
//
目前用这个命令用的最多,嘿嘿
9.
平滑曲线:
smooth
()
;
10.
关闭平滑曲线:
noSmooth();
11.
设置线宽:
strokeWeight(x);
12.
背景颜色:
< br>background(x,x,x);//
只填一个参数为灰度,
0
为黑
255
为白;填三个
参
数为
RGB
颜色
13.
fill
(
x,x,x,x
)
//<
/p>
颜色同上,第四个参数为透明度
14.
鼠标当前坐标:
mouseX,mouseY
15.
上一帧鼠标坐标:
pmouseX,pmouseY
16.
测量两点之间的距离:
dist
(
x1,y1,x2,y2
)
;
17.
mousePressed:
布尔量,鼠标按下后为真(
true
,
false
)<
/p>
18.
mo
useButton:
返回值:
LEFT,CENTER,RI
GHT
用来判断哪个键按下
p>
。
。
。
。
。
还有一些图形命令,目前用的还很少,暂时没学。
二、编程格式
1.
< br>首先创建不在
setup
()和
draw
()函数的变量(全局变量)
()
{
…
}
;
内的命令执行一遍
(){
…
};
内的命令持续执行
三、面向对象编程
由于
《代码本色》
完全用的是面向对象的编程方式,
而本
人大学时学的计算机语言是老掉牙
的
FORTRAN
,基本上就没听说过面向对象的编程,只有重新学习面向对象的编程方法,不
过学习了两天,
试着编了几个小程序,发现这种编程方法确实很强大。
这里就照搬一下
《爱
上
pro
cessing
》里的描述
,
具体的还
是得自己编几个程才能体会:
class
xxx
//1.
创建一个类
{
int x
;
//2.
添加值域
。
。
。
float y;
xxx
(参数
1
,参数
2.<
/p>
。
。
。
。
)
//3.
添加构建函数
{
…
..
}
void xxxx()
//4.
添加方法
{
…
..
}
…
void yyyyy()
//
{
…
..
}
}
5.
用类声明一个对象:
xxx aa
;
6.
创建对象并传递参数
aa=new xxx
;
先发几个前段时间练习的小程序:
1.
弹球:一个小球在屏幕里弹来弹去:
class BouncBall
{
float x;
float y;
int d;
float xspeed;
float yspeed;
BouncBall(float
tempX,float tempY
,int tempD,float
tempXspeed,float tempYspeed)
{
x=tempX;
y=tempY;
d=tempD;
xspeed=tempXspeed;
yspeed=tempYspeed;
}
void move()
{
x+=xspeed;
y+=yspeed;
2. noise
if(x>width||x
{
xspeed=xspeed*-1;
}
if(y>height||y
{
yspeed=yspeed*-1;
}
}
void display()
{
ellipse(x,y,d,d);
}
}
BouncBall ball;
void setup()
{
size(480,480);
smooth();
ball=new
BouncBall(width/2,height/2,20,2,2.5);
}
void draw()
{
();
fill(255,10);
rect(0,0,width,height);
fill(255,0,0);
y();
}
加强版弹球,想起以前玩的一个弹砖块的游戏,试着编了一个不完全版的
class BouncBall
{
float x;
float y;
int d;
float xspeed;
float yspeed;
BouncBall(float
tempX,float tempY
,int tempD,float
tempXspeed,float tempYspeed)
{
x=tempX;
y=tempY;
d=tempD;
xspeed=tempXspeed;
yspeed=tempYspeed;
}
void move(bangbang bang1)
{
x+=xspeed;
y+=yspeed;
if(x>width||x
{
xspeed=xspeed*-1;
}
p>
if((y>(height-30)&&(mouseX
{
xspeed=xspeed+(mouseX-pmouseX)*0.1;
yspeed=yspeed*(-1);
}
if(y
{
yspeed=yspeed*(-1);
}
}
void
crash(brick bb,BouncBall BB)
{
if(dist((bb.x+b
b.w/2),(bb.y+bb.h/2),BB.x,BB.y)<(23+BB.d/2))
{
=*-1;
bb.x=0;
bb.y=0;
bb.w=0;
bb.h=0;
}
}
void display()
{
ellipse(x,y,d,d);
}
}
class bangbang
{
int ShapeWidth;
//int
ShapeHeight;
//float x;
//float y;
//int x;
bangbang(int tempW)
{
//x=tempx;
ShapeWidth=tempW;
}
void display()
{
fill(0);
rect(mouseX,height-20,ShapeWidth,10);
}
}
class brick
{
float x;
float y;
float w;
float h;
brick(float tmpx,float
tmpy,float tmpw,float tmph)
{
x=tmpx;
y=tmpy;
w=tmpw;
h=tmph;
}
void display()
{
fill(100);
stroke(255);
rect(x,y,w,h);
}
}
BouncBall ball;
bangbang
bang;
brick[] bks;//=new brick[24];
void setup()
{
size(480,480);
smooth();
ball=new
BouncBall(width/2,height/2,20,2,2.5);
bang=new bangbang(120);
bks=new
brick[24];///
int k=0;
for(int i=0;i<6;i++)
{
for(int
j=0;j<4;j++)
{
bks[k]=new
brick(i*80,j*20,80,20);
k++;
}
}
}
void draw()
{
for(int i=0;i<;i++)
{
bks[i].display();
(bks[i],ball);
}
(bang);
fill(255,10);
noStroke();
rect(0,0,width,height-20);
noStroke();
fill(255,255);
rect(0,height-20,width,20);
fill(255,0,0);
//stroke(255);
y();
y();
}
3.
学习使用
()函数,修改自《代码本色》中的一个例子:一个随机游走的小球,颜
色随时间变化
class
Walker{
float
x,y;
float
tx,ty;
float
r,b,g;
float
tr,tb,tg;
Walker()
{
tx=0;
ty=10000;
tr=0;
tb=1000;
tg=2000;
}
void step()
{
x=map(noise(tx),0,1,0,width);
y=map(noise(ty),0,1,0,height);
tx+=0.01;
ty+=0.01;
}
void c()
//color
{
r=map(noise(tr),0,1,0,255);
b=map(noise(tb),0,1,0,255);
g=map(noise(tg),0,1,0,255);
tr+=0.01;
tb+=0.01;
tg+=0.01;
}
}
Walker w;
void setup()
{
size(480,480);
w=new Walker();
}
void draw()
{
();
w.c();
fill(w.r,w.b,w.g);
ellipse(w.x,w.y,20,20);
} <
/p>
4.
学习《代码本色》后编的两个相互吸引运动的小球,在边界会
转到另一边去:
class Mover
{
PVector location1,location2;
-
-
-
-
-
-
-
-
-
上一篇:工控专业英语
下一篇:processing 学习1~5