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,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1},
{1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,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;
}
}
}
자,이제 이 게임 도 끝 났 습 니 다!이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
C 언어 체인 시계는 뱀을 탐식하는 작은 게임을 실현한다본고의 실례는 여러분에게 C 언어 체인표가 뱀 탐식 게임을 실현하는 구체적인 코드를 공유하여 참고하도록 하였으며, 구체적인 내용은 다음과 같다. 프로젝트 이름: 뱀놀이 운영 환경: Linux 프로그래밍 언어: C 언...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.