Ejemplo n.º 1
0
//Main program
int main() {
	
	/* Initialize SDL’s video system and check for errors */
	if (SDL_Init(SDL_INIT_VIDEO) != 0) {

		printf("Unable to initialize SDL: %s\n", SDL_GetError());
		return 1;
	}
	
	/* Make sure SDL_Quit gets called when the program exits! */
	atexit(SDL_Quit);
	
	/*set window title*/
	SDL_WM_SetCaption("Essi Invaders", "P");
	
	/* Attempt to set a 800x600 8 bit color video mode */
	screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 8, SDL_DOUBLEBUF );
	
	if (screen == NULL) {
		
		printf("Unable to set video mode: %s\n", SDL_GetError());
		return 1;
	}

	//load images
	load_image("titlescreen.bmp", &title_screen, magenta);
	load_image("cmap.bmp", &cmap, magenta);
	load_image("target1.bmp", &invaders_img[0], magenta);
	load_image("target2.bmp", &invaders_img[1], magenta);
	load_image("target3.bmp", &invaders_img[2], magenta);
	load_image("target4.bmp", &invaders_img[3], magenta);
	load_image("target5.bmp", &invaders_img[4], magenta);
	load_image("target6.bmp", &invaders_img[5], magenta);
	load_image("target7.bmp", &invaders_img[6], magenta);
	load_image("target8.bmp", &invaders_img[7], magenta);
	load_image("target9.bmp", &invaders_img[8], magenta);
	load_image("target10.bmp", &invaders_img[9], magenta);
	load_image("target11.bmp", &invaders_img[10], magenta);
	load_image("target12.bmp", &invaders_img[11], magenta);
	load_image("spaceship.bmp", &player_img, magenta);
	load_image("essi.bmp", &logo_img, magenta);
	load_image("saucer.bmp", &saucer_img, magenta);
	load_image("gameover.bmp", &game_over_img, magenta);
	load_image("damage.bmp", &damage_img, lime);
	load_image("damagetop.bmp", &damage_top_img, lime);

	Uint32 next_game_tick = SDL_GetTicks();
	int sleep = 0;
	Uint8 *keystate = 0;
	int quit = 0;
	SDL_Event event;

	init_score();
	init_bases();
	init_misil();
	init_invaders();
	init_player();
	init_saucer();
	init_bullets(bullets, P_BULLETS);
	init_bullets(enemy_bullets, E_BULLETS);
	state = menu;
	title_time = SDL_GetTicks();
		
	/* Animation */
	while (quit == 0) {
		
		/* Grab a snapshot of the keyboard. */
		keystate = SDL_GetKeyState(NULL);
		
		while (SDL_PollEvent(&event)) {

			switch(event.type) {
				
				case SDL_KEYDOWN:
					
					switch( event.key.keysym.sym ) {
					
						//exit out of game loop if escape is pressed
						case SDLK_ESCAPE:
							
							quit = 1;
						break;
						
						case SDLK_SPACE:	
						
							if (state == menu) {

								state = game;

							} else if (state == game){
								
								player_shoot();
								saucer_ai();
							
							} else if (state == game_over) {
							
								init_invaders();
								init_bases();
								init_score();
								init_player();
								state = game;
								match = -1;
							}
						break;
						
						default:
						break;
					}
				break;
				
				case SDL_KEYUP:
					switch( event.key.keysym.sym ) {
						
						case SDLK_UP:
							
							move_misil(up);
						break;
						
						case SDLK_DOWN:	
						
							move_misil(down);
						break;
						
						default:
						break;
					}
			}
		}
	
		draw_background();

		if (state == menu) {
			
			char s[] = "Press SPACEBAR to start";
			SDL_Rect src[60];
			
			int i;

			if (title_time + 2000 < SDL_GetTicks())  {
			
				src[0].x = (SCREEN_WIDTH/100)*30;		//Yellow Background of Space Invaders
				src[0].y = 40;
				src[0].w = 440;
				src[0].h = 230;
			
				SDL_FillRect(screen, &src[0], 248);
			
			} else {
			
				int y = 0;

				for (i = 0; i < 60; i++) {
				
					src[i].x = 0;
					src[i].y = y;
					src[i].w = SCREEN_WIDTH;
					src[i].h = 10;

					SDL_FillRect(screen, &src[i], 227);
				
					y += 10;							
				}
			
				for (i = 0; i < 60; i++) {

					SDL_FillRect(screen, &src[i], rand() % 255);

				}
			}
			
			draw_title_screen();	
			draw_string(s, (SCREEN_WIDTH / 2) - (strlen(s) * 10), 500);

		} else if (state == game) {

			//move player
			if (keystate[SDLK_LEFT]) {
				
				move_player(left);
			}

			if (keystate[SDLK_RIGHT]) {
				
				move_player(right);
			}
			
			draw_hud();
			draw_player();
			draw_bases();
			draw_invaders();
			draw_saucer();
			draw_serviceProjectMatch(match);
			draw_bullets(bullets, P_BULLETS);
			draw_bullets(enemy_bullets, E_BULLETS);
			enemy_hit_collision();
			player_hit_collision();
			enemy_base_collision();
			saucer_hit_collision();
			bullet_base_collision(enemy_bullets, E_BULLETS, 1);
			bullet_base_collision(bullets, P_BULLETS, 0);
			enemy_player_collision();
			move_invaders(invaders.speed);
			move_saucer();
			movenemy_bullets(bullets, P_BULLETS, -30);
			movenemy_bullets(enemy_bullets, E_BULLETS, 15);
			calculate_level();
			enemy_ai();
			game_over_ai();
			pause_game();
		
		} else if (state == game_over) {
			
			draw_hud();
			draw_player();
			draw_bases();
			draw_invaders();
			draw_saucer();
			draw_bullets(bullets, P_BULLETS);
			draw_bullets(enemy_bullets, E_BULLETS);
			draw_game_over();
		
		} else if (state == pause) {
			
			draw_hud();
			draw_player();
			draw_bases();
			draw_invaders();
			draw_saucer();
			draw_bullets(bullets, P_BULLETS);
			draw_bullets(enemy_bullets, E_BULLETS);
			pause_game();
		}

		/* Ask SDL to update the entire screen. */
		SDL_Flip(screen);

		next_game_tick += 1000 / 30;
		sleep = next_game_tick - SDL_GetTicks();
	
		if( sleep >= 0 ) {

            		SDL_Delay(sleep);
        	}
	}
	
	return 0;
}
Ejemplo n.º 2
0
void space(void) {
	byte c;
	byte move_left, move_right;
	char delay_count;
	byte do_exit;

	move_left = 0;
	move_right = 0;
	shot_y = 0;
	shot_x = 0;
	inv_shot_y = 0;
	inv_shot_x = 0;
	do_exit = 0;

	clrbuf();
	init_game();

	delay_count = 0;
	do {
		clrbuf();
		draw_invaders();
		draw_tank(tank_pos);
		
		// tank shot
		tank_shot();

		// invader shot
		invaders_shot();

		buf2screen();

		// delay invader movement
		if (++delay_count >= 5 - ((char)inv_pos_y / 3)) {
			delay_count = 0;
			move_invaders();
		}

		// delay
		if (inv_count < 14)
			delay_ms((14 - inv_count));

		// read keyboard
		c = io_read(129);
		if (c == 0xe0) {
			c = io_read(129);
			if (c == 0x6b) { // left down
				move_left = 1;
			} else if (c == 0x74) { // right down
				move_right = 1;
			} else if (c == 0x6b + 0x80) { // left up
				move_left = 0;
			} else if (c == 0x74 + 0x80) { // right up
				move_right = 0;
			}
		} else if (c == 0x29) { // shoot
			if (shot_y == 0) {
				shot_x = (tank_pos + 1) * 2 + 1;
				shot_y = 90;
			}
		}
		else if (c == 0x76) { // escape
			do_exit = 1;
		}

		// move tank
		if (move_left) {
			if (tank_pos > 0)
				tank_pos -= 1;
		} else if (move_right) {
			if (tank_pos < 76)
				tank_pos += 1;
		}

	} while (!do_exit);
}