21xrx.com
2024-11-22 02:47:36 Friday
登录
文章检索 我的文章 写文章
"C++俄罗斯方块游戏源代码"
2023-07-13 20:29:42 深夜i     --     --
C++ 俄罗斯方块 游戏 源代码 编程

C++俄罗斯方块游戏源代码

俄罗斯方块是一款经典的游戏,也是程序员们喜爱的一个练手项目。在C++编程语言中实现俄罗斯方块游戏是一项很有趣的挑战。如果你对编程感兴趣,那么你要尝试写一个自己的俄罗斯块游戏。

下面是一份基于C++的俄罗斯块游戏源代码:


#include <iostream>

#include <windows.h>

#include <conio.h>

#include <cstdlib>

#include <ctime>

#include <cstdio>

#include <iomanip>

using namespace std;

string color[7] = "3";//定义颜色

string graph[7] = {"[]", "##", "%%", "&&", "**", "..", "@@"}; //定义方块的图案

int getkey();//获取按键信息的函数

//打印地图、分数

void print(int screen[], int score){

  system("cls");

  cout << "Score: " << score << endl;

  cout << "+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;

  for(int i = 0; i < 20; i++){

    cout << "|";

    for(int j = 0; j < 10; j++){

      int k = screen[i * 10 + j] + 1;

      cout << color[k] << graph[k] << "\033[0m";

    }

    cout << "|";

    cout << endl;

  }

  cout << "+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;

  cout << "操作方法:" << endl;

  cout << "A/D控制方块左右移动,W键将方块旋转,空格键加速下落" << endl;

  cout << "-----------------------------------------------------------------------" << endl << endl;

}

int main(){

  srand((unsigned int)time(NULL));

  int screen[200] = {0};

  int score = 0;

  int x = 3, y = 0, kind = rand() % 7, rotate = 0;

  while(true){

    if(screen[y * 10 + x] != 0){

      print(screen, score);

      cout << "游戏结束!" << endl;

      return 0;

    }

    int key = getkey();

    if(key == 97){  //当按下a键,方块向左移动

      if(x > 0){

        if(screen[y * 10 + x - 1] == 0){

          x--;

        }

      }

    }else if(key == 100){ //当按下d键,方块向右移动

      if(x < 9){

        if(screen[y * 10 + x + 1] == 0){

          x++;

        }

      }

    }else if(key == 119){  //当按下w键,方块旋转

      int nrotate = (rotate + 1) % 4;

      string nextgraph[4][4];

      for(int i = 0; i < 4; i++){

        for(int j = 0; j < 4; j++){

          nextgraph[i][j] = graph[kind * 16 + i * 4 + j];

        }

      }

      for(int i = 0; i < nrotate; i++){

        string temp[4][4];

        for(int j = 0; j < 4; j++){

          for (int k = 0; k < 4; k++){

            temp[k][3 - j] = nextgraph[j][k];

          }

        }

        for(int j = 0; j < 4; j++){

          for (int k = 0; k < 4; k++){

            nextgraph[j][k] = temp[j][k];

          }

        }

      }

      bool can = true;

      for(int i = 0; i < 4; i++){

        for (int j = 0; j < 4; j++){

          if(nextgraph[i][j] != "."){

            int nx = x + j;

            int ny = y + i;

            if(nx < 0 || nx > 9 || ny < 0 || ny > 19 || screen[ny * 10 + nx] != 0){

              can = false;

            }

          }

        }

      }

      if(can){

        rotate = nrotate;

        for(int i = 0; i < 4; i++){

          for (int j = 0; j < 4; j++){

            graph[kind * 16 + i * 4 + j] = nextgraph[i][j];

          }

        }

      }

    }else if(key == 32){ //当按下空格键,方块向下移动加速

      bool over = false;

      while(!over){

        y++;

        for(int i = 0; i < 4; i++){

          for(int j = 0; j < 4; j++){

            if(graph[kind * 16 + i * 4 + j] != "."){

              int nx = x + j;

              int ny = y + i;

              if(nx < 0 || nx > 9 || ny > 19 || screen[ny * 10 + nx] != 0){

                over = true;

                break;

              }

            }

          }

        }

      }

    }

    int wait = 60;

    int newy = y + 1;

    for(int i = 0; i < 4; i++){

      for (int j = 0; j < 4; j++){

        if(graph[kind * 16 + i * 4 + j] != "."){

          int nx = x + j;

          int ny = newy + i;

          if(ny > 19 || screen[ny * 10 + nx] != 0){

            if(newy - y <= 1){

              print(screen, score);

              cout << "游戏结束!" << endl;

              return 0;

            }

            for(int k = 0; k < 4; k++){

              for(int l = 0; l < 4; l++){

                if(graph[kind * 16 + k * 4 + l] != "."){

                  int nx = x + l;

                  int ny = y + k;

                  screen[ny * 10 + nx] = kind + 1;

                }

              }

            }

            int newscore = 0;

            for(int k = 0; k < 4; k++){

              int ny = y + k;

              bool full = true;

              for(int l = 0; l < 10; l++){

                if(screen[ny * 10 + l] == 0){

                  full = false;

                  break;

                }

              }

              if(full){

                newscore++;

                for(int l = ny; l > 0; l--){

                  for(int m = 0; m < 10; m++){

                    screen[l * 10 + m] = screen[(l - 1) * 10 + m];

                  }

                }

              }

            }

            score += (1 << newscore) * 100;

            x = 3;

            y = 0;

            kind = rand() % 7;

            rotate = 0;

          }

        }

      }

    }

    if(wait){

      wait--;

      Sleep(10);

    }else{ //当等待次数为0,方块向下移动一格

      y = newy;

    }

    print(screen, score); //每次循环都要更新一下画面

  }

  return 0;

}

int getkey(){ //获取按键信息的函数

  if(_kbhit()){

    int ch = _getch();

    if (ch == 224 || ch == 0){

      ch = _getch();

    }

    return ch;

  }

  return -1;

}

如上代码就实现了一个完整的俄罗斯方块游戏,你可以在自己的电脑上运行一下这个程序,体验一下这个经典游戏的乐趣。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复