处理:如果在抽奖中进行更改,我将在哪里初始化计数器?

梅根
Table t1;
import ddf.minim.*;
AudioPlayer player;
Minim minim;
int stickColR=0;
int stickColG=0;
int stickColB=0;
int counter=0;
//--------------
void setup(){
  size(1000,600);
  smooth();
  t1 = new Table(1000,600);
  t1.startGame();
  //sound player setup
  minim= new Minim(this);
  player=minim.loadFile("ballsound.mp3");

}
//--------------
void draw(){
  strokeWeight(10);
  stroke(255,0,0);
  fill(26, 218, 35);
  rect(0, 0, 1000, 600);
  fill(0);
  noStroke();
  ellipse(0, 0, 100, 100);
  ellipse(1000, 0, 100, 100);
  ellipse(0, 600, 100, 100);
  ellipse(1000, 600, 100, 100);
  t1.updatePos();  //calling the function to update the position of the balls
  t1.visualize();  // Function responsible for drawing the balls


}

//--------------
void mousePressed(){
  t1.mousePressed(mouseX-t1.x,mouseY-t1.y);
}
//--------------
void mouseReleased(){
  t1.mouseReleased(mouseX-t1.x,mouseY-t1.y);
}
//=============================
class Table{
  boolean GameIsOver=false;
  float drag = 0.99;
  float bounce = 0.4;
  float wallBounce = 0.3;
  float pushFactor = 0.4;
  float maxPush = 20;
  color[] ballColors = new color[]{color (255), color (216,7,17),color(17,242,225), color ( #45B4CE) , color ( #6A6347) , color (#E80909) ,color (#CEA9A9)};
  Ball[] balls;
  Ball selectedBall;
  int x,y,width,height;


  //--------------
  Table(int w, int h){
    width = w;
    height = h;
  }
  //--------------
  void startGame(){
    buildBalls(5);
  }
  //--------------
  void buildBalls(int count){
    balls = new Ball[2*count+1];
    int x_coor_red=600;
    int y_coor_red=300;
    int x_coor_green=626;
    int y_coor_green=313;
    for(int i=0;i<count;i++)
    {
      balls[i] = new Ball(x_coor_red, y_coor_red,i+1, this);
      x_coor_red+=26;
      y_coor_red-=13;
      if(i>=3)
      {
        x_coor_red-=26;
        y_coor_red+=26;
      }

    }

    for(int i=0;i<count;i++)
    {
      balls[count+i] = new Ball( x_coor_green, y_coor_green,i+2, this);
      x_coor_green+=26;
      y_coor_green+=13;
      if(i==1)
      {
        x_coor_green-=26;
        y_coor_green-=20;
      }
      if(i==2)
      {
        y_coor_green-=20;
      }
      if(i==3)
      {
        x_coor_green-=45;
      }
    }
    balls[2*count] = new Ball( 0.5*(width), 0.5*(height),0, this);
  }
  //--------------
  void updatePos(){
    //simulation
    for(int i=0;i<balls.length;i++)
      balls[i].update();

    //collision detection
    for(int i=0;i<balls.length;i++)
      for(int j=i+1;j<balls.length;j++)
        balls[i].collisionDetect(balls[j]);
  }
  //--------------
  void visualize(){
    translate(x,y);
    noStroke();

    //draw stick

    if(mousePressed && selectedBall!= null && (mouseX<=26+selectedBall.x || mouseX>26-selectedBall.x) && selectedBall.ballColor==color(255)){
      stickColR+=2;
      stickColB+=2;
      strokeWeight(4);

      stroke(stickColR, stickColG, stickColB);
      line ( mouseX , mouseY , mouseX + cos (atan2 ( mouseY -selectedBall.y , mouseX - selectedBall.x) )*300 , mouseY +  sin( atan2 ( mouseY - selectedBall.y , mouseX -selectedBall.x ))*300);
    }
    //drawing
    for(int i=0;i<balls.length;i++){
      balls[i].visualize();

      //Balls"disappearing" in corners
      if(balls[i].x<50 && (balls[i].y<50 || balls[i].y>550) || balls[i].x>950 &&(balls[i].y<50 || balls[i].y>550)){
      player.rewind();
      player.play();
       if(balls[i].ballColor==color(255))
       {
         textSize(25);
         text("Cue Ball Sunk. GAME OVER",350,560);
       }

       fill(0);
       ellipse(0,0,100,100);
       ellipse(1000, 0, 100, 100);
       ellipse(0, 600, 100, 100);
       ellipse(1000, 600, 100, 100);
       balls[i].x=1200;
       balls[i].y=0;
       counter++;
       if (balls[i].ballColor != 255 && counter>=3)
       {   
         textSize(25);
         text("YOU WIN", 350,560);
       }  
    }        
  }
  }
  //--------------
  float kineticEnergy(){
    float energy=0;
    for(int i=0;i<balls.length;i++)
      energy += mag( balls[i].vx, balls[i].vy );
    return energy;
  }
  //--------------
  void mousePressed(int mx, int my){
    for(int i=0;i<balls.length;i++)
      if( dist(balls[i].x,balls[i].y,mx,my) < balls[i].radius) {
        selectedBall = balls[i];
        break;
      }
  }
  //--------------
  void mouseReleased(int mx, int my){
    if(selectedBall != null){
      float px = (selectedBall.x-mx) * pushFactor;
      float py = (selectedBall.y-my) * pushFactor;
      float push = mag(px,py);
      stickColR=0;
      stickColB=0;
      if( push > maxPush ){
        px = maxPush*px/push;
        py = maxPush*py/push;
      }
      selectedBall.push(px,py);
    }
    selectedBall = null;
  }
}
class Ball{
  float x,y,vx,vy,radius,diameter;
  int type;
  Table table;
  color ballColor;
  //--------------
  Ball(float x, float y, int type, Table t){
    this.x = x;
    this.y = y;
    this.type = type;
    diameter = 26;
    radius = diameter/2;
    table = t;
    ballColor = table.ballColors[type];
  }
  //--------------
  void update(){
    vx *= table.drag;
    vy *= table.drag;

    x += vx;
    y += vy;

    wallBounce();
  }
  //--------------
  void wallBounce(){
    if(x<=radius){
      vx = -table.wallBounce*vx;
      x = radius;
      player.rewind();
      player.play();

    }
    if(x>=t1.width-radius){
      vx = -table.wallBounce*vx;
      x = table.width-radius;
      player.rewind();
      player.play();
    }
    if(y<=radius){
      vy = -table.wallBounce*vy;
      y = radius;
      player.rewind();
      player.play();
    }
    if(y>=t1.height-radius){
      vy = -table.wallBounce*vy;
      y = table.height-radius;
      player.rewind();
      player.play();
    }

  }
  //--------------
  void visualize(){
    fill(ballColor);
    stroke(0);
    strokeWeight(2);
    stroke(0);
    ellipse(x,y,diameter,diameter);
  }
  //--------------
  void push(float dx, float dy){
    vx += dx;
    vy += dy;
  }
  //--------------
  void collisionDetect(Ball b){
    float distance = dist(x,y,b.x,b.y);
    if( distance < diameter && (b.x>50 && (b.y>50 || b.y<550) || b.x<950 &&(b.y>50 || b.y<550))){
      float vxSum = 0.5*(vx + b.vx);
      float vySum = 0.5*(vy + b.vy);

      float forceMagnitude = ((b.x-x)*(vx-vxSum) + (b.y-y)*(vy-vySum));
      float xForce = 0.25*(x-b.x)*forceMagnitude/distance;
      float yForce = 0.25*(y-b.y)*forceMagnitude/distance;

      vx   = vx + table.bounce * xForce;
      vy   = vy + table.bounce * yForce; 
      b.vx = b.vx - table.bounce * xForce;
      b.vy = b.vy - table.bounce * yForce;

      b.x = x + (diameter+1)*(b.x-x)/distance;
      b.y = y + (diameter+1)*(b.y-y)/distance;

      player.rewind();
      player.play();
      }
    }
  }

我正在处理桌球游戏,我差不多完成了,但是我被困在这里。我想添加一个计数器,当一个球进入角球时进行计数,然后一旦该计数器等于10,即球的数量,它将显示获胜消息。我认为出问题了,因为每次抽奖都会重置计数器?但是我不确定还有什么地方可以初始化它。顺便说一句,现在我只将它设置为> = 3,这样我就可以快速查看它是否在工作。谢谢大家

Choasia

由于我的声誉低下,即使我认为我还没有解决代码中的所有错误,也必须向我展示我的解决方案。

我认为您在问题中陈述的错误与计数器变量关系不大我运行了您的代码,发现您试图通过更改坐标来隐藏“消失的”球。这意味着“消失的”球仍可以通过以下if语句进行限定,结果,counter的将继续增加。

//Balls"disappearing" in corners
if(balls[i].x<50 && (balls[i].y<50 || balls[i].y>550) || balls[i].x>950 &&(balls[i].y<50 || balls[i].y>550))

因此,您可以向Ball添加一个布尔字段

class Ball{
    float x,y,vx,vy,radius,diameter;
    int type;
    Table table;
    color ballColor;
    // add this variable.
    boolean disappeared = false;
    // your constructor, wallBounce(), push(), collisionDetect()
    void visualize(){
        // only draw the ball when it hasn't disappeared.
        if(!this.disappeared) {
            fill(ballColor);
            stroke(0);
            strokeWeight(2);
            stroke(0);
            ellipse(x,y,diameter,diameter);
        }  
    }
 }

然后使用它来避免重复计算。

// other code in visualize()
if(!balls[i].disappeared) {
    counter++;
    balls[i].disappeared = true;
}
// other code

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如果在Spring Framework的@PostConstruct中初始化对象属性,是否应该将对象属性标记为volatile?

如果在静态初始化程序块中创建线程,程序将挂起

如果在方法内部初始化模型,为什么不能访问模型?

如果在Go中构造,则无法初始化struct

在Cassandra中,如果我运行一个增加计数器的查询,那么从该计数器中进行选择是原子的吗?

添加新商品时如何初始化计数器

我在哪里放置代码以初始化控件?

如何从键/初始计数对列表中初始化计数器?

尽管尝试了所有方法仍无法在C中初始化我的数组计数器

如果在类初始化时创建手势识别器,则无法使用

在LabVIEW中初始化计数器

如果在声明时或在构造函数中初始化变量,会有区别吗

如果在c ++构造函数中以错误的顺序初始化对象数据,会发生什么样的错误

如果我们不显式传递数组的大小,则在哪里初始化数组?

计数器的输出未在Verilog仿真中显示为已初始化

如果在哪里使用Flutter,我将无法从Firestore使用orderBy查询

减少对象数组并使用JavaScrip更新初始化的计数器

在哪里初始化SSE常数

如果在已经初始化的变量上使用`var`会发生什么情况

如果在contoller中进行更改,则哪个dll会更改

类的静态计数器值无法初始化数组

初始化字典,循环浏览列表,在字典中添加单词并增加计数器

数组和初始化多个计数器

初始化时将在哪里将内存分配给“未初始化的静态变量”?

如何在VBA Excel中初始化我的计数器

领域迁移,在哪里初始化

在 Common Lisp 中初始化计数器变量

动态结构初始化和计数器实现

将 Provider 与 TickerProviderStateMixin 一起使用:我可以在哪里初始化我的控制器?