热门标签 | HotTags
当前位置:  开发笔记 > IOS > 正文

Linux下用C++实现俄罗斯方块

这篇文章主要为大家详细介绍了Linux下用C++实现俄罗斯方块的相关资料,感兴趣的小伙伴们可以参考一下

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

1.block.c

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "block.h"
 
//init for globle
void
init_for_globle(void)
{
  x = X / 2 - 2;   // the first diamond appear postion
  flag_erase = 1;
  srand(getpid());  //srand
  num = rand() % 7;  // random appear first diamond
  mode = rand() % 4; // random appear first diamond mode
  color = rand() % 7 + 41;  // random first diamond color
 
  next_num = rand() % 7;
  next_mode = rand() % 4;
  save_color = rand() % 7 + 41;
 
  print_start_interface();  // print game start interface
// print_score();   // print init score 0
// print_level();   // print init level 1
}
 
//print start interface
void
print_start_interface(void)
{
  int x, y;
  printf("\33[2J");
  printf("\33[%d;%dH\33[32m分数:\33[0m", p_y + 10, p_x + 25);
  printf("\33[%d;%dH\33[32m等级:\33[0m", p_y + 14, p_x + 25);
  for (x = p_x, y = p_y; x <= 46; x++)
    printf("\33[%d;%dH\33[41m==\33[0m", y, x);
  for (x = p_x, y = p_y + 1; y <= 25; y++)
    printf("\33[%d;%dH\33[41m||\33[0m", y, x);
  for (x = p_x + 22, y = p_y + 1; y <= 25; y++)
    printf("\33[%d;%dH\33[41m||\33[0m", y, x);
  for (x = p_x + 36, y = p_y + 1; y <= 25; y++)
    printf("\33[%d;%dH\33[41m||\33[0m", y, x);
  for (x = p_x + 24, y = p_y + 8; x <= 44; x++)
    printf("\33[%d;%dH\33[41m--\33[0m", y, x);
  for (x = p_x, y = p_y + 21; x <= 46; x++)
    printf("\33[%d;%dH\33[41m==\33[0m", y, x);
  printf("\33[&#63;25l");
  fflush(stdout);
}
 
//erase last diamonds
void
erase_last(void)
{
  int j, x1, y1, n;
  x1 = save_x + p_x + 2;
  for (j = 0, n = 0; j <16; j++) {
    if (j / 4 >= shape[num][save_mode][16] && j % 4 == 0) {
      y1 = save_y + p_y + 1 + n;
      printf("\33[%d;%dH", y1, x1);
      n++;
    }
    if (j / 4 >= shape[num][save_mode][16]
      && j % 4 >= shape[num][save_mode][17]) {
      if (shape[num][save_mode][j] == 0) {
        printf("\33[2C");
      }
      if (shape[num][save_mode][j] == 1) {
        printf(" ");
      }
    }
  }
  fflush(stdout);
}
 
//print modes shape
void
print_mode_shape(void)
{
  int j, x1, y1, n;
  int left_flag = 0;
  if (flag_erase == 0) {
    erase_last();
  }
  x1 = x + p_x + 2;
  for (j = 0, n = 0; j <16; j++) {
    if (j / 4 >= shape[num][mode][16] && j % 4 == 0) {
      y1 = y + p_y + 1 + n;
      printf("\33[%d;%dH", y1, x1);
      n++;
    }
    if (j / 4 >= shape[num][mode][16]
      && j % 4 >= shape[num][mode][17]) {
      if (shape[num][mode][j] == 0) {
        printf("\33[2C");
      }
      if (shape[num][mode][j] == 1) {
        printf("\33[%dm[]\33[0m", color);
      }
    }
    fflush(stdout);
  }
  printf("\33[0m");
  fflush(stdout);
  save_x = x;
  save_y = y;
  save_mode = mode;
  save_row = 4 - shape[num][mode][16];
  save_col = 4 - shape[num][mode][17];
  flag_erase = 0;
 
}
 
//store diamonds to matrix by color to flag
void
store_flag_color(void)
{
  int i, a = 0, b = 0;
  for (i = 0; i <16; i++) {
    if (i / 4 >= shape[num][mode][16] && i % 4 == 0) {
      a++;
      b = 0;
    }
    if (i / 4 >= shape[num][mode][16]
      && i % 4 >= shape[num][mode][17]) {
      if (shape[num][save_mode][i] == 0) {
        b = b + 2;
      }
      if (shape[num][save_mode][i] == 1) {
        matirx[save_y + a - 1][save_x + b] = color;
        b++;
        matirx[save_y + a - 1][save_x + b] = color;
        b++;
      }
    }
  }
}
 
//print the save matrix
void
print_save_matrix(void)
{
  int i, j, n = 0;
  for (i = 0; i = X - save_col * 2 && save_col = X - save_col * 2 || judge_by_color(x + 2, mode) == 1) {
    return;
  }
  x = x + 2;
  print_mode_shape();
  fflush(stdout);
}
 
// move left
void
move_left(void)
{
  int i;
  if (x <= 0 || judge_by_color(x - 2, mode) == 1) {
    return;
  }
  x = x - 2;
  print_mode_shape();
  fflush(stdout);
}
 
// move down
void
move_down()
{
  y++;
  if (y >= Y - save_row + 1 || judge_by_color(x, mode) == 1) {
    store_flag_color();
    game_over();
    y = 0;
    save_row = 0;
    save_col = 0;
    x = X / 2 - 2;
 
    num = next_num;
    mode = next_mode;
    color = save_color;
    next_num = random() % 7;
    next_mode = random() % 4;
    save_color = random() % 7 + 41;
    print_next();
    flag_erase = 1;
    destroy_line();
    fflush(stdout);
    return;
  }
  print_mode_shape();
  fflush(stdout);
}
 
 
void
fall_down()
{
  while (1) {
    y++;
    if (y >= Y - save_row + 1 || judge_by_color(x, mode) == 1) {
      store_flag_color();
      game_over();
      y = 0;
      save_row = 0;
      save_col = 0;
      x = X / 2 - 2;
 
      num = next_num;
      mode = next_mode;
      color = save_color;
      next_num = rand() % 7;
      next_mode = rand() % 4;
      save_color = rand() % 7 + 41;
      print_next();
      flag_erase = 1;
      destroy_line();
      fflush(stdout);
      return;
    }
    print_mode_shape();
    fflush(stdout);
  }
 
}
 
//erase next tip diamond
void
erase_next(void)
{
  int i, j, n = 0;
  for (i = 0; i <4; i++) {
    printf("\33[%d;%dH", p_y + 3 + n, p_x + X + 7);
    n++;
    for (j = 0; j <4; j++) {
      printf(" ");
    }
  }
  printf("\33[30;4H\33[&#63;25l");
  fflush(stdout);
}
 
//print next tip diamond
void
print_next(void)
{
  int j, n = 0;
  erase_next();
  for (j = 0; j <16; j++) {
    if (j / 4 >= shape[next_num][next_mode][16] && j % 4 == 0) {
      printf("\33[%d;%dH", p_y + 3 + n, p_x + X + 7);
      n++;
    }
    if (j / 4 >= shape[next_num][next_mode][16]
      && j % 4 >= shape[next_num][next_mode][17]) {
      if (shape[next_num][next_mode] == 0) {
        printf("\33[2C");
      }
      if (shape[next_num][next_mode][j] == 1) {
        printf("\33[%dm[]\33[0m", save_color);
      }
    }
  }
}
 
//print scores info
void
print_score(void)
{
  printf("\33[%d;%dH\33[31m%d\33[0m", p_y + 10, p_x + X + 10, score);
  fprintf(stdout, "\33[%d;0H", p_y + 20 + 2);
}
 
//print grades info
void
print_level(void)
{
  printf("\33[%d;%dH\33[31m%d\33[0m", p_y + 14, p_x + X + 10, level);
  fprintf(stdout, "\33[%d;0H", p_y + 20 + 2);
}
 
//destroy a line or lines  
void
destroy_line(void)
{
  int i, j, full;
  int a, b, c;
  for (i = 0; i = 9)
          level = 9;
        change_level();
        print_level();
 
      }
      print_score();
    }
  }
}
 
//change level , change rate  
void
change_level(void)
{
  switch (level) {
  case 1:
    setitimer(ITIMER_REAL, &level_01, NULL);
    break;
  case 2:
    setitimer(ITIMER_REAL, &level_02, NULL);
    break;
  case 3:
    setitimer(ITIMER_REAL, &level_03, NULL);
    break;
  case 4:
    setitimer(ITIMER_REAL, &level_04, NULL);
    break;
  case 5:
    setitimer(ITIMER_REAL, &level_05, NULL);
    break;
  case 6:
    setitimer(ITIMER_REAL, &level_06, NULL);
    break;
  case 7:
    setitimer(ITIMER_REAL, &level_07, NULL);
    break;
  case 8:
    setitimer(ITIMER_REAL, &level_08, NULL);
    break;
  case 9:
    setitimer(ITIMER_REAL, &level_09, NULL);
    break;
  default:
    break;
  }
}
 
//by the color to judge whether went across or not
int
judge_by_color(int x, int mode)
{
  int i, a = 0, b = 0;
  for (i = 0; i <16; i++) {
    if (i / 4 >= shape[num][mode][16] && i % 4 == 0) {
      a++;
      b = 0;
    }
    if (i / 4 >= shape[num][mode][16]
      && i % 4 >= shape[num][mode][17]) {
      if (shape[num][mode][i] == 0) {
        b = b + 2;
      }
      if (shape[num][mode][i] == 1) {
        if (matirx[a + y - 1][b + x] != 0) {
          return 1;
        } else
          b = b + 2;
      }
    }
 
  }
}
 
//control the diamonds shape by the key    
void
key_control(void)
{
  int ch, flag = 1;
  struct termios save, raw;
 
  tcgetattr(0, &save);
  cfmakeraw(&raw);
  tcsetattr(0, 0, &raw);
  if (setjmp(env) == 0) {
    while (flag) {
      ch = getchar();
      if (ch == '\r') {
        fall_down();
      }
      if (ch == '\33') {
        ch = getchar();
        if (ch == '[') {
          ch = getchar();
          switch (ch) {
          case 'A':
            change_shape();
            break;
          case 'B':
            move_down();
            break;
          case 'C':
            move_right();
            break;
          case 'D':
            move_left();
            break;
          }
        }
      }
 
      if (ch == 'q' || ch == 'Q') {
        flag = 0;
      }
    }
    printf("\33[%d;%dH\33[31m-----game interrupt exit!-----\33[0m",
        p_y + Y + 3, p_x);
    printf("\33[%d;0H\33[&#63;25h", p_y + Y + 4);
  }
  tcsetattr(0, 0, &save);
}
 
//reach the top line, the game is over    
void
game_over(void)
{
  int i;
  for (i = 0; i 

2.block.h

#ifndef _BLOCK_H_
#define _BLOCK_H_
 
#define p_x 10      //init postion row;
#define p_y 5      //init postion col;
#define X 20          // game_window_size
#define Y 20
#define LEVEL_SCORE 500     // need scores to upgrade
 
 
jmp_buf env;
static int x, y;                   //  current diamonds postion
static int flag_erase;                  //  erase flag
static int num, mode, next_num, next_mode;        //  current and next diamonds
static int save_row, save_col, save_x, save_y, save_mode;//  save coordinate, save graph
static int color, save_color, flag_color;           //  save the color of the next diamonds
static int matirx[Y][X] = { 0 };             //  save diamonds' matrix
static int level = 1;                   //  game levels
static int score = 0;           //  game scores
 
typedef struct itimerval LEVEL;
static LEVEL level_00 = { {0,   0}, {0,   0} };
static LEVEL level_01 = { {0, 800000}, {1,   0} };
static LEVEL level_02 = { {0, 500000}, {0, 500000} };
static LEVEL level_03 = { {0, 400000}, {0, 300000} };
static LEVEL level_04 = { {0, 300000}, {0, 300000} };
static LEVEL level_05 = { {0, 200000}, {0, 300000} };
static LEVEL level_06 = { {0, 150000}, {0, 300000} };
static LEVEL level_07 = { {0, 100000}, {0, 300000} };
static LEVEL level_08 = { {0, 80000 }, {0, 300000} };
static LEVEL level_09 = { {0, 60000 }, {0, 300000} };
 
//three-dimensional for saving diamonds and diamonds' shape :
//first-dimensional for kind of diamonds-shape
//second-dimensional for alterable's mode
//third-dimensional for reality value of row and col
static const int shape[7][4][18] = {
               {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1,   2, 1},  //    
                {0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0,   1, 2},  //  []  []  [][][]   []
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0,   2, 1},  // [][][] [][]  []   [][]
                {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1,   1, 2}}, //     []        []
 
               {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1,   2, 1},  //
                {0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1,   1, 2}, //     []      [][]
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0,   2, 1},  //   [] []  [][][]  []
                {0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1,   1, 2}},  // [][][] [][] []    []
 
               {{0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1,   1, 2},  //
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1,   2, 1},  //     [][]      []
                {0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0,   1, 2},  // []    []  [][][]  []
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1,   2, 1}}, // [][][]  []    [] [][]
 
               {{0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1,   1, 2},  // 
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0,   2, 1},  //  []
                {0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1,   1, 2},  //  [][]   [][]
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0,   2, 1}}, //    []  [][]
 
               {{0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0,   1, 2},  //
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1,   2, 1},  //   []
                {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0,   1, 2},  //  [][]  [][]
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1,   2, 1}}, //  []    [][]
 
               {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1,   2, 2},  //
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1,   2, 2},  //
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1,   2, 2},  //   [][]
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1,   2, 2}}, //   [][]
 
               {{0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1,   0, 3},  //   []
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,   3, 0},  //   []   [][][][]
                {0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1,   0, 3},  //   [] 
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,   3, 0}}  //   []   
               };
 
void init_for_globle(void);
void print_start_interface(void);
void print_mode_shape(void);
void print_save_matrix(void);
void change_shape(void);
void move_left(void);
void move_right(void);
void move_down();
void fall_down();
void store_flag_color(void);
void key_control(void);
void erase_last(void);
void destroy_line(void);
void print_next(void);
void erase_next(void);
void change_level(void);
void print_score(void);
void print_level(void);
int judge_by_color(int x, int mode);
void game_over(void);
 
#endif

3.main.c

#include 
#include 
#include 
#include 
#include "block.h"
 
int
main(int argc, char **argv)
{
  init_for_globle(); //init for globle
  print_mode_shape(); //print first diamond 
  print_next();    //print next diamond
  setitimer(ITIMER_REAL, &level_01, NULL);  //init one leve ;interval 800ms
  signal(SIGALRM, move_down); //diamond down base on the interval time
  key_control();   //using zhe key to play games
 
  return 0;
}

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


推荐阅读
  • Monkey《大话移动——Android与iOS应用测试指南》的预购信息发布啦!
    Monkey《大话移动——Android与iOS应用测试指南》的预购信息已经发布,可以在京东和当当网进行预购。感谢几位大牛给出的书评,并呼吁大家的支持。明天京东的链接也将发布。 ... [详细]
  • 本文介绍了解决Netty拆包粘包问题的一种方法——使用特殊结束符。在通讯过程中,客户端和服务器协商定义一个特殊的分隔符号,只要没有发送分隔符号,就代表一条数据没有结束。文章还提供了服务端的示例代码。 ... [详细]
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • 本文介绍了C++中省略号类型和参数个数不确定函数参数的使用方法,并提供了一个范例。通过宏定义的方式,可以方便地处理不定参数的情况。文章中给出了具体的代码实现,并对代码进行了解释和说明。这对于需要处理不定参数的情况的程序员来说,是一个很有用的参考资料。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • 本文讨论了使用差分约束系统求解House Man跳跃问题的思路与方法。给定一组不同高度,要求从最低点跳跃到最高点,每次跳跃的距离不超过D,并且不能改变给定的顺序。通过建立差分约束系统,将问题转化为图的建立和查询距离的问题。文章详细介绍了建立约束条件的方法,并使用SPFA算法判环并输出结果。同时还讨论了建边方向和跳跃顺序的关系。 ... [详细]
  • 本文介绍了MyBioSource转甲状腺素蛋白定量检测ELISA试剂盒的应用方法及特点。ELISA法作为一项新技术在免疫诊断中的应用范围不断扩大,不仅适用于多种病原微生物引起的传染病、非传染病的免疫诊断,也可用于大/小分子抗原的定量检测。ELISA法具有灵敏、特异、简单、快速、稳定及易于自动化操作等特点,是一种早期诊断的良好方法,也可用于血清流行病学调查。MyBioSource转甲状腺素蛋白定量检测ELISA试剂盒使用方法包括对血清和血浆的操作要求。 ... [详细]
  • Android Studio Bumblebee | 2021.1.1(大黄蜂版本使用介绍)
    本文介绍了Android Studio Bumblebee | 2021.1.1(大黄蜂版本)的使用方法和相关知识,包括Gradle的介绍、设备管理器的配置、无线调试、新版本问题等内容。同时还提供了更新版本的下载地址和启动页面截图。 ... [详细]
  • 本文介绍了一种划分和计数油田地块的方法。根据给定的条件,通过遍历和DFS算法,将符合条件的地块标记为不符合条件的地块,并进行计数。同时,还介绍了如何判断点是否在给定范围内的方法。 ... [详细]
  • 本文讲述了作者通过点火测试男友的性格和承受能力,以考验婚姻问题。作者故意不安慰男友并再次点火,观察他的反应。这个行为是善意的玩人,旨在了解男友的性格和避免婚姻问题。 ... [详细]
  • 安卓select模态框样式改变_微软Office风格的多端(Web、安卓、iOS)组件库——Fabric UI...
    介绍FabricUI是微软开源的一套Office风格的多端组件库,共有三套针对性的组件,分别适用于web、android以及iOS,Fab ... [详细]
  • 本文介绍了多因子选股模型在实际中的构建步骤,包括风险源分析、因子筛选和体系构建,并进行了模拟实证回测。在风险源分析中,从宏观、行业、公司和特殊因素四个角度分析了影响资产价格的因素。具体包括宏观经济运行和宏经济政策对证券市场的影响,以及行业类型、行业生命周期和行业政策对股票价格的影响。 ... [详细]
  • 宁德时代与第四范式达成合作,将利用第四范式的AI技术,打造规模化的人工智能平台,并将AI技术融入电池生产线。通过全流程AI技术和低门槛的AI生产工具,宁德时代实现了对生产线数据的实时分析与决策。第四范式是一家人工智能技术与服务提供商,其先知平台降低了AI在各行业内的应用门槛。宁德时代是国内具备国际竞争力的动力电池制造商之一,专注于新能源汽车动力电池系统、储能系统的研发、生产和销售。 ... [详细]
author-avatar
mobiledu2502869077
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有