#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
#include <time.h>
#define box_length 15
#define box_height 15
#define BLOCK_COLS 15
#define BOX_HEIGHT 15
#define BLOCK_WIDTH 2
#define LEFT_WALL 1
#define TOP_WALL 1
#define START_X 3
#define START_Y 2
#define RIGHT_WALL (START_X + (BLOCK_COLS - 1) * BLOCK_WIDTH + BLOCK_WIDTH)
#define BOTTOM_WALL (START_Y + BOX_HEIGHT)
int block_stack[box_length * 2+1] = {0};
void intro_game(void);
void game_control(void);
void gotoxy(int x, int y);
int left_right_move(void);
void move_down(int x);
void drow_rectangle();
int max_block(void);
void setColor(int color){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color);
//ex) setColor(14);
}
int main(){
intro_game();
game_control();
gotoxy(1,box_height+3);
printf("게임이 종료되었습니다.\n");
// drow_rectangle();
// left_right_move();
return 0;
}
void gotoxy(int x, int y){
COORD pos = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
//void hide_cursor(void){
// CONSOLE_CURSOR_INFO cursorInfo;
// cursorInfo.dwSize = 1;
// cirsorInfo.bVis
//}
void intro_game(void){
system("cls");
printf(" 블록쌓기 게임\n\n");
printf("블록이 좌우로 움직일 때\n");
printf("스페이스키를 누르면\n");
printf("블록이 떨어집니다.\n\n");
printf("아무 키나 누르면 시작합니다.");
getch();
}
void drow_rectangle(void){
int x, y, i, j;
system("cls");
for(x=LEFT_WALL; x<=RIGHT_WALL; x++){
gotoxy(x, TOP_WALL);
printf("-");
gotoxy(x, BOTTOM_WALL);
printf("-");
}
for(y=TOP_WALL; y<=BOTTOM_WALL; y++){
gotoxy(LEFT_WALL, y);
printf("|");
gotoxy(RIGHT_WALL, y);
printf("|");
}
}
int col_to_x(int col){
return START_X + col * BLOCK_WIDTH;
}
int left_right_move(void){
int col = 0;
int dir = 1;
int x;
int key;
while(1){
x = col_to_x(col);
gotoxy(x, START_Y);
setColor(4);
printf("▩");
setColor(15);
Sleep(70);
if(kbhit()){
key = getch();
if(key == ' '){
gotoxy(x, START_Y);
printf(" ");
block_stack[col]++;
return col;
}
}
gotoxy(x, START_Y);
printf(" ");
col += dir;
if(col >= BLOCK_COLS - 1){
col = BLOCK_COLS - 1;
dir = - 1;
}
if(col <= 0){
col = 0;
dir = 1;
}
}
}
//void game_control(void){
// int x;
// int count = 0;
//
// system("cls");
// draw_ractangle(box_length, box_height);
//
// while(count < box_height){
// gotoxy(box_length * 2 + 5, 4);
// printf("시도한 횟수:%2d", count + 1);
//
// gotoxy(box_length * 2 + 5, 5);
// printf("쌓인 블록수:%2d", max_block());
//
// x = left_right_move();
// getch();
// move_down(x);
// count++;
// }
//}
void move_down(int col){
int x = col_to_x(col);
int y;
int drop_y;
drop_y = BOTTOM_WALL - block_stack[col];
for(y=START_Y; y<=drop_y; y++){
gotoxy(x, y);
setColor(4);
printf("▩");
setColor(15);
Sleep(70);
if(y != drop_y){
gotoxy(x, y);
printf(" ");
}
}
}
int max_block(void){
int max = 0;
for(int x=0; x<box_length * 2+1; x++){
if(max < block_stack[x]) max = block_stack[x];
// printf("%2d", block_stack[x]);
}
return(max);
}
void game_control(void){
int count = 0;
int col;
int score = 15;
int key;
system("cls");
drow_rectangle();
gotoxy(1, BOTTOM_WALL + 2);
printf("Press SPACE to drop the block.");
while(count < BOX_HEIGHT){
setColor(12);
gotoxy(RIGHT_WALL +4, 3);
printf("Total BLocks: %2d", BOX_HEIGHT);
gotoxy(RIGHT_WALL +4, 4);
printf("Try Count : %2d", count);
gotoxy(RIGHT_WALL +4, 5);
printf("Max Height : %2d", max_block());
setColor(14);
gotoxy(RIGHT_WALL +4, 6);
printf("Your Score : %2d", score-max_block());
setColor(15);
col = left_right_move();
move_down(col);
count++;
}
setColor(12);
gotoxy(RIGHT_WALL +4, 5);
printf("Max Height : %2d", max_block());
gotoxy(RIGHT_WALL +4, 4);
printf("Try Count : %2d", count);
gotoxy(RIGHT_WALL +4, 5);
printf("Max Height : %2d", max_block());
setColor(15);
}
To embed this project on your website, copy the following code and paste it into your website's HTML: