You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Processing贪吃蛇游戏:蛇进食后食物随机变色的实现问题求助

问题描述

刚学习Processing,已制作一款贪吃蛇游戏,希望实现蛇每次进食后,随机生成的食物同时变为随机颜色。尝试声明r、g、b变量并赋值随机颜色,但食物颜色并未在蛇进食时改变,代码如下:

snake s;
int grid = 15;
PVector food;
int r;
int g;
int b;
int warna;

void setup() {
  size(600, 600);
  s = new snake();
  food = new PVector();
  r = (int)random(255);
  g = (int)random(255);
  b = (int)random(255);
  frameRate(15);
  newFood();
}


void draw() {
  background(0);

  s.showScore();
  s.display();
  if (s.gameOver()) {
    background(0);
    textAlign(LEFT);
    textSize(25);
    fill(255);
    text("Game Over", 10, 10, width - 20, 50);
    noLoop();
  }
  if (s.eat(food)) {
    newFood();
  }
  s.move();
  
  fill (r, g, b);
  rect (food.x, food.y, grid, grid);
  
}

void newFood() {

  food.x = floor(random(width));
  food.y = floor(random(height));
  food.x = floor(food.x/grid) * grid;
  food.y = floor(food.y/grid) * grid;
  
  if (food.x == floor(random(width)) && food.y == floor(random(height)) ){
    fill (r = (int)random(255), g = (int)random(255), b = (int)random(255));
    rect( food.x, food.y, grid, grid);
    
  }
 
  
}

void keyPressed() {
  if (keyCode == UP) {
    s.arah(0, -1);
  } else if (keyCode == DOWN) {
    s.arah(0, 1);
  } else if (keyCode == RIGHT) {
    s.arah(1, 0);
  } else if (keyCode == LEFT) {
    s.arah(-1, 0);
  }
}

class snake {
  float x = 0;
  float y = 0;
  float xspd = 1;
  float yspd = 0;
  int panjang = 0;
  ArrayList<PVector> body =  new ArrayList<PVector>();

  snake() {
  }

  boolean eat(PVector pos) {
    float d = dist(x, y, pos.x, pos.y);
    if (d < 1) {
      panjang++;
      return true;
    } else {
      return false;
    }
  }

  void arah(float x, float y) {
    xspd = x;
    yspd = y;
  }

  boolean gameOver() {
    for (int i = 0; i < body.size(); i++) {
      PVector pos = body.get(i);
      float d = dist(x, y, pos.x, pos.y);
      if (d < 1) {
        panjang = 0;
        body.clear();
        return true;
      }
    }
    return false;
  }


  void move() {
    if (panjang > 0) {
      if (panjang == body.size() && !body.isEmpty()) {
        body.remove(0);
      }
      body.add(new PVector(x, y));
    }

    x = x + xspd*grid;
    y = y + yspd*grid;

    x = (x + width) % width;
    y = (y + height) % height;
  }

  void display() {
    noStroke();
    fill(255);
    for (PVector bagi : body) {
      rect(bagi.x, bagi.y, grid, grid);
    }
    rect(x, y, grid, grid);
  }

  void showScore() {
    textAlign(LEFT);
    textSize(25);
    fill(255);
    text("Score: " + body.size(), 10, 10, width - 20, 50);
  }
}
解决建议

问题出在newFood()函数的颜色更新逻辑上:

  1. 你写的if (food.x == floor(random(width)) && food.y == floor(random(height)))条件几乎不可能触发,两次random()调用生成相同值的概率极低,导致颜色根本不会更新。
  2. 就算这个条件触发了,在newFood()里绘制的食物会被下一次draw()开头的background(0)清空,最终还是用setup()里初始化的r、g、b值绘制食物。

修改方案:
直接在newFood()函数末尾更新r、g、b的随机值,删掉那个无效的判断和绘制代码,让draw()函数负责统一绘制食物。

修改后的newFood()函数:

void newFood() {
  food.x = floor(random(width));
  food.y = floor(random(height));
  food.x = floor(food.x/grid) * grid;
  food.y = floor(food.y/grid) * grid;
  
  // 直接更新食物颜色的RGB值
  r = (int)random(255);
  g = (int)random(255);
  b = (int)random(255);
}

这样每次蛇吃掉食物调用newFood()时,都会随机生成新的RGB颜色值,draw()里的fill(r,g,b)就会用新颜色绘制食物,实现你想要的效果。

内容的提问来源于stack exchange,提问作者AsuRaassth

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 11:05:47