【问题标题】:Need help building billiards game in Processing需要帮助在处理中构建台球游戏
【发布时间】:2013-11-23 23:11:49
【问题描述】:

我正在尝试弄清楚如何使黑球(在本例中标记为 bBall)朝着从白球 (wBall) 击中的方向移动,然后在击中一侧或角落时消失口袋,我该怎么做呢?

ball wBall, bBall;
int click;
String msg;
Boolean moving = false;
int difx, dify;
float cdistance;
int steps = 20;
void setup(){
   click=0;
   size(800,400);
   background(16,77,27);
   wBall = new ball(35,#ffffff);
   bBall = new ball(35,#000000);
   msg="";
}

void mouseClicked(){
  if(!moving){
 click++; 
  }
}

void draw(){
  background(16,77,27);
  String msg;
  fill(0,0,0);
 ellipse(15,15,30,30); 
 ellipse(785,15,30,30);
 ellipse(15,385,30,30);
 ellipse(785,385,30,30);
 ellipse(410,15,30,30);
 ellipse(410,385,30,30);
 msg="the click count is "+click;
 println("the click count is "+click);
 //Moving Balls\\
 fill(255,255,255);
 noStroke();  
 bBall.xpos=(250);
 bBall.ypos=height/2;
 bBall.update();
 if(click==0){
  wBall.xpos=mouseX;
  wBall.ypos=mouseY;
 }else if(click==1){
   difx = wBall.xpos-bBall.xpos;
   dify = wBall.ypos-bBall.ypos;
 }else if(click==2){
  cdistance = dist(wBall.xpos,wBall.ypos,bBall.xpos,bBall.ypos);
   if (cdistance>bBall.ballDiam/2){
    moving = true;
    wBall.xpos-=difx/steps;
    wBall.ypos-=dify/steps;
   }else{
      moving = false;
      wBall.visible=false;
      click=3;
   } 
 }
 wBall.update();
}

class ball{
  int xpos, ypos;
  color myColor;
  int ballDiam;
  boolean visible = true;
  ball(int tempdiam, color tempColor){
    myColor=tempColor;
    ballDiam=tempdiam;
  }

   void update(){
   if(visible){
    fill(myColor);
    ellipse(xpos,ypos,ballDiam,ballDiam);
   }
  }
}

【问题讨论】:

    标签: processing


    【解决方案1】:

    如果您将询问分解为多个问题,而不是说“修复我的游戏”,这将使事情变得更容易......所以请努力直到您偶然发现无法解决的问题,然后在此处询问(针对特定问题) ,得到答案,然后重新开始相同的过程。

    球可以用鼠标正确移动,但会留下痕迹。那是因为您没有在每个循环中绘制背景。发生的情况是,每次调用 draw() 时,画布都会填充在前一个画布上。为了避免这种情况,只需调用

    background(16,77,27);
    

    就在 void draw(){ 行之后,它将用你告诉它的任何颜色填充画布!

    【讨论】:

    • 非常有帮助!谢谢!我将尝试更具体地回答我的下一个问题!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多