首页 > 编程语言 >c++ 游戏:俄罗斯方块

c++ 游戏:俄罗斯方块

时间:2024-06-12 22:59:32浏览次数:15  
标签:游戏 point int void c++ break id 方块 cout

​​​​​​​#include<iostream>#include<string>#include<cstdlib>#include<windows.h>#include<ctime>#include<conio.h>#include<cstdio>using namespace std; class Tetris{private: int rank; //游戏难度等级 int score; // 得分 int id; //图形ID int point[2]; //两基点 int top; //最高点高度public: Tetris(); void Welocme(); //首界面 void DrawMap(); //游戏界面 void SetColor(int); //控制颜色 void Draw(int, int, int); //画图形 void Run(); //运行游戏 void ReDraw(int, int, int); //清除图形 bool Judge(int, int, int); void Turn(int); //旋转 void Updata(); // 更新界面 void Pause(); //游戏暂停 void Input_score();}; const int sharp[15][8] = //组成图形的各个点的各个坐标,先纵后横{{0,0,1,0,2,0,3,0},{0,0,0,1,0,2,0,3},{0,0,1,0,0,1,1,1},{0,0,1,0,1,1,1,2},{0,1,1,1,2,0,2,1},{0,0,0,1,0,2,1,2},{0,0,0,1,1,0,2,0},{1,0,1,1,1,2,0,2},{0,0,0,1,1,1,2,1},{0,0,0,1,0,2,1,0},{0,0,1,0,2,0,2,1},{0,0,0,1,1,1,1,2},{0,1,1,0,1,1,2,0},{0,1,0,2,1,0,1,1},{0,0,1,0,1,1,2,1}}; const int high[15] = { 4,1,2,2,3,2,3,2,3,2,3,2,3,2,3 };int map[28][16]; #define a1 0 //条形#define a2 1#define b 2 // 方块 #define c1 3 //L形#define c2 4#define c3 5#define c4 6 #define d1 7 //T形#define d2 8 #define d3 9#define d4 10 #define e1 11 //闪电1形#define e2 12 #define f1 13 //闪电2形#define f2 14 Tetris::Tetris() //构造函数, 初始化各个值{ point[0] = 0; point[1] = 5; score = 0; top = 25;} void Tetris::Turn(int num) //旋转函数{ switch (num) { case a1: id = a2; break; //条形互换 case a2: id = a1; break; case b: id = b; break; //方块无法旋转 case c1: id = c2; break; //各种L形互换 case c2: id = c3; break; case c3: id = c4; break; case c4: id = c1; break; case d1: id = d2; break; //各种T形互换 case d2: id = d3; break; case d3: id = d4; break; case d4: id = d1; break; case e1: id = e2; break; //两种闪电形互换 case e2: id = e1; break; case f1: id = f2; break; case f2: id = f1; break; }} void SetPos(int i, int j) //控制光标位置, 列, 行{ COORD pos = { i,j }; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);} void Tetris::Pause() // 暂停函数{ SetPos(32, 10); cout << "游戏暂停!" << endl; SetPos(30, 11); cout << "你的分数为 " << score; char temp; while (1) { while (1) { if (_kbhit()) { temp = _getch(); break; } } if (temp == 32) break; } SetPos(32, 10); // 清除暂停时显示的信息 cout << " "; SetPos(30, 11); cout << " ";} void Tetris::Updata() //更新函数{ int i, flag; int nx, ny; for (i = 0; i < 4; i++) { nx = point[0] + sharp[id][i * 2]; ny = point[1] + sharp[id][i * 2 + 1]; SetPos((ny + 1) * 2, nx + 1); SetColor(0); cout << "■"; map[nx][ny] = 1; //界面各个点是否为空的更新 } if (point[0] < top) top = point[0]; //最高点的更新 for (i = point[0]; i < point[0] + high[id]; i++) //消除行 { flag = 1; for (int j = 0; j < 13; j++) //判定某一行是否满, 用flag来标记 if (map[i][j] == 0) flag = 0; if (flag == 1) { for (int k = i; k >= top; k--) { for (int p = 0; p < 13; p++) { map[k][p] = map[k - 1][p]; SetPos((p + 1) * 2, k + 1); if (map[k][p] == 1) cout << "■"; else cout << " "; } } score += 10; Input_score(); } }} void Tetris::Input_score(){ SetColor(3); SetPos(30, 19); cout << "得分: " << score;} void Tetris::Welocme() //欢迎界面{ SetColor(1); char x; while (1) { system("cls"); cout << "■■■■■■■■■■■■■■■■■■■■■" << endl; cout << " 俄罗斯方块 " << endl; cout << "■■■■■■■■■■■■■■■■■■■■■" << endl; cout << " 操作方式:" << endl; cout << " ↑ - 旋转" << endl; cout << " ↓ - 加速下移" << endl; cout << " ← - 左移" << endl; cout << " → - 右移" << endl; cout << " 空格 - 暂停" << endl; cout << "■■■■■■■■■■■■■■■■■■■■■" << endl; cout << "■ 按1—3选择难度■" << endl; SetPos(20, 10); x = getchar(); if (x <= '9' && x >= '0') { rank = x - '0'; break; } }} void Tetris::SetColor(int color_num) //设置颜色{ int n; switch (color_num) { case 0: n = 0x08; break; case 1: n = 0x0C; break; case 2: n = 0x0D; break; case 3: n = 0x0E; break; case 4: n = 0x0A; break; } SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), n);} void Tetris::DrawMap() //画游戏时界面{ int i; SetColor(0); for (i = 0; i < 24; i++) //宽24格 { SetPos(i * 2, 0); cout << "■"; SetPos(i * 2, 26); cout << "■"; } for (i = 0; i < 26; i++) //高26格 { SetPos(0, i); cout << "■"; SetPos(28, i); cout << "■"; SetPos(46, i); cout << "■"; } for (i = 14; i < 24; i++) { SetPos(i * 2, 16); cout << "■"; } SetColor(3); Input_score(); SetPos(30, 21); cout << "难度等级: " << rank; SetPos(32, 2); cout << "下一图形";} void Tetris::Draw(int x, int y, int num) //画图形{ int nx, ny; for (int i = 0; i < 4; i++) { nx = x + sharp[num][2 * i]; ny = y + sharp[num][2 * i + 1]; SetPos((ny + 1) * 2, nx + 1); SetColor(i + 1); cout << "■"; }} void Tetris::ReDraw(int x, int y, int num) //为更新图形的位置清除图形{ int nx, ny; for (int i = 0; i < 4; i++) { nx = x + sharp[num][2 * i]; ny = y + sharp[num][2 * i + 1]; SetPos((ny + 1) * 2, nx + 1); cout << " "; }} bool Tetris::Judge(int x, int y, int num) //判定在x, y 所指位置是否可画编号为{ //num 的图形, 若不可画则反回true int nx, ny; for (int i = 0; i < 4; i++) { nx = x + sharp[num][2 * i]; ny = y + sharp[num][2 * i + 1]; if (!(nx < 25 && nx >= 0 && ny < 13 && ny >= 0 && !map[nx][ny])) return true; } return false;} void Tetris::Run() //运行游戏{ int next_id; srand((int)time(0)); id = rand() % 15; next_id = rand() % 15; Draw(point[0], point[1], id); Draw(5, 16, next_id); int count; if (rank == 1) count = 150; else if (rank == 2) count = 100; else if (rank==3) count = 50; else count = 5; int i = 0; //不同等级对应不同count while (1) { if (!(i < count)) //i 与 count 用于控制时间 { i = 0; if (Judge(point[0] + 1, point[1], id)) //在某一位置不能下落的话 { Updata(); id = next_id; ReDraw(5, 16, next_id); next_id = rand() % 15; point[0] = 0; point[1] = 5; Draw(point[0], point[1], id); Draw(5, 16, next_id); if (Judge(point[0], point[1], id)) { system("cls"); SetPos(20, 10); cout << "游戏结束!" << endl; SetPos(20, 11); cout << "你的分数为 " << score << endl; system("pause"); exit(1); } } else //继续下落 { ReDraw(point[0], point[1], id); point[0]++; Draw(point[0], point[1], id); } } if (_kbhit()) //键盘输入值时 { int key, key2; key = _getch(); if (key == 224) { key2 = _getch(); if (key2 == 72) //按向上方向键时 { int temp = id; Turn(id); if (Judge(point[0], point[1], id)) id = temp; ReDraw(point[0], point[1], temp); Draw(point[0], point[1], id); } if (key2 == 80) //按向下方向键时 { if (!Judge(point[0] + 2, point[1], id)) { ReDraw(point[0], point[1], id); point[0] += 2; Draw(point[0], point[1], id); } } else if (key2 == 75) //按向左方向键时 { if (!Judge(point[0], point[1] - 1, id)) { ReDraw(point[0], point[1], id); point[1]--; Draw(point[0], point[1], id); } } else if (key2 == 77) //按向右方向键时 { if (!Judge(point[0], point[1] + 1, id)) { ReDraw(point[0], point[1], id); point[1]++; Draw(point[0], point[1], id); } } } else if (key == 32) // 按下空格暂停 Pause(); } Sleep(1); //等待1毫秒 i++; //控制下落间隔 }} int main(){ Tetris game; game.Welocme(); system("cls"); //清除欢迎界面 game.DrawMap(); game.Run();}

标签:游戏,point,int,void,c++,break,id,方块,cout
From: https://blog.csdn.net/bbc121223/article/details/139638657

相关文章

  • C++ 新特性 | C++ 11 | typename关键字
    文章目录一、typename关键字前言:在C++的模板编程中,typename关键字扮演着至关重要的角色。它主要用于指示编译器将一个特定的标识符解释为类型名称,而不是变量名或其他实体。本文将深入探讨typename的用法,帮助读者更好地理解其在模板编程中的作用。一、typename关......
  • C++基础入门学习记录
    本系列基于黑马程序员|c++课程,记录学习相关视频——黑马程序员匠心之作|C++教程从0到1入门编程,学习编程不再难_哔哩哔哩_bilibiliC++基础入门2.6字符串型作用:用于表示一串字符两种风格bool类型占==1个字节==大小示例:C风格字符串: char变量名[]="字符串值"示例:......
  • C++基础入门学习记录
    本系列基于黑马程序员|c++课程,记录学习相关视频——黑马程序员匠心之作|C++教程从0到1入门编程,学习编程不再难_哔哩哔哩_bilibiliC++基础入门3运算符**作用:**用于执行代码的运算本章我们主要讲解以下几类运算符:运算符类型作用算术运算符用于处理四则运算赋值运算符用于......
  • c++哈希表hash_table的深度学习(hash_map,un和hash_set的底层实现)
    什么是哈希表?哈希表(HashTable)是一种数据结构,它使用哈希函数将键(key)映射到桶(bucket)或槽(slot)中,可以直接通过相应的键值直接对数据进行访问,高效的插入,删除,查找 哈希表的组成部分和特性哈希函数:哈希函数接受一个键作为输入,并返回一个索引值(通常是一个整数),该索引值用于确定键......
  • 用 Visual C++ 2022 和 CMake 编译 CUnit 静态库
    准备工作源代码获取CUnit是知名的C语言单元测框架,其源代码最初发布在sourceforge上,网址为:https://sourceforge.net/projects/cunit/截止到目前为止,最新Release版的版本号是:2.1-3,发布时间是2014年4月24日。有一些Fork自sourceforge的后续改进版本,我们选取的是https://g......
  • C++学习笔记,文件操作;文件写入读取
    目录5文件操作5.1文本文件5.1.1写文件5.1.2读文件 5.2二进制文件  5.2.1写文件5.2.2读文件 5文件操作程序运行时产生的数据都属于临时数据,程序一旦运行结束都会被释放通过文件可以将数据持久化C++中对文件操作需要包含头文件<fstream>文件类型分为两......
  • 【C++】多线程(基于Windows以及pthread库)
    文章目录一、前言1.1进程和线程二、创建线程2.1线程函数pthread_self(void)2.2创建线程三、线程退出3.1线程函数pthread_exit()四、线程回收4.1线程函数pthread_join()4.2线程数据回收五、线程分离5.1线程函数pthread_detach()六、C++线程类七、线程同......
  • 110.网络游戏逆向分析与漏洞攻防-装备系统数据分析-装备与技能描述信息的处理
    免责声明:内容仅供学习参考,请合法利用知识,禁止进行违法犯罪活动!如果看不懂、不知道现在做的什么,那就跟着做完看效果,代码看不懂是正常的,只要会抄就行,抄着抄着就能懂了内容参考于:易道云信息技术研究院上一个内容:109.商店与捨取窗口数据的处理码云版本号:4275a0966772e3fd4941ee......
  • DP经典问题----背包问题的代码实现(入门级)(C++/PYTHON)
    背包的状态转换方程i:表示物品序号j:表示背包大小W[i]:表示第i件物品的重量f[i,j]:表示在前i件物品中选择若干件放在承重为j的背包中,可以取得的最大价值f[i-1,j-Wi]:表示在前i-1件物品中选择若干件放在承重为j-Wi的背包中,可以取得的最大价值Pi(j>=Wi):表示第i件物品的价值,要......
  • 贪吃蛇c++
    来源5.贪吃蛇-从头开始编程_哔哩哔哩_bilibili我对代码进行了理解,并进行了改写,代码如下。因为水平有限,理解有误的地方,敬请指正。#include<iostream>#include<list>#include<thread>#include<Windows.h>usingnamespacestd;intnScreenWidth=120;intnScreenH......