c++俄罗斯方块源代码(免费复制)

#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<time.h>
#include<conio.h>
 
#define MOD 28
#define SIZE_N 19
#define SIZE_M 12
 
int cur_x,cur_y;
int score,mark,next,map[SIZE_N][SIZE_M],Gamespeed=300;
 
int shape[28][6]= {
    {0,-1,0,-2,1,0}, {0,1,1,0,2,0}, {-1,0,0,1,0,2}, {0,-1,-1,0,-2,0},
    {0,-1,0,1,-1,0}, {0,1,1,0,-1,0}, {1,0,0,-1,0,1}, {1,0,-1,0,0,-1},
    {-1,1,0,1,1,0}, {0,-1,1,0,1,1}, {-1,0,0,-1,1,-1}, {-1,-1,-1,0,0,1},
    {-1,0,0,1,1,1}, {0,1,1,-1,1,0}, {-1,0,0,1,1,1}, {0,1,1,-1,1,0},
    {-1,0,0,-1,0,-2}, {-1,0,-2,0,0,1}, {0,1,0,2,1,0}, {0,-1,1,0,2,0},
    {0,1,1,0,1,1}, {0,-1,1,0,1,-1}, {-1,0,0,-1,-1,-1}, {-1,0,-1,1,0,1},
    {0,1,0,2,0,3}, {1,0,2,0,3,0}, {0,-1,0,-2,0,-3}, {-1,0,-2,0,-3,0}
};
 
void gotoxy(int x,int y) {
    COORD c;
    c.X=x-1;
    c.Y=y-1;
    SetConsoleCursorPosition (GetStdHandle(STD_OUTPUT_HANDLE), c);
}
void Gameover() {
    int i,j,flag=0;
    for(j=1; j<SIZE_M-1; j++) {
        if(map[1][j]!=0) {
            flag=1;
            break;
        }
    }
    if(flag==1) {
        for(i=1; i<SIZE_N-1; i++) {
            gotoxy(2,i+1);
            for(j=1; j<SIZE_M-1; j++) {
                printf("□");
            }
            puts("");
        }
        gotoxy(7,9);
        printf("GAME OVER!");
        gotoxy(

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是一个简单的 C++ 俄罗斯方块游戏源代码,仅供参考: ```c++ #include <iostream> #include <conio.h> #include <windows.h> using namespace std; const int Width = 20; // 游戏区域宽度 const int Height = 25; // 游戏区域高度 int score = 0; // 得分 char screen[Height][Width]; // 方块结构体 struct Block { int x, y; char shape[4][4]; }; // 方块数组,共7种不同形状的方块 Block blocks[7] = { { 0, 0, { { ' ', ' ', ' ', ' ' }, { ' ', ' ', ' ', ' ' }, { '*', '*', '*', '*' }, { ' ', ' ', ' ', ' ' } } }, { 0, 0, { { '*', '*', ' ', ' ' }, { '*', '*', ' ', ' ' }, { ' ', ' ', ' ', ' ' }, { ' ', ' ', ' ', ' ' } } }, { 0, 0, { { '*', ' ', ' ', ' ' }, { '*', ' ', ' ', ' ' }, { '*', '*', ' ', ' ' }, { ' ', ' ', ' ', ' ' } } }, { 0, 0, { { ' ', ' ', '*', ' ' }, { '*', '*', '*', ' ' }, { ' ', ' ', ' ', ' ' }, { ' ', ' ', ' ', ' ' } } }, { 0, 0, { { ' ', '*', '*', ' ' }, { '*', '*', ' ', ' ' }, { ' ', ' ', ' ', ' ' }, { ' ', ' ', ' ', ' ' } } }, { 0, 0, { { '*', '*', ' ', ' ' }, { ' ', '*', '*', ' ' }, { ' ', ' ', ' ', ' ' }, { ' ', ' ', ' ', ' ' } } }, { 0, 0, { { '*', '*', '*', ' ' }, { ' ', ' ', '*', ' ' }, { ' ', ' ', ' ', ' ' }, { ' ', ' ', ' ', ' ' } } } }; // 随机生成一个新方块 void newBlock(Block& blk) { int index = rand() % 7; blk = blocks[index]; blk.x = Width / 2 - 2; blk.y = 0; } // 将方块画到屏幕上 void drawBlock(const Block& blk) { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (blk.shape[i][j] == '*') { screen[blk.y + i][blk.x + j] = blk.shape[i][j]; } } } } // 将屏幕清空 void clearScreen() { system("cls"); memset(screen, ' ', sizeof(screen)); for (int i = 0; i < Width; i++) { screen[Height - 1][i] = '-'; } } // 将屏幕画出来 void renderScreen() { cout << "Score: " << score << endl; for (int i = 0; i < Height; i++) { for (int j = 0; j < Width; j++) { cout << screen[i][j]; } cout << endl; } } // 判断方块是否可以向下移动 bool canMoveDown(const Block& blk) { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (blk.shape[i][j] == '*') { if (blk.y + i >= Height - 1 || screen[blk.y + i + 1][blk.x + j] != ' ') { return false; } } } } return true; } // 将方块向下移动一格 void moveDown(Block& blk) { blk.y++; } // 将方块向左移动一格 void moveLeft(Block& blk) { blk.x--; } // 将方块向右移动一格 void moveRight(Block& blk) { blk.x++; } // 将方块旋转90度 void rotate(Block& blk) { Block temp = blk; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { blk.shape[j][3 - i] = temp.shape[i][j]; } } } // 将方块固定在屏幕上,更新得分,并检查是否有行已经填满 void fixBlock(const Block& blk) { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (blk.shape[i][j] == '*') { screen[blk.y + i][blk.x + j] = blk.shape[i][j]; } } } // 检查是否有行已经填满 for (int i = Height - 2; i >= 0; i--) { bool full = true; for (int j = 0; j < Width; j++) { if (screen[i][j] == ' ') { full = false; break; } } if (full) { score += 10; // 将上面的所有行向下移动一格 for (int k = i; k > 0; k--) { memcpy(screen[k], screen[k - 1], Width); } i++; } } } // 游戏循环 void gameLoop() { Block curBlock, nextBlock; newBlock(curBlock); newBlock(nextBlock); while (true) { clearScreen(); drawBlock(curBlock); renderScreen(); if (!canMoveDown(curBlock)) { fixBlock(curBlock); curBlock = nextBlock; newBlock(nextBlock); } else { moveDown(curBlock); } if (_kbhit()) { char ch = _getch(); switch (ch) { case 'a': if (curBlock.x > 0) { moveLeft(curBlock); } break; case 'd': if (curBlock.x < Width - 4) { moveRight(curBlock); } break; case 'w': rotate(curBlock); break; case 's': while (canMoveDown(curBlock)) { moveDown(curBlock); } fixBlock(curBlock); curBlock = nextBlock; newBlock(nextBlock); break; } } Sleep(50); } } int main() { gameLoop(); return 0; } ``` 注意:以上源代码可能存在一些不足或者bug,需要根据实际情况进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值