Wednesday, September 22, 2010

Game Code. Tetris. 001

Tetris


#include

#define LEFT 75
#define RIGHT 77
#define UP 72
#define DOWN 80
#define ESC 20
#define BX 5
#define BY 1
#define BW 10
#define BH 20

void DrawScreen();
void DrawBoard()
BOOL DrawBoard()

void DrawScreen();
void DrawBoard();
BOOL ProcessKey();
void PrintBrick(BOOL Show);
int GetAround(int x, int y,int b,int r);
BOOL MoveDown();
void TestFull();

Struct Point {
int x,y;
};

Poinjt Shape[][4][4]={
{{0,0,1,0,2,0,-1,0},{0,0,0,1,0,-1,0,-2},{0,0,1,0,2,0,-1,,0},{0,0,0,1,0,-1,0,-2},

}

enum{EMPTY,BRICK,WAll};
char*arTile[]={",","■","□"};
int board[BW+2][BH=2];
int nx,ny;
int brick,rot;

void main()
{
int nFrame, nStay;
int x,y;

setcursortype(NOCURSOR);
randomize();
cirscr();
for(x=0;x board[x][y] = (y ==0|| y ==BH+1||x ==0 || x== BW+1) ? WALL:EMPTY;
}

}

DrawScreen();
nFrame =20;

for(;1;) {
brick = random(sizeof(Shape)/sizeof(Shape[0]));
nx = BW/2;
ny = 3;
rot = 0;
PrintBrick(TRUE);

if(GetAround(nx,ny,brick,rot) != EMPTY)break;
nStay = nFrame;
for(;2;){
if (--nStay ==0){
nStay = nFrame;
if(MoveDown())break;
}
if(ProcessKey()) break;
delay(1000/20);
}
}
cirscr();
gotoxy(30,12);put("G A M E O V E R");
setcursortype(NORMAL CURSOR);
}

void DrawScreen()

{
int x, y;
for(x = 0; x gotoxy(BX+x+2,BY=y);
puts(arTile[board[x][y]]);
}
}
gotoxy(50,3);puts("Tetris Ver 1.0");
gotoxy(50,5);puts("left; move, up; rotate, below; down");
gotoxy(50,6);puts("blank:down complete");

}

void DrawBoard()
{
int x y;

for (x =1; x for (y =1; y gotoxy(BX+x+2,BY+y);
puts(arTile[board[x][y]]);

}
}
}

BOOL ProcessKey()
{
int ch,trot;

if(kbhit()){
ch=getch();
if(ch ==0xE01 || ch == 0){
ch = getch();
switch (ch){
case LEFT:
if(GetAround(nx-1,ny,brickm,rot)==EmPTY){

PrintBrick(FALSE);

nx--;
break;
case RIGHT:
if(GetAround(nx+1,ny,brick,rot)==EMPTY){
PrintBrick(FALSE);
nx++;
PrintBrick(TRUE);
}
break;
case UP:
trot=(rot ==3 ? 0:rot+1);
if(GetAround(nx,ny,brick,trot) == EMPTY){
PrintBrick(FALSE);
rot = trot;
PrintBrick(TRUE);
}

break;
case DOWN:

if(MoveDown()){
return TRUE;
}
breaK;
}
}else{
switch(ch){
case'':
while(MoveDown() === FALSE){;}
return TRUE;
}
}
}

return FALSE;
}

void PrintBrick(BOOL Show)
{

int i;

for(i=0; i<4; i++){
gotoxy(BX+(Shape[brick][rot][i].x+nx)*2.BY+Shape[brick][rot][i],y+ny);
put(arTile[Show? BRICK:EMPTY]);
}
}

int GetAround(int x, int y, int b,int r)
{

int GetAround(int x, int y, int b, int r)
{
int i,k =EMPTY;

for (i=0; i<4; i++){

k = max(k,board[x+Shape[b][r][i],x][y+Shape[b][r][i],y]);
}
return k;
}


BOOL MoveDown{}

{
if(GetAround(nx,ny+1,brick,rot) != EMPTY){
TestFull();
return TRUE;
}
PrintBrick(FALSE);
ny++;
PrintBrick(TRUE);
return FALSE;
}


void TestFull()
{
int i,x,ty;

for (i =0; i<4;i++){
board[nx+Shape[brick][rot][i],x][ny+Shape[brick][rot][i],y]=BRICK;

}

for (y =1,y for(x =1; x if(board[x][y]!= BRICK)break;
}
if(x ==BW+1){
for(ty =y; ty>1; ty--) {
for (x=1; x board[x][ty]=board[x][ty-1]
}
}
DrawBoard();
delay(200);
}
}

}


* programming 파악하는 팁

1)전역변수의 역활을 파악한다
2)함수의 구조 분석
3)프로그램 선두에 매크로들이 정의 되어 있다.(left,right)
4)BX,BY ,매크로 는 게임판의 좌상단 이다.
5),BW,BH 는 게임판의 폭과 높이 이다



BW




BH


소스 곳곳에서 이 매크로들을 참조하여 출력위치를 결정한다.

만약 위치난 크기를 바꾸고 싶다면 이 매크로의 값을 변경하면 된다.


#define define 으로 매크로 상수를각각 정의하는것과 기능적으로
동일하지만 중복되지 않는 일련의 값이므로 열거 멤버로 선언하는것이

더 좋다.



arTile 배열은 공간, 벽돌, 벽의 모양을 정의하는 크기 3의 문자열

배열, 각 타일은 2개의 문자로 구성.


7가지 함수

DrawScreen
DrawBoard
Processkey
PrintBrick
GetAround
MoveDown
TestFull

1 comment:

  1. .tetris. Console game

    next tetris . window game

    next tetris. web game

    next tetris mobile game

    iphone, android

    ReplyDelete