当前位置: 首页 > 编程笔记 >

linux环境下C++实现俄罗斯方块

曹旭
2023-03-14
本文向大家介绍linux环境下C++实现俄罗斯方块,包括了linux环境下C++实现俄罗斯方块的使用技巧和注意事项,需要的朋友参考一下

本文实例为大家分享了C++实现俄罗斯方块的具体代码,供大家参考,具体内容如下

本程序的运行环境是linux,用到了多线程。创建了一个用来绘图的线程和一个获取按键的线程。程序中有一些需要改善的地方,比如336-338行定义的全局变量以及声明的对象。本来声明的Block和Table对象应该在main函数里面,然后将这两个对象作为参数传递给线程函数getkey。但是好像只能传递一个对象参数给线程函数。希望高手能够对程序进行改进。

ps:由于用到了多线程,而pthread不是linux的默认库,所以编译的时候需要指定线程库。即:g++ -o block -lpthread block.cpp

#include <iostream> 
#include <cstdlib> 
#include <pthread.h> 
#include <time.h> 
 
#include<termios.h> 
#include<fcntl.h> 
 
 
#define TABLE_SIZE 20 
#define BLOCK_SIZE 4 
#define SLEEP_TIME 500 
 
using namespace std; 
 
struct grid{int x; int y;};    //坐标 
 
/////////////////////Block 类////////////////////// 
class Block 
{ 
public: 
  enum direct{UP, DOWN, LEFT, RIGHT};         //定义方向 
  grid g[BLOCK_SIZE];                 //方块的坐标信息 
 
  void def_block(grid g1, grid g2, grid g3, grid g4); //定义方块 
  void rotate();                   //旋转方块 
  void move(int dir);                 //移动方块 
  void set_cen(grid g);                //设置方块旋转中心 
  grid get_cen();                   //获取方块旋转中心 
  void set_type(int t);                //设置方块种类 
  int get_type();                   //获取方块种类 
  void back();                    //旋转还原 
  void creat_block(int x, int y);           //随机生成方块 
 
private: 
  grid center;                    //方块旋转中心 
  int type;                      //方块类型 
   
 
}; 
 
void Block::def_block(grid g1, grid g2, grid g3, grid g4) { 
  g[0]=g1; g[1]=g2; g[2]=g3; g[3]=g4; 
} 
 
void Block::rotate() { 
  int x, y, i=0; 
 
  for(i; i<=3; i++) { 
    x=g[i].x-center.x; y=g[i].y-center.y; 
    g[i].x=center.x+y; g[i].y=center.y-x; 
  } 
} 
 
void Block::move(int dir) { 
  int d=dir, i=0; 
 
  switch(d) { 
  case UP: {  
    for(i; i<=3; i++) g[i].y++; 
    center.y++; break; 
       } 
  case DOWN: { 
    for(i; i<=3; i++) g[i].y--; 
    center.y--; break; 
        } 
  case LEFT: { 
    for(i; i<=3; i++) g[i].x--; 
    center.x--; break; 
        } 
  case RIGHT: { 
    for(i; i<=3; i++) g[i].x++; 
    center.x++; break; 
        } 
  } 
} 
 
void Block::set_cen(grid g) { 
  center=g; 
} 
 
grid Block::get_cen() { 
  return center; 
} 
 
void Block::set_type(int t) { 
  type=t; 
} 
 
int Block::get_type() { 
  return type; 
} 
 
void Block::back() { 
  int x, y, i=0; 
 
  for(i; i<=3; i++) { 
    x=g[i].x-center.x; y=g[i].y-center.y; 
    g[i].x=center.x-y; g[i].y=center.y+x; 
  } 
} 
 
void Block::creat_block(int x, int y) {  //随机创建方块 
  int ran; 
  grid g[BLOCK_SIZE]; 
 
   
  ran=1+rand()%7; 
  switch(ran) { 
  //L 
  case 1: { 
    g[0].x=x/2; g[0].y=y-3; 
    g[1].x=g[0].x; g[1].y=g[0].y+1; 
    g[2].x=g[0].x; g[2].y=g[0].y+2; 
    g[3].x=g[0].x+1; g[3].y=g[0].y;  
    set_cen(g[0]); set_type(1); break; 
      } 
  //反L 
  case 2: { 
    g[0].x=x/2; g[0].y=y-3; 
    g[1].x=g[0].x; g[1].y=g[0].y+1; 
    g[2].x=g[0].x; g[2].y=g[0].y+2; 
    g[3].x=g[0].x-1; g[3].y=g[0].y;  
    set_cen(g[0]); set_type(2); break; 
      } 
  //Z 
  case 3: { 
    g[0].x=x/2; g[0].y=y-2; 
    g[1].x=g[0].x; g[1].y=g[0].y+1; 
    g[2].x=g[0].x+1; g[2].y=g[0].y+1; 
    g[3].x=g[0].x-1; g[3].y=g[0].y;  
    set_cen(g[0]); set_type(3); break; 
      } 
  //反Z 
  case 4: { 
    g[0].x=x/2; g[0].y=y-2; 
    g[1].x=g[0].x; g[1].y=g[0].y+1; 
    g[2].x=g[0].x+1; g[2].y=g[0].y+1; 
    g[3].x=g[0].x-1; g[3].y=g[0].y;  
    set_cen(g[0]); set_type(4); break; 
      } 
  //田 
  case 5: { 
    g[0].x=x/2; g[0].y=y-2; 
    g[1].x=g[0].x; g[1].y=g[0].y+1; 
    g[2].x=g[0].x+1; g[2].y=g[0].y+1; 
    g[3].x=g[0].x+1; g[3].y=g[0].y;  
    set_cen(g[0]); set_type(5); break; 
      } 
  //1 
  case 6: { 
    g[0].x=x/2; g[0].y=y-3; 
    g[1].x=g[0].x; g[1].y=g[0].y+1; 
    g[2].x=g[0].x; g[2].y=g[0].y+2; 
    g[3].x=g[0].x; g[3].y=g[0].y-1;  
    set_cen(g[0]); set_type(6); break; 
      } 
  //山 
  case 7: { 
    g[0].x=x/2; g[0].y=y-2; 
    g[1].x=g[0].x; g[1].y=g[0].y+1; 
    g[2].x=g[0].x-1; g[2].y=g[0].y; 
    g[3].x=g[0].x+1; g[3].y=g[0].y;  
    set_cen(g[0]); set_type(7); break; 
      } 
  default: ; 
  } 
  def_block(g[0], g[1], g[2], g[3]); 
} 
 
 
///////////////////////////////////////// 
 
////////////////////Table 类////////////////////// 
class Table 
{ 
public: 
   
  Table() {             //构造棋盘 
    height=20; width=10; count=0; 
    init_table(); 
  } 
  Table(int x, int y); 
  int set_block(Block bl);     //安设方块 
  void clr_block(Block bl);     //清除方块 
  int clr_line(int y);       //消行 
  int get_h();           //获取棋盘高度 
  int get_w();           //获取棋盘宽度 
  int if_full(int y);        //判定是否满行 
  int get_table(int x, int y);   //获取棋盘上点信息 
  void paint();           //绘制棋盘 
  void move_line(int y);      //整行下移 
  void set_count(int c);      //记录得分 
  int get_count();         //获取得分 
 
private: 
  int table[TABLE_SIZE][TABLE_SIZE];//棋盘 
  int height, width;        //棋盘的高和宽 
  int count;            //得分 
 
  void init_table();        //棋盘初始化 
 
}; 
 
void Table::init_table() { 
  int i=0, j=0; 
 
  for(i; i<width; i++) { 
    for(j=0; j<height; j++) { 
      table[i][j]=0; 
    } 
  } 
} 
 
Table::Table(int x, int y) { 
  height=y; width=x; count=0; 
  init_table(); 
} 
 
int Table::set_block(Block bl) { 
  int x, y; 
  int i; 
  for(i=0; i<=3; i++) { 
    x=bl.g[i].x; y=bl.g[i].y; 
    if(table[x][y]!=0 || x>=width || x<0 || y>=height || y<0) { 
      return 0; 
    } 
  } 
  for(i=0; i<=3; i++) { 
    x=bl.g[i].x; y=bl.g[i].y; 
    table[x][y]=1; 
  } 
  return 1; 
} 
 
void Table::clr_block(Block bl) { 
  int x, y; 
 
  for(int i=0; i<=3; i++) { 
    x=bl.g[i].x; y=bl.g[i].y; 
    table[x][y]=0; 
  } 
} 
 
int Table::clr_line(int y) { 
  if(y<0 || y>=height) return 0; 
  for(int i=0; i<width; i++) { 
    table[i][y]=0; 
  } 
  return 1; 
} 
 
int Table::get_h() { 
  return height; 
} 
 
int Table::get_w() { 
  return width; 
} 
 
int Table::if_full(int y) { 
  int i=0; 
 
  for(i; i<width; i++) { 
    if(table[i][y]==0) return 0; 
  } 
  return 1; 
} 
 
int Table::get_table(int x, int y) { 
  return table[x][y]; 
} 
 
void Table::paint() { 
  int i, j; 
 
  for(i=0; i<width+2; i++) cout<<"-"<<flush; 
  cout<<"\n"<<flush; 
  for(i=height-1; i>=0; i--) { 
    cout<<"|"<<flush; 
    for(j=0; j<width; j++) { 
      if(table[j][i]==0) cout<<" "<<flush; 
      else cout<<"▣"<<flush; 
    } 
    if(i==10) 
      cout<<"|  得分:"<<get_count()<<endl; 
    else if(i==7) 
      cout<<"|  Press 'q' to quit!"<<endl; 
    else 
      cout<<"|"<<endl; 
  } 
  for(i=0; i<width+2; i++) cout<<"-"<<flush; 
  cout<<"\n"<<flush; 
  //cout<<"得分:"<<get_count()<<endl; 
} 
 
void Table::move_line(int y) { 
  int i, j; 
 
  for(i=y; i<height-1; i++) { 
    for(j=0; j<width; j++) { 
      table[j][i]=table[j][i+1]; 
    } 
  } 
} 
 
void Table::set_count(int c) { 
  count+=c; 
} 
 
int Table::get_count() { 
  return count; 
} 
 
/////////////////////////////////////////////////////// 
class Mythread 
{ 
public: 
  void init(); 
  static void *getkey(void *arg);//线程函数在类里面定义必须定义为static型,以去除类指针。 
  static void *paint_loop(void *arg); 
}; 
 
void Mythread::init() 
{ 
  pthread_t ntid,ntid2; 
  int err,err2;     
  err = pthread_create(&ntid,NULL,getkey,NULL); 
  err2 = pthread_create(&ntid2,NULL,paint_loop,NULL); 
  if(err != 0 || err2 != 0){ 
    cout<<"can't create thread!"<<endl; 
    exit(0); 
  } 
} 
 
unsigned char flag=1,buf[2];//全局变量 
Table tab(15, 20); //构造一个15,20的棋盘 
Block bl;      //构造一个落下方块 
void* Mythread::paint_loop(void *arg) 
{ 
  while(1) 
  { 
    system("clear"); 
    tab.paint(); 
    usleep(50000);    //暂停50 MS 
  } 
} 
void* Mythread::getkey(void *arg) 
{ 
  struct termios saveterm,nt; 
  fd_set rfds,rs; 
  struct timeval tv; 
  int i=0,q,r,fd=0; 
  tcgetattr(fd,&saveterm); 
  nt=saveterm; 
 
  nt.c_lflag &= ~ECHO; 
  nt.c_lflag &= ~ISIG; 
  nt.c_lflag &= ~ICANON; 
 
  tcsetattr(fd,TCSANOW,&nt); 
 
  FD_ZERO(&rs); 
  FD_SET(fd,&rs); 
  tv.tv_sec=0; 
  tv.tv_usec=0; 
  while(1) 
  {   
    read(0,buf,1); 
    r=select(fd+1,&rfds,NULL,NULL,&tv); 
    if(r<0) 
    { 
      write(1,"select() error.\n",16); 
    } 
    rfds=rs; 
    if(flag==2||buf[0]==113)//游戏结束或者用户按下'q'键,则程序退出 
    { 
      tcsetattr(0,TCSANOW,&saveterm); 
      exit(0); 
    } 
    if(buf[0]<=68&&buf[0]>=65) flag=0;//如果按的键是方向键,则将标志位置0并执行相应的处理. 
    if(flag==0) 
    { 
      if(buf[0]==65) { 
      //if(dir!=0) { 
        if(bl.get_type()==5) continue; //如果出现田字形则不作旋转 
        tab.clr_block(bl);      //清空方块上一次位置 
        bl.rotate();         //开始旋转 
        if(!tab.set_block(bl)) {   //将旋转后的方块写在棋盘上 
          bl.back();       //如果写失败(例如到边线了,或卡住了)则还原旋转前位置 
          continue; 
          tab.set_block(bl);     
        } 
      } 
      //下(加速下落) 
      //dir=GetAsyncKeyState(VK_DOWN);  //获取向下 
      if(buf[0]==66) { 
        tab.clr_block(bl);     //清空方块上一次位置 
        bl.move(bl.DOWN);      //向下移动一步 
        if(!tab.set_block(bl)) {  //将移动后的方块写在棋盘上 
          bl.move(bl.UP);     //如果失败,则还原到移动前的位置(即上移一步) 
          tab.set_block(bl); 
        } 
      } 
      //左(左移) 
      //dir=GetAsyncKeyState(VK_LEFT); 
      if(buf[0]==68) { 
        tab.clr_block(bl); 
        bl.move(bl.LEFT); 
        if(!tab.set_block(bl)) { 
          bl.move(bl.RIGHT); 
          tab.set_block(bl); 
        } 
      } 
      //右(右移) 
      //dir=GetAsyncKeyState(VK_RIGHT); 
      if(buf[0]==67) { 
        tab.clr_block(bl); 
        bl.move(bl.RIGHT); 
        if(!tab.set_block(bl)) { 
          bl.move(bl.LEFT); 
          tab.set_block(bl); 
        } 
      } 
      flag=1; 
    } 
  } 
  tcsetattr(0,TCSANOW,&saveterm); 
} 
 
////////////主函数部分/////////////////////// 
 
int main() 
{ 
  //Table tab(15, 20); //构造一个15,20的棋盘 
  //Block bl;      //构造一个落下方块 
  Mythread thread; 
  thread.init(); 
  int dir,i,c; 
  while(true) { 
    //生成方块 
    srand(time(0)); 
    bl.creat_block(tab.get_w(), tab.get_h()); 
    //判断游戏是否结束 
    if( !tab.set_block(bl) ) { 
      system("clear"); 
      cout<<"GAME OVER!"<<endl; 
      flag=2; 
      cout<<"PRESS ANY KEY TO CONTINUE!"<<endl; 
      while(1); 
    } 
    ///////////行动按键判定 
    while(true){ 
      usleep(500000);    //暂停500 MS 
      /////////////向下移动一格 
      tab.clr_block(bl);    //清空上一次方块位置 
      bl.move(bl.DOWN);    //向下移动一步 
      if(!tab.set_block(bl)) {   //是否触底 
        bl.move(bl.UP);    //如果触底,还原触底前位置 
        tab.set_block(bl); 
        break; 
      } 
    } 
    //如果满行则消行 
    for(i=0; i<tab.get_h(); i++) { 
      if(tab.if_full(i)) { //是否满行 
        tab.clr_line(i); //如果是,消行 
        tab.move_line(i); //将所消行的上面的棋盘信息下移 
        i--;      //下移后,重新检查这一行是否满(可能出现几行同时消去) 
        tab.set_count(100); //记录得分 
      } 
    } 
     
  } 
  return 0; 
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持小牛知识库。

 类似资料:
  • 本文向大家介绍C++实现俄罗斯方块(windows API),包括了C++实现俄罗斯方块(windows API)的使用技巧和注意事项,需要的朋友参考一下 本文分享的这些俄罗斯方块代码是我最近放假在家里自己写的,虽然以前有过看别人写的代码,但是那个游戏代码好像不是很全面,因为无法实现全部的方块和实现随机的产生任意方向的方块,现在也基本上是忘光了当时的代码,下面的这些代码是我最近写的,没有参考其他人

  • 本文向大家介绍C# 实现俄罗斯方块(附源码),包括了C# 实现俄罗斯方块(附源码)的使用技巧和注意事项,需要的朋友参考一下 概述 俄罗斯方块(Tetris)是一款由俄罗斯人阿列克谢·帕基特诺夫发明的休闲游戏,帕基特诺夫爱玩拼图,从拼图游戏里得到灵感,设计出了俄罗斯方块。由于上手简单、老少皆宜,从而家喻户晓,风靡世界。本文简述如何通过C#来实现俄罗斯方块,仅供学习分享使用,如有不足之处,还请指正。

  • 本文向大家介绍C语言代码实现俄罗斯方块,包括了C语言代码实现俄罗斯方块的使用技巧和注意事项,需要的朋友参考一下 这里为大家敲写一段怎样用C语言实现俄罗斯方块: 首先推荐大家使用CodeBlocks这个软件,方便添加不同的工程。 代码中有很多注释便于理解! 下面是效果图和全部的代码以及注释,大家可以观看并自己新增内容! 1、首先是main.c文件: 2、然后是mywindows.h文件: 3、接下来

  • 本文向大家介绍C语言源码实现俄罗斯方块,包括了C语言源码实现俄罗斯方块的使用技巧和注意事项,需要的朋友参考一下 介绍 俄罗斯方块(Tetris, 俄文:Тетрис)是一款电视游戏机和掌上游戏机游戏,它由俄罗斯人阿列克谢·帕基特诺夫发明,故得此名。俄罗斯方块的基本规则是移动、旋转和摆放游戏自动输出的各种方块,使之排列成完整的一行或多行并且消除得分。由于上手简单、老少皆宜,从而家喻户晓,风靡世界。

  • 本文向大家介绍Python Pygame实现俄罗斯方块,包括了Python Pygame实现俄罗斯方块的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了Python Pygame实现俄罗斯方块的具体代码,供大家参考,具体内容如下 源码: 效果: 更多俄罗斯方块精彩文章请点击专题:俄罗斯方块游戏集合 进行学习。 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程

  • 本章实现一个俄罗斯方块游戏。 简介 俄罗斯方块游戏是有史以来最受欢迎的电脑游戏之一。最初的游戏是由俄罗斯程序员 Alexey Pajitnov 在1985年设计并编写的。从那时起,《俄罗斯方块》便以多种形式出现在几乎所有平台上。 俄罗斯方块被称为掉落方块拼图游戏。在这款游戏中,我们有7种不同的形状,叫做砖块(tetrminoes):S形、Z形、T形、L形、线形、反向L形和方形。每个形状都是由四个正