«   2022/07   »
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Tags
more
Archives
Today
0
Total
33,758
관리 메뉴

보안을 꿈꾸는 학생

C언어 테트리스 프로젝트 본문

C, C++ 프로젝트

C언어 테트리스 프로젝트

lovelyoverflow 2017. 6. 14. 21:29
StatusMenu.h
#ifndef __SCORE_H_
#define __SCORE_H_

#define SCORE_BOARD_X	40
#define SCORE_BOARD_Y	5

#define CLEAR_LINE_X	40
#define CLEAR_LINE_Y	8

#define NEXT_BLOCK_X	45
#define NEXT_BLOCK_Y	20

void IncScore(int combo);
int GetScore(void);

void IncClearLine(void);
int GetClearLine(void);

void ShowNextBlock(void);

void ShowStatus(void);

#endif 


StatusMenu.c
#include "common.h"
#include "point.h"
#include "StatusMenu.h"
#include "keyCurControl.h"
#include "blockControl.h"
#include "BlockInfo.h"

static int score = 0;
static int numOfClearLine = 0;

void IncScore(int combo)
{
	score += (combo * 10);

	if(combo > 1)
		score += combo * 3;
}

int GetScore(void)
{
	return score;
}

void IncClearLine(void)
{
	numOfClearLine++;
}

int GetClearLine(void)
{
	return numOfClearLine;
}

void ShowScore(void)
{
	Point CurPos = GetCurrentCursorPos();

	SetCurrentCursorPos(SCORE_BOARD_X, SCORE_BOARD_Y);
	printf("┏━━━━━━━━ \n");
	SetCurrentCursorPos(SCORE_BOARD_X, SCORE_BOARD_Y + 1);
	printf("┃ 현재 점수: %d \n", GetScore());
	SetCurrentCursorPos(SCORE_BOARD_X, SCORE_BOARD_Y + 2);
	printf("┗━━━━━━━━ \n");

	SetCurrentCursorPos(CurPos.x, CurPos.y);
}

void ShowClearLine(void)
{
	Point CurPos = GetCurrentCursorPos();

	SetCurrentCursorPos(CLEAR_LINE_X, CLEAR_LINE_Y);
	printf("┏━━━━━━━━━━━ \n");
	SetCurrentCursorPos(CLEAR_LINE_X, CLEAR_LINE_Y + 1);
	printf("┃ 클리어 라인 수: %d \n", GetClearLine());
	SetCurrentCursorPos(CLEAR_LINE_X, CLEAR_LINE_Y + 2);
	printf("┗━━━━━━━━━━━ \n");

	SetCurrentCursorPos(CurPos.x, CurPos.y);
}

void ShowNextBlock(void)
{
	Point CurPos = GetCurrentCursorPos();

	SetCurrentCursorPos(NEXT_BLOCK_X - 2, NEXT_BLOCK_Y);
	fputs("-----NEXT-----", stdout);

	SetCurrentCursorPos(NEXT_BLOCK_X, NEXT_BLOCK_Y + 1);

	Delete();
	DrawBlock(BlocksModel[GetNextBlockIdx()]);

	SetCurrentCursorPos(CurPos.x, CurPos.y);
}

void ShowStatus(void)
{
	ShowScore();
	ShowClearLine();
	ShowNextBlock();
} 


Point.h
#ifndef __SCORE_H_
#ifndef __POINT_H_
#define __POINT_H_

typedef struct _point
{
	int x;
	int y;
} Point;

#endif

 


MapInfo.h
#ifndef __SCORE_H_
#ifndef __MAP_INFO_H_
#define __MAP_INFO_H_

#define GBOARD_WIDTH		10
#define GBOARD_HIEGHT		20

void drawBoard(void);

#endif

 


Point.h
#ifndef __SCORE_H_
#ifndef __POINT_H_
#define __POINT_H_

typedef struct _point
{
	int x;
	int y;
} Point;

#endif

 


Common.h
#ifndef __SCORE_H_
#ifndef __COMMON_H_
#define __COMMON_H_

#include 
#include 
#include 

#endif

 


BlockInfo.h
#ifndef __SCORE_H_
#ifndef __BLOCK_INFO_H_
#define __BLOCK_INFO_H_

#define NUM_OF_BLOCK_MODEL	7

static char BlocksModel[][4][4] =
{
	/*------------------
			 ■
			 ■ ■ ■
	-------------------*/

	{
		{ 0, 0, 0, 0 },
		{ 0, 1, 0, 0 },
		{ 0, 1, 1, 1 },
		{ 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, 1, 1, 0 },
		{ 0, 0, 1, 0 },
		{ 0, 0, 0, 0 }
	},

	{
		{ 0, 0, 0, 0 },
		{ 0, 1, 1, 0 },
		{ 0, 1, 0, 0 },
		{ 0, 1, 0, 0 }
	},

	/*---------------------
	            ■
		    ■ ■ ■
	---------------------*/
	{
		{ 0, 0, 0, 0 },
		{ 0, 0, 0, 1 },
		{ 0, 1, 1, 1 },
		{ 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, 1, 1, 1 },
		{ 0, 1, 0, 0 },
		{ 0, 0, 0, 0 }
	},

	{
		{ 0, 1, 0, 0 },
		{ 0, 1, 0, 0 },
		{ 0, 1, 1, 0 },
		{ 0, 0, 0, 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, 1, 1, 0 },
		{ 0, 0, 1, 0 }
	},

	{
		{ 0, 0, 0, 0 },
		{ 0, 0, 0, 0 },
		{ 0, 1, 1, 1 },
		{ 0, 0, 1, 0 }
	},

	{
		{ 0, 0, 0, 0 },
		{ 0, 0, 1, 0 },
		{ 0, 0, 1, 1 },
		{ 0, 0, 1, 0 }
	},

	/*---------------------
	         ■ ■ ■ ■
	----------------------*/

	{
		{ 0, 0, 0, 0 },
		{ 0, 0, 0, 0 },
		{ 1, 1, 1, 1 },
		{ 0, 0, 0, 0 }
	},

	{
		{ 0, 1, 0, 0 },
		{ 0, 1, 0, 0 },
		{ 0, 1, 0, 0 },
		{ 0, 1, 0, 0 }
	},

	{
		{ 0, 0, 0, 0 },
		{ 0, 0, 0, 0 },
		{ 1, 1, 1, 1 },
		{ 0, 0, 0, 0 }
	},

	{
		{ 0, 1, 0, 0 },
		{ 0, 1, 0, 0 },
		{ 0, 1, 0, 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, 0, 1, 1 },
		{ 0, 1, 1, 0 },
		{ 0, 0, 0, 0 }
	},

	{
		{ 0, 1, 0, 0 },
		{ 0, 1, 1, 0 },
		{ 0, 0, 1, 0 },
		{ 0, 0, 0, 0 }
	},

	{
		{ 0, 0, 0, 0 },
		{ 0, 0, 1, 1 },
		{ 0, 1, 1, 0 },
		{ 0, 0, 0, 0 }
	},

	{
		{ 0, 1, 0, 0 },
		{ 0, 1, 1, 0 },
		{ 0, 0, 1, 0 },
		{ 0, 0, 0, 0 }
	},

	/*--------------------
		  ■ ■
		    ■ ■
	--------------------*/

	{
		{ 0, 0, 0, 0 },
		{ 0, 1, 1, 0 },
		{ 0, 0, 1, 1 },
		{ 0, 0, 0, 0 }
	},

	{
		{ 0, 0, 1, 0 },
		{ 0, 1, 1, 0 },
		{ 0, 1, 0, 0 },
		{ 0, 0, 0, 0 }
	},

	{
		{ 0, 0, 0, 0 },
		{ 0, 1, 1, 0 },
		{ 0, 0, 1, 1 },
		{ 0, 0, 0, 0 }
	},

	{
		{ 0, 0, 1, 0 },
		{ 0, 1, 1, 0 },
		{ 0, 1, 0, 0 },
		{ 0, 0, 0, 0 }
	},
};

#endif
 


KeyCursorControl.h
#ifndef __SCORE_H_
#ifndef __CURSOR_H_

#define __CURSOR_H_

#include "Point.h"
#define FRAME_PER_SECOND		60
#define SYS_DELAY				60

#define	LEFT	75
#define RIGHT	77
#define UP		72
#define	DOWN	80
#define SPACE	32

void SetCurrentCursorPos(int x, int y);
Point GetCurrentCursorPos(void);
void RemoveCursor(void);

void KeyInput(void);
void KeyDelayInit(int rate);

#endif

 


KeyCursorControl
#ifndef __SCORE_H_
#include "common.h"
#include "keyCurControl.h"
#include "Point.h"
#include "blockControl.h"
#include 

static int KeyDelay;

void SetCurrentCursorPos(int x, int y)
{
	COORD pos = { x, y };
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}

Point GetCurrentCursorPos(void)
{
	Point curPoint;
	CONSOLE_SCREEN_BUFFER_INFO curInfo;
	
	GetConsoleScreenBufferInfo(
		GetStdHandle(STD_OUTPUT_HANDLE), &curInfo);
	curPoint.x = curInfo.dwCursorPosition.X;
	curPoint.y = curInfo.dwCursorPosition.Y;

	return curPoint;
}

void RemoveCursor(void)
{
	CONSOLE_CURSOR_INFO curInfo;
	GetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &curInfo);
	curInfo.bVisible = 0;
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &curInfo);
}

void KeyInput(void)
{
	int i;
	int key;

	for (i = 0; i < FRAME_PER_SECOND; i++)
	{
		if (_kbhit())
		{
			key = _getch();

			switch (key)
			{
			case LEFT:
				MoveBlockLeft();
				break;
			case RIGHT:
				MoveBlockRight();
				break;
			case UP:
				RotateBlock();
				break;
			case DOWN:
				BlockDown();
				break;
			case SPACE:
				HardDrop();
				break;

			}
		}

		if (i % KeyDelay == 0)
			Sleep(SYS_DELAY);
	}
}

void KeyDelayInit(int rate)
{
	if (rate < 1)
		return;
	else
		KeyDelay = rate;
}
 


BlockControl.h
#ifndef __SCORE_H_
#ifndef __BLOCKS_H_
#define __BLOCKS_H_

#define START_CURPOS_X	(5 * 4 - 2)
#define START_CURPOS_Y	(2)

void InitNewBlockPos(int x, int y);

void FirstSelectBlock(void);
void SelectBlock(void);
int GetNextBlockIdx(void);
int GetCurrentBlockIdx(void);

void DrawBlock(char(*blockInfo)[4]);
void DeleteBlock(char(*blockInfo)[4]);
void Delete(void);
void ReDrawBlock(void);
int BlockDown(void);

void MoveBlockLeft(void);
void MoveBlockRight(void);
void RotateBlock(void);
void KeyInput(void);

void drawMap(void);

int CheckCollision(int curX, int curY, char blockModel[][4]);
void AddBlockgameBoard(void);
int IsGameOver(void);

int IsFull(int CurY);
void removeWid(void);
void rm(int CurY);
void ReDraw(void);
void HardDrop(void);

#endif

 


BlockControl.c
#ifndef __SCORE_H_
#include "common.h"
#include 
#include "blockControl.h"
#include "keyCurControl.h"
#include "BlockInfo.h"
#include "Point.h"
#include "MapInfo.h"
#include "StatusMenu.h"

#define MAP_POS_X	8
#define MAP_POS_Y	5

static int currentBlockModel;
static int nextBlockModel;
static int curPosX, curPosY;
static char gameBoardInfo[GBOARD_HIEGHT + 1][GBOARD_WIDTH + 2] = { 0, };

void InitNewBlockPos(int x, int y)
{
	if (x < 0 || y < 0)
		return;

	curPosX = x;
	curPosY = y;

	SetCurrentCursorPos(curPosX, curPosY);
}

void FirstSelectBlock(void)
{
	srand((size_t)time(NULL));
	currentBlockModel = (rand() % NUM_OF_BLOCK_MODEL) * 4;
}

void SelectBlock(void)
{
	srand((size_t)time(NULL));
	nextBlockModel = (rand() % NUM_OF_BLOCK_MODEL) * 4;
}

int GetCurrentBlockIdx(void)
{
	return currentBlockModel;
}

int GetNextBlockIdx(void)
{
	return nextBlockModel;
}

void DrawBlock(char(*blockInfo)[4])
{	
	Point curPos = GetCurrentCursorPos();

	for (int y = 0; y < 4; y++)
	{
		for (int x = 0; x < 4; x++)
		{
			SetCurrentCursorPos(curPos.x + (x * 2),curPos.y + y);				

			if (blockInfo[y][x] == 1)
			{
				fputs("■", stdout);
			}
		}
	}

	SetCurrentCursorPos(curPos.x, curPos.y);
}

void DeleteBlock(char(*blockInfo)[4])
{
	Point curPos = GetCurrentCursorPos();

	for (int y = 0; y < 4; y++)
	{
		for (int x = 0; x < 4; x++)
		{
			SetCurrentCursorPos(curPos.x + (x * 2), curPos.y + y);		

			if (blockInfo[y][x] == 1)
			{
				fputs("  ", stdout);
			}
		}
	}

	SetCurrentCursorPos(curPos.x, curPos.y);
}

void Delete(void)
{
	Point curPos = GetCurrentCursorPos();

	for (int y = 0; y < 4; y++)
	{
		for (int x = 0; x < 4; x++)
		{
			SetCurrentCursorPos(curPos.x + (x * 2), curPos.y + y);
			fputs("  ", stdout);
		}
	}

	SetCurrentCursorPos(curPos.x, curPos.y);
}

int CheckCollision(int curX, int curY, char blockModel[][4])
{
	int IdxX, IdxY;

	IdxX = (curX - MAP_POS_X) / 2;
	IdxY = curY - MAP_POS_Y ;

	for (int x = 0; x < 4; x++)
	{
		for (int y = 0; y < 4; y++)
		{
			if (blockModel[y][x] == 1 && gameBoardInfo[IdxY + y][IdxX + x] == 1)
				return 1;
		}
	}

	return 0;
}

void ReDrawBlock(void)
{
	DeleteBlock(BlocksModel[GetCurrentBlockIdx()]);
	DrawBlock(BlocksModel[GetCurrentBlockIdx()]);
}

int BlockDown(void)
{
	if (CheckCollision(curPosX, curPosY + 1, (BlocksModel[GetCurrentBlockIdx()])))
		return 0;

	DeleteBlock(BlocksModel[GetCurrentBlockIdx()]);
	curPosY++;

	SetCurrentCursorPos(curPosX, curPosY);
	DrawBlock(BlocksModel[GetCurrentBlockIdx()]);

	return 1;
}

void MoveBlockLeft(void)
{
	if (CheckCollision(curPosX - 2, curPosY, (BlocksModel[GetCurrentBlockIdx()])))
		return;

	DeleteBlock(BlocksModel[GetCurrentBlockIdx()]);

	curPosX -= 2;
	SetCurrentCursorPos(curPosX, curPosY);

	DrawBlock(BlocksModel[GetCurrentBlockIdx()]);
}

void MoveBlockRight(void)
{
	if (CheckCollision(curPosX + 2, curPosY, (BlocksModel[GetCurrentBlockIdx()])))
		return;

	DeleteBlock(BlocksModel[GetCurrentBlockIdx()]);
	
	curPosX += 2;
	SetCurrentCursorPos(curPosX, curPosY);

	DrawBlock(BlocksModel[GetCurrentBlockIdx()]);
}

void RotateBlock(void)
{	
	int BeforeModel = GetCurrentBlockIdx();

	DeleteBlock(BlocksModel[GetCurrentBlockIdx()]);

	currentBlockModel++;

	if (GetCurrentBlockIdx() % 4 == 0)
		currentBlockModel -= 4;

	if (CheckCollision(curPosX, curPosY, BlocksModel[GetCurrentBlockIdx()]))
	{
		currentBlockModel = BeforeModel;
		return;
	}

	DrawBlock(BlocksModel[GetCurrentBlockIdx()]);
}

void drawMap(void)
{
	Point curPos = GetCurrentCursorPos();

	for (int y = 0; y < GBOARD_HIEGHT + 1; y++)
	{
		for (int x = 0; x < GBOARD_WIDTH + 2; x++)
		{
			SetCurrentCursorPos(curPos.x + MAP_POS_X + (x * 2), curPos.y + (y + MAP_POS_Y));

			if (y != GBOARD_HIEGHT && (x == 0 || x == GBOARD_WIDTH + 1))
			{
				printf("┃");
			}

			if (y == GBOARD_HIEGHT)
			{
				if (x == 0)
				{
					printf("┗");
				}
				else if (x == GBOARD_WIDTH + 1)
				{
					printf("┛");
				}
				else
				{
					printf("━");
				}
			}
		}
	}

	for (int y = 0; y


TetrisMain.c
#ifndef __SCORE_H_
#include "common.h"
#include 
#include 
#include "keyCurControl.h"
#include "blockControl.h"
#include "BlockInfo.h"
#include "MapInfo.h"
#include "StatusMenu.h"

int main(void)
{
	drawMap();
	ShowStatus();

	KeyDelayInit(20);
	RemoveCursor();

	FirstSelectBlock();
	
	while (!IsGameOver())
	{
		InitNewBlockPos(START_CURPOS_X, START_CURPOS_Y);		

		while (BlockDown())
		{
			KeyInput();
		}

		AddBlockgameBoard();
		removeWid();
		ReDraw();		
		SelectBlock();
		ShowStatus();
	}

	return 0;
}

 

올해 1월 쯤에 완성했던 테트리스 프로젝트 입니다.

중1 때 C언어를 배우면서 이런 거는 도대체 어떻게 만들지 하던 때가 있었는데 드디어 완성하는 날이 왔네요

 

'C, C++ 프로젝트' 카테고리의 다른 글

C++ 뱀게임 프로젝트  (0) 2017.06.14
C언어 테트리스 프로젝트  (1) 2017.06.14
1 Comments
댓글쓰기 폼