#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
#include <time.h>

#define LEFT 1
#define TOP 1
#define RIGHT 30
#define BOTTOM 28

#define PLAYER_Y 26

#define COLOR_WHITE 7
#define COLOR_RED 12
#define COLOR_GREEN 10
#define COLOR_YELLOW 14
#define COLOR_CYAN 11
#define COLOR_GRAY 8

int level;

void Difficulty_Level(void){
	system("cls");
	printf("난이도를 설정해 주세요.\n");
	printf("난이도(배) 만큼 어려워 집니다.\n");
	printf("자연수만 입력해 주세요.\n");
	while(1){
		scanf("%d", &level);
		if(level>0)break;
	}
	printf("\n\n");
	printf("아무 키나 누르면 시작합니다");
	getch();
}

void gotoxy(int x, int y){
	COORD pos = {x, y};
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}

void setColor(int color){
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color);
	//ex) setColor(14);
}

void hide_cursor(void){
	CONSOLE_CURSOR_INFO ci;
	ci.dwSize = 1;
	ci.bVisible = FALSE;
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &ci);
}

void draw_frame_once(void){
	int x, y;

    system("cls");
    
    setColor(COLOR_GRAY);

    for(x=LEFT; x<=RIGHT; x++){
        gotoxy(x, TOP);
        printf("-");
        gotoxy(x, BOTTOM);
        printf("-");
    }

    for(y=TOP; y<=BOTTOM; y++){
        gotoxy(LEFT, y);
        printf("|");
        gotoxy(RIGHT, y);
        printf("|");
    }
    gotoxy(LEFT, TOP);
    printf("┌");
    gotoxy(RIGHT, TOP);
    printf("┐");
    gotoxy(LEFT, BOTTOM);
    printf("└");
    gotoxy(RIGHT, BOTTOM);
    printf("┘");
    
    setColor(COLOR_WHITE);
}

void explosion(int x, int y){
	
	setColor(COLOR_YELLOW);

    gotoxy(x, y);
    printf("*");
    Sleep(40/level);

    gotoxy(x, y);
    printf(" ");

    gotoxy(x-1, y);
    printf("*");
    gotoxy(x+1, y);
    printf("*");
    gotoxy(x, y-1);
    printf("*");
    gotoxy(x, y+1);
    printf("*");
    Sleep(50/level);

    setColor(COLOR_RED);
    
    gotoxy(x-1, y);
    printf(" ");
    gotoxy(x+1, y);
    printf(" ");
    gotoxy(x, y-1);
    printf(" ");
    gotoxy(x, y+1);
    printf(" ");

    gotoxy(x-1, y-1);
    printf("+");
    gotoxy(x+1, y-1);
    printf("+");
    gotoxy(x-1, y+1);
    printf("+");
    gotoxy(x+1, y+1);
    printf("+");
    Sleep(50/level);
    
    gotoxy(x-1, y-1);
    printf(" ");
    gotoxy(x+1, y-1);
    printf(" ");
    gotoxy(x-1, y+1);
    printf(" ");
    gotoxy(x+1, y+1);
    printf(" ");

    setColor(COLOR_WHITE);
}

int main(){
	srand((unsigned)time(NULL));
	hide_cursor();
	
	int player_x = (RIGHT / 2)+1;
	int player_old_x = player_x;
	
	int missile_x = LEFT + 1 + rand() % (RIGHT - LEFT - 1);
	int missile_y = TOP + 1;
	
	int bullet_x = -1;
	int bullet_y = -1;
	
	int key;
	int score = 0;
	
	Difficulty_Level();
	
	draw_frame_once();
	
	while(1){
		gotoxy(player_old_x, PLAYER_Y);
		printf(" ");
		
		gotoxy(missile_x, missile_y);
		printf(" ");
		
		if(bullet_y >= 0){
			gotoxy(bullet_x, bullet_y);
			printf(" ");
		}
		
		if(kbhit()){
			key = getch();
			
			if(key == 224){
				key = getch();
				
				player_old_x = player_x;
				
				if(key == 75 && player_x > LEFT + 1)
					player_x--;
				if(key == 77 && player_x < RIGHT - 1)
					player_x++;
			}
			
			if(key == ' '){
				bullet_x = player_x;
				bullet_y = PLAYER_Y-1;
			}
		}
		missile_y++;
		
		if(bullet_y >= 0)
			bullet_y--;
			
			if(bullet_y <= TOP)
        		bullet_y = -1;
			
		if(bullet_x == missile_x && 
		(bullet_y == missile_y || bullet_y == missile_y + 1)){
			explosion(missile_x, missile_y);
			Beep(1200, 120);
			
			score++;
			
			gotoxy(45, 3);
			printf("SCORE: %d", score);
			
			missile_x = LEFT + 1 + rand() % (RIGHT - LEFT - 1);
			missile_y = TOP + 1;
			bullet_y = -1;
		}
		
		if(missile_y >= PLAYER_Y){
			Beep(400, 200);
			
			gotoxy(45, 5);
			printf("MISS!");
			
			missile_x = LEFT + 1 + rand() % (RIGHT - LEFT - 1);
			missile_y = TOP +1;
		}
		
		gotoxy(player_x, PLAYER_Y);
		setColor(COLOR_GREEN);
		printf("A");
		
		gotoxy(missile_x, missile_y);
		setColor(COLOR_RED);
		printf("V");
		
		if(bullet_y >= 0){
		    gotoxy(bullet_x, bullet_y);
		    setColor(COLOR_CYAN);
		    printf("|");
		}
			
		setColor(COLOR_WHITE);
		
		Sleep(50 / level);
		
	}
	
	return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: