C语言大作业 俄罗斯方块 主界面和排行榜界面

本文介绍了使用Easyx库实现的俄罗斯方块游戏中,主界面的创建与音乐控制,包括透明背景、音乐循环播放、按钮设计以及排行榜界面的展示。关键部分包括音乐控制(暂停/播放/关闭)、排行榜读取与排序以及主界面与排行榜的切换功能。
摘要由CSDN通过智能技术生成

目录

主界面

排行榜界面


       

主界面

1.创建绘图窗口,大小为640x360像素

	initgraph(640, 360, SHOWCONSOLE);
	setbkmode(TRANSPARENT);

SHOWCONSOLE 显示控制台窗口;

TRANSPARENT 背景是透明的;

2.打开音乐,循环播放音乐

	mciSendString("open BGM.mp3", NULL, 0, NULL);

	mciSendString("play BGM.mp3 repeat", NULL, 0, NULL);

open 打开音乐;

play   播放音乐,repeat   重复播放;

stop   关闭音乐;

3.设置背景图片,文字,声音图标

//背景图片
IMAGE background;
loadimage(&background, "background.jpg", 640, 360);
putimage(0, 0, &background);
//字体
settextstyle(70, 0, FONT);
settextcolor(RGB(65, 105, 225));
outtextxy(160, 50, "俄罗斯方块");
//声音图片
IMAGE sound;
loadimage(&sound, "sound.png", 40, 40);
putimage(600, 0, &sound);

4.设置按钮(圆角矩形+文字)

void button(int x, int y, int w, int h, const char* text)
{
	setfillcolor(RGB(240, 248, 255));
	fillroundrect(x, y, x + w, y + h, 10, 10);
	settextstyle(20, 0, FONT);
	settextcolor(RGB(105, 147, 211));
	int tx = x + (w - textwidth(text)) / 2;
	int ty = y + (h - textheight(text)) / 2;
	outtextxy(tx, ty, text);
}

5.按钮点击(关闭和暂停音乐)

void buttonhit()
{
	int flag = 1;//flag为0时为mute,为1时为sound
	//FlushBatchDraw();
	ExMessage msg;
	while (true)
	{
		if (peekmessage(&msg, EM_MOUSE))
		{
			if (msg.message == WM_LBUTTONDOWN && msg.x > 242 && msg.y > 150 && msg.x < 392 && msg.y < 190)
			{
				BOOL flag1 = newcontinue();
				//背景
				initgraph(640, 720, SHOWCONSOLE);
				setbkmode(TRANSPARENT);
				setbkcolor(WHITE);
				clearcliprgn();
				singlemode();
				singlemodeoperate(flag1);
				return;
			}
			else if (msg.message == WM_LBUTTONDOWN && msg.x > 242 && msg.y > 200 && msg.x < 392 && msg.y < 240)
			{
				//背景
				initgraph(1280, 720, SHOWCONSOLE);
				setbkmode(TRANSPARENT);
				setbkcolor(WHITE);
				clearcliprgn();
				doublemode();
				doublemodeoperate();
				IMAGE victory, failure;
				loadimage(&victory, "VICTORY.jpg", 350, 200);
				loadimage(&failure, "FAILURE.jpg", 350, 200);
				if (winflag == 1)
				{
					doublemode();
					doubledrawgrid();
					putimage(40, 260, &victory);
					putimage(890, 260, &failure);
					Sleep(3000);
				}
				else if (winflag == 2)
				{
					doublemode();
					doubledrawgrid();
					putimage(40, 260, &failure);
					putimage(890, 260, &victory);
					Sleep(3000);
				}
				return;
			}
			else if (msg.message == WM_LBUTTONDOWN && msg.x > 242 && msg.y > 250 && msg.x < 392 && msg.y < 290)
			{
				initgraph(320, 720, SHOWCONSOLE);
				setbkmode(TRANSPARENT);
				setbkcolor(WHITE);
				clearcliprgn();
				list();
				listoperate();
				return;
			}
			else if (msg.message == WM_LBUTTONDOWN && msg.x > 580 && msg.y > 0 && msg.x < 640 && msg.y < 60 && flag == 1)
			{
				mciSendString("stop BGM.mp3", NULL, 0, NULL);
				IMAGE mute;
				loadimage(&mute, "mute.png", 40, 40);
				putimage(600, 0, &mute);
				flag = 0;
			}
			else if (msg.message == WM_LBUTTONDOWN && msg.x > 580 && msg.y > 0 && msg.x < 640 && msg.y < 60 && flag == 0)
			{
				mciSendString("play BGM.mp3 repeat", NULL, 0, NULL);
				IMAGE sound;
				loadimage(&sound, "sound.png", 40, 40);
				putimage(600, 0, &sound);
				flag = 1;
			}
		}
	}
}

 WM_LBUTTONDOWN   左键按下;

排行榜界面

 1.排行榜界面

void list()
{
	HWND hwnd = GetHWnd();
	SetWindowText(hwnd, "排行榜");
	IMAGE background;
	loadimage(&background, "background4.jpg", 320, 720);
	putimage(0, 0, &background);
	settextstyle(80, 0, FONT);
	settextcolor(RGB(240, 248, 255));
	int tx = 160 - textwidth("排行榜") / 2;
	outtextxy(tx, 20, "排行榜");
	settextstyle(30, 0, FONT);
	outtextxy(20, 100, "排名");
	outtextxy(135, 100, "名称");
	outtextxy(250, 100, "分数");
	IMAGE backimage1, backimage2;
	loadimage(&backimage1, "back1.png", 50, 50);
	loadimage(&backimage2, "back2.png", 50, 50);
	putimage(0, 0, &backimage2, SRCAND);
	putimage(0, 0, &backimage1, SRCPAINT);
}

 SRCAND,SRCPAINT涉及位运算;关于Easyx如何显示透明无背景贴图_Brokenrivers的博客-CSDN博客_easyx透明图片

 2.排行榜界面操作(从文件中读取,用动态数组储层,排序)

int readrank()
{
	info temp;
	int n = 0;
	FILE* fp;
	int flag = 0;
	//计算行数
	char buf[100];
	FILE* p;
	flag = fopen_s(&p, "rank.txt", "rb");
	if (flag != 0)
	{
		printf("cannot open the files\n");
		system("pause");
		return -1;
	}
	else {
		while (!feof(p))
		{
			memset(buf, 0, sizeof(buf));//每次读取一行之前都把这个buf清空
			fgets(buf, sizeof(buf), p);//从文件中读取一行
			n++;
		}
		fclose(p);
	}
	n /= 2;
	info* array = (info*)malloc(n * sizeof(temp));//建立一个动态数组,动态数组的成员数量和.txt文件的行一样多
	flag = fopen_s(&fp, "rank.txt", "rb");
	if (flag != 0)
	{
		printf("cannot open rank.txt\n");
		system("pause");
		return -1;
	}
	for (int i = 0;i < n;i++)
	{
		fgets(array[i].name, 30, fp);
		char* tmp = NULL;
		if ((tmp = strstr(array[i].name, "\n")))
		{
			*tmp = '\0';
		}
		fscanf_s(fp, "%d", &array[i].score);
		fgets(buf, sizeof(buf), fp);//读取完分数这一行
	}
	for (int i = 0;i < n - 1;i++)
	{
		int k = i;
		for (int j = i + 1;j < n;j++)
		{
			if (array[j].score > array[i].score)
			{
				k = j;
				temp = array[i];
				array[i] = array[j];
				array[j] = temp;
			}
		}
	}
	fclose(fp);
	for (int i = 0;i < n;i++)
	{
		char str1[30];
		sprintf_s(str1, "%d", i + 1);//将数字格式化输出为字符串
		outtextxy(20 + textwidth("排名") / 2 - textwidth(str1) / 2, 130 + i * textheight(str1), str1);
		outtextxy(135 + textwidth("名称") / 2 - textwidth(array[i].name) / 2, 130 + i * textheight(array[i].name), array[i].name);
		char str2[30];
		sprintf_s(str2, "%d", array[i].score);//将数字格式化输出为字符串
		outtextxy(250 + textwidth("得分") / 2 - textwidth(str2) / 2, 130 + i * textheight(str2), str2);
	}
	free(array);
	return 0;
}

3.返回主界面

void listoperate() 
{
	readrank();
	ExMessage msg;
	while (true)
	{
		if(peekmessage(&msg, EM_MOUSE) && msg.message == WM_LBUTTONDOWN && msg.x > 0 && msg.y > 0 && msg.x < 55 && msg.y < 55)
		{
			return;
		}
	}
}
  • 11
    点赞
  • 64
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值