C 언어 코드 러시아 블록 실현

여기 서 여러분 을 위해 어떻게 C 언어 로 러시아 블록 을 실현 하 는 지 두 드 려 보 겠 습 니 다.
우선 서로 다른 프로젝트 를 추가 할 수 있 도록 CodeBlocks 라 는 소프트웨어 를 사용 하 는 것 을 추천 합 니 다.
코드 에 많은 주석 이 있어 서 이해 하기 쉽 습 니 다!
다음은 효과 도와 모든 코드 및 주석 입 니 다.여러분 은 새로운 내용 을 보고 스스로 추가 할 수 있 습 니 다!

1.우선 main.c 파일:

#include <stdio.h>
#include <stdlib.h>
#include "game.h"

int main()
{
 gameInit();
 return 0;
}
2.그리고 my windows.h 파일:

#ifndef MYWINDOWS_H_INCLUDED
#define MYWINDOWS_H_INCLUDED

//       -      
#include <windows.h>

//      
extern void initHandle();

//     
extern void setColor(int color);

//       
extern void setPos(int x, int y);

//         
extern void setCursorVisible(int flag);

//     
extern void closeHandle();

#endif // MYWINDOWS_H_INCLUDED
3.다음은 my windows.c 파일 입 니 다.

#include "mywindows.h"

HANDLE handle;

//      
void initHandle()
{
 handle = GetStdHandle(STD_OUTPUT_HANDLE);
}

//     
void setColor(int color)
{
 SetConsoleTextAttribute(handle, color);
}

void setPos(int x, int y)
{
 //, ,
 COORD coord = {x*2, y};
 SetConsoleCursorPosition(handle, coord);
}

//         
void setCursorVisible(int flag)
{
 CONSOLE_CURSOR_INFO info;
 info.bVisible = flag; //      
 info.dwSize = 100; //    1-100
 SetConsoleCursorInfo(handle, &info);
}

//     
void closeHandle()
{
 CloseHandle(handle);
}
4.다음은 data.h 파일,즉 데이터베이스 의 저장 주소 입 니 다.

#ifndef DATA_H_INCLUDED
#define DATA_H_INCLUDED


//    \    
//     -    
//    
extern int windowShape[25][26];

extern int block[7][4][4][4];

#endif // DATA_H_INCLUDED
5.데이터베이스 내용:data.c

#include "data.h"

//     

int windowShape[25][26] =
{
 {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
 {1,0,0,0,0,0,0,0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},{0,0,0,0}},
 {{1,0,0,0},{1,0,0,0},{1,0,0,0},{1,0,0,0}},
 {{1,1,1,1},{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,0,0}}
 },
 {
 //T
 {{1,1,1,0},{0,1,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,0}},
 {{0,1,0,0},{1,1,1,0},{0,0,0,0},{0,0,0,0}},
 {{1,0,0,0},{1,1,0,0},{1,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,0,0},{1,1,0,0},{0,0,0,0},{0,0,0,0}}
 }


};
6.게임 머리:game.h:

#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED

//       

#include <stdio.h>
#include <time.h>

typedef struct{
 int x;
 int y;
 int shape;
 int status;
 int color;
}BLOCK;

//            ,      
BLOCK curBlock;
BLOCK nextBlock;

//        
extern void gamePool(int x, int y);

//       
extern void printRule();

//        
extern void printGradeLevel(int num);

//     
extern void gameTime(clock_t startTime);

//       
extern void printBlock(int x, int y, int shape, int status, int color);

//       
extern void delBlock(int x, int y, int shape, int status);

//    
extern void leftBlock();

//    
extern void rightBlock();

//    
extern int downBlock();

//    
extern void changeBlock();

//      
extern void bottomBlock();

//    
extern void pause();

//           
extern void startBlock();

//         
extern void blockNext();

//    :                 
extern void copyBlock();

//    
extern int crash(int x, int y, int shape, int status);

//    
extern void saveBlock();

//     
extern void updateGamePool();

//    
extern void lineClear();

//    
extern void lineDown(int line);

//      
extern void gameInit();


#endif // GAME_H_INCLUDED
7.마지막 부분,게임 내용,game.c 파일:

#include "game.h"
#include "mywindows.h"
#include "data.h"
#include <conio.h>

int level = 1;
int grade = 100;

//      
void gamePool(int x, int y)
{
 int i, j;
 for(i=0;i<25;i++)
 {
 for(j=0;j<26;j++)
 {
  if(windowShape[i][j] == 1)
  {
  setColor(0xc0);
  setPos(x+j,y+i);
  printf(" "); // printf("%2s", "");
  }
 }
 }
}

//       
void printRule()
{
 setColor(0x0f);
 setPos(31, 9);
 printf("    :");
 setPos(32, 11);
 printf(" a A  ");
 setPos(32, 12);
 printf(" d D  ");
 setPos(32, 13);
 printf(" s S  ");
 setPos(32, 14);
 printf(" w W  ");
 setPos(32, 15);
 printf("     ");
 setPos(32, 16);
 printf("       ");

}

void printGradeLevel(int num)
{
 switch(num)
 {
 case 0: break;
 case 1: grade += 10;break;
 case 2: grade += 25;break;
 case 3: grade += 50;break;
 case 4: grade += 70;break;
 }

 //  -->  

 setColor(0x09);
 setPos(3,6);
 printf("  :%d", grade);

 setColor(0x0a);
 setPos(3,7);
 printf("  :%d", level);
}

void gameTime(clock_t startTime)
{
 //clock_t endTime = clock();
 //clock_t ti = (endTime-startTime)/CLOCKS_PER_SEC;
 setColor(0x0d);
 setPos(3,8);
 printf("        :%ld", (clock()-startTime)/CLOCKS_PER_SEC);
}

//       
void printBlock(int x, int y, int shape, int status, int color)
{
 int i,j;
 setColor(color);
 for(i=0;i<4;i++)
 {
 for(j=0;j<4;j++)
 {
  if(1 == block[shape][status][i][j])
  {
  setPos(x+j,y+i);
  printf("■");
  }
 }
 }
}

//       
void delBlock(int x, int y, int shape, int status)
{
 int i, j;
 for(i=0;i<4;i++)
 {
 for(j=0;j<4;j++)
 {
  if(1 == block[shape][status][i][j])
  {
  setPos(x+j,y+i);
  printf(" "); //      
  }
 }
 }
}

//    
void leftBlock()
{
 //         ,    ,    
 if(crash(curBlock.x-1, curBlock.y, curBlock.shape,curBlock.status) == -1)
 {
 return;
 }
 delBlock(curBlock.x,curBlock.y,curBlock.shape,
  curBlock.status);
 curBlock.x -= 1;
 printBlock(curBlock.x,curBlock.y,curBlock.shape,
  curBlock.status,curBlock.color);
}

//    
void rightBlock()
{
 if(crash(curBlock.x+1, curBlock.y, curBlock.shape,curBlock.status) == -1)
 {
 return;
 }
 delBlock(curBlock.x,curBlock.y,curBlock.shape,
  curBlock.status);
 curBlock.x += 1;
 printBlock(curBlock.x,curBlock.y,curBlock.shape,
  curBlock.status,curBlock.color);
}

//    
int downBlock()
{
 if(crash(curBlock.x, curBlock.y+1,curBlock.shape,curBlock.status) == -1)
 {
 //       ,     
 saveBlock();
 lineClear();
 updateGamePool();
 copyBlock();
 return -1;
 }else if(crash(curBlock.x, curBlock.y+1,curBlock.shape,curBlock.status) == -2)
 {
 //    
 return -2;
 }else{
 delBlock(curBlock.x,curBlock.y,curBlock.shape,
  curBlock.status);
 curBlock.y += 1;
 printBlock(curBlock.x,curBlock.y,curBlock.shape,
  curBlock.status,curBlock.color);
 return 0;
 }

}

//    
void changeBlock()
{
 if(crash(curBlock.x, curBlock.y, curBlock.shape, (curBlock.status+1)%4) == -1){
 return;
 }
 delBlock(curBlock.x,curBlock.y,curBlock.shape,
  curBlock.status);
 curBlock.status = (curBlock.status+1)%4;
 printBlock(curBlock.x,curBlock.y,curBlock.shape,
  curBlock.status,curBlock.color);
}

//      
void bottomBlock()
{
 while(1){
 //        
 if(downBlock() !=0){
  return;
 }
 }
}

//    
void pause()
{
 //
}

//           
void startBlock()
{
 //          
 srand((unsigned)time(NULL));
 //   curBlock
 curBlock.x = 22;
 curBlock.y = 1;
 //rand       
 curBlock.shape = rand()%7; //0-6
 curBlock.status = rand()%4; //0-3
 curBlock.color = rand()%0x10;
 if(0x00 == curBlock.color)
 {
 curBlock.color = 0x0f;
 }
 printBlock(curBlock.x, curBlock.y, curBlock.shape,
  curBlock.status, curBlock.color);
}

//         
void blockNext()
{
 //   nextBlock
 delBlock(nextBlock.x,nextBlock.y,
  nextBlock.shape,nextBlock.status);
 nextBlock.x = 34;
 nextBlock.y = 2;
 nextBlock.shape = rand()%7;
 nextBlock.status = rand()%4;
 nextBlock.color = rand()%0x10;
 if(0x00 == nextBlock.color)
 {
 nextBlock.color = 0x0f;
 }
 printBlock(nextBlock.x,nextBlock.y,
  nextBlock.shape,nextBlock.status,nextBlock.color);
}

//    :                 
void copyBlock()
{
 //    =     
 curBlock = nextBlock;
 curBlock.x = 22;
 curBlock.y = 1;
 printBlock(curBlock.x, curBlock.y, curBlock.shape,
  curBlock.status, curBlock.color);
 blockNext();
}

//    
//x,y         , status     
//    -1,     0,       -2
int crash(int x, int y, int shape, int status)
{
 int i,j;
 for(i=0;i<4;i++)
 {
 for(j=0;j<4;j++)
 {
  if(block[shape][status][i][j] == 1)
  {
  if(windowShape[i+y][j+x-15] == 1)
  {
   //          
   if(curBlock.x == 22 && curBlock.y == 1)
   {
   return -2;
   }
   return -1;
  }
  }
 }
 }
 return 0;
}

//    
void saveBlock()
{
 int i,j;
 for(i=0;i<4;i++)
 {
 for(j=0;j<4;j++)
 {
  if(block[curBlock.shape][curBlock.status][i][j] == 1)
  {
  windowShape[i+curBlock.y][j+curBlock.x-15] = 1;
  }
 }
 }
}

//     
void updateGamePool()
{
 int i,j;
 //         
 for(i=23;i>0;i--)
 {
 for(j=1;j<15;j++)
 {
  if(windowShape[i][j] == 1)
  {
  setColor(0x0e);
  setPos(j+15,i);
  printf("■");
  }else{
  setColor(0x00);
  setPos(j+15,i);
  printf(" ");
  }
 }
 }
}

//    
//    ->        1
void lineClear()
{
 int i,j;
 int num = 0; //        
 for(i=23;i>0;i--)
 {
 int total = 0;
 for(j=1;j<15;j++)
 {
  total += windowShape[i][j];
 }
 if(total == 14)
 {
  //  
  lineDown(i);
  i += 1;
  num += 1;
 }
 }
 printGradeLevel(num);
}

//    
//        ,         
void lineDown(int line)
{
 int i,j;
 for(i=line;i>1;i--)
 {
 for(j=1;j<15;j++)
 {
  windowShape[i][j] = windowShape[i-1][j];
 }
 }
}


//      
void gameInit()
{
 //   ,       
 //clock_t startTime = clock();
 initHandle();
 setCursorVisible(FALSE);

 gamePool(15,0);
 printRule();
 printGradeLevel(0);

 startBlock();
 blockNext();
 //      ,    ,           
 clock_t start,stop;
 start = clock();

 while(1) //for(;;)
 {
 //         
 if(kbhit())
 {
  switch(getch())
  {
  case 'a':
  case 'A':
  case 75:
  leftBlock();break;
  case 'd':
  case 'D':
  case 77:
  rightBlock();break;
  case 's':
  case 'S':
  case 80:
  downBlock();break;
  case 'w':
  case 'W':
  case 72:
  changeBlock();break;
  case 32:
  pause();break;
  case 13:
  bottomBlock();break;
  }
 }
 //    
 stop = clock();
 if(stop-start>0.5*CLOCKS_PER_SEC)
 {
  downBlock();
  //    
  start = stop;
 }
 }
}
자,이제 이 게임 도 끝 났 습 니 다!
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기