iRisc / Demo / spi_demo_game.c
spi_demo_game.c
Raw
int printspi(int num) {
    // Recursively print the digits of the number
    if (num / 10 != 0) {
        printUart(num / 10);
    }

	return ('0' + (num % 10));

}

void game()
{

	lcd_init(1);

	char player[] = "X\n";
	uint8_t row_player = 0;

	char obstacle[] = "O\n";
	uint8_t random_rows[8] = {0, 2, 3, 1, 2, 3, 0, 1};
	int row_index = 0;

	uint8_t row_obstacle[3];
	int8_t col_obstacle[3];
	int element_index = 0;

	//setCursor(19, row_player);
	//lcd_print(player);

	int timestep = 0;
	int score = 0;
	
	int flag = 0;
	int flag2 = 0;
	int j;
	int oldest = 0;

	while (1) {

		
		if (GPIO_READ == 0x55000000) {
			//delay(1);
			if (row_player < 3) {
				row_player++;
			//	setCursor(19, row_player);
			}
		} else if (GPIO_READ == 0xAA000000) {
			//delay(1);
			if (row_player > 0) {
				row_player--;
			//	setCursor(19, row_player);
			}
		}
	

		//if ((timestep == 0) || (timestep % 8 == 0)) {
		if ((timestep == 0) || (timestep % 7 == 0)) {
			//timestep = 0;
			col_obstacle[element_index] = -1;
			row_obstacle[element_index] = random_rows[row_index];
			row_index = (row_index + 1) % 8;
			element_index = (element_index + 1) % 3;
			
			if (flag < 2) {
				flag++;
				oldest = 0;
			} else if (flag2 == 0 && flag == 2) {
				flag2 = 1;
				oldest = 0;
			}
			else
				oldest = (oldest + 1) % 3;

		} 
		
		for (j = 0; j < flag+1; j++)
				col_obstacle[j]++;
		
		setCursor(19, row_player, 1);
		lcd_print(player, 1);

		for (j = 0; j < flag+1; j++) {

			if (col_obstacle[j] < 20) {
				setCursor((uint8_t) col_obstacle[j], row_obstacle[j], 1);
				lcd_print(obstacle, 1);
			}
		}

		if (col_obstacle[oldest] == 19 && row_obstacle[oldest] == row_player)
			break;
		else
			score++;

		// printf("Timestep: %d\n", timestep);
		// printf("Oldest: %d\n", oldest);
		// for (j = 0; j < 3; j++) {
			
		// 	printf("Index %d: (%u, %u)\n", j, (uint8_t) col_obstacle[j], row_obstacle[j]);

		// }

		// printf("\n");

		delay2(500);
		timestep++;
		clear();

		//if (timestep == 40)
		//	break;
	}

	clear();
	// char *score_char = (char *) printspi(score);

	char str[10] = "GAME OVER\n";
	// char str2[] = "Score: ";
	setCursor(5, 1, 10);
	lcd_print(str, 10);
	setCursor(2, 2, 10);
	// lcd_print(str2, 10);
	// lcd_print(score_char, 10);
	// play_note(200, 40);

	delay(60000);
	return;

	//char str[10] = "Hello, Wo\n";

	//lcd_print(str);

	/*
	char str1[] = "Hello, W\n";
	char str2[] = "orld Ama\n";
	char str3[] = "r Vassil\n";
	char str4[] = "is James\n";
	char str5[] = " and the\n";
	char str6[] = " disappe\n";
	char str7[] = "ared Tog\n";
*/
	//str = "Hello\n";
/*
	lcd_print(str1);
	lcd_print(str2);
	lcd_print(str3);
	lcd_print(str4);
	lcd_print(str5);
	lcd_print(str6);
	lcd_print(str7);
	lcd_print(str1);
	lcd_print(str2);
	lcd_print(str3);
	lcd_print(str4);

*/

	//clear();

}