Problem with width/2 and height/2

Good evening,

So, I want my snakePosX to be equal to width/2 and snakePosY to be equal to height/2. There is a bug in my code making my snakePosX and snakePosY equal to 50.

I just want my snake to start in the middle of the canvas and be the size of three ellipses. I am pulling my hair out lol… Everything else seems to work so far. I am still working on the end game options, but I think I have a plan for those.

Is my call for help clear enough??

``
int growth = 3; //this variable will help the snake grow as it captures the food, it will also effect the initial length of the snake
int snakeSpeed = 1; //how fast the snake moves

final int SNAKE_SIZE = 15; //the snake is made up of ellipses, this variable determines the size of each ellipse
final int SNAKE_LENGTH = SNAKE_SIZE*growth; //initial snake length will be three ellipses based off of SNAKE_SIZE

int snakePosX = width/2; //the starting X position for the snake
int snakePosY = height/2; //the starting Y position for the snake

float tgtX = random(0, 500); //this will assign the food a randomw position
float tgtY = random(0, 500); //this will assign the food a randomw position
int tgtSize = 15; //the food size should match the snake size

boolean hit = false; //controls hit detection logic for when the snake hits the food
int totalHits = 0; //the game will start at 0 points and in increase by 1 with each food catpure
final int MAX_HITS =5; //the player wins the game if they catch the food 5 times

boolean borderHit = false; //the game will be over if the snake hits the wall

String restart = “RESTART”;
String exit = “EXIT”;

//the following boolean variables will control the movement of the snake, this seemed to be the easiest way to do this
boolean moveUp = false;
boolean moveDown = false;
boolean moveRight = false;
boolean moveLeft = false;

//variables for the end game option box sizes and locations
int endGameBoxSize = 100;
int endGameBoxX = 250;
int endGameBoxY = 150;

void setup()
{
size(500, 500); // set the canvas to 500 pixels as per the instructions
rectMode(CENTER); //I prefer drawing rectangles in this mode
strokeWeight(5); //thicker lines make the game look more fun
textAlign(CENTER); //this is my preferred method for using text
}

void draw()
{
background(200); //clear the background each frame
moveSnake(color(246, 255, 0)); //draw the snake at the the centre of the canvas
checkIfKeyPressed(); //up,down,left,right for controlling the snake
drawTarget (color(random(0, 255), random(0, 255), random(0, 255))); //the flashing food on the canvas for the snake to eat
checkIfHitTarget(); //score increases by 1 each time the snake gets the food
checkIfBorderHit(); //game over if the snake hits the wall
displayExitOptions(color(255)); //end game options “EXIT” & “RESET”
//println(width/2); //de-bugging
}

void moveSnake(int snakeColor) //this function will draw the snake at its starting position
{
{
fill(snakeColor); //the snake should be a different colour from the target
ellipse(snakePosX, snakePosY, SNAKE_SIZE, SNAKE_SIZE);
for (int iX = 0; iX < growth; iX++)
{
if (moveRight) //contonal statemtents for right and left movement
{
ellipse(snakePosX - iXSNAKE_SIZE, snakePosY, SNAKE_SIZE, SNAKE_SIZE);
} else if (moveLeft)
{
ellipse(snakePosX + iX
SNAKE_SIZE, snakePosY, SNAKE_SIZE, SNAKE_SIZE);
}
}
for (int iY = 0; iY < growth; iY++) //contonal statemtents for up and down movement
{
if (moveUp)
{
ellipse(snakePosX, snakePosY + iYSNAKE_SIZE, SNAKE_SIZE, SNAKE_SIZE);
} else if (moveDown)
{
ellipse(snakePosX, snakePosY - iY
SNAKE_SIZE, SNAKE_SIZE, SNAKE_SIZE);
}
}
}
println(width/2);
}

void checkIfKeyPressed() //assigns keyboad inputs to move the snakke
{
if (keyCode == UP) //move the snake up with up arrow
{
snakePosY-=snakeSpeed; //increment up
moveUp=true;
moveLeft= false;
moveDown= false;
moveRight = false;
} else if (keyCode == DOWN) //move the snake down with down arrow
{
snakePosY+=snakeSpeed; //increment down
moveDown=true;
moveLeft= false;
moveUp= false;
moveRight = false;
} else if (keyCode == LEFT) //move the snake left with left arrow
{
snakePosX-=snakeSpeed; //increment left
moveUp=false;
moveLeft= true;
moveDown= false;
moveRight = false;
} else if (keyCode == RIGHT) //move the snake right with right arrow
{
snakePosX+=snakeSpeed; //increment right
moveUp=false;
moveLeft= false;
moveDown= false;
moveRight = true;
}
}

void drawTarget(int tgtColor) //simple function to draw the food at a random location
{
fill(tgtColor); //the food should be a different colour from the snake
ellipse(tgtX, tgtY, tgtSize, tgtSize); //the food
}

void checkIfBorderHit() //this determines if the snake hits the borders
{
// if the snake crashes into the walls it will end the game
if (snakePosX<0+SNAKE_SIZE/2 || snakePosX>500-SNAKE_SIZE/2 || snakePosY<0+SNAKE_SIZE/2 || snakePosY>500-SNAKE_SIZE/2 )
{
borderHit = true;
}
}

void checkIfHitTarget() //this function check to see if the snake has eaten the snake
{
if (dist(snakePosX, snakePosY, tgtX, tgtY)<=tgtSize-5) //-5 for better hit detection and playability
{
tgtX = random(0+tgtSize/2, 500-tgtSize/2); //the food will jump to a random locaion each time it is hit by the snake
tgtY = random(0+tgtSize/2, 500-tgtSize/2);
hit = true;
}
if (hit)
{
totalHits += 1;
hit=!hit; //reset the boolean
growth++; //increase the size of the snake as it eats the food (target)
}
}

void displayExitOptions(int endgameBoxColor) //this function runs at the end of the game
{
if (totalHits==MAX_HITS || borderHit) //if the total hits match the maximum amount of hits allowed run this code
{
fill(endgameBoxColor);
rect(endGameBoxX, endGameBoxY, endGameBoxSize, endGameBoxSize-50); //game over box “RESTART”
fill(0);
text(restart, 250, 150);

fill(endgameBoxColor);
rect(endGameBoxX, endGameBoxY+200, endGameBoxSize, endGameBoxSize-50); //game over box "EXIT"
fill(0);
text(exit, 250, 350);

snakePosX = 250; //return snake to middle of canvas at game over
snakePosY = 200;
// this if condtion will determing if the mouse is pressed in the restart box and restart the 
if ((mouseX >endGameBoxX-endGameBoxSize/2)&&(mouseX<endGameBoxX+endGameBoxSize/2)&&
  (mouseY>endGameBoxY-endGameBoxSize/4)&&(mouseY<endGameBoxY+endGameBoxY/4)&&mousePressed)
{
  
}

}
//println(endGameBoxY-endGameBoxSize/4);
}

Summary

This text will be hidden

you need to write
snakePosX = width / 2;
snakePosY = height / 2;

after the size (500, 500); because before that the window size is (100, 100) also the generic size

1 Like

As mentioned before, The proper place to access width and height are in setup (also draw, mousePressed, etc). Setup is a great function to init your variables that depend on the size of your sketch. If you init them in the global scope, it is not guarantee size() is executed on time and your global variables would be evaluated first.

Kf

Unfortunately it is still not working for me. I’ve used this method in all of my previous sketches with no problems. I guess I have more de-bugging to do on this one though.

I have another bug where once the game reaches the end state. It will freeze for about 3 seconds before displaying the end game option boxes.

The answer from @wesley is correct if it didn’t work for you then you implemented it incorrectly.
When a sketch is executed the variables width and height start at 100 but are changed when the size(...) method is executed. In your code above the snakes position variables are created and initialised before the call to setup() so use the value 100

You are correct, here is my new code de-bugged... Thank-you everyone!!!

int growth = 3; //this variable will help the snake grow as it captures the food, it will also effect the initial length of the snake
int snakeSpeed = 1; //how fast the snake moves

final int SNAKE_SIZE = 15; //the snake is made up of ellipses, this variable determines the size of each ellipse
final int SNAKE_LENGTH = SNAKE_SIZE*growth; //initial snake length will be three ellipses based off of SNAKE_SIZE


**int snakePosX; //the starting X position for the snake**
**int snakePosY; //the starting Y position for the snake**



float tgtX = random(0, 500); //this will assign the food a randomw position
float tgtY = random(0, 500); //this will assign the food a randomw position
int tgtSize = 15; //the food size should match the snake size

boolean hit = false; //controls hit detection logic for when the snake hits the food 
int totalHits = 0; //the game will start at 0 points and in increase by 1 with each food catpure
final int MAX_HITS =5; //the player wins the game if they catch the food 5 times

boolean borderHit = false; //the game will be over if the snake hits the wall

String restart = "RESTART";
String exit = "EXIT";

//the following boolean variables will control the movement of the snake, this seemed to be the easiest way to do this
boolean moveUp = false; 
boolean moveDown = false;
boolean moveRight = false;
boolean moveLeft = false;

//variables for the end game option box sizes and locations
int endGameBoxSize = 100;
int endGameBoxX = 250;
int endGameBoxY = 150;



void setup()
{
  size(500, 500);  // set the canvas to 500 pixels as per the instructions
  rectMode(CENTER); //I prefer drawing rectangles in this mode
  strokeWeight(5); //thicker lines make the game look more fun
  textAlign(CENTER); //this is my preferred method for using text
  **snakePosX = width/2;**
 **snakePosY = height/2;**
}

Thanks Wesley, this helped me.