Processing【5】终章不存在

彩色棋盘实验报告

一. 思路:

  1. 初始化棋盘格子,初始化最上和最下两边的颜色参数。
  2. 监测鼠标点击的位置的颜色和状态(0,-1,1)。
  3. 定义ball和square,包含了颜色,半径,位置(x,y)属性(property),并且定义了绘画ball和square的方法(method)。
  4. 在格子边缘确定这个颜色覆盖在哪个格子上。计算出鼠标点击的位置和该位置所占百分比,从而确定格子的位置i,j。
  5. 每次draw()函数调用时,刷新格子的状态。

二. 实现过程:

  1. 使用setcolor()来设置初始的上下两边的颜色参数,setNumbers()刷新ifUsed[i][j]数组。
  2. drawcirle()和drawblocks()设置上下的颜色。drawgrid()设置格子。
  3. 监测点击所选择的位置是否有填充颜色。具体利用ifUsed[i][j]数组和color[15][15]数组来确定。
  4. drawMain()刷新颜色和棋盘。
  5. mousePressed(),mouseReleased(),keyPressed()来检测键盘和鼠标的状态。点击鼠标则拖动方块,按下键盘空格则刷新界面。

三. 代码:见工程文件。

//Dear Reader:CopyRight by qingling


class rec {
  color reccolor;
  int positionX;
  int positionY;
  float radius=height/num-3.8;

  rec(color bc, 
    int X, int Y) {
    reccolor=bc;
    positionX=X;
    positionY=Y;
  }
  void drawrec() 
  { 
    noStroke();
    fill(reccolor);
    rectMode(CENTER);
    rect(mouseX, mouseY, height/num-3.8, height/num-3.8);
  }

  void drawgrid() //Draw the background
{
  int i;
  for (i=0; i<num; i++)
  {
    stroke(255, 0, 0);
    line(0, height*i/num, width, height*i/num);
    line(width*i/num, 0, width*i/num, height);
  }
}
}


class ball {
  color ballcolor;
  int positionX;
  int positionY;
  float radius=height/num-3.8;

  ball(color bc, 
    int X, int Y) {
    ballcolor=bc;
    positionX=X;
    positionY=Y;
  }

  void drawball() 
  { 
    noStroke();
    fill(ballcolor);
    ellipse(mouseX, mouseY, height/num-3.8, height/num-3.8);
  }
}

////////////////Start variable

ball b;
rec r;
int X;//position of XPressed
int Y;//position of YPressed
int x;//position of XReleased
int y;//position of XReleased
int icircle = 0;
int iblocks = 0;
int itemp, jtemp;//save the point's color and time.
int num = 15;//the number of rows and vols
int[][] ifUsed = new int[15][15];//1 for circle;-1 for rec;0 for nothing
color[] colorInit = new color[15];
color[][] colorBlock = new color[15][15]; //15 rows and 15 vols.
boolean ifReleased=false, ifRec=false, ifCircle=false,ifCopy=false;



void setup() {
  size(650, 650);
  setColor();
  background(255);
  setNumbers();
}

void draw() {
  int i=0, j=0;
  background(255);
  drawblocks();
  drawcirle();
  if (mousePressed)
  {
    drawMain();
    i=X/(width/num);//decide the location of blocks;
    j=Y/(height/num);

    if (Y<height/num && Y>0)
    {
      r=new rec(colorInit[i], mouseX, mouseY);
      r.drawrec();
      colorBlock[i][j]=colorInit[i];
      ifUsed[i][j]=-1;
    } else if (Y>height/num*(num-1) && Y<height)
    {
      b=new ball(colorInit[i], mouseX, mouseY);
      b.drawball();
      colorBlock[i][j]=colorInit[i];
      ifUsed[i][j]=1;
    } else
    {
      ifCopy=true;
      if (ifUsed[i][j]==1)
      {
        b=new ball(colorBlock[i][j], mouseX, mouseY);
        b.drawball();
        ifUsed[i][j]=1;
      } 
      else if (ifUsed[i][j]==-1)
      {
        r=new rec(colorBlock[i][j], mouseX, mouseY);
        r.drawrec();
        ifUsed[i][j]=-1;
      }
    }
    itemp=i;
    jtemp=j;
    ifCircle=true;
    ifRec=true;
  }

  if (ifReleased)
  {
    int px=x/(width/num);//decide the location of blocks;
    int py=y/(height/num);

    if (y<height/num*(num-1)&&y>height/num&&ifRec&&ifCircle&&!ifCopy&&x>0&&x<width)
    {
      ifUsed[px][py]=ifUsed[itemp][jtemp];
      ifUsed[itemp][jtemp]=0;
      colorBlock[px][py]=colorBlock[itemp][jtemp];
      colorBlock[itemp][jtemp]=color(0, 0, 0);
    }
     if(ifCopy&&y<height/num*(num-1)&&y>height/num&&x>0&&x<width)
    {
      ifUsed[px][py]=ifUsed[itemp][jtemp];
      colorBlock[px][py]=colorBlock[itemp][jtemp];
    }


    drawMain();
  }
  r=new rec(colorBlock[i][j], mouseX, mouseY);
  r.drawgrid();//initial black grid
}

void mousePressed()
{
  ifReleased=false;
  X=mouseX;
  Y=mouseY;
}

void mouseReleased()
{
  ifReleased=true;
  x=mouseX;
  y=mouseY;
}

void keyPressed() 
{
  if (key==32)
  {
    setColor();
    setNumbers();
  }
}

void drawcirle() //Initial circle up
{
  icircle=0;
  for (; icircle<num; icircle++)
  {
    noStroke();
    fill(colorInit[num-1-icircle]);
    ellipse(width*(num-icircle)/num-width/num/2, height-height/num/2, height/num-3.8, height/num-3.8);
  }
}

void drawblocks() //Initial blocks up
{
  iblocks=0;
  for (; iblocks<num; iblocks++)
  {
    noStroke();
    rectMode(CENTER);
    fill(colorInit[iblocks]);
    rect(width*iblocks/num+width/num/2+1, height/num-height/num/2, height/num-3.8, height/num-3.8);
  }
}


void setColor()
{
  int i;
  for (i=0; i<num; i++)
  {
    colorInit[i]=color(random(255), random(255), random(255));
  }
}

void drawMain()//draw the lefted circles and rects
{
  for (int i=0; i<num; i++)
  {
    for (int j=0; j<num; j++)
    {
      if (ifUsed[i][j]==1)
      {
        ifCircle=false;
        noStroke();
        fill(colorBlock[i][j]);
        ellipse(width*i/num+width/num/2, height*j/num+height/num/2, height/num-3.8, height/num-3.8);
      } else if (ifUsed[i][j]==-1)
      {
        ifRec=false;
        noStroke();
        fill(colorBlock[i][j]);
        rectMode(CENTER);
        rect( width*i/num+width/num/2+1, height*j/num+height/num/2+1, height/num-3.8, height/num-3.8);
      }
    }
  }
}

void setNumbers()//reset ifUsed[k][j] and colorBlock[k][j] array
{
  for (int k = 0; k<num; k++)
  {
    for (int j = 0; j<num; j++)
    {
      colorBlock[k][j] = color(0, 0, 0);
      ifUsed[k][j] = 0;
    }
  }
}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值