# Problem with width/2 and height/2

**URL:** <https://discourse.processing.org/t/problem-with-width-2-and-height-2/1869>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [July 18, 2018, 1:08am UTC](https://discourse.processing.org/t/problem-with-width-2-and-height-2/1869 "2018-07-18T01:08:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![the-junk](https://avatars.discourse-cdn.com/v4/letter/t/d07c76/32.png) [@the-junk](https://discourse.processing.org/u/the-junk)\
**Post date:** [July 18, 2018, 1:08am UTC](https://discourse.processing.org/t/problem-with-width-2-and-height-2/1869/1 "2018-07-18T01:08:32Z")

</div>

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 - iX_SNAKE\_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 + iY_SNAKE\_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`

```auto

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [July 18, 2018, 1:22am UTC](https://discourse.processing.org/t/problem-with-width-2-and-height-2/1869/2 "2018-07-18T01:22:45Z")

</div>

- [https://Forum.Processing.org/two/discussion/27966/conventions-re-scope-where-to-initialize-variables#Item\_3](https://Forum.Processing.org/two/discussion/27966/conventions-re-scope-where-to-initialize-variables#Item_3)

---

<div class="post-metadata">

**Author:** ![wesley](https://avatars.discourse-cdn.com/v4/letter/w/eb9ed0/32.png) [@wesley](https://discourse.processing.org/u/wesley)\
**Post date:** [July 18, 2018, 2:23am UTC](https://discourse.processing.org/t/problem-with-width-2-and-height-2/1869/3 "2018-07-18T02:23:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [July 18, 2018, 2:58am UTC](https://discourse.processing.org/t/problem-with-width-2-and-height-2/1869/4 "2018-07-18T02:58:35Z")

</div>

> [@the-junk](#):
>
> int snakePosX = width/2; //the starting X position for the snake  
> int snakePosY = height/2; //the starting Y position for the snake

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

---

<div class="post-metadata">

**Author:** ![the-junk](https://avatars.discourse-cdn.com/v4/letter/t/d07c76/32.png) [@the-junk](https://discourse.processing.org/u/the-junk)\
**Post date:** [July 18, 2018, 9:34am UTC](https://discourse.processing.org/t/problem-with-width-2-and-height-2/1869/5 "2018-07-18T09:34:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [July 18, 2018, 10:15am UTC](https://discourse.processing.org/t/problem-with-width-2-and-height-2/1869/6 "2018-07-18T10:15:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![the-junk](https://avatars.discourse-cdn.com/v4/letter/t/d07c76/32.png) [@the-junk](https://discourse.processing.org/u/the-junk)\
**Post date:** [July 18, 2018, 10:29am UTC](https://discourse.processing.org/t/problem-with-width-2-and-height-2/1869/7 "2018-07-18T10:29:05Z")

</div>

```auto
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;**
}

```

---

<div class="post-metadata">

**Author:** ![the-junk](https://avatars.discourse-cdn.com/v4/letter/t/d07c76/32.png) [@the-junk](https://discourse.processing.org/u/the-junk)\
**Post date:** [July 18, 2018, 10:29am UTC](https://discourse.processing.org/t/problem-with-width-2-and-height-2/1869/8 "2018-07-18T10:29:36Z")

</div>

Thanks Wesley, this helped me.
