这个东西挺好用的,简单,比pygame作图更有意思。
C类语言是函数体,从main函数开始执行的,python可以不写__main__函数。
在我现在有限的学习生涯中,我只能拿这两个举例。
processing的自由度像级了python,但是比后者更好用。类似写单片机程序时,整个程序会不断得执行。但和单片机的不一样的是,processing并非执行完毕了。只是陷入了一个循环中。
在我寒假前,学习的几天内我烦了许久。对于setup()、draw()这些函数,不写也行,但是写上去了就会有默认的执行规定。我想,是不是对于processing ,一些规定了执行顺序的函数可以被忽视(
听说是基于java的。。。母鸡啊
定义一个Ball类,随机初始化位置,定义两个球的实例对象。然后进行一些计算和绘制操作。
processing中类的定义和函数的定义没有顺序
processing成员函数只能写在类里,没有什么声明之说
这样虽然不能像C艹一样整个乱七八糟,但是这样的简洁实在让人舒服。所以这个语言也可以用于硬件编程(arduino,即单片机的二次开发),虽然这种编程成本太高,但是其简单的操作必定是未来硬件编程的主流。
Ball b1,b2;//变量一般都定义成全局的
void setup(){ //程序默认入口,只执行一次
size(400,400);//窗口宽高 必须是常量
smooth();
frameRate(30);
//initialize balls
b1 = new Ball(64);
b2 = new Ball(32);
}
void draw(){
background(#00FF00,255);
fill(#FFF000);
//move and display balls
b1.move();
b2.move();
b1.display();
b2.display();
if( b1.intersect(b2)){
//println("The circles are intersecting!");
b1.hightlight();
b2.hightlight();
}
b1.display();
b2.display();
}
class Ball{
float r;//radius
float x,y;//location
float xSpeed,ySpeed;//speed
color c=color(100,50);
//constructor
Ball(float tempR){
r=tempR;
x=random(width);
y=random(height);
xSpeed=random(-5,5);
ySpeed=random(-5,5);
}
void move(){
x+=xSpeed;//increment
y+=ySpeed;
//check horizontal edges
if(x>width||x<0)xSpeed*=-1;
//check vertical edges
if(y>height||y<0)ySpeed*=-1;
}
//draw the ball
void display(){
stroke(0);//marigin of object
fill(c);
ellipse(x,y, r* 2,r*2);
c=color(100,50);
}
boolean intersect(Ball b){
float distance = dist(x,y,b.x,b.y);//calculate distance by a default function
if(distance < r + b.r) //compare distance to r1 + r2
return true;
return false;
}
void hightlight(){
c=color(0,150);
}
}
Catcher catcher;//one catcher object
Timer timer;//one timer object
Drop[] drops;//an array of drop objects
int totalDrops=0;
PFont myFont;//one font
void setup(){
size(400,400);
smooth();
catcher = new Catcher(32);//create the catcher with a radius of 32
drops= new Drop[1000];//create 1000 spots in the array
timer=new Timer(900);//create a timer that goes every 2 seconds
timer.start();///starting the timer
//create a font by me and here snippet is not important
myFont=createFont("隶书",20);
textFont(myFont);//initialize the font
textAlign(CENTER);//对齐格式
}
void draw(){
background(255);//reset and clear the screen
catcher.setLocation(mouseX,mouseY);//set catcher location
catcher.display();//display the catcher
//check the timer
if(timer.isFinished()){
//deal with raindrops
//initialize one drop
drops[totalDrops]=new Drop();
//increment totalDrops
totalDrops++;
//if we hit the end of the array
if(totalDrops >= drops.length)totalDrops=0;//start over
println("2 seconds have passed");
timer.start();
}
//move and display all rain drops
for(int i=0;i<totalDrops;i++){
drops[i].move();
drops[i].display();
if(catcher.intersect(drops[i]))drops[i].caught();
}
}
class Drop{
float x;
float y;
float speed;
color c;
float r;
Drop(){
r=8;
x=random(width);
y= -r*4;
speed=random(1,5);
c=color(50,100,150);
}
//move the raindrop down
void move(){
y+=speed++;
}
//check is it hits the bottom
boolean reachBottom(){
if(y > height + r*4)return true;//if we go a little beyond the bottom
return false;
}
//display the raindrop
void display(){
noStroke();
fill(c);
for(int i=2;i<r;i++)ellipse(x,y+i*4 ,i*2,i*2);//center location and width and height of the ellipse
}
//if the drop is caught
void caught(){
speed=0;//stop it from moving by setting speed equal to zero
y-=1000;//set the location to somewhere way off-screen
}
}
class Catcher{
int sumOfcaught;
float r;
color c;
float x;
float y;
Catcher(float tempR){
r=tempR;
c=color(50,10,10,150);
x=0;
y=0;
sumOfcaught=0;
}
void setLocation(float tempX,float tempY){
x=tempX;
y=tempY;
}
//a function that returns true or false based if the catcher intersects a raindrop
boolean intersect(Drop d){
float distance = dist(x,y,d.x,d.y);
if(distance < r + d.r){
sumOfcaught++;
c=color(random(255),random(255),random(255));
return true;
}
return false;
}
void display(){
textSize(20);
text("Caught drops:",10,50);
textSize(50);
text(sumOfcaught,150,70);
stroke(0);
fill(c);
ellipse(x,y,r*2,r*2);//center location and width and height of the ellipse
}
}
class Timer{
int savedTime;//when timer started
int totalTime;//how long timer should last
Timer(int tempT){
totalTime=tempT;
}
void start(){
savedTime=millis();
}
boolean isFinished(){
//check how much time has passed
if(millis() - savedTime > totalTime)return true;
return false;
}
}
processing中可供使用的3D绘制模式有P3D、OPENGL
下面这个是使用P3D绘制两个4面体的示例。
采用的是以左上角为原点的左手坐标系,X向右,Y向下,Z向屏幕外
translate()将坐标系原点平移到某个坐标上去 … \dots …
float theta =0.0;
void setup(){
size(200,200,P3D);//3D render engine
}
void draw(){
background(144);
theta+=0.01;
translate(100,100,0);
rotateX(theta);
rotateY(theta);
drawPyramid(50);
//translate the scene again
translate(50,50,20);
drawPyramid(10);
}
void drawPyramid(int t){
stroke(0);
/*
this pyramid has 4 sides,each drawn as a separate trangle .each side has 3 vertices ,making up a triangle shape
the parameter t determines the size of the the pyramid
*/
beginShape(TRIANGLES);//use the triangles mode to create shape
fill(150,0,0,127);
vertex(-t,-t,-t);
vertex(t,-t,-t);
vertex(0,0,t);
fill(0,150,0,127);
vertex(t,-t,-t);
vertex(t,t,-t);
vertex(0,0,t);
fill(0,0,150,127);
vertex(t,t,-t);
vertex(-t,t,-t);
vertex(0,0,t);
fill(150,0,150,127);
vertex(-t,t,-t);
vertex(-t,-t,-t);
vertex(0,0,t);
endShape();
}
参考:《Learning Processing》