Hi, I'm working on a project where a function checks if a ball touches a wall, which is an array of 15 "slices" with each one 50u in size. I need to know what part of the wall it touches and for that I have done the following, but it is exaggeratedly cumbersome:
if(bolaY == INIPANTALLAY + ALCADAMAO)//Comprueba que llegue hasta la altura del maon
{
if (bolaX > 30 && bolaX < 80)
{
if (Mao[0].maotrencat == false)//Si el maon tocado no esta roto continua
{
Mao[0].maotrencat = true;//Cambia el estado del maon
punts += 1;//Suma 1 punto
dirX *= -1;// cambio de direccion
dirY *= -1;// cambio de direccion
}
}
else if (bolaX > 80 && bolaX < 140)
{
if (Mao[1].maotrencat == false)//Si el maon tocado no esta roto continua
{
Mao[1].maotrencat = true;//Cambia el estado del maon
punts += 1;//Suma 1 punto
dirX *= -1;// cambio de direccion
dirY *= -1;// cambio de direccion
}
}
else if (bolaX > 140 && bolaX <200)
{
if (Mao[2].maotrencat == false)//Si el maon tocado no esta roto continua
{
Mao[2].maotrencat = true;//Cambia el estado del maon
punts += 1;//Suma 1 punto
dirX *= -1;// cambio de direccion
dirY *= -1;// cambio de direccion
}
}
else if (bolaX > 200 && bolaX <260)
{
if (Mao[3].maotrencat == false)//Si el maon tocado no esta roto continua
{
Mao[3].maotrencat = true;//Cambia el estado del maon
punts += 1;//Suma 1 punto
dirX *= -1;// cambio de direccion
dirY *= -1;// cambio de direccion
}
}
else if (bolaX > 260 && bolaX <320)
{
if (Mao[4].maotrencat == false)//Si el maon tocado no esta roto continua
{
Mao[4].maotrencat = true;//Cambia el estado del maon
punts += 1;//Suma 1 punto
dirX *= -1;// cambio de direccion
dirY *= -1;// cambio de direccion
}
}
else if (bolaX > 320 && bolaX <380)
{
if (Mao[5].maotrencat == false)//Si el maon tocado no esta roto continua
{
Mao[5].maotrencat = true;//Cambia el estado del maon
punts += 1;//Suma 1 punto
dirX *= -1;// cambio de direccion
dirY *= -1;// cambio de direccion
}
}
else if (bolaX > 380 && bolaX <440)
{
if (Mao[6].maotrencat == false)//Si el maon tocado no esta roto continua
{
Mao[6].maotrencat = true;//Cambia el estado del maon
punts += 1;//Suma 1 punto
dirX *= -1;// cambio de direccion
dirY *= -1;// cambio de direccion
}
}
else if (bolaX > 440 && bolaX <500)
{
if (Mao[7].maotrencat == false)//Si el maon tocado no esta roto continua
{
Mao[7].maotrencat = true;//Cambia el estado del maon
punts += 1;//Suma 1 punto
dirX *= -1;// cambio de direccion
dirY *= -1;// cambio de direccion
}
}
else if (bolaX > 500 && bolaX <560)
{
if (Mao[8].maotrencat == false)//Si el maon tocado no esta roto continua
{
Mao[8].maotrencat = true;//Cambia el estado del maon
punts += 1;//Suma 1 punto
dirX *= -1;// cambio de direccion
dirY *= -1;// cambio de direccion
}
}
else if (bolaX > 560 && bolaX <620)
{
if (Mao[9].maotrencat == false)//Si el maon tocado no esta roto continua
{
Mao[9].maotrencat = true;//Cambia el estado del maon
punts += 1;//Suma 1 punto
dirX *= -1;// cambio de direccion
dirY *= -1;// cambio de direccion
}
}
I've tried to improve it with a Switch, but I'm not sure if it's possible considering the comparison and the && in each case.
You must take advantage of the fact that each portion has the same size, that is the key . So you could use a for loop like this:
This way your code is all reduced.
Try the following if it gives the same result; I see that the common thing is always to perform:
The
if
interns asif(bolaY == INIPANTALLAY + ALCADAMAO)
follows:Another way is that instead of evaluating
if ([...] && (!Mao[INDICE].maotrencat))
only evaluatesbolaX
(it is the only one that I see you evaluate) and theif
one that I put where it is evaluated index:You can use an array to get the result directly.
I don't understand exactly how your wall is organized ; from what I see in your code, they are chunks of different sizes. But in any case, all chunks are multiples of 10, so you can use something like this:
Now, you only have to check once:
This is a basic technique in game development: precalculate as much as you can .
After reading all the comments, I understand that what you want to do is this:
You have a wall, divided into several pieces, and a ball:
The pieces with contiguous , without separation. The coordinate
X
of the first piece of wall is20
. The width of all chunks is the same,60
pixels .And you know the position
X
of the ball at all times.To know the piece of wall you hit, you don't have to do any
if
. A simple division is enough for you .We subtract the start of the first wall from the current position of the ball, and divide it by the width of the walls; with that, we already have directly an index for your
Mao[]
.Since, in all cases, you do the same thing, it would suffice for you to do
In the event that you had to perform different operations depending on the wall, that is when you would use the solution proposed by @Trauma: add the specific data of each wall in an array . In your case, from what I see, you would add that extra data to your
Mao[]
.