C语言小游戏:俄罗斯方块

C语言小游戏:俄罗斯方块

本项目为娱乐项目,使用vs2019搭配easyx编写,以下为全部代码(代码以本页面内为准)

1.头文件(main.h)
2.主函数(main.cpp)

头文件(main.h)

#pragma once
#include
#include
#include
#include
#include
#include 		// 引用图形库头文件
#include 
#include
#pragma comment(lib,"Winmm.lib")	// 加载静态库

#define width 10
#define height 20
#define X 3
#define Y 0
#define BLOCK_COUNT 28
#define BLOCK_UNIT 4
#define maxpicwidth 500
#define maxpicheight 610
#define eachsize 30
#define piancha 12

int background[height][width] = { 0 };
int grade = 0;
int maxgrade;
int if_continue=1;
int maxh=height;
int g_block[BLOCK_COUNT][BLOCK_UNIT][BLOCK_UNIT] =
{
	//I形方块
	{
		0,1,0,0,
		0,1,0,0,
		0,1,0,0,
		0,1,0,0,
	},
	{
		0,0,0,0,
		1,1,1,1,
		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,0,0,0,
		1,1,1,1,
		0,0,0,0,
		0,0,0,0,
	},
	//L形方块
	{
		1,0,0,0,
		1,0,0,0,
		1,1,0,0,
		0,0,0,0,
	},
	{
		1,1,1,0,
		1,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,1,0,
		1,1,1,0,
		0,0,0,0,
		0,0,0,0,
	},
	//T型
	{
		0,0,0,0,
		1,1,1,0,
		0,1,0,0,
		0,0,0,0,
	},
	{
		1,0,0,0,
		1,1,0,0,
		1,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,
		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,
	},
	//反L型
	{
		0,1,0,0,
		0,1,0,0,
		1,1,0,0,
		0,0,0,0,
	},
	{
		0,0,0,0,
		1,1,1,0,
		0,0,1,0,
		0,0,0,0,
	},
	{
		0,1,1,0,
		0,1,0,0,
		0,1,0,0,
		0,0,0,0,
	},
	{
		1,0,0,0,
		1,1,1,0,
		0,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,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,
		0,1,1,0,
		1,1,0,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,
		1,1,0,0,
		0,0,0,0,
	},
	{
		0,1,0,0,
		0,1,1,0,
		0,0,1,0,
		0,0,0,0,
	},
};

主程序(main.cpp)

#include"main.h"
int if_stop(int n,int x,int y) {
	for (int i = y; i < y + 4; i++)
	{
		for (int j = x; j < x + 4; j++)
		{
			if (g_block[n][i-y][j-x] == 1&&background[i+1][j]==1) {
				return 0;
			}
		}
	}
	return 1;
}
int if_can_right(int n,int x,int y) {
	for (int i = 0; i < BLOCK_UNIT; i++)
	{
		for (int j = 0; j < BLOCK_UNIT; j++)
		{
			if (g_block[n][i][j] == 1 && background[y + i][x + 1 + j] == 1)
				return 0;
		}
	}
	return 1;
}
int if_can_left(int n, int x, int y) {
	for (int i = 0; i < BLOCK_UNIT; i++)
	{
		for (int j = 0; j < BLOCK_UNIT; j++)
		{
			if (g_block[n][i][j] == 1 && background[y + i][x - 1 + j] == 1)
				return 0;
		}
	}
	return 1;
}
void get_in_map(int n,int x,int y) {
	for (int i = y; i < y+4; i++)
	{
		for (int j = x; j < x+4; j++)
		{
			if (g_block[n][i-y][j-x] == 1) {
				background[i][j] = 1;
				if (i < maxh) {
					maxh = i;
				}
			}
			
		}
	}
}
void delect() {
	for (int i = 0; i < height; i++)
	{
		int n=0;
		for (int j = 0; j < width; j++)
		{
			if (background[i][j]) {
				n++;
			}
			else {
				j = width;
			}
		}
		if (n == width) {
			grade++;
			for (int k = 0; k < width; k++)
			{
				background[i][k] = 0;
			}
			for (int h = i; h > 0; h--)
			{
				for (int w = 0; w < width; w++)
				{
					background[h][w] = background[h - 1][w];
				}
			}
		}
	}
}
int type() {
	srand(time(0));
	return rand() % 7*4;
}
int get_real_height(int n) {
	int num = 0;
	for (int i = BLOCK_UNIT-1; i >= 0; i--,num++)
	{
		for (int j = 0; j < BLOCK_UNIT; j++)
		{
			if (g_block[n][i][j] == 1)
				return num;
		}
	}
	return num;
}
int get_real_left(int n) {
	int num = 0;
	for (int i = 0; i < BLOCK_UNIT; i++, num++)
	{
		for (int j = 0; j < BLOCK_UNIT; j++)
		{
			if (g_block[n][j][i] == 1)
				return num;
		}
	}
	return num;
}
int get_real_right(int n) {
	int num = 0;
	for (int i = BLOCK_UNIT-1; i >=0 ; i--, num++)
	{
		for (int j = 0; j < BLOCK_UNIT; j++)
		{
			if (g_block[n][j][i] == 1) {
				return num;
			}
		}
	}
	return num;
}
void cleantxt() {
	settextcolor(RGB(0x99, 0xD9, 0xEA));
	char s[100];
	sprintf(s, "共消除:%d行", grade);
	outtextxy(350, 250, s);
	sprintf(s, "最高成绩:%d行", maxgrade);
	outtextxy(330, 300, s);
	for (int i = 0; i < BLOCK_UNIT; i++)
	{
		for (int j = 0; j < BLOCK_UNIT; j++)
		{
			settextcolor(WHITE);
			outtextxy(345 + j * 30, 45 + i * 30, "■");
		}
	}
}
void clean(int n, int x, int y) {
	for (int i = 0; i < height; i++)
	{
		for (int j = 0; j < width; j++)
		{
			settextcolor(WHITE);
			outtextxy(eachsize * j + piancha, eachsize * i + piancha, "■");
		}
	}
}
void print(int n,int x,int y) {	
	clean(n, x, y);
	for (int i = 0; i < height; i++)
	{
		for (int j = 0; j < width; j++)
		{
			if (background[i][j] == 1) {
				settextcolor(RGB(0xFF, 0xB4, 0x0));
				outtextxy(eachsize*j+piancha, eachsize*i+piancha, "■");
			}
			else
				if (i - y < 4 && i - y >= 0 && j - x >= 0 && j - x < 4) {

					if (g_block[n][i - y][j - x] == 1) {
						settextcolor(RGB(0xFF, 0xB4, 0x0));
						outtextxy(eachsize * j + piancha, eachsize * i + piancha, "■");
						
					}
				}
		}
	}
	settextcolor(WHITE);
}
void printtxt(int next) {
	char s[100] = "";
	outtextxy(350, 200, "下一个");
	sprintf(s, "共消除:%d行", grade);
	outtextxy(350, 250, s);
	sprintf(s, "最高成绩:%d行", maxgrade);
	outtextxy(330, 300, s);
	settextcolor(RGB(0xFF, 0xB4, 0x0));
	for (int i = 0; i < BLOCK_UNIT; i++)
	{
		for (int j = 0; j < BLOCK_UNIT; j++)
		{
			if (g_block[next][i][j]) {
				
				outtextxy(345+ j*30, 45+i*30, "■");
			}
		}
	}
	outtextxy(340, 410, "按下w变形");
	outtextxy(340, 450, "按下a左移");
	outtextxy(340, 490, "按下d右移");
	outtextxy(340, 530, "按esc暂停");
	settextcolor(WHITE);
}
void game_start() {
	srand(time(0));
	char s[100] = "";
	int t = type(), x = X, y = Y, v = t, next = type();
	initgraph(maxpicwidth, maxpicheight);
	IMAGE kk;
	loadimage(&kk, ".//img//back1.jpg", maxpicwidth, maxpicheight);
	putimage(0, 0, &kk);
	HWND hand = GetHWnd();
	mciSendString("open .//music//1047321003.mp3 alias bgm", 0, 0, 0);
	mciSendString("play bgm repeat", 0, 0, 0);
	SetWindowText(hand, "俄罗斯方块");
	settextstyle(25, 0, "宋体");
	setbkmode(TRANSPARENT);
	FILE* f = fopen(".//score//score.txt", "r");
	if (f != NULL && !feof(f)) {
		fscanf(f, "%d", &maxgrade);
	}
	fclose(f);
	while (1)
	{
		settextcolor(WHITE);
		printtxt(next);
		if (maxh == 0)
			break;
		if (y + BLOCK_UNIT - get_real_height(t) < height && if_stop(t, x, y)) {
			if (_kbhit())
			{
				char key = _getch();
				if (key == 'a') {
					if (x - 1 >= -get_real_left(t)&&if_can_left(t,x,y)) {//x>=-u
						print(t, --x, ++y);
					}
					else {
						print(t, x, ++y);
					}
				}
				else if (key == 'd') {
					if (x + 1 <= width - BLOCK_UNIT + get_real_right(t)&&if_can_right(t,x,y)) {// x 0-9
						print(t, ++x, ++y);
					}
					else {
						print(t, x, ++y);
					}
				}
				else if (key == 'w') {
					++t;
					if (t - 4 == v)
						t = v;
					print(t, x, ++y);
				}
				else if (key == 27) {
					int ResultMB = MessageBox(hand, "已暂停,是否继续挑战", "game stop", MB_OKCANCEL);
					if (IDOK == ResultMB) {
						print(t, x, ++y);
					}
					if (IDCANCEL == ResultMB) {
						if_continue = 0;
						closegraph();
						return;
					}
				}
				else {
					print(t, x, ++y);
				}
			}
			else {
				print(t, x, ++y);
			}

		}
		else {
			get_in_map(t, x, y);
			cleantxt();
			delect();
			t = next;
			v = t;
			x = X, y = Y;
			next = type();
			if (grade > maxgrade)
				maxgrade = grade;
			print(t, x, y);
			printtxt(next);
			
		}
		Sleep(500);
	}
	f = fopen(".//score//score.txt", "w");
	if (f != NULL) {
		fprintf(f, "%d", maxgrade);
	}
	fclose(f);
	mciSendString("close bgm", 0, 0, 0);
	sprintf(s, "你输了,你共消除:%d是否重新挑战", grade);
	int ResultMB = MessageBox(hand, s, "game over", MB_OKCANCEL);
	if (IDOK == ResultMB) {
		maxh = height;
		grade = 0;
		for (int i = 0; i < height; i++)
		{
			for (int j = 0; j < width; j++)
			{
				background[i][j] = 0;
			}
		}
		closegraph();
		return;
	}
	if (IDCANCEL == ResultMB) {
		if_continue = 0;
		closegraph();
		return;
	}
}

int main() {
	while(if_continue)
		game_start();
	return 0;
}

你可能感兴趣的:(c语言,算法,开发语言)