作者 主题: 怎样让直线在屏幕里上下弹?  (阅读 4243 次)

Homway

  • Newbie
  • *
  • 帖子: 12
怎样让直线在屏幕里上下弹?
« 于: 十一月 22, 2011, 10:16:58 下午 »
程序代码
int N = 20;
int W = 400,
    H = W;
int E = 20;
int L = 200;
int Y = H/2;
color Yellow = color(255, 255, 0);
int dy = 2;

void setup() {
  size(W,H);
  smooth();
  fill (Yellow);
}
   
void DrawLines() {
  stroke(Yellow);
  line(E,L,E+200,L);
}

void draw() {
  DrawLines();
 
 }


请问在void draw里需要添加什么才能让画出来的直线在屏幕里上下弹?
« 最后编辑时间: 十一月 22, 2011, 10:25:23 下午 作者 hongweilala »

RavenKwok

  • Sr. Member
  • ****
  • 帖子: 277
  • Artist/ Animator/ Coder/ Cynical Asshole
Re: 怎样让直线在屏幕里上下弹?
« 回复 #1 于: 十一月 29, 2011, 01:27:10 上午 »
需要一个if 结构来判断线段是否到达场景的边缘,并在做出相应的执行内容的变更。

Homway

  • Newbie
  • *
  • 帖子: 12
Re: 怎样让直线在屏幕里上下弹?
« 回复 #2 于: 十一月 29, 2011, 08:25:59 下午 »
需要一个if 结构来判断线段是否到达场景的边缘,并在做出相应的执行内容的变更。
已解决,谢谢
程序代码
int N = 20;
int W = 400,
    H = W;
int E = 20;
int L = 200;
int Y = H/2;
color Yellow = color(255,255,0);
int dy = 2;
void setup() {
  size(W,H);
  smooth();
}
void draw() {
  background (Yellow);
  N = N + dy;
  if ((N > Y) || (N < 0)) {
    dy = dy * -1;
  }
  stroke(0);
 fill(175);
 line(E,N,L,N);
}


Tags: