Ejemplo n.º 1
0
void setup_snake_field()
{
	int x, y;

	for(y=0;y<29;y++)
		for(x=0;x<40;x++)
			snake_array[y][x] = 0;
	
	for(y=0;y<29;y++)
	{
		snake_array[y][0] = 0x81;
		snake_array[y][39] = 0x81;
	}
	
	for(x=0;x<40;x++)
	{
		snake_array[0][x] = 0x81;
		snake_array[28][x] = 0x81;
	}
	
	new_apple();
	
	for(x=1;x<=snake_size;x++)
		snake_array[1][x]=x;
		
	snake_dir = SNAKE_DIR_RIGHT;
}
Ejemplo n.º 2
0
Archivo: snake.c Proyecto: 2bt/PentaJoy
uint8_t tick_snake() {
	int8_t x = buffer[pointer],
	       y = buffer[pointer + 1];

	// init?

	if(end < 0) {
		new_apple();
		end = 0;
	}

	// the end?

	if(end > 0) {
		for(int8_t i = length - 1; i >= 0; --i) {
			const int8_t cur = CYCLE(pointer - i * 2, ARRAY_SIZE(buffer));

			setLedXY(buffer[cur], buffer[cur + 1], (end % 3) * 2 + (i == 0) * 3);
		}

		++end;

		if(end > 9) {
			end = -1;
			length = 1;

			return 1;
		} else {
			return 0;
		}
	}

	// game step or animation?

	if(tick == 0) {
		// move

		switch(direction) {
			case 0:
				++x;
				break;
			case 1:
				++y;
				break;
			case 2:
				--x;
				break;
			case 3:
				--y;
				break;
		}

		x = CYCLE(x, LED_WIDTH);
		y = CYCLE(y, LED_HEIGHT);

		// save

		pointer = (pointer + 2) % ARRAY_SIZE(buffer);
		buffer[pointer] = x;
		buffer[pointer + 1] = y;

		// eaten?

		if(apple[0] == x && apple[1] == y) {
			++length;
			new_apple();
		}

		// collision

		if(collision(x, y, 1)) {
			end = 1;
		}

		// paint start of tail

		const int8_t last = CYCLE(pointer - 2, ARRAY_SIZE(buffer));
		setLedXY(buffer[last], buffer[last+1], 3);

		// clear tail

		const int8_t end = CYCLE(pointer - length * 2, ARRAY_SIZE(buffer));
		setLedXY(buffer[end], buffer[end + 1], 0);

		// paint apple

		setLedXY(apple[0], apple[1], MAX_TICK);

		// paint head

		setLedXY(x, y, 7);
	} else {
		// pulsing apple
		setLedXY(apple[0], apple[1], tick);
	}

	// tick on

	if(++tick >= MAX_TICK) {
		tick = 0;
	}

	return 0;
}
Ejemplo n.º 3
0
void game_run()
{
	int x, y, a, b, c;

	unsigned short padbuf;

	while(speed_counter > 0)
	{
		scc++;
		
		if(vibration_cntdown > 0)
		{
			printf("vibration = %d\n", vibration_cntdown);
			pad_enable_vibration(0);
			pad_set_vibration(0, 0xFF, 0x80);
			vibration_cntdown--;
			
			if(vibration_cntdown == 0)
				pad_set_vibration(0, 0, 0);
		}	
		
		PSX_ReadPad(&padbuf, NULL);
		
		if(scc == 5 && !game_over)
		{
			if(snake_dir <= SNAKE_DIR_RIGHT)
			{
				if(padbuf & PAD_UP)
					snake_dir = SNAKE_DIR_UP;
							
				if(padbuf & PAD_DOWN)
					snake_dir = SNAKE_DIR_DOWN;
			}
			else
			{	
				if(padbuf & PAD_LEFT)	
					snake_dir = SNAKE_DIR_LEFT;
							
				if(padbuf & PAD_RIGHT)
					snake_dir = SNAKE_DIR_RIGHT;
			}
			
			for(y = 0; y < 29; y++)
				for(x = 0; x < 40; x++)
				{
					if(snake_array[y][x] == snake_size)
					{
						switch(snake_dir)
						{
							case SNAKE_DIR_LEFT:
								b = y;
								a = x-1;
							break;
							case SNAKE_DIR_RIGHT:
								b = y;
								a = x+1;
							break;								
							case SNAKE_DIR_UP:
								b = y-1;
								a = x;
							break;
							case SNAKE_DIR_DOWN:
								b = y+1;
								a = x;
							break;
						}
						
						c = check_snake_collision(a,b);
						
						if(c)
						{
							snake_array[b][a] = snake_size+1;
							
							if(c==2)
							{
								snake_size++;
								score+=100;
								//printf("%d\n", score);
								SsKeyOn(2);
								new_apple();
							}
						}
						else
						{
							vibration_cntdown = 10;
							game_over = 1;
							SsKeyOff(0);
							SsKeyOn(1);
							scc = 0;
						}
						
						if(snake_array[y][x] == 1 && c!=2)
							snake_array[y][x] = 0;
						
						goto out_of_collision_checking;
					}
				}
out_of_collision_checking:				
			for(y = 0; y < 29; y++)
				for(x = 0; x < 40; x++)
					if(snake_array[y][x]&&snake_array[y][x]<0x80&&c!=2)
						snake_array[y][x]--;
			
			scc = 0;
		}
		else if(game_over)
		{
			scc++;
			
			if(scc >= 510)
			{
				if((padbuf & PAD_CROSS) && !cross_pressed)
				{
					game_setup();
					cross_pressed = 1;
				}
				else if((padbuf & PAD_CIRCLE) && !circle_pressed)
				{
					circle_pressed = 1;
					GsSetVideoMode(320, 240, pal_or_ntsc_selection());
					game_setup();
				}
			
				if(!(padbuf & PAD_CROSS))
					cross_pressed = 0;
				
				if(!(padbuf & PAD_CIRCLE))
					circle_pressed = 0;
			}
		}
			
		speed_counter--;
	}
	

	if(screen_old)
	{
		game_rect.x = 0;
		game_rect.y = 0;
		game_rect.w = 320;
		game_rect.h = 240;
		game_rect.r = 0;
		game_rect.g = 0;
		game_rect.b = 0;
		game_rect.attribute = 0;
			
		GsSortRectangle(&game_rect);
		
		game_sprite.x = 0;
		game_sprite.y = 0;
		game_sprite.w = 256;
		game_sprite.h = 240;
		game_sprite.u = 0;
		game_sprite.v = 0;
		game_sprite.r = NORMAL_LUMINOSITY;
		game_sprite.g = NORMAL_LUMINOSITY;
		game_sprite.b = NORMAL_LUMINOSITY;
		game_sprite.tpage = 5;
		game_sprite.attribute = COLORMODE(COLORMODE_16BPP);
		
		GsSortSimpleSprite(&game_sprite);
		
		game_sprite.x += 256;
		game_sprite.w = 64;
		game_sprite.tpage = 9;
			
		GsSortSimpleSprite(&game_sprite);	
			
		game_rect.w = 8;
		game_rect.h = 8;
		game_rect.attribute = ENABLE_TRANS | TRANS_MODE(0);
			
		for(y = 0; y < 29; y++)
		{
			for(x = 0; x < 40; x++)
			{
				game_rect.x = x * 8;
				game_rect.y = y * 8;
					
				if(snake_array[y][x] >= 1 && snake_array[y][x] <= 0x7F)
				{
					game_rect.r = 0;
					game_rect.g = 255;
					game_rect.b = 0;
					GsSortRectangle(&game_rect);
				}
				else if(snake_array[y][x] == 0x80)
				{
					game_rect.r = 255;
					game_rect.g = 0;
					game_rect.b = 255;
					GsSortRectangle(&game_rect);
				}
				else if(snake_array[y][x] == 0x81)
				{
					game_rect.r = 0;
					game_rect.g = 0;
					game_rect.b = 128;
					GsSortRectangle(&game_rect);
				}
			}
		}
		
		sprintf(string_buf, "SCORE: %d", score);
		game_print(string_buf, 0, 232);

		if(game_over)
		{

			game_rect.w = 320;
			game_rect.h = 240;
			
			game_rect.x = 0;
			game_rect.y = 0;
			
			if(scc<=255)x=scc;else x=255;
			
			game_rect.r = x;
			game_rect.g = x;
			game_rect.b = x;
			game_rect.attribute = ENABLE_TRANS|TRANS_MODE(2);
					
			GsSortRectangle(&game_rect);
			
			if(scc>=300)
			{
				game_center_print("GAME OVER!", 160, 120);
			}
			
			if(scc>=420)
				game_center_print("WHAT DO YOU WANT TO DO NOW?", 160, 136);
				
			if(scc>=450)
				game_center_print("PRESS X TO RESTART THE GAME.",160, 152);
			
			if(scc>=480)
				game_center_print("PRESS O FOR PAL/NTSC SELECTION SCREEN.",160,168);	
				
			if(scc>=510)
				game_center_print("MADE WITH PSXSDK BY GIUSEPPE GATTA, 2010", 160, 200);
		}
			
		GsDrawList();
		
// While the graphic synthesizer (video card) is drawing
// just sleep.				
		while(GsIsDrawing());

// Swap drawing and display Y position, and swap list array
// to use. In this way we achieve a double buffer.
		
		if(game_dispenv.y == 0)
		{
			game_dispenv.y = 256;
			game_drawenv.y = 0;
		}
		else
		{
			game_dispenv.y = 0;
			game_drawenv.y = 256;
		}
			
		GsSetDispEnv(&game_dispenv);
		GsSetDrawEnv(&game_drawenv);
			
		screen_old = 0;
	}
}