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

Go To Last Post
12 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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][0] == 1 && twod[i-3][0]==twod[0][0])
				{
					twod[i][0] = 0;//paddle is created
					twod[i-1][0] = 1;
					twod[i-2][0] = twod[i-1][0];
					twod[i-3][0] = twod[i-2][0];
					break;

			    }

		     }

	     }

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

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

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

				}
			 }
		  }		

		vTaskDelay( 100 );
	}
}

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[5][7]=twod[5][7] 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[5][7] 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[0][1] = 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[0][1]==1)
			{
				twod[0][2]=1;
				twod[0][1]=0;
			}
			vTaskDelay( 50 );
			if (twod[0][j] == 1)
			{
				twod[0][j] = 0;
				twod[0][j+1] = 1;

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

I don't 

Last Edited: Thu. Dec 6, 2018 - 08:35 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

maria1571 wrote:

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

 

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

will be using a negative index on the twod array.

 

--Mike

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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

 

Jim

 

 

FF = PI > S.E.T

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

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

 

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

 

--Mike

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the advice! :)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

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.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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
			{
				p1_paddle_y+=1;//increment value of paddle
				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);	
			if(p1_paddle_y >0)
			{
				p1_paddle_y-=1;
				draw_game_objects(p1_paddle_x,p1_paddle_y);
				//xSemaphoreGive(xMutex);
			}else
			{
				break;
			}
			
			}
		
		vTaskDelay(50);
			}
}

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?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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) {
    read_inputs();
    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

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ok,thank you :).I will see what i can manage out of what i have.