俄罗斯方块_c++实现
语言: c++
思路:
- 画出游戏地图, 并留出下一图形和分数显示的位置
- 图形的建立和颜色
- 图形下落的实现以及上一图形的清除
- 是否能继续下落或变形的检测
- 某一行是否已满需清除以及清除功能与分数更新
简要介绍:
整个游戏用一个类实现
class Tetris
{
private:int rank; //游戏难度等级int score; // 得分int id; //图形IDint 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();
};
核心函数为run()函数, 其他函数均为其提供功能。
几个重要函数:
附上全部代码, 均有注解
#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; //图形IDint 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 14Tetris::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 的图形, 若不可画则反回trueint 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;elsecount = 50;elsecount = 5;int i = 0; //不同等级对应不同countwhile(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();
}
门户:Portal
俄罗斯方块_c++实现相关推荐
- c++ 小游戏_C/C++编程新手入门基础系列:俄罗斯方块小游戏制作源代码
这篇文章主要为大家详细介绍了C语言实现俄罗斯方块小游戏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下. 1.要先下载一个 graphics.h 的头文件来绘图. 2.初始化窗口:initgraph ...
- 3d游戏编程大师技巧 源代码_C/C++编程入门基础系列:俄罗斯方块小游戏制作,直接源代码分享...
这篇文章主要为大家详细介绍了C语言实现俄罗斯方块小游戏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 1.要先下载一个 graphics.h 的头文件来绘图. 2.初始化窗口:initgraph( ...
- C++【EasyX】俄罗斯方块
文章目录 一.前言 二.效果 三.实现流程 1.界面设计 2.方块元素设计 3.方块颜色 4.俄罗斯方块设计 5.当前俄罗斯方块与下一俄罗斯方块 (1).俄罗斯方块的随机生成 (2).俄罗斯方块的迭代 ...
- python编的俄罗斯方块游戏下载_python写的俄罗斯方块游戏
python写的俄罗斯方块游戏 功能包括:记录所花费时间;消去的总行数;总分;排行榜,最高记录查看等. 排行榜中包含一系列的统计功能,如单位时间消去的行数,单位时间得分等. from Tkinter ...
- GitHub开源的超逼真俄罗斯方块游戏
俄罗斯方块一直是各类程序语言热衷实现的经典游戏,有很多JavaScript实现版本,本开源项目使用React实现. 项目试玩网址:https://chvin.github.io/react-tetri ...
- Python实现俄罗斯方块
目录 1.外形设计 2.方块设计 3.停靠设计 俄罗斯方块是儿时最经典的游戏之一,刚开始接触 pygame 的时候就想写一个俄罗斯方块.但是想到旋转,停靠,消除等操作,感觉好像很难啊,等真正写完了发现 ...
- AI玩俄罗斯方块(Python实现)
目录 1.环境 2.实现机制(Pierre Dellacherie算法) 3.代码实现 人工智能大火的今天,如果还是自己玩俄罗斯方块未免显得太LOW,为什么不对游戏升级,让机器自己去玩俄罗斯方块呢?有 ...
- 如何让AI教机器自己玩俄罗斯方块?
作者 | Ahab 转载自公众号Ahab杂货铺(ID:PythonLearningCamp) 人工智能大火的今天,如果还是自己玩俄罗斯方块未免显得太 LOW,为什么不对游戏升级,让机器自己去玩俄罗斯方 ...
- 500行代码写一个俄罗斯方块游戏
导读:本文我们要制作一个俄罗斯方块游戏. 01 俄罗斯方块 Tetris 俄罗斯方块游戏是世界上最流行的游戏之一.是由一名叫Alexey Pajitnov的俄罗斯程序员在1985年制作的,从那时起,这 ...
最新文章
- hdu-4302-Holedox Eating-线段树-单点更新,有策略的单点查询
- 吃鸡服务器炸了会显示什么,绝地求生服务器再爆炸,还能不能好好吃鸡了?
- 浅谈Java SE、Java EE、Java ME三者的区别
- 007_SpringBoot文件上传
- HDMI_VGA_CBVS同时显示
- 在vim粘贴系统剪切板里的内容
- Python列表排序 reverse、sort、sorted 操作方法详解
- 菜鸟要飞java_Java Android视频教程 下载
- HttpClient在传参和返回结果的中文乱码问题
- C#编程中的66个好习惯,你有多少个?(转)
- 开源应用架构之asterisk
- java 枚举迭代_Java中的枚举和迭代器之间的区别
- windows下使用net-snmp实现agent扩展(一)
- 从0到1,手把手教你如何使用哈工大NLP工具——PyLTP
- C++:mutable关键字
- CentOS6.4下Mysql数据库的安装与配置
- 【OpenCV】图像旋转详解,边缘用黑色填充
- 关于java字符串编译优化问题
- 基于(7,4 ) 线性分组码编码和 BPSK 调制
- 找不到主类或无法加载主类
热门文章
- BZOJ 1778: [Usaco2010 Hol]Dotp 驱逐猪猡(高斯消元+期望dp)
- 计算机支持最大内存大小,64位电脑系统可以支持多大内存【详细介绍】
- Go安装:语言环境+开发工具GoLand
- 运维之思科篇 -----5. NAT及静态转换 、 动态转换及PAT
- LMT NEW PBS作业排队系统的队列通信机制
- Android低功耗蓝牙
- ChatGPT检测到可疑登陆行为求高手解惑
- python实现笑傲江湖人物关系网
- AVT工业相机Windows、Opencv开发——相机的配置
- Java面试之Java基础3——字符型常量与字符串常量的区别