栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++控制台实现俄罗斯方块游戏

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++控制台实现俄罗斯方块游戏

之前学了些C++的课程,一直想着说编点小游戏,可是MFC又不想学,所以就只能变成控制台的小游戏。

俄罗斯方块一定是很多人小时候玩过的游戏。接下来就说说设计想法。

主要实现,选择游戏的等级,加速下降,不同形状不同颜色,暂停和退出功能。

首先是类的设计。

class Box 
{ 
 private: 
  int map[23][12];//画面坐标,记录有方块的点,也是游戏界面 
  int hotpoint[2];//当前活动的点,所有图形都是以此为基准绘制的 
  int top;//当前最高位置 
  int point;//分数 
  int level;//等级 
  int ID;//当前活动图形的ID号 
  int colorID;//图形的颜色ID。 
 public: 
  Box()//初始化 
  { 
   int i,j; 
   for(i=0;i<23;i++) 
    for(j=0;j<12;j++) 
     map[i][j]=0; 
   hotpoint[0]=0; 
   hotpoint[1]=5; 
   point=0; 
   level=1; 
   top=99; 
   ID=0; 
  } 
  void SetColor(int color);//颜色 
  void DrawMap();//画游戏的大界面 
  bool Judge(int x,int y);//判断当前位置能否绘制图形 
  void Welcome();//欢迎界面 
  void DrawBox(int x,int y,int num);//绘制图形 
  void Redraw(int x,int y,int num);//擦除图形 
  void Run();//运行 
  void Turn();//转动方块 
  void UpdataMap();//更新画面 
  void Pause();//暂停 
}; 

接下来就是一些常量和光标函数,便于保存和调用

#define A1 0//A代表长条型,B为方块,C为L型,D为闪电型 
#define A2 1 
 
 
#define B 2 
 
 
#define C11 3 
#define C12 4 
#define C13 5 
#define C14 6 
 
 
#define C21 7 
#define C22 8 
#define C23 9 
#define C24 10 
 
 
#define D11 11 
#define D12 12 
 
 
#define D21 13 
#define D22 14 
 
 
void SetPos(int i,int j)//设定光标位置 
{ 
COORD pos={i,j}; 
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos); 
} 
 
 
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};//这个数组是用来保存各个形状高度的,以上面的坐标相对应 

类方法的实现

void Box::SetColor(int colorID) 
{ 
 int n_color; 
 switch(colorID) 
 { 
  case 0: n_color = 0x08;break; 
  case 1: n_color = 0x0C;break; 
  case 2: n_color = 0x0D;break; 
  case 3: n_color = 0x0E;break; 
  case 4: n_color = 0x0A;break; 
 } 
 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), n_color); 
} 
void Box::DrawMap()//画界面 
{ 
 int i; 
 
 SetColor(0);//界面颜色 
 
 for(i=0;i<14;i++) 
 { 
   SetPos(i*2,0); 
   cout<<"■"; 
 } 
 for(i=1;i<=24;i++) 
 { 
  SetPos(0,i); 
  cout<<"■"; 
  SetPos(13*2,i); 
  cout<<"■"; 
 } 
 for(i=0;i<14;i++) 
 { 
   SetPos(i*2,24); 
   cout<<"■"; 
 } 
  
 i=15; 
 for(i=15;i<=25;i++) 
 { 
   SetPos(i*2,0); 
   cout<<"■"; 
 } 
 for(i=1;i<=8;i++) 
 { 
  SetPos(15*2,i); 
  cout<<"■"; 
  SetPos(25*2,i); 
  cout<<"■"; 
 } 
 for(i=15;i<=25;i++) 
 { 
   SetPos(i*2,9); 
   cout<<"■"; 
 } 
 
 SetPos(16*2,16); 
 cout<<"俄罗斯方块"; 
 SetPos(16*2,17); 
 cout<<"分数:"<='1')//设置等级 
  { 
   level=x-'0'; 
   break; 
  } 
 } 
} 
 
void Box::UpdataMap()//更新画面(关键) 
{ 
  int clear; 
  int i,j,k; 
  int nx,ny; 
  int flag; 
  for(i=0;i<4;i++)//更新map数组的信息 
  { 
  nx=hotpoint[0]+sharp[ID][i*2]; 
  ny=hotpoint[1]+sharp[ID][i*2+1]; 
  map[nx][ny]=1; 
  } 
  if(hotpoint[0]=top;k--)//从当前位置向上所有的点下移一行 
    { 
     if(k==0)//最高点特殊处理 
      for(j=0;j<12;j++) 
      { 
map[k][j]=0; 
SetPos((j+1)*2,k+1); 
cout<<" "; 
      } 
     else 
     { 
      for(j=0;j<12;j++) 
      { 
map[k][j]=map[k-1][j]; 
SetPos((j+1)*2,k+1); 
if(map[k][j]==0) 
cout<<" "; 
else 
cout<<"■"; 
      } 
     } 
    } 
    top++;//消除成功,最高点下移 
    clear++; 
    point+=clear*10*level; 
   } 
  } 
  SetColor(0); 
  SetPos(16*2,17); 
  cout<<"分数:"<=Count) 
  { 
   i=0;//计数器清零 
   if(Judge(hotpoint[0]+1,hotpoint[1]))//如果下个位置无效(即到底) 
   { 
     UpdataMap();//更新画面 
     ID=nextID;//生成新ID,用原等待ID替换为当前ID 
     hotpoint[0]=0;//热点更新 
     hotpoint[1]=5; 
     Redraw(3,17,nextID); 
     nextID=rand()%15; 
     DrawBox(hotpoint[0],hotpoint[1],ID); 
     DrawBox(3,17,nextID); 
     if(Judge(hotpoint[0],hotpoint[1]))//无法绘制开始图形,游戏结束 
     { 
      //getch(); 
      system("cls"); 
      SetPos(25,15); 
      cout<<"游戏结束!!!最终得分为:"<=23||ny<0||ny>=12||map[nx][ny]==1)//不能,返回1 
   return 1; 
 } 
 return 0; 
} 
void Box::Pause() 
{ 
 system("cls"); 
 while(1) 
 {  
  SetPos(30,13); 
  cout<<"暂停等待,咖啡时间^-^"<


void main()//主函数 
{ 
 Box game; 
 game.Welcome(); 
 system("cls"); 
 game.DrawMap(); 
 game.Run(); 
} 

待改进的点: 

1、加速下降的时候,从代码中也可以发现,最后几格没法加速。 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/62848.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号