在Processing中制作游戏“下落方块”的问题。

huangapple go评论113阅读模式
英文:

Problems on making the game "Falling Blocks" on Processing

问题

void difficulty()
{
  if (/* 在这里写条件 */){
    lives--;
  }
  else{
    fill(0, 255, 0);
  }
}

你需要在上面的代码中填写条件,以便在满足条件时减少生命值(lives)。在你的游戏中,应该有一个条件来检测当你的圆(circle)触碰到一个方块(block)时触发生命值减少。在条件中,你需要检查圆与方块是否发生碰撞。

英文:

So I am making a game on Processing and I cant seem to be finding how to make this if statements condition. the condition is supposed to deduct a life when my circle touches a block I have the idea but I just can't put it in code please help me. The "if" the condition is below and the full game code is right after that.

void difficulty()
      {
        if(/*what do i write here*/){
        lives--;    
      }
      else{
       fill(0, 255, 0); 
      }
      }     

/////// The main game code starts from here////////

int score;
    int score1;
    int miss;
    int lives=10;
    int ballx, bally;
     
    class Rect
    {
      float x;
      float y;
      float speed;
      float leng;
      color c;
      boolean valid;
     
      final int MAX_COLOR = 255;
      final int MIN_X = 50, MAX_X = 750;
      final int MIN_Y = -800, MAX_Y = -100;
      int MIN_SPEED = 1, MAX_SPEED = 2;
      final int MIN_Leng = 50, MAX_Leng =100 ;
     
      Rect()
      {
        initAll();
      }
     
      void initAll() {
        valid = true;
        c     = color(random(255), random(255), random(255));
        x     = random(MIN_X, MAX_X); 
        y     = random(MIN_Y, MAX_Y);
        speed = random(MIN_SPEED, MAX_SPEED);
        leng  = random(MIN_Leng, MAX_Leng);
      }
     
     
      void update() {
        if (!valid) {
          initAll();
          return;
        }
        move();
        draw_rect();
      }
      void draw_rect()
      {
        fill(c);
        rect (x, y, leng, leng);
      }
     
     
      void move()
      {
        if (y-leng <= height)
        {
          y += speed;
        } else if (y-leng > height )
        {
          valid = false;
          miss++;
        }
      }
     
      void difficulty()
      {
        if(){
        lives--;    
      }
      else{
       fill(0, 255, 0); 
      }
      }
      void GameOver()
      {
        if (lives==0)
        {
          for (int i = 0; i < Obj.length; i++)
          {
            Obj[i] = new Rect();
          }
     
          background(0);
          textSize(50 );
          fill(255);
          text( "You Lose ", 15, 150);
          text( "Score: " + score, 15, 100);
        }
      }
     
     
      boolean isOver(int mx, int my) {
        float disX = x - mx;
        float disY = y - my;
        if (sqrt(sq(disX) + sq(disY)) < leng/2 ) {
          return true;
        } else {
          return false;
        }
      }
    }
     
     
    Rect [] Obj = new Rect [10];
     
     
    void setup() {
      size (400, 600);
      ballx=200;
      bally=300;
      for (int i = 0; i < Obj.length; i++)
      {
        Obj[i] = new Rect();
      }
    }
     
     
    void draw() {
      
      background(0);
      textSize(50);
      text( "Score: " + score, 0, 100);
      text("Lives: " + lives, 0, 50);
      ellipse(ballx,bally,20,20);
     
      for (int i = 0; i < Obj.length; i++) {
        Obj[i].update();
        Obj[i].difficulty();
        Obj[i].GameOver();
      }
      surface.setTitle(nf(frameRate, 3, 2));
    }
    
    
    
    void keyPressed(){
      
      for(Rect s : Obj){
        if ( key =='q' ){
         ballx=ballx-2;
         score++;
         score1++;
        s.valid = false;
         break;
        }
        if ( key =='e'){
         ballx=ballx+2;
         score++;
         score1++;
         s.valid = false;
         break;
        }
      }
          
    }

答案1

得分: 2

你想要做的是检查玩家是否与任何矩形发生了碰撞。

思考一下,玩家与矩形发生碰撞意味着什么?

这意味着玩家的 x 位置必须大于左侧的 x 位置,但小于矩形右侧的 x 位置。
y 位置同理。

因为你需要检查 所有 矩形的位置,你应该使用一个 for 循环或 foreach 循环。

现在,你可以按照我之前说的去做,这会起作用,但你还应考虑到玩家本身的大小。玩家仍然应该在与矩形的左侧/右侧发生碰撞时与其发生碰撞,而不仅仅是玩家的中心。
从技术上讲,玩家是一个圆,但为了简化,我们将其近似为一个矩形。

boolean isColliding = false;
for (Rect r : Obj) {
  if (ballx + 10 > r.x &&          //球的右侧是否在矩形的左侧之右?
  ballx - 10 < r.x + r.leng &&     //球的左侧是否在矩形的右侧之左?
                                   //如果上述两者都成立,则球的 x 位置在矩形的左侧和右侧之间
  
  bally + 10 > r.y &&              //球的底部是否在矩形的顶部之下?
  bally - 10 < r.y + r.leng) {     //球的顶部是否在矩形的底部之上?
    isColliding = true;            //如果上述所有条件都成立,则球与矩形发生碰撞
  }
} 
if (isColliding) {
  lives--;
} else {
  fill(0, 255, 0);
}
英文:

What you want to do is check if the player is colliding with any of the rectangles.

Think about this, what does it mean for the player to collide with a rectangle?

It means that the x-position of the player has to be larger than the x-position of the left, but smaller than the x-positon of the right side of the rectangle.
The same goes for the y-positions.

Since you need to check the position of all rectangles, you should use a for or a foreach loop.

You could now just do what I said until now, and it would work, but you should also take into account the size of the player itself. The player should still collide with the rectangles if his left/right side collides with them, not just his center.
The player is technically a circle, but for simplification, we will approximate it as a rectangle.

boolean isColliding = false;
for(Rect r : Obj)
{
  if (ballx + 10 &gt; r.x &amp;&amp;          //is the right side of the ball further right than the left side of the rectangle?
  ballx - 10 &lt; r.x + r.leng &amp;&amp;     //is the left side of the ball further left than the right side of the rectangle?
                                   //if both of the above are true, the balls x-position is between the left and right side of the ball
  
  bally + 10 &gt; r.y &amp;&amp;              //is the bottom of the ball further down than the top of the rectangle?
  bally - 10 &lt; r.y + r.leng) {     //is the top of the ball further up than the bottom of the rectangle?
    isColliding = true;            //if all of the above are true, the ball is colliding with the rectangle
  }
} 
if (isColliding) {
  lives--;
} else {
  fill(0, 255, 0);
}

huangapple
  • 本文由 发表于 2020年8月28日 16:44:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63630412.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定