初试processing(2022/3/23)

Processing 常用函数及变量表

函数

基本执行事件函数块:

函数名简单使用描述
void setup()初始化函数,内容只执行一次
void draw()绘图函数,其内容会循环执行
void size()设置画布的大小,可加参数P3D,使用3D渲染器
void mousePressed()在鼠标被点击后才会执行一次的函数块
void mouseReleased()在鼠标释放后会执行一次的函数块
void mouseClicked()在鼠标单击被释放后才会执行一次的函数块
void mouseMoved()在鼠标移动且无任何键按下时才会执行的函数块
void mouseDragged()在鼠标有键按下同时鼠标移动时才会执行的函数块
void mouseWheel(MouseEvent)1.参数为鼠标事件,为系统预定义的一种类型,在鼠标滚轮动时,会将滚动值赋给参数的对应属性,2.可在该函数内对传入参数使用getCount()方法读出该值,上滚为1,下滚为-1,3.同样在滑轮滚动时,该函数执行一次;
void cursor()绘制鼠标图标的形状(可填HAND,ARROW,CROSS,WAIT,MOVE,TEXT等)及位置

绘图命令函数:

函数名简单使用描述
rect/ellipse/triangle/line……根据名字就可以知道的绘画函数
translate/rotate/scale转换坐标原点(平移)/坐标系旋转/坐标轴放缩
popMatrix/pushMatrix保存坐标系和恢复坐标系

常见的系统预设变量:

屏幕变量:

变量类型意义
widthint窗口宽度像素值
heightint窗口高度像素值
frameCountint运行的帧数
frameRateint每秒运行的帧数
screen.width整个屏幕宽度的像素值
screen.height整个屏幕高度的像素值

键盘变量:

变量类型意义
keyPressedboolen键盘是否被敲击
keychar最近一次键盘的敲击
keyCodeint键盘敲击的数字代码

鼠标变量:

变量类型意义
mousePressedboolen鼠标是否在运行屏幕内被点击
mouseButtonint(与其匹配的值有LEFT和RIGHT)鼠标哪个键被点击
mouseX(/Y)int当前鼠标点的横(纵)坐标值
pmouseX(/Y)int上次鼠标点的横(纵)坐标值

一些注意

  • 建议使用对象先声明,后再new对象以初始化。

    如下:

    import controlP5.*;
    ControlP5 cp5;
    

小例子

初学processing,写个小例子练练手(上面有些欠缺的,以后再补充了)

import controlP5.*;
Small_Ball first_ball;
boolean motion=true;
ControlP5 cp5;
 void setup()
 {
   colorMode(RGB,255,255,255);
   size(800,800,P2D);
   background(255);
   cp5 = new ControlP5(this);
   gui();
   noStroke();
   first_ball=new Small_Ball(width/2,height/2,40);
   first_ball.start();
 }
 void draw()
 {

  if(motion)
  {
     color temp;
     background(138,139,138);  
     temp = color(random(100,255),random(100,255),random(100,255));
     first_ball.move(temp);
  }
 }

void gui()
{
  cp5.addToggle("Stop_Start")
      .setPosition(0,0)
      .setSize(100,50)
      .setValue(true)
      .setColorLabel(color(28,234,43))
      .setLabel("stop/start");
}
void Stop_Start(boolean flag)
{
 motion = flag; 
}


 class Small_Ball
 {
   float x,y,r,speed_x,speed_y;
   Small_Ball(float x,float y,float r)
   {
     this.x = x;
     this.y = y;
     this.r = r;    
   }
   void start()
   {
     ellipse(x,y,r,r);
     speed_x = random(10);
     speed_y = random(10);
   }
   void move(color _color)
   {
     x +=speed_x;
     y +=speed_y;
     if( ( ((x+r)>width)&&(speed_x>0) ) || ( (x-r<0)&&(speed_x<0) ) )
     {
       speed_x=-speed_x;
       fill(_color);  
     }
     if( ( ((y+r)>height)&&(speed_y>0) ) || ( (y-r<0)&&(speed_y<0) ) )
     {
       speed_y=-speed_y;
       fill(_color);
     }
     ellipse(x,y,r,r);
   }
 }

效果如下:

jump_ball

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值