# Creating screen boundaries

Hello, I am making a simple game but as a beginner I cannot find a way to do the following. It a moving square but I want to write an IF statement so that when it hits the edge of the window for the game to restart. I already have the restart part done.
Thank you

so, its better to see your code.

But for example when your rectangle is at x,y and has a size w,h

``````// check x
if(x<1)
restart();
if(x>width-w)
restart();

// same for y

if(y<1)
restart();
if(y>height-h)
restart();
``````

a simple boundary check using constrain.

``````float x,w,y,h,rectW = 50;

void setup(){
size(400,400);

};

void draw(){
background(0);
fill(255);

rectMode(CENTER);

x = constrain(mouseX,rectW/2,width-rectW/2);
y = constrain(mouseY,rectW/2,height-rectW/2);
rect(x,y,rectW,rectW);
};
``````

a simple circle square collision.

``````float x,y;
void setup(){
size(400,400);
};

void draw(){

background(0);
fill(255);
rectMode(CENTER);
rect(width/2,height/2,200,200);
fill(255,0,0);

//fill(0,0,255);
x = pos().x;y = pos().y;
};

PVector pos(){
PVector p = new PVector(mouseX,mouseY);
return p;
else return new PVector(x,y);

};
``````

Please pay attention to the for loop. Some improvements could be made but thats a start.

So this is part of my code to give some context. I am looking for something so that when the rect hits the end of the window the game restarts, I already have the restart part done I’m just struggling with the when it hits the edge and I have tried applying the suggested bits of code but cannot do it. Any help would be appreciated.

``````int width = 10;
int height = 10;
int size = 10;
float snakeX = 10;
float snakeY = 10;
ArrayList<PVector>body = new ArrayList<PVector>(); // new array (PVector is x,y)

int foodX=(int) random(10, 440);
int foodY=(int) random(10, 440);
boolean up = false;
boolean down = false;
boolean left = false;
boolean right = true;
int score = 0;

void setup() {
size(450,450);
frameRate(60);
}

void draw(){
background(255);

fill(0);
textSize(18);
text("Score:"+score,370,445);

if (up){
snakeY -= 2;
}
else if (down){
snakeY += 2;
}
else if (left){
snakeX -= 2;
}
else if (right){
snakeX += 2;
}

rect(snakeX, snakeY, width, height);
fill(255);
rect(snakeX, snakeY, height, width);

//snake hits food and generates new location + adds length to snake + adds 1 to score
if ( snakeX + 10 >= foodX && // Snakes right hits foods left
snakeX <= foodX + 10 && // Snakes left hits foods right
snakeY + 10 >= foodY && // Snakes top hits foods bottom
snakeY <= foodY +  10)  // Snakes bottom hits foods top

{
foodX = (int) random(10, 440);
foodY = (int) random(10, 440);
score += 1;
}

}

void keyPressed()
{
if (key == CODED && keyPressed)
{
{
up = true;
down = false;
left = false;
right = false;
}
if (keyCode == DOWN)
{
up = false;
down = true;
left = false;
right = false;
}
if (keyCode == LEFT)
{
up = false;
down = false;
left = true;
right = false;
}
if (keyCode == RIGHT)
{
up = false;
down = false;
left = false;
right = true;
}
}
}

``````

can you please show me your implementation because at the last post just contains the same code as before.

Please read my code as its very self explanatory. The PVector boolean can be amended to fit any of your requirements.

you are currently not checking if your player is within bounds

``````if (key == CODED && keyPressed)
``````

you need to include a check here to make sure you are within bounds.

I cannot complete this exercise for you but I can guide you, but currently anymore would mean you fail to learn the point of the exercise.

So I’ve attempted this one, I basically just copied it over and changed the values to mine to see what exactly it did and it just kept a square in the corner.
This is what I did

``````int width = 10;
int height = 10;
int size = 10;
float snakeX = 10;
float snakeY = 10;
ArrayList<PVector>body = new ArrayList<PVector>(); // new array (PVector is x,y)

int foodX=(int) random(10, 440);
int foodY=(int) random(10, 440);
boolean up = false;
boolean down = false;
boolean left = false;
boolean right = true;
int score = 0;

float x,w,y,h,rectW = 50;

void setup() {
size(400,400);
frameRate(60);
}

void draw(){
background(255);

fill(0);
textSize(18);
text("Score:"+score,370,445);

if (up){
snakeY -= 2;
}
else if (down){
snakeY += 2;
}
else if (left){
snakeX -= 2;
}
else if (right){
snakeX += 2;
}

rect(snakeX, snakeY, width, height);
fill(255);
rect(snakeX, snakeY, height, width);

//snake hits food and generates new location + adds length to snake + adds 1 to score
if ( snakeX + 10 >= foodX && // Snakes right hits foods left
snakeX <= foodX + 10 && // Snakes left hits foods right
snakeY + 10 >= foodY && // Snakes top hits foods bottom
snakeY <= foodY +  10)  // Snakes bottom hits foods top

{
foodX = (int) random(10, 440);
foodY = (int) random(10, 440);
score += 1;
}

x = constrain(snakeX,rectW/2, width-rectW/2);
y = constrain(snakeY,rectW/2,height-rectW/2);
rect(x,y,rectW,rectW);
};

void keyPressed()
{
if (key == CODED && keyPressed)
{
{
up = true;
down = false;
left = false;
right = false;
}
if (keyCode == DOWN)
{
up = false;
down = true;
left = false;
right = false;
}
if (keyCode == LEFT)
{
up = false;
down = false;
left = true;
right = false;
}
if (keyCode == RIGHT)
{
up = false;
down = false;
left = false;
right = true;
}
}
}

``````

thats because you arent testing for the boundaries.

``````if ( snakeX + 10 >= foodX && // Snakes right hits foods left
snakeX <= foodX + 10 && // Snakes left hits foods right
snakeY + 10 >= foodY && // Snakes top hits foods bottom
snakeY <= foodY +  10)  // Snakes bottom hits foods top

{
foodX = (int) random(10, 440);
foodY = (int) random(10, 440);
score += 1;
}
``````

you are testing for the food currently, you need to repeat what youve done but for the boundaries. Also at no point in your sketch are you drawing you food.

Yes this is a shorter and simplified version on my full code so that I can test for boundaries easier with less clutter, I have everything else working.
You are saying test or boundaries, if this a built-in function?

I know how to do it with the food because I have the positions of the food but I don’t have the position of the boundaries, I know the screen size but I can’t put them into variables

Chrisir

Chrisir I’m not expecting to be handed the answer but I don’t seem to be understanding, could you rephrase okease

Well you do have the position of the boundaries. The sketch boundaries are 0,0 height, width unless you want to specify something else.

Yes I know that but my issue is with how to would write it because I can to Sketch +450 but how do I declare it

Please review my initial sketch you need to taylor the code and consider what boundaries you are testing and therefore what points each line should have. In the code there are four tests for four lines, each line has two points. What are the 2 points for each boundary?

1 Like

is this what you mean? I have tried this but i continuously get errors

So your boundaries are as follows

Left

0,0 0,height

Top

0,0 width, 0

Right

Width,0 width, height

Bottom

0,0 width height.

You are not currently testing all of those so it wont produce the logic you are currently looking for. You then need to consider wether the test uses a less than or greater than sign. If the boundary is left of your screen then the mouse should always be greater and vice versa. Now apply that same thinking to the top and bottom boundaries.

At the moment though your tests are conflicting take a minute to review your code and think why would the logic conflict, atm you are 50% done but two logical statements need to be amended.

Hint where is the 0 value in your test?

What variable type can take a 0,0 variable I didn’t know that there was one for co-ordanites?

You can use pvectors or you can assign them separately as x and y or any other name for that matter.

To further your understanding perhaps this might help.