#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <Windows.h>
#define LEFT 75
#define RIGHT 77
#define UP 72
#define DOWN 80
int x=15, y=15,i,j,x1,y1,dx,dy,s,l=1,level=1;
void textcolor(int colorNum) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colorNum);
}
void setup()
{
int i,j,s;
system("cls");
gotoxy(0,0);
for(i=1;i<=10;i++){
for(j=1;j<=15;j++){
if(i==1||j==1||j==15){
printf("*");
}
else if(i==6&&j==5){
textcolor(9);
printf("*");
textcolor(7);
}
else{
printf(" ");
}
}
printf("\n");
}
s=17;
gotoxy(17,0);
for(i=1;i<=10;i++){
for(j=1;j<=15;j++){
if(i==1||j==1||j==15){
printf("*");
}
else if(i==6&&j==5){
textcolor(9);
printf("*");
textcolor(7);
}
else{
printf(" ");
}
}
gotoxy(s,i);
}
}
void gotoxy(int x, int y)
{
COORD Pos = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);
}
void print_front()
{
char str[50] = "L O A D I N G . . . ";
for(int i=0;str[i]!=NULL;i++)
{
printf("%c",str[i]);
Sleep(250);
}
}
int main()
{
srand(time(NULL));
char c;
int size=1;
gotoxy(5,6);
printf("glass bridge game");
gotoxy(5,10);
print_front();
setup();
gotoxy(20,15);
if(rand()%2==0){
x1=4;
y1=5;
dx=21;
dy=5;
}
else{
x1=21;
y1=5;
dx=4;
dy=5;
}
while (1)
{
if (_kbhit()) // 2. while문안에서 키보드 눌렸을 시 if문이 실행된다.
{
c = _getch(); // 3. 방향키가 입력됬을 때 224 00 이 버퍼에 있다. 224부터 빼준다.
if (c == -32) // 4. -32로 입력되면
{
c = _getch();// 5. 뒤의 값 00을 판별하여 좌우상하 출력
gotoxy(x,y);
printf(" ");
switch (c)
{
case LEFT:
if(x-1==0||(x-1==17&&y<10)||(x-1==14&&y<10)||(x-1==31&&y<10))
{
}
else{
x=x-size;
}
break;
case RIGHT:
if((x+1==17&&y<10)||(x+1==14&&y<10)||(x+1==31&&y<10))
{
}
else{
x=x+size;
}
break;
case UP:
if((x==17&&y-1<10)||(x==14&&y-1<10)||(x==31&&y-1<10))
{
}
else{
y=y-size;
}
break;
case DOWN:
if(y<48)
{
y=y+size;
}
break;
}
if(x==x1&&y==y1){
setup();
x=15;
y=15;
if(rand()%2==0){
x1=4;
y1=5;
dx=21;
dy=5;
}
else{
x1=21;
y1=5;
dx=4;
dy=5;
}
l++;
}
if(l==6){
printf("you win!\n");
printf("play again?(1=yes,0=no)");
scanf("%d",&s);
if(s==1){
setup();
l=0;
x=15;
y=15;
}
else{
break;
}
}
if(x==dx&&y==dy){
system("cls");
gotoxy(5,5);
printf("Game over: wrong glass\n");
printf("your score: %d \n",l);
printf("play again?(1=yes,0=no)");
scanf("%d",&s);
if(s==1){
setup();
l=0;
x=15;
y=15;
}
else{
break;
}
}
gotoxy(x,y);
printf("#");
gotoxy(40,5);
printf("score: %d",l);
gotoxy(41,6);
}
}
}
}