## Using two dimensional array for ball movement in ping-pong game

12 posts / 0 new
Author
Message

Hi,

I am new to FreeRTOS and AVR programming and I am currently working on a school project that involves a two-player game,one from the console which is composed from a joystick JS5208, Atmega 324PA and a 10x14 led matrix display.I choose ping-pong because it seemed easy at the moment and I have declared a two dim-array to start with for representing the rows and columns.I have managed to do the paddles like this:

```void moveBat(void *pvParameters)
{

( void ) pvParameters;
#if (configUSE_APPLICATION_TASK_TAG == 1)
// Set task no to be used for tracing with R2R-Network
vTaskSetApplicationTaskTag( NULL, ( void * ) 1 );
#endif

while(1)
{

if(!(PINC & up))//the paddle will go right
{

for (int i = 9; i > 0; --i)
{

if (twod[i] == 1 && twod[i-3]==twod)
{
twod[i] = 0;//paddle is created
twod[i-1] = 1;
twod[i-2] = twod[i-1];
twod[i-3] = twod[i-2];
break;

}

}

}

if(!(PINC & down))//paddle will go left
{
for (int i = 0; i < 9; i++)
{

if (twod[i] == 1 && twod[i+3]==twod)
{

twod[i] = 0;
twod[i+1] = 1;
twod[i+2]= twod[i+1];
twod[i+3]= twod[i+2];
break;

}
}
}

}
}
```

My problem comes when i have to make another two dim array called ball_pos to represent the direction of the ball on the matrix display because if i use it simple without assigning the values of the twod array to the new array ball_pos like this:ball_pos=twod assign the values of the twod array after several logic if statements and for loops,it won't work either if i declare it as uint8_t or as a simple int ball_pos telling me that i have an invalid identifier.I managed to make it work up and down,but only if i set the specific limits for the ball in the dim array.This is the movement of the ball up and down:

```void ballMovement1(void *pvParameters)
{
( void ) pvParameters;
//twod = 1;

#if (configUSE_APPLICATION_TASK_TAG == 1)
// Set task no to be used for tracing with R2R-Network
vTaskSetApplicationTaskTag( NULL, ( void * ) 1 );
#endif

//int x = 1;
//int y = 2;
while(1)
{
for(int j = 1; j < 13; j++)//this will make the ball go down
{
if(twod==1)
{
twod=1;
twod=0;
}
if (twod[j] == 1)
{
twod[j] = 0;
twod[j+1] = 1;

if (twod == 1)//this will make the ball go up
{
for (int i = 12; i>1; --i)
{
twod[i] = 0;
twod[i-1] = 1;
}
break;
}
break;
}
}
}
}
```

I don't

Last Edited: Thu. Dec 6, 2018 - 08:35 PM

maria1571 wrote:

```for (int i = 9; i > 0; --i)
{
if (twod[i] == 1 && twod[i-3]==twod)
{
```

Just looking at this part, if i ever drops below 3, then you

will be using a negative index on the twod array.

--Mike

Thanks Mike for the heads up but i have no idea what other solution i could use for it since i have tried to used separate classes and defining constants in an include file which later on i assigned to some variables in a c file and the i called the include file in the main file using the variables in the c class which represented the include file along with other methods and constants.

See if this gives you some ideas to try:  https://kivy.org/doc/stable/tuto...

Jim

FF = PI > S.E.T

I was suggesting you need to change the for loop like this:

`for (int i = 9; i >= 3; --i)`

--Mike

I did that,thanks for the feedback :).Do you have an idea on how can i move a dot considering the possibilities of collision and paddles?I tried to make the direction of the ball based on the three dots of the paddle,but since i am using the same array for both of them,the dot of the paddle that the ball hits is confused with the ball and it will increment by one every time the ball hits the paddle.

I'd keep track of the position of the ball separately,

and also the direction:

```uint8_t ball_x;
uint8_t ball_y;
uint8_t ball_dir;  // use two different values, maybe 0 and 1```

--Mike

Thanks for the advice! :)

I agree with what others have written, don't try to do the game logic based on an array.  Keep track of the ball and both paddles based on data structures (x,y,direction for ball, y and height for paddles should do it, I think).  Ball direction will include both left-right movement and up-down angle.  Then do the collision detection and movement calculations, and write the resulting bitmap to the display.

Thanks for the advice.I have done what you suggested,but for some reason when i press the joystick to go up,the paddle will not move at all.Instead,when i press "down",it will go both directions(up and down) depending on the increment or decremented value of p1_ paddleY.Also i have noticed that if i don't call the function init_players_objects(uint8_t x,uint8_t y) after p1_paddleY it will not work.The method specified is for the initialization of the two paddles as 3 pixels.The method for moving the paddles is as follows:

```void movePaddle()
{
while(1)
{
if(!(PINC & 1 << down))
{
//xSemaphoreTake(xMutex,portMAX_DELAY);
if(p1_paddle_y < 9)//verify if the paddle is less than 9 pixels
{
draw_game_objects(p1_paddle_x,p1_paddle_y);//whithout calling this function it will not work.

//xSemaphoreGive(xMutex);
}

}else if(!(PINC & 1 << up))//this part does not work due to the fact that the paddle won't decrement the value when button is up
{
//xSemaphoreTake(xMutex,portMAX_DELAY);
{
//xSemaphoreGive(xMutex);
}else
{
break;
}

}

}
}```

The values for up and down are:int up = 0b10000011;
int down = 0b11000010;

and the pixels are made from the frame buffer:

```void set_pixel(uint8_t x, uint8_t y) {
frame_buf[x] = frame_buf[x] | (1<<y);
}
void init_players(uint8_t x, uint8_t y)
{
set_pixel(x, y-1);
set_pixel(x, y);
set_pixel(x, y+1);
}```

Can you please help me figure out why is this behavior in my code?

I find that it's easier to debug code that's been highly factored.

In your program this would mean you'd have code that handles

input (joysticks, buttons), code that handles output (display),

and code that manages the logic of your game, all completely

separate from each other.

Then glue everything together in the main loop.  One possible

way to do this for pong would be to structure it like:

```while (1) {
update_game();
update_display();
}```

The read_inputs function would check whether any buttons were

pressed or joystick movements made and update a few variables

indicating the location of each paddle and the ball position and

direction.

The update_game function would perform collision detection to

check whether the ball hit the top/bottom or a paddle and then

move the ball, update the score, etc.

Update_display would just draw to the display device.  Easiest is

to clear and redraw, but if you end up with flicker then more work

is needed.

OK, so they're not completely separate since they share variables,

but it's easier to check whether the input was processed correctly,

for example, by looking at the variables before and after the

read_inputs function.  Send them out a serial port for analysis.

--Mike