Processing编程【3】

carrunning

Car myCar;
PFont font;

void setup() {
  size(400, 400);
  myCar = new Car();
  //background(255);
  font = createFont("Courier-Bold", 18);
  textFont(font);
  textAlign(CENTER, CENTER);
}

void draw() {
  background(255);
  myCar.drive();
  myCar.display();
  myCar.make();
  myCar.model();
}

汽车的类:

class Car {
  color c;
  float x;
  float y;
  float speed;
  void make(){
   text("make:Ford.",width/2,height*4/5);};
  void model(){
   text("model:Kuga.",width/2,height*6/8);};

  Car() {
    c = color(0);
    x = width/2;
    y = height/2;
    speed = 2;
  }  

  void display() {
    rectMode(CENTER);
    fill(c);
    rect(x, y, 20, 10);
  }

  void drive() { 
    x = x + speed;
    if (x > width)
      x = 0;
  }
} // end of class

小球弹动

主函数

void setup()
{
  size(500,600);
  b=new ball(80,10,10,(int)random(252));
  b.play();
}
void draw(){
  background(0, 0, 255);
  b.play();
}

小球类


class ball {
  int rad;    // radius
  int x, y;    // center's position of the ball
  int speedx=3; // for horizontal speed
  int speedy=3; // for vertical speed
  color c;    // color of the ball

  // constructor for initialization
  ball(int r, int spx, int spy, int co) { 

    rad=r;
    x=spx;
    y=spy;
    c=co;
  }

  // display a ball with the given x, y and rad, filled with c
  void display() { 
    colorMode(HSB, 360, 255, 100);
    fill(c,255,255);
    noStroke();
    ellipse(x, y, rad*2, rad*2);
  }

  // set the new position according to speedx and speedy 
  // and check if a bounce happens
  void moveBall() { 


    if (y>=height-rad)
    {
      speedx=(int)random(1,4);
      speedy=-3;
    } else if (x>=width-rad) {
      speedx=-3;
      speedy=(int)random(1, 4);
    }
    else if(y<=rad)
    {
    speedy=3;}
    else if(x<=rad)
    {speedx=(int)random(1,4);
  speedy=(int)random(1,4);}

    x=x+speedx;
    y=y+speedy;
  }


  // display a ball and let it move
  void play() { 
    display();
    moveBall();
  }
}

转载于:https://www.cnblogs.com/hitWTJ/p/9865446.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 以下是一些常见的 Processing 编程代码示例: 1. 绘制一个圆形: ``` void setup() { size(400, 400); } void draw() { background(255); ellipse(width/2, height/2, 100, 100); } ``` 2. 绘制一个矩形: ``` void setup() { size(400, 400); } void draw() { background(255); rect(width/2 - 50, height/2 - 50, 100, 100); } ``` 3. 绘制一个三角形: ``` void setup() { size(400, 400); } void draw() { background(255); triangle(width/2, height/2 - 50, width/2 - 50, height/2 + 50, width/2 + 50, height/2 + 50); } ``` 4. 绘制一条直线: ``` void setup() { size(400, 400); } void draw() { background(255); line(width/2 - 50, height/2 - 50, width/2 + 50, height/2 + 50); } ``` 5. 绘制一个文本: ``` void setup() { size(400, 400); } void draw() { background(255); textSize(32); textAlign(CENTER, CENTER); text("Hello, world!", width/2, height/2); } ``` 以上只是一些示例,Processing 还有很多其他的功能,如图像处理、音频处理等等,可以根据自己的需求进行编程。 ### 回答2: Processing是一种用于视觉艺术和计算创作的编程语言和开发环境。它的目标是让编程变得简单并且易于理解,特别适合初学者。下面是一些我认为重要的Processing编程代码的介绍。 1. `size()`函数:该函数用于设置布的尺寸,如`size(800, 600)`表示创建一个宽800像素,高600像素的布。 2. `background()`函数:该函数用于设置布的背景颜色,如`background(255, 255, 255)`表示将布的背景设置为白色。 3. 绘图函数:Processing提供了许多绘图函数,如`rect()`用于绘制矩形,`ellipse()`用于绘制椭圆等。 4. 颜色函数:Processing允许指定各种颜色,可以使用RGB或HSB颜色模式。例如,`fill(255, 0, 0)`表示将填充颜色设置为红色。 5. `if`语句:该语句用于根据条件执行不同的代码块。例如, ``` if(mousePressed) { ellipse(mouseX, mouseY, 50, 50); } ``` 表示当鼠标点击时,在鼠标位置一个椭圆。 6. `for`循环:该循环用于重复执行代码块一定的次数。例如, ``` for(int i = 0; i < 10; i++) { rect(i * 50, 0, 50, 50); } ``` 表示10个宽50像素,高50像素的矩形,每个矩形之间的距离为50像素。 7. 数组:Processing支持数组的使用,可以存储和访问多个相同类型的值。 这些是Processing编程中的一些常见代码示例。通过学习和运用这些代码,我们可以实现各种有趣的效果和交互体验。但是值得注意的是,这只是一个简单的介绍,并不涵盖所有的代码。要深入学习Processing,还需要进一步研究其语法和函数库。 ### 回答3: Processing是一种开发环境和编程语言,用于创造图形、音频和交互式媒体的艺术和设计项目。Processing的核心目标是易于学习和使用,它提供了许多功能强大的库和函数,使程序员可以快速地创建出各种视觉效果和交互效果。 Processing编程代码大全是指包含了各种各样的Processing编程代码的资源集合。这些代码可以是处理图形、动、音频、视频等各种媒体的代码,也可以是用于创建交互式效果的代码,还可以是用于处理数据和算法的代码等等。这个代码大全可以帮助程序员们在自己的项目中快速找到合适的代码片段,并进行适当的修改和调整,以满足自己的需求。 一份Processing编程代码大全可能包含了许多不同的主题和领域的代码,比如绘图和动、音频处理、交互设计、数据可视化等等。在这些代码中,程序员们可以学习到各种编程技巧和方法,比如如何使用不同的库和函数来实现特定的效果,如何处理和转换各种媒体数据,如何设计和实现交互界面等等。通过学习这些代码,程序员们可以不断提升自己的编程能力,并将其运用到自己的艺术和设计项目中。 总而言之,Processing编程代码大全是一个包含大量Processing编程代码的资源集合,它可以帮助程序员们快速找到合适的代码片段,并学习和应用其中的编程技巧和方法。这对于那些希望深入学习Processing编程或者在自己的项目中使用Processing来实现艺术和设计效果的人来说,是一个非常有价值的资源。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值