CC++SFML编写俄罗斯方块小程序 附代码和下载链接

CC++SFML编写俄罗斯方块小程序 附代码和下载链接,第1张

C/C++SFML编写俄罗斯方块小程序

文章目录
  • C/C++SFML编写俄罗斯方块小程序
  • 前言

  • 一、游戏下载链接


  • 二、游戏截图


  • 三、小程序功能

    • 1.俄罗斯方块游戏的实现
    • 2.主菜单功能
    • 3.游戏音乐,音效
    • 4.游戏设置
    • 5.游戏时长、最高分、当前分数和消行显示
    • 6.显示下一个俄罗斯方块,最终下落的位置
    • 7.游戏暂停

  • 四、运用知识


  • 五、游戏代码


前言

这几天学完了C语言初阶和进阶,想做个小项目练练手,就想到了俄罗斯方块这个小游戏,于是通过网上的资料学习了如何制作,然后自己通过三天终于写完了,写了上千行代码,也是我进入编程学习之路后编写的第一个那么长的代码~ 但是感觉有很多地方写的不是很好,希望能有大佬能指点下。



一、游戏下载链接

大家感兴趣的话帮我试玩一下吧! 帮我康康有没有bug 直接打开程序就可以玩拉~
俄罗斯方块小游戏
下载链接:https://pan.baidu.com/s/1mI_f65ZdiyK_a6d_4wX0Fw
提取码:9zy7


二、游戏截图


三、小程序功能 1.俄罗斯方块游戏的实现

这个是最主要的~其实游戏的实现只用几百行代码,后面为了加一些功能写着写着就上千了。


2.主菜单功能

可以在主菜单通过按钮进入游戏,查看游戏教程,进行游戏设置,退出游戏

3.游戏音乐,音效

使用了mciSendString(播放音乐)函数和Playsound(播放音效)函数实现

4.游戏设置

可以通过游戏设置去关闭背景音乐或音效,设置游戏的难度。


5.游戏时长、最高分、当前分数和消行显示

在消行后会增加消行数和得分,并显示在屏幕上,同时记录游戏时长,记录历史最高分。


6.显示下一个俄罗斯方块,最终下落的位置

游戏内可以是看到下一个方块是什么,以及最终下落的位置也会显示出来。


7.游戏暂停

有别的事情的时候或者觉得游戏背景音乐太难听了想关闭下游戏背景音乐或音效,可以通过点击右上角的暂停按钮暂停游戏,并在暂停界面中选择重开、设置关闭背景音乐或继续游戏。



四、运用知识

主要是C语言的知识和SFML图形库基础知识。



C语言学习:
https://www.bilibili.com/video/BV1cq4y1U7sg?share_source=copy_web

SFML学习:
https://www.sfml-dev.org/tutorials/2.5/

俄罗斯方块实现:
https://www.bilibili.com/video/BV1uK4y187zT?share_source=copy_web


五、游戏代码

一千多行滴代码 有很多地方写的不是很好 仅供参考o

#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
#include"resource.h"
#include"string.h"
#include
#include
#include
#pragma comment(lib,"winmm.lib")
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")//隐藏控制台

using namespace sf; //声明命名空间

const int Hang = 23;//行
const int Lie = 10;//列

struct Point//记录方块的坐标
{
	int x;
	int y;
};

Point now[4];//当前方块的四个坐标
Point back[4];//备份

//俄罗斯方块七个类别
int blocks[7][4] =
{
	{1,3,5,7},//1字形     第0行
	{2,4,5,7},//z         1
	{3,5,4,6},//z 反转    2
	{3,5,4,7},// T        3
	{2,4,6,7},//L         4
	{3,5,7,6},//J (L反转) 5
	{2,3,4,5} //田        6
};

int sort = 0;//表示当前是哪种类型
int table[Hang][Lie] = { 0 };//非0表示有方块,0表示无方块
float timer = 0;
float speed_quick=0;//快速下降速度
float speed_grade=0;//当前难度下降速度
float nowspeed=0;//当前速度
int goal = 0;//记录分数
int cleanline = 0;
char Goal[20];//分数字符串
char Cleanline[20];//消行字符串
int maxgoal = 0;;//历史最高
char Maxgoal[20];//历史最高字符串

bool enterplay = false;
bool enterhelp = false;
bool enterset = false;
bool enterexit = false;
bool enterstop = false;
bool entermenu = false;
bool enterstopset = false; 

bool isplay = false;
bool ishelp = false;
bool isset = false;
bool isexit = false;
bool isquick = false;

int nextsort = 0;//下一个方块的种类
bool initnextsort = false;//是否初始化了下一个种类的方块
Point next[4];//记录下一个方块的四个坐标

int gamemusic = 0;//游戏音乐
int gamewav = 0;//游戏音效

int gamegrade = 0;//游戏难度等级0 1 2 3

float grade0 = 0.5;//难度0
float grade1 = 0.35;//难度1
float grade2 = 0.25;//难度2
float grade3 = 0.1;//难度3

//计时器游戏时长 
Clock kclock;//计时器
time_t total = 57600;
struct tm* tmtotal;
Clock gameclock;//游戏时长计时器
float gametime = 0;//计时总时长,达到1清零,total+1;
float onetime = 0;//每次获取的时间
char Gametime[10] = { 0 };
 
//分装函数分界线》》》》》》》》》》》》》》》》》》》》》》》》》》
void newblock(int c)//产生一个方块 
{
	sort = c; //生成1-7随机一个数,表示生成哪种俄罗斯方块
	int i = sort - 1;
	int j = 0;
	int ret = rand() % 9;
	for (j = 0; j < 4; j++)
	{
		now[j].x = (blocks[i][j]) % 2 +ret;
		now[j].y = (blocks[i][j]) / 2 ;
	}
}

void drawspr(RenderWindow* window, Sprite* sp)//渲染显示方块
{
	//1.显示已经下落的方块
	int i = 0;
	int j = 0;
	for (i = 0; i < Hang; i++)
	{
		for (j = 0; j < Lie; j++)
		{
			if (table[i][j] != 0)//如果有方块 显示
			{
				if (table[i][j] != 0)
				{
					sp->setTextureRect(IntRect((table[i][j]-1) * 29, 0, 29, 29));
					sp->setPosition(j * 29, i * 29);
					sp->move(61, 35);
					window->draw(*sp);
				}
			}
		}
	}
	//2.显示正在降落的方块
	for (i = 0; i < 4; i++)
	{
		sp->setTextureRect(IntRect((sort-1) * 29, 0, 29, 29));
		sp->setPosition(now[i].x * 29, now[i].y * 29);
		sp->move(61, 35);
		window->draw(*sp);
	}
}

bool checkblock()//检查方块是否合法
{
	int i = 0;
	for (i = 0; i < 4; i++)
	{
		if (now[i].x > Lie - 1 ||
			now[i].y > Hang - 1 ||
			now[i].x < 0 ||
			table[now[i].y][now[i].x] != 0)
		{
			return false;
		}
	}
	return true;
}

void drop()//让方块下降一格
{
	int i = 0;

	for (i = 0; i < 4; i++)
	{
		back[i] = now[i];
		now[i].y += 1;
	}

	if (initnextsort==false)
	{
		initnextsort = true;
		nextsort = 1 + rand() % 7;
		for (int j = 0; j < 4; j++)
		{
			next[j].x = (blocks[nextsort - 1][j]) % 2;;
			next[j].y = (blocks[nextsort - 1][j]) / 2;
		}
	}

	if (checkblock() == false)
	{
		for (i = 0; i < 4; i++)
		{
			now[i] = back[i];
			table[now[i].y][now[i].x] = sort;
		}
		initnextsort = false;
		newblock(nextsort);
	}
}

void blockmove(int move)//移动方块
{
	int i = 0;
	for (i = 0; i < 4; i++)
	{
		back[i] = now[i];
		now[i].x += move;
	}

	if (checkblock() == false)
	{
		for (i = 0; i < 4; i++)
		{
			now[i] = back[i];
		}
	}
}

void spin()
{
	if (sort == 7)
	{
		return;
	}
	int i = 0;
	for (i = 0; i < 4; i++)
	{
		back[i] = now[i];
	}
	Point p = now[1];

	for (i = 0; i < 4; i++)
	{
		Point tmp = now[i];
		now[i].x = p.x - tmp.y + p.y;
		now[i].y = tmp.x - p.x + p.y;
	}
	if (checkblock() == false)
	{
		for (i = 0; i < 4; i++)
		{
			now[i] = back[i];
		}
	}
}

void changeblocks()
{
	while (checkblock() == true)
	{
		for (int i = 0; i < 4; i++)
		{
			back[i] = now[i];//备份
			now[i].y++;
		}
	}
	for (int i = 0; i < 4; i++)
	{
		now[i] = back[i];
	}
}


void keyinput(RenderWindow* window)
{
	Event e;
	while (window->pollEvent(e))
	{
		if (e.type == Event::Closed || e.key.code == Keyboard::Escape)
		{		
			mciSendString(L"close music/music1.mp3", 0, 0, 0);//关闭音乐文件
			window->close();
		}
		if (e.type == Event::KeyPressed)
		{
			switch (e.key.code)
			{
			case Keyboard::Left:
				blockmove(-1);//处理左
				if (gamewav % 2 == 0)
				{
					PlaySound(LPWSTR(IDR_WAVE2), GetModuleHandle(NULL), SND_RESOURCE | SND_ASYNC | SND_SYNC);
				}
				break;
			case Keyboard::Right:
				blockmove(1);//处理右键
				if (gamewav % 2 == 0)
				{
					PlaySound(LPWSTR(IDR_WAVE2), GetModuleHandle(NULL), SND_RESOURCE | SND_ASYNC | SND_SYNC);
				}
				break;	
			case Keyboard::Up:
				spin();//旋转 处理上键
				if (gamewav % 2 == 0)
				{
					PlaySound(LPWSTR(IDR_WAVE1), GetModuleHandle(NULL), SND_RESOURCE | SND_ASYNC | SND_PURGE);
				}
				break;
			case Keyboard::Space://直接到底部 *** 作
				if (gamewav % 2 == 0)
				{
					PlaySound(LPWSTR(IDR_WAVE5), GetModuleHandle(NULL), SND_RESOURCE | SND_ASYNC | SND_SYNC);//播放急速音
				}
				changeblocks();
				break;
			case Keyboard::Down://处理下键
				nowspeed = speed_quick;
				break;
			default:
				break;
			}
		}

		if (e.mouseButton.button == Mouse::Left &&
			e.mouseButton.x > 478 && e.mouseButton.y > 18 && e.mouseButton.x < 526 && e.mouseButton.y < 66)//判断是否按了暂停按钮
		{
			enterstop = true;
		}

		if (e.mouseButton.button == Mouse::Left &&
			e.mouseButton.x > 550 && e.mouseButton.y > 18 && e.mouseButton.x < 595 && e.mouseButton.y < 66)//判断是否按了返回菜单按钮
		{
			entermenu = true;
		}

		if (e.mouseButton.button == Mouse::Left && e.type == Event::MouseButtonPressed &&
			e.mouseButton.x > 476 && e.mouseButton.y > 541 && e.mouseButton.x < 527 && e.mouseButton.y < 592)//判断是否按了上按钮
		{
			spin();//旋转 处理上键
			if (gamewav % 2 == 0)
			{
				PlaySound(LPWSTR(IDR_WAVE1), GetModuleHandle(NULL), SND_RESOURCE | SND_ASYNC | SND_PURGE);
			}

		}

		if (e.mouseButton.button == Mouse::Left && 
			e.mouseButton.x  > 475 && e.mouseButton.y > 605 && e.mouseButton.x < 528 && e.mouseButton.y < 658)//判断是否按了下按钮
		{
			nowspeed = speed_quick;
		}

		if (e.mouseButton.button == Mouse::Left && e.type == Event::MouseButtonPressed &&
			e.mouseButton.x > 404 && e.mouseButton.y > 611 && e.mouseButton.x < 463 && e.mouseButton.y < 658)//判断是否按了左按钮
		{
			blockmove(-1);//处理左
			if (gamewav % 2 == 0)
			{
				PlaySound(LPWSTR(IDR_WAVE2), GetModuleHandle(NULL), SND_RESOURCE | SND_ASYNC | SND_SYNC);
			}

		}

		if (e.mouseButton.button == Mouse::Left && e.type == Event::MouseButtonPressed &&
			e.mouseButton.x > 541 && e.mouseButton.y > 606 && e.mouseButton.x < 598 && e.mouseButton.y < 658)//判断是否按了右按钮
		{
			blockmove(1);//处理右
			if (gamewav % 2 == 0)
			{
				PlaySound(LPWSTR(IDR_WAVE2), GetModuleHandle(NULL), SND_RESOURCE | SND_ASYNC | SND_SYNC);
			}
		}


		if (e.type==Event::MouseButtonPressed && (e.mouseButton.x > 424 && e.mouseButton.y > 674 && e.mouseButton.x < 582 && e.mouseButton.y < 725))//判断是否在急速按钮内
		{
			if (gamewav % 2 == 0)
			{
				PlaySound(LPWSTR(IDR_WAVE5), GetModuleHandle(NULL), SND_RESOURCE | SND_ASYNC | SND_SYNC);//播放急速音效
			}
			changeblocks();
			isquick = true;
		}
		else
		{
			isquick = false;
		}
	}
}

void clean()
{
	int k = Hang - 1;
	for (int i = Hang - 1; i >=0; i--)
	{
		int count = 0;
		for (int j = Lie - 1; j >= 0; j--)
		{
			if (table[i][j] != 0)
			{
				count++;
			}
			table[k][j] = table[i][j];
		}
		if (count < Lie)
		{
			k--;
		}
		else
		{

			goal += 10*(gamegrade%4+1);
			cleanline += 1;
			if (gamewav % 2 == 0)
			{
				PlaySound(LPWSTR(IDR_WAVE3), GetModuleHandle(NULL), SND_RESOURCE | SND_ASYNC | SND_SYNC);//播放消除音效
			}
			
		}
	}
}

bool checkfailed()//检查游戏是否失败
{
	
	for (int i = 0; i < Lie; i++)
	{
		if (table[3][i] != 0)
		{
			return false;
		}
	}
	return true;
}

void menuinput(RenderWindow* window)
{
	Event m;
	while (window->pollEvent(m))
	{
		if (m.type == Event::Closed || m.key.code == Keyboard::Escape)
		{		
			mciSendString(L"close music/music1.mp3", 0, 0, 0);//关闭音乐文件
			enterexit = true;
		}

		//判断鼠标是否在开始游戏按钮内
		if (m.mouseMove.x > 172 && m.mouseMove.y > 111 && m.mouseMove.x < 453 && m.mouseMove.y < 204)//开始游戏按钮
		{		
			isplay = true;
		}
		else
		{
			isplay = false;
		}
		//判断是否点击了开始游戏按钮
		if (m.mouseButton.button == Mouse::Left && 
			m.mouseButton.x > 172 && m.mouseButton.y > 111 && m.mouseButton.x < 453 && m.mouseButton.y < 204)
		{		
			enterplay = true;
		}

		//判断是否在帮助按钮内
		if (m.mouseMove.x > 172 && m.mouseMove.y > 255 && m.mouseMove.x < 453 && m.mouseMove.y < 348)//开始游戏按钮
		{
			ishelp = true;
		}
		else
		{
			ishelp = false;
		}

		//判断是否按了帮助按钮
		if (m.mouseButton.button == Mouse::Left && m.type == Event::MouseButtonReleased &&
			m.mouseButton.x > 172 && m.mouseButton.y > 255 && m.mouseButton.x < 453 && m.mouseButton.y < 348)
		{
			enterhelp = true;
		}


		//判断是否在设置按钮内
		if (m.mouseMove.x > 172 && m.mouseMove.y > 399 && m.mouseMove.x < 453 && m.mouseMove.y < 492)//开始游戏按钮
		{
			isset = true;
		}
		else
		{
			isset = false;
		}

		//判断是否按了设置按钮
		if (m.mouseButton.button == Mouse::Left && m.type == Event::MouseButtonReleased &&
			m.mouseButton.x > 172 && m.mouseButton.y > 399 && m.mouseButton.x < 453 && m.mouseButton.y < 492)
		{
			enterset = true;
		}

		//判断是否在退出按钮内
		if (m.mouseMove.x > 172 && m.mouseMove.y > 543 && m.mouseMove.x < 453 && m.mouseMove.y < 636)//开始游戏按钮
		{
			isexit = true;
		}
		else
		{
			isexit = false;
		}

		//判断是否按了退出按钮
		if (m.mouseButton.button == Mouse::Left && m.type == Event::MouseButtonReleased &&
			m.mouseButton.x > 172 && m.mouseButton.y > 543 && m.mouseButton.x < 453 && m.mouseButton.y < 636)
		{
			enterexit = true;		
		}
	} 
}

void drawnext(RenderWindow* window,Sprite* allblocks)//显示下一个方块
{
	for (int i = 0; i < 4; i++)
	{
		allblocks->setTextureRect(IntRect((nextsort - 1) * 29, 0, 29, 29));
		allblocks->setPosition(next[i].x * 29, next[i].y * 29);
		if (nextsort == 1)
		{
			allblocks->move(492, 151);
		}
		else if (nextsort == 2)
		{
			allblocks->move(502, 141);
		}
		else if (nextsort == 3)
		{
			allblocks->move(504, 141);
		}
		else if (nextsort == 4)
		{
			allblocks->move(496, 137);
		}
		else if (nextsort == 5)
		{
			allblocks->move(512, 136);
		}
		else if (nextsort == 6)
		{
			allblocks->move(502, 136);
		}
		else if (nextsort == 7)
		{
			allblocks->move(507, 151);
		}
		window->draw(*allblocks);
	}
}

//主函数分界线》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
int main()
{
	//读取历史最高分数
	FILE* pr = fopen("gamedate.txt", "r");
	if (pr == NULL)
	{
		perror("fopen");
	}

	while ((fread(&maxgoal, sizeof(maxgoal), 1, pr))< 1)
	{
		;
	}
	fclose(pr);
	pr = NULL;

	srand((unsigned int)time(NULL));//设置随机值起点
	sf::RenderWindow window(sf::VideoMode(626, 748), "elsfk", Style::Close);   //sf声明后以后不用写sf:: 
	//window.setPosition (Vector2i(10, 10));//重设置窗口开始位置
	//window.setSize(Vector2u(633,823));//重设置窗口大小
	window.setTitle(L"俄罗斯方块");//重设置窗口标题 前面加L才能显示中文
	window.setFramerateLimit(60);//设置帧率
	window.clear(Color::White);//设置窗口背景颜色

	//导入游戏背景图片并创建精灵
	Texture Game_picture;
	Game_picture.loadFromFile("image/game.png");//导入图片到内存
	Sprite gamephoto(Game_picture);//根据图片创建精灵

	//导入主菜单图片并创建精灵
	Texture menu;
	menu.loadFromFile("image/menu.jpg");
	Sprite menuphoto(menu);
	
	//导入暂停界面
	Texture stop;
	stop.loadFromFile("image/stop.png");
	Sprite stopphoto(stop);

	//导入失败界面
	Texture gameover;
	gameover.loadFromFile("image/gameover.png");
	Sprite gameoverphoto(gameover);

	//导入帮助界面
	Texture help;
	help.loadFromFile("image/help.png");
	Sprite helpphoto(help);

	//导入暂停设置界面
	Texture stopset;
	stopset.loadFromFile("image/stopset.png");
	Sprite stopsetphoto(stopset);

	//导入返回主菜单界面
	Texture gotomenu;
	gotomenu.loadFromFile("image/gotomenu.png");
	Sprite gotomenuphoto(gotomenu);

	//导入设置界面
	Texture gameset;
	gameset.loadFromFile("image/set.png");
	Sprite setphoto(gameset);



	//导入按钮并创建精灵
	Texture buttonplayoff, buttonhelpoff, buttonsetoff, buttonexitoff,buttonplayon,buttonhelpon,buttonseton,buttonexiton,
		buttonquickon,buttonquickoff,buttonyyoff,buttonyxoff ,buttongamemusicoff,buttongamewavoff,buttongrade0, buttongrade1, buttongrade2, buttongrade3;
	buttonplayoff.loadFromFile("button/playoff.jpg");
	buttonhelpoff.loadFromFile("button/helpoff.jpg");
	buttonsetoff.loadFromFile("button/setoff.jpg");
	buttonexitoff.loadFromFile("button/exitoff.jpg");
	buttonplayon.loadFromFile("button/playon.jpg");
	buttonhelpon.loadFromFile("button/helpon.jpg");
	buttonseton.loadFromFile("button/seton.jpg");
	buttonexiton.loadFromFile("button/exiton.jpg");
	buttonquickon.loadFromFile("button/quickon.png");
	buttonquickoff.loadFromFile("button/quickoff.png");
	buttonyyoff.loadFromFile("button/yyoff.png");
	buttonyxoff.loadFromFile("button/yxoff.png");

	buttongamemusicoff.loadFromFile("button/gamemusicoff.png");
	buttongamewavoff.loadFromFile("button/gamewavoff.png");
	buttongrade0.loadFromFile("button/grade0button.png");
	buttongrade1.loadFromFile("button/grade1button.png");
	buttongrade2.loadFromFile("button/grade2button.png");
	buttongrade3.loadFromFile("button/grade3button.png");

	Sprite playoff(buttonplayoff);
	Sprite helpoff(buttonhelpoff);
	Sprite setoff(buttonsetoff);
	Sprite exitoff(buttonexitoff);
	Sprite quickoff(buttonquickoff);
	Sprite yxoff(buttonyxoff);
	Sprite yyoff(buttonyyoff);

	Sprite playon(buttonplayon); 
	Sprite helpon(buttonhelpon);
	Sprite seton(buttonseton);
	Sprite exiton(buttonexiton);
	Sprite quickon(buttonquickon);

	Sprite setmusicoff(buttongamemusicoff);
	Sprite setwavoff(buttongamewavoff);
	Sprite setgrade0(buttongrade0);
	Sprite setgrade1(buttongrade1);
	Sprite setgrade2(buttongrade2);
	Sprite setgrade3(buttongrade3);

	//导入俄罗斯方块图片
	Texture all;
	all.loadFromFile("image/allblocks.jpg");
	Sprite allblocks(all);

	Font font;//字体变量
	font.loadFromFile("words/ytxwz.ttf");
	Text text,text2 ,text3 ,text4 , text5 ,text6 ,text7,text8 ,text9,text10;

	text.setPosition(410, 350);//分数是
	text.setString(L"分数:"); //设置字符串
	text.setFont(font); //设置字体
	text.setCharacterSize(36); //文字大小
	text.setFillColor(sf::Color::Black); //颜色 黑
	text.setStyle(sf::Text::Bold  );//格式 加粗

	text2.setPosition(495, 350);  //得分
	//text2.setString(Goal); //设置字符串
	text2.setFont(font); //设置字体
	text2.setCharacterSize(36); //文字大小
	text2.setFillColor(sf::Color::Black); //颜色 黑
	text2.setStyle(sf::Text::Bold);//格式 加粗

	text3.setPosition(410, 415);  //消行是
	text3.setString(L"消行:"); //设置字符串
	text3.setFont(font); //设置字体
	text3.setCharacterSize(36); //文字大小
	text3.setFillColor(sf::Color::Black); //颜色 黑
	text3.setStyle(sf::Text::Bold);//格式 加粗

	text4.setPosition(410, 290);  //最高分
	text4.setString(L"最高分:"); //设置字符串
	text4.setFont(font); //设置字体
	text4.setCharacterSize(25); //文字大小
	text4.setFillColor(sf::Color::Black); //颜色 黑
	text4.setStyle(sf::Text::Bold);//格式 加粗

	text5.setPosition(410, 480);  //时间
	text5.setString(L"时间:"); //设置字符串
	text5.setFont(font); //设置字体
	text5.setCharacterSize(36); //文字大小
	text5.setFillColor(sf::Color::Black); //颜色 黑
	text5.setStyle(sf::Text::Bold);//格式 加粗

	text6.setPosition(495, 415);  //消行数
	text6.setFont(font); //设置字体
	text6.setCharacterSize(36); //文字大小
	text6.setFillColor(sf::Color::Black); //颜色 黑
	text6.setStyle(sf::Text::Bold);//格式 加粗

	text7.setPosition(477, 88);  //消行数
	text7.setString(L"下一个"); //设置字符串
	text7.setFont(font); //设置字体
	text7.setCharacterSize(36); //文字大小
	text7.setFillColor(sf::Color::Black); //颜色 黑
	text7.setStyle(sf::Text::Bold);//格式 加粗

	text8.setPosition(495, 282);  //最高分数
	text8.setFont(font); //设置字体
	text8.setCharacterSize(36); //文字大小
	text8.setFillColor(sf::Color::Black); //颜色 黑
	text8.setStyle(sf::Text::Bold);//格式 加粗

	text9.setPosition(0,0);  //游戏时间秒
	text9.setFont(font); //设置字体
	text9.setCharacterSize(36); //文字大小
	text9.setFillColor(sf::Color::Black); //颜色 黑
	text9.setStyle(sf::Text::Bold);//格式 加粗

	text10.setPosition(100,20);  //游戏难度
	text10.setFont(font); //设置字体
	text10.setCharacterSize(36); //文字大小
	text10.setFillColor(sf::Color::Black); //颜色 黑
	text10.setStyle(sf::Text::Bold);//格式 加粗

//游戏界面分界线》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
	
	

	//mciSendString(L"open music/music1.mp3", 0, 0, 0);//打开音乐文件
	//mciSendString(L"play music/music1.mp3 repeat", 0, 0, 0);//播放音乐文件
	//mciSendString(L"pause music/music1.mp3", 0, 0, 0);//暂停音乐文件



	remenu://回到主菜单
	while (window.isOpen())//展示菜单
	{
		window.draw(menuphoto);
		window.draw(playoff);
		window.draw(helpoff);
		window.draw(setoff);
		window.draw(exitoff);	

		menuinput(&window);//等待用户按按钮
		if (isplay == true)
		{
			window.draw(playon);					
		}
		if (ishelp == true)
		{
			window.draw(helpon);	
		}
		if (isset == true)
		{
			window.draw(seton);
		}
		if (isexit == true)
		{
			window.draw(exiton);
		}
		if (enterplay == true)//进入游戏
		{
			enterplay = false;
			break;
		}
		if (enterexit == true)
		{
			window.close();
			return 1;
		}
		if (enterhelp == true)
		{
			Event h;
			enterhelp = false;
			window.draw(helpphoto);
			window.display();
			while (1)
			{
				while (window.pollEvent(h))
				{
					if (h.type == Event::Closed || h.key.code == Keyboard::Escape)
					{
						mciSendString(L"close music/music1.mp3", 0, 0, 0);//关闭音乐文件
						window.close();
						return 1;

					}

					if (h.mouseButton.button == Mouse::Left && h.type == Event::MouseButtonReleased &&
						h.mouseButton.x > 523 && h.mouseButton.y > 15 && h.mouseButton.x < 608 && h.mouseButton.y < 98)//判断是否按了返回游戏按钮
					{				
						goto remenu;
					}
				}
			}
		}
		if (enterset == true)
		{
			enterset = false;
			Event tt;
			reset:
			window.draw(setphoto);

			if (gamemusic % 2 == 1)
			{
				window.draw(setmusicoff);
			}
			if (gamewav % 2 == 1)
			{
				window.draw(setwavoff);
			}

			switch (gamegrade)
			{
			case 0:

				window.draw(setgrade0);
				break;
			case 1:
				window.draw(setgrade1);
				break;
			case 2:
				window.draw(setgrade2);
				break;
			case 3:
				window.draw(setgrade3);
				break;
			}

			window.display();

			while (1)
			{
				while (window.pollEvent(tt))
				{

					if (tt.type == Event::Closed || tt.key.code == Keyboard::Escape)
					{
						mciSendString(L"close music/music1.mp3", 0, 0, 0);//关闭音乐文件
						window.close();
						return 1;

					}

					if (tt.mouseButton.button == Mouse::Left && tt.type == Event::MouseButtonReleased &&
						tt.mouseButton.x > 490 && tt.mouseButton.y > 24 && tt.mouseButton.x < 593 && tt.mouseButton.y < 133)//判断是否按了返回游戏按钮
					{
						goto remenu;
					}

					if (tt.mouseButton.button == Mouse::Left &&tt.type==Event::MouseButtonReleased &&
						tt.mouseButton.x > 306 && tt.mouseButton.y > 193 && tt.mouseButton.x < 480 && tt.mouseButton.y < 248)//判断是否按了音乐按钮
					{					
						gamemusic++;
						goto reset;
					}
					if (tt.mouseButton.button == Mouse::Left && tt.type == Event::MouseButtonReleased &&
						tt.mouseButton.x > 306 && tt.mouseButton.y > 299 && tt.mouseButton.x < 480 && tt.mouseButton.y < 353)//判断是否按了音效按钮
					{
						gamewav++;
						goto reset;
					}

					if (tt.mouseButton.button == Mouse::Left && tt.type == Event::MouseButtonReleased &&
						tt.mouseButton.x > 85 && tt.mouseButton.y > 522 && tt.mouseButton.x < 257 && tt.mouseButton.y < 575)//判断是否按了简单按钮
					{
						gamegrade = 0;
						goto reset;
					}

					if (tt.mouseButton.button == Mouse::Left && tt.type == Event::MouseButtonReleased &&
						tt.mouseButton.x > 362 && tt.mouseButton.y > 522 && tt.mouseButton.x < 533 && tt.mouseButton.y < 575)//判断是否按了普通按钮
					{
						gamegrade = 1;
						goto reset;
					}

					if (tt.mouseButton.button == Mouse::Left && tt.type == Event::MouseButtonReleased &&
						tt.mouseButton.x > 85 && tt.mouseButton.y > 645 && tt.mouseButton.x < 257 && tt.mouseButton.y < 698)//判断是否按了困难按钮
					{
						gamegrade = 2;
						goto reset;
					}

					if (tt.mouseButton.button == Mouse::Left && tt.type == Event::MouseButtonReleased &&
						tt.mouseButton.x > 362 && tt.mouseButton.y > 645 && tt.mouseButton.x < 534 && tt.mouseButton.y < 689)//判断是否按了地狱按钮
					{
						gamegrade = 3;
						goto reset;
					}
				}
			}	
		}
		window.display();//展示窗口
	}


	//设置游戏难度字符串
	switch (gamegrade)
	{
	case 0:
		text10.setFillColor(sf::Color::Black); //颜色 黑
		text10.setString(L"难度:      简单"); //设置难度字符串
		break;
	case 1:
		text10.setFillColor(sf::Color::Black); //颜色 黑
		text10.setString(L"难度:      普通 "); //设置难度字符串
		break;
	case 2:
		text10.setFillColor(sf::Color::Blue); //颜色 黑
		text10.setString(L"难度:      困难 "); //设置难度字符串
		break;
	case 3:
		text10.setFillColor(sf::Color::Red); //颜色 黑
		text10.setString(L"难度:      地狱 "); //设置难度字符串
		break;
	}


	mciSendString(L"close bgm", 0, 0, 0);//关闭音乐文件


	if (gamegrade % 4 == 0)//根据难度选择bgm
	{
		mciSendString(L"open music/music1.mp3 alias bgm", 0, 0, 0);//打开音乐文件1

	}
	if (gamegrade % 4 == 1)
	{
		mciSendString(L"open music/music2.mp3 alias bgm", 0, 0, 0);//打开音乐文件2

	}
	if (gamegrade % 4 == 2)
	{
		mciSendString(L"open music/music3.mp3 alias bgm", 0, 0, 0);//打开音乐文件3

	}
	if (gamegrade % 4 == 3)
	{
		mciSendString(L"open music/music4.mp3 alias bgm", 0, 0, 0);//打开音乐文件4
	}


	mciSendString(L"play bgm repeat", 0, 0, 0);//播放音乐文件
	mciSendString(L"pause bgm", 0, 0, 0);//暂停音乐文件


	//重新开始游戏 初始化
	replaygame:

	if (gamemusic % 2 == 0)
	{	
		mciSendString(L"resume bgm", 0, 0, 0);//播放背景音乐音乐
	}
	
	if (gamegrade % 4 == 0)
	{
		speed_grade = grade0;//当前难度下降速度设置为等级0
	}
	if (gamegrade % 4 == 1)
	{
		speed_grade = grade1;//当前难度下降速度设置为等级1
	}
	if (gamegrade % 4 == 2)
	{
		speed_grade = grade2;//当前难度下降速度设置为等级2
	}
	if (gamegrade % 4 == 3)
	{
		speed_grade = grade3;//当前难度下降速度设置为等级3
	}

	nowspeed = speed_grade;//当前速度调为当前等级速度
	speed_quick = speed_grade/10;//急速速度调为当前等级速度十倍


	goal = 0;//初始化分数
	cleanline = 0;//初始化消行数
	initnextsort = false;
	total = 57600;//初始化游戏时长
	gametime = 0;//计时总时长,达到1清零,total+1;
	onetime = 0;//每次获取的时间


	for (int i = 0; i < Hang; i++)//初始化table
	{
		for (int j = 0; j < Lie; j++)
		{
			table[i][j] = 0;
		}
	}

	newblock(1 + rand()%7);//产生新的方块

	while (window.isOpen())//进入游戏界面
	{
		playgame:
		window.clear(Color::White);//设置窗口背景颜
		float time = kclock.getElapsedTime().asSeconds();
		kclock.restart();//重启计时器
		timer += time;
		keyinput(&window);
		if (timer > nowspeed)
		{
			drop();
			timer = 0;
		}
		nowspeed = speed_grade;

		//清除满行
		clean();

		//游戏时长计时器	
		onetime = gameclock.getElapsedTime().asSeconds();
		gameclock.restart();//重启计时器
		gametime += onetime;
		if (gametime > 1)
		{
			total += 1;
			gametime = 0;
		}
		tmtotal = localtime(&total);
		sprintf(Gametime, "%02u : %02u", tmtotal->tm_min, tmtotal->tm_sec);

		drawspr(&window, &allblocks);//渲染俄罗斯方块
		window.draw(gamephoto);//渲染背景图片
		sprintf(Goal, "%d", goal);
		sprintf(Cleanline, "%d", cleanline);
		sprintf(Maxgoal, "%d", maxgoal);

		text2.setString(Goal); //设置得分字符串
		text6.setString(Cleanline); //设置消行数字符串
		text8.setString(Maxgoal); //设置最高分字符串
		text9.setString(Gametime); //设置游戏时长字符串

	
		//显示文字》
		text2.setPosition(495, 350);  //得分
		text6.setPosition(495, 415);  //消行数
		text8.setPosition(495, 282);  //最高分数
		text9.setPosition(495, 481);//游戏时长

		window.draw(text);//显示分数是
		window.draw(text2);//显示得分
		window.draw(text3);//显示消行是
		window.draw(text4);//显示最高分是
		window.draw(text5);//显示时间是
		window.draw(text6);//显示消行数
		window.draw(text7);//显示下一个方块是
		window.draw(text8);//显示下一个方块是
		window.draw(text9);//显示下一个方块是
		window.draw(text10);//显示难度

		//显示下一个方块	
		drawnext(&window, &allblocks);

		window.draw(quickoff);
		if (isquick == true)
		{
			window.draw(quickon);
		}

		window.display();//展示窗口

		
		if (checkfailed() == false)//检验游戏是否失败
		{
			if (goal >= maxgoal)
			{
				maxgoal = goal;//更换最高分
				FILE* pf = fopen("gamedate.txt", "w");//打开文件
				if (pf == NULL)
				{
					perror("fopen");
					return 1;
				}

				//使用
				fwrite(&maxgoal,sizeof(maxgoal),1,pf);				
				fclose(pf);//关闭文件;
				pf = NULL;
			}

			window.draw(gameoverphoto);//显示失败图

			sprintf(Maxgoal, "%d", maxgoal);
			text8.setString(Maxgoal); //设置最高分字符串

			text2.setPosition(257, 330);
			text6.setPosition(257, 395);
			text8.setPosition(257, 460);
			text9.setPosition(257, 525);
			window.draw(text2);//得分
			window.draw(text6);//消行数
			window.draw(text8);//最高分数
			window.draw(text9);//游戏时长
			window.display();

			Event g;
			if (gamewav % 2 == 0)
			{
				PlaySound(LPWSTR(IDR_WAVE4), GetModuleHandle(NULL), SND_RESOURCE | SND_ASYNC | SND_SYNC);//播放失败音效
			}	
			mciSendString(L"pause bgm", 0, 0, 0);//暂停播放背景音乐音乐
	
			while (1)
			{
				while (window.pollEvent(g))
				{
					if (g.type == Event::Closed || g.key.code == Keyboard::Escape)
					{
						mciSendString(L"close music/music1.mp3", 0, 0, 0);//关闭音乐文件
						window.close();
						return 1;

					}

					if (g.mouseButton.button == Mouse::Left && g.type == Event::MouseButtonReleased &&
						g.mouseButton.x > 33 && g.mouseButton.y > 310 && g.mouseButton.x < 195 && g.mouseButton.y < 469)//判断是否按了重开游戏按钮
					{
						if (gamemusic % 2 == 0)
						{
							mciSendString(L"resume bgm", 0, 0, 0);//继续播放背景音乐							
						}
						goto replaygame;
					}

					if (g.mouseButton.button == Mouse::Left && g.type == Event::MouseButtonReleased &&
						g.mouseButton.x > 436 && g.mouseButton.y > 310 && g.mouseButton.x < 600 && g.mouseButton.y < 469)//判断是否按了返回主菜单按钮
					{
						//mciSendString(L"close music/music1.mp3", 0, 0, 0);//关闭播放背景音乐
						goto remenu;
					}
				}
			}
			//游戏结束
		}

		if (enterstop == true)//判断是否按了暂停按钮
		{	
			gametime = 0;
			mciSendString(L"pause bgm", 0, 0, 0);//暂停播放背景音乐
			enterstop = false;
			Event s;
			Event ss;
			restop://返回暂停
			window.draw(stopphoto);
			window.display();//展示窗口
			while (1)
			{		
				while (window.pollEvent(s))
				{
					if (s.type == Event::Closed || s.key.code == Keyboard::Escape)
					{
						mciSendString(L"close music/music1.mp3", 0, 0, 0);//关闭音乐文件
						window.close();
						return 1;

					}


					if (s.mouseButton.button == Mouse::Left && s.type == Event::MouseButtonReleased &&
						s.mouseButton.x > 390 && s.mouseButton.y > 318 && s.mouseButton.x < 506 && s.mouseButton.y < 434)//判断是否按了继续游戏按钮
					{
						if (gamemusic % 2 == 0)
						{
							mciSendString(L"resume bgm", 0, 0, 0);//继续播放背景音乐
						}			
						goto playgame;
					}
					if (s.mouseButton.button == Mouse::Left && s.type == Event::MouseButtonReleased &&
						s.mouseButton.x > 261 && s.mouseButton.y > 318 && s.mouseButton.x < 376 && s.mouseButton.y < 434)//判断是否按了重开按钮
					{
						if (gamemusic % 2 == 0)
						{
							mciSendString(L"resume bgm", 0, 0, 0);//继续播放背景音乐
						}
						goto replaygame;//继续游戏
					}
					if (s.mouseButton.button == Mouse::Left && s.type == Event::MouseButtonReleased &&
						s.mouseButton.x > 130 && s.mouseButton.y > 318 && s.mouseButton.x < 243 && s.mouseButton.y < 434)//判断是否按了设置按钮
					{
						enterstopset = true;
					}
				}		

				
				if(enterstopset==true)
				{
					enterstopset = false;
					restopset:
					window.draw(stopsetphoto);
					
					if (gamemusic % 2 == 1)//检查音乐是否关闭
					{
						window.draw(yyoff);//显示音乐关闭图标
					}
					if (gamewav % 2 == 1)//检查音效是否关闭
					{
						window.draw(yxoff); //显示音效关闭图标
					}

					window.display();

					while (1)
					{
						while (window.pollEvent(ss))
						{

							if (ss.type == Event::Closed || ss.key.code == Keyboard::Escape)
							{
								mciSendString(L"close music/music1.mp3", 0, 0, 0);//关闭音乐文件
								window.close();
								return 1;

							}

							if (ss.mouseButton.button == Mouse::Left && ss.type == Event::MouseButtonPressed &&
								ss.mouseButton.x > 461 && ss.mouseButton.y > 250 && ss.mouseButton.x < 507 && ss.mouseButton.y < 297)//判断是否按了返回按钮
							{
								goto restop;
							}

							if (ss.mouseButton.button == Mouse::Left && ss.type == Event::MouseButtonPressed &&
								ss.mouseButton.x > 155 && ss.mouseButton.y > 317 && ss.mouseButton.x < 291 && ss.mouseButton.y < 447)//判断是否按了音乐按钮
							{	
								gamemusic++;
								goto restopset;
							}

							if (ss.mouseButton.button == Mouse::Left && ss.type == Event::MouseButtonPressed &&
								ss.mouseButton.x > 350 && ss.mouseButton.y > 314 && ss.mouseButton.x < 485 && ss.mouseButton.y < 446)//判断是否按了音效按钮
							{
								gamewav++;
								goto restopset;
							}
						}
					}
				}
				
			}
		}
		if (entermenu == true)//判断是否按了返回主菜单按钮
		{	
			gametime = 0;
			entermenu = false;
			window.draw(gotomenuphoto);
			window.display();
			mciSendString(L"pause bgm", 0, 0, 0);//暂停播放背景音乐
			Event go;
			while (1)
			{
				while (window.pollEvent(go))
				{
					if (go.type == Event::Closed || go.key.code == Keyboard::Escape)
					{
						mciSendString(L"close music/music1.mp3", 0, 0, 0);//关闭音乐文件
						window.close();
						return 1;
					}

					if (go.mouseButton.button == Mouse::Left && go.type == Event::MouseButtonReleased &&
						go.mouseButton.x > 355 && go.mouseButton.y > 320 && go.mouseButton.x < 486 && go.mouseButton.y < 448)//判断是否按了继续游戏按钮
					{
						if (gamemusic % 2 == 0)
						{
							mciSendString(L"resume bgm", 0, 0, 0);//继续播放背景音乐
						}			
						goto playgame;
					}

					if (go.mouseButton.button == Mouse::Left && go.type == Event::MouseButtonReleased &&
						go.mouseButton.x > 157 && go.mouseButton.y > 320 && go.mouseButton.x < 287 && go.mouseButton.y < 448)//判断是否按了退出游戏按钮
					{
						goto remenu;
					}
				}
			}
		}			
	}
	return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/564687.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-06
下一篇 2022-04-06

发表评论

登录后才能评论

评论列表(0条)

保存