#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77
int a=0;
int b=0;
int j,k;
void showBlock(int shape,int,int);
void remove(int,int,int);
void setCursor(int x, int y);
int Block[7][4][4][4]=
{
{
{{0,1,1,1},{0,0,0,1},{0,0,0,0},{0,0,0,0}}, //┘
{{0,0,0,1},{0,0,0,1},{0,0,1,1},{0,0,0,0}},
{{0,0,0,0},{0,1,0,0},{0,1,1,1},{0,0,0,0}},
{{0,1,1,0},{0,1,0,0},{0,1,0,0},{0,0,0,0}},
},
{
{{0,1,0,0},{1,1,1,0},{0,0,0,0},{0,0,0,0}},//┴
{{0,1,0,0},{0,1,1,0},{0,1,0,0},{0,0,0,0}},
{{1,1,1,0},{0,1,0,0},{0,0,0,0},{0,0,0,0}},
{{0,0,1,0},{0,1,1,0},{0,0,1,0},{0,0,0,0}},
},
{
{{1,1,1,1},{0,0,0,0},{0,0,0,0},{0,0,0,0}},//─
{{0,0,0,1},{0,0,0,1},{0,0,0,1},{0,0,0,1}},
{{1,1,1,1},{0,0,0,0},{0,0,0,0},{0,0,0,0}},
{{0,0,0,1},{0,0,0,1},{0,0,0,1},{0,0,0,1}},
},
{
{{0,1,1,0},{0,1,1,0},{0,0,0,0},{0,0,0,0}}, //┌┐
{{0,1,1,0},{0,1,1,0},{0,0,0,0},{0,0,0,0}}, //└┘
{{0,1,1,0},{0,1,1,0},{0,0,0,0},{0,0,0,0}},
{{0,1,1,0},{0,1,1,0},{0,0,0,0},{0,0,0,0}},
},
{
{{0,1,1,1},{0,1,0,0},{0,0,0,0},{0,0,0,0}}, //└
{{0,0,1,1},{0,0,0,1},{0,0,0,1},{0,0,0,0}},
{{0,0,0,0},{0,0,0,1},{0,1,1,1},{0,0,0,0}},
{{0,1,0,0},{0,1,0,0},{0,1,1,0},{0,0,0,0}},
},
{
{{0,1,1,0},{0,0,1,1},{0,0,0,0},{0,0,0,0}}, //┐
{{0,0,1,0},{0,1,1,0},{0,1,0,0},{0,0,0,0}}, //└
{{0,1,1,0},{0,0,1,1},{0,0,0,0},{0,0,0,0}},
{{0,0,1,0},{0,1,1,0},{0,1,0,0},{0,0,0,0}},
},
{
{{0,1,1,0},{0,0,1,1},{0,0,0,0},{0,0,0,0}}, //┌
{{0,0,1,0},{0,1,1,0},{0,1,0,0},{0,0,0,0}}, //┘
{{0,1,1,0},{0,0,1,1},{0,0,0,0},{0,0,0,0}},
{{0,0,1,0},{0,1,1,0},{0,1,0,0},{0,0,0,0}},
},
};
int main()
{
int x=0,y=0;
srand((unsigned int)time(NULL));
int shape=rand()%7;
setCursor(x,y);
showBlock(shape,x,y);
while(1)
{
int ge=getch();
if(ge==UP)
{
setCursor(x,y);
a++;
remove(shape,x,y);
showBlock(shape,x,y);
}
else if(ge==DOWN)
{
setCursor(x,y-1);
remove(shape,x,y);
y++;
setCursor(x,y);
showBlock(shape,x,y);
}
else if(getch()==RIGHT)
{
//setCursor(x+2,y);
remove(shape,x,y);
x+=2;
showBlock(shape,x,y);
}
else if(getch()==LEFT)
{
//setCursor(x+2,y);
remove(shape,x,y);
x-=2;
showBlock(shape,x,y);
}
}
return 0;
}
void showBlock(int shape,int x, int y)
{
for(j=0;j<4;j++)
{
setCursor(x,y+j);
for(k=0;k<4;k++)
{
if(Block[shape][a%4][j][k]==1)
{
printf("■");
}
else
printf(" ");
}
printf("\n");
}
}
void remove(int shape,int x, int y)
{
for(j=0;j<4;j++)
{
setCursor(x,y+j);
for(k=0;k<4;k++)
{
if(Block[shape][a%4][j][k]==1)
{
printf(" ");
}
}
printf("\n");
}
}
void setCursor(int x, int y) //커서 생성 및 이동 함수 .
{
COORD pos = {x, y};
pos.X=x;
pos.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
테트리스를 만들려고하는데 소스를 현재 이렇게 짯습니다.
근데.. 블럭을 이동시킬때마다 이전것을 지우고 이동 하려 하는데 잘 안되네요 ..
제가 생각하기에는 지우는 remove함수가 맞는 것 같은데 잔상이 남네요.. 왜그런지 잘 좀 알려주시면 감사하겠습니다. ㅠ
빨간줄로좀 해주세요 .ㅠㅠ