• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C语言 > Linux下用C++实现俄罗斯方块

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

作者:东X哥 字体:[增加 减小] 来源:互联网 时间:2017-05-28

东X哥 通过本文主要向大家介绍了linux下c++编程教程,linux c++项目实例,linux c++编程,linux c++编译器,c++ linux等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

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

1.block.c

#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <stdlib.h>
#include <setjmp.h>
#include <sys/time.h>
#include <string.h>
#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[?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 < Y; i++) {
    printf("\33[%d;%dH", i + p_y + 1, p_x + 2);
    for (j = 0; j < X; j++) {
      if (matirx[i][j] != 0) {
        n = (n + 1) % 2;
        fprintf(stdout, "\33[%dm", matirx[i][j]);
        (n == 1) ? printf("[") : printf("]");
      }
      if (matirx[i][j] == 0) {
        printf("\33[0m");
        printf(" ");
      }
      fflush(stdout);
    }
  }
}
 
// change shape
void
change_shape(void)
{
  int i, n;
  for (i = 0; i < save_row; i++) {
    if (num == 6) {
      n = 4;
    } else {
      n = 0;
    }
    if (((x + n) >= X - save_col * 2 && save_col < save_row) ||
      judge_by_color(x, (mode + 1) % 4) == 1) {
      return;
    }
  }
  mode = (mode + 1) % 4;
  fflush(stdout);
  print_mode_shape();
  fflush(stdout);
}
 
//move right
void
move_right(void)
{
  int i;
  if (x >= 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[?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 < Y; i++) {
    full = 1;
    for (j = 0; j < X; j++) {
      if (matirx[i][j] == 0) {
        full = 0;
      }
    }
    if (full == 1) {
      for (a = 0; a < i; a++) {
        for (b = 0; b < X; b++) {
          matirx[i - a][b] = matirx[i - a - 1][b];
        }
      }
      print_save_matrix();
      score = score + 100;
      if (score % LEVEL_SCORE == 0) {
        level = level



 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • Linux下用C++实现俄罗斯方块
  • C++实现Linux下弹出U盘的方法
  • 在Linux下编译C或C++程序的教程
  • C++操作SQLite简明教程

相关文章

  • 2017-05-28深入解析最长公共子串
  • 2017-05-28sigsetjmp的用法总结
  • 2017-05-28解析为何要关闭数据库连接,可不可以不关闭的问题详解
  • 2017-05-28C++中的函数指针与函数对象的总结
  • 2017-05-28C++ 中const 类型限定符不兼容问题
  • 2017-05-28c语言中字符串分割函数及实现方法
  • 2017-05-28基于list循环删除元素,迭代器失效的问题详解
  • 2017-05-28C++对象的浅复制和深复制详解及简单实例
  • 2017-05-28c语言打开文件函数使用方法
  • 2017-05-28C++ COM编程之什么是接口?

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • 使用C语言详解霍夫曼树数据结构
    • Qt实现图片移动实例(图文教程)
    • 利用C语言实现顺序表的实例操作
    • 详解在C++中显式默认设置的函数和已删除的函数的方法
    • C语言判断字符是否为可打印字符的方法
    • VS2010/MFC编程(常用控件:树形控件Tree Control控件创建h和实例)
    • 线程池的原理与实现详解
    • C++临时性对象的生命周期详细解析
    • 通过c++11改进我们的模式之改进命令模式
    • 红黑树的使用详解

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有