rect() not creating rectangles

edited September 2017 in Questions about Code

hi everyone, i've been creating code for an assignment for school but i'm having trouble. my goal is to re-create the old game snake. i've got the automatic movement handled but everytime i eat a piece of food, the rect() doesn't create a rectangle at all. all i want is to able to see the rectangle, i should be able to make it move with the head myself but any help is greatly appreciated (:

code:

PVector head;
int headSize = 10;
int foodX = int( random(50));
int foodY = int( random(50));
int foodXX = round(foodX);
int foodYY = round(foodY);
int foodXXX = foodXX * 10;
int foodYYY = foodYY * 10;
int snakeSpeed = 10;
int bodypos = 10;
char direction;

//setup
void setup() {
  size(500, 500);
  frameRate(60);
  head = new PVector(250, 250);
  direction = 'w';
}

//drawing
void draw() {
  background(255);
  rect(head.x, head.y, 10, 10);
  rect(foodXXX, foodYYY, 10, 10);
  if (keyPressed && frameCount % 1 == 0) 
    direction = key;
  move();
  if (head.x == foodXXX) {
    if (head.y == foodYYY) {
      foodX = int( random(50));
      foodY = int( random(50));
      foodXX = round(foodX);
      foodYY = round(foodY);
      foodXXX = foodXX * 10;
      foodYYY = foodYY * 10;
    }
  }
  if (head.x == foodXXX) {
    if (head.y == foodYYY) {
      if (direction == 'w') {
        delay(100);
        rect(head.x, head.y - bodypos, 10, 10);
        bodypos = bodypos + 10;
      }
      else if (direction == 'a') {
        delay(100);
        rect(head.x - bodypos, head.y, 10, 10);
        bodypos = bodypos + 10;
      }
      else if (direction == 's') {
        delay(100);
        rect(head.x, head.y + bodypos, 10, 10);
        bodypos = bodypos + 10;
      }
      else if (direction == 'd') {
        delay(100);
        rect(head.x + bodypos, head.y, 10, 10);
        bodypos = bodypos + 10;
      }
    }
  }
}

void move() {
  switch(direction) {

  case 'w' :
    head = new PVector(head.x, head.y - snakeSpeed);
    delay(100);
    break;
  case 'a' :
    head = new PVector(head.x - snakeSpeed, head.y);
    delay(100);
    break;
  case 's' :
    head = new PVector(head.x, head.y + snakeSpeed);
    delay(100);
    break;
  case 'd' :
    head = new PVector(head.x + snakeSpeed, head.y);
    delay(100);
    break;
  }
  delay(10);
}
Tagged:

Answers

  • In draw():

     if (head.x == foodXXX) {
        if (head.y == foodYYY) {
          foodX = int( random(50));
          foodY = int( random(50));
          foodXX = round(foodX);
          foodYY = round(foodY);
          foodXXX = foodXX * 10;
          foodYYY = foodYY * 10;
        }
      }
      if (head.x == foodXXX) {
        if (head.y == foodYYY) {
    

    Pseudocode:

    If(head of the snake is above the food){
    Move the food
    }
    
    If(head of the snake is above the food){ //but you just moved the food? It is impossible for this statement to be true
    Add a rectangle
    }
    
  • Possible solution:

    if(head of the snake is above the food){
    Move the food;
    Add a rectangle;
    }
    
Sign In or Register to comment.