俄罗斯方块 编程代码

本文介绍了如何在10分钟内快速编写完成俄罗斯方块的编程代码,包括必要的头文件和函数实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include "StdAfx.h"
#include "Game.h"
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <conio.h>


/*
 1. 画墙
 2. 显示一个新方块
 3. 控制方块
 4. 到底部后,固定方块
 5. 消行
 6. 重复2-5
*/


#define WALL_ROW 15
#define WALL_COL 11 
#define INIT_ROW 0
#define INIT_COL 4
unsigned char g_szWall[WALL_ROW][WALL_COL] = {0};


int g_nCurBoxRow = 0;
int g_nCurBoxCol = 0;
int g_nType = 0;
int g_nSubType = 0;


unsigned char g_szBox[][4] = 
{
  1, 1, 1, 1,
  0, 0, 0, 0,
  0, 0, 0, 0,
  0, 0, 0, 0,


  0, 1, 0, 0,
  0, 1, 0, 0,
  0, 1, 0, 0,
  0, 1, 0, 0,


  1, 1, 1, 1,
  0, 0, 0, 0,
  0, 0, 0, 0,
  0, 0, 0, 0,


  0, 1, 0, 0,
  0, 1, 0, 0,
  0, 1, 0, 0,
  0, 1, 0, 0,


  1, 0, 0, 0,
  1, 1, 0, 0,
  1, 0, 0, 0,
  0, 0, 0, 0,


  1, 1, 1, 0,
  0, 1, 0, 0,
  0, 0, 0, 0,
  0, 0, 0, 0,


  0, 1, 0, 0,
  1, 1, 0, 0,
  0, 1, 0, 0,
  0, 0, 0, 0,


  0, 1, 0, 0,
  1, 1, 1, 0,
  0, 0, 0, 0,
  0, 0, 0, 0
};


void StartGame()
{
  srand(time(NULL));
  InitWall();


  g_nCurBoxRow = INIT_ROW;
  g_nCurBoxCol = INIT_COL;
  g_nType = rand() % (sizeof(g_szBox)/64);
  g_nSubType = rand() % 4;
  CreateBox(g_nCurBoxRow, g_nCurBoxCol, g_nType, g_nSubType);


  Show();


  clock_t tStart, tEnd;
  tStart = clock();
  while(1)
  {
    if(_kbhit())
    {
      char ch = getch();
      switch(ch)
      {
      case 'a':
      case 'A':
        {
          OnCharA();
        }
        break;
      case 's':
      case 'S':
        {
          OnCharS();
        }
        break;
      case 'w':
      case 'W':
        {
          OnCharW();
        }
        break;
      case 'D':
      case 'd':
        {
          OnCharD();
        }
        break;
      }
    }


    tEnd = clock();
    if(tEnd - tStart > 500)
    {
      OnCharS();
      tStart = clock();
    }
  }
}


void InitWall()
{
  for(int nRow = 0; nRow < WALL_ROW; nRow++)
  {
    for(int nCol = 0; nCol < WALL_COL; nCol++)
    {
      if(nCol == 0 || nCol == WALL_COL - 1 || nRow == WALL_ROW - 1)
      {
        g_szWall[nRow][nCol] = 1;
      }
    }
  }
}


void Show()
{
  system("cls");


  for(int nRow = 0; nRow < WALL_ROW; nRow++)
  {
    for(int nCol = 0; nCol < WALL_COL; nCol++)
    {
      if(g_szWall[nRow][nCol] == 1 || g_szWall[nRow][nCol] == 2)
      {
        printf("■");
      }
      else
      {
        printf("□");
      }
    }
    printf("\r\n");
  }
}


void CreateBox(int nRowInWall, int nColInWall, int nType, int nSubType)
{
  int nRowInBoxAry = nType * 16 + nSubType * 4;
  int nColInBoxAry = 0;


  for(int nOffsetX = 0; nOffsetX < 4; nOffsetX++)
  {
    for(int nOffsetY = 0; nOffsetY < 4; nOffsetY++)
    {
      //把方块数组中对应的数据 --》 当前方块
      if(g_szBox[nRowInBoxAry + nOffsetX][nColInBoxAry + nOffsetY] == 1)
      {
        g_szWall[g_nCurBoxRow + nOffsetX][g_nCurBoxCol + nOffsetY] = 2;
      }
    }
  }
}


void OnCharW()
{
  int nSubType = (g_nSubType + 1) % 4;
  if(IsCanRotate(nSubType))
  {
    g_nSubType = nSubType;
    ClearOldBox();
    CreateBox(g_nCurBoxRow, g_nCurBoxCol, g_nType, g_nSubType);
    Show();
  }
}


void OnCharA()
{
  int nTmpCol = g_nCurBoxCol - 1;
  if(IsCanMove(g_nCurBoxRow, nTmpCol))
  {
    ClearOldBox();
    
    g_nCurBoxCol--;
    CreateBox(g_nCurBoxRow, g_nCurBoxCol, g_nType, g_nSubType);
    Show();  
  }
}


void OnCharS()
{
  int nTmpRow = g_nCurBoxRow + 1;
  if(IsCanMove(nTmpRow, g_nCurBoxCol))
  {
    ClearOldBox();
    
    g_nCurBoxRow++;
    CreateBox(g_nCurBoxRow, g_nCurBoxCol, g_nType, g_nSubType);
    Show(); 
  }
  else
  {
    //固定方块
    FixBox();
    //消行
    ClearLine();


    //创建新的方块
    g_nCurBoxRow = INIT_ROW;
    g_nCurBoxCol = INIT_COL;
    g_nType = rand() % (sizeof(g_szBox)/64);
    g_nSubType = rand() % 4;
    CreateBox(g_nCurBoxRow, g_nCurBoxCol, g_nType, g_nSubType);
    //显示
    Show();
  }
}


void OnCharD()
{
  int nTmpCol = g_nCurBoxCol + 1;
  if(IsCanMove(g_nCurBoxRow, nTmpCol))
  {
    ClearOldBox();
    
    g_nCurBoxCol++;
    CreateBox(g_nCurBoxRow, g_nCurBoxCol, g_nType, g_nSubType);
    Show(); 
  } 
}


void ClearOldBox()
{
  for(int nOffsetX = 0; nOffsetX < 4; nOffsetX++)
  {
    for(int nOffsetY = 0; nOffsetY < 4; nOffsetY++)
    {
      if(g_szWall[g_nCurBoxRow + nOffsetX][g_nCurBoxCol + nOffsetY] == 2)
      {
        g_szWall[g_nCurBoxRow + nOffsetX][g_nCurBoxCol + nOffsetY] = 0;
      }
    }
  }
}


int IsCanMove(int nRow, int nCol)
{
  int nRowInBoxAry = g_nType * 16 + g_nSubType * 4;
  int nColInBoxAry = 0;
  for(int nOffsetX = 0; nOffsetX < 4; nOffsetX++)
  {
    for(int nOffsetY = 0; nOffsetY < 4; nOffsetY++)
    {
      //方块数组为1  墙数组为1
      if(g_szBox[nRowInBoxAry + nOffsetX][nColInBoxAry + nOffsetY] == 1 &&
        g_szWall[nRow + nOffsetX][nCol + nOffsetY] == 1)
      {
        return 0;
      }
    }
  } 


  return 1;
}


int IsCanRotate(int nSubType)
{
  int nRowInBoxAry = g_nType * 16 + nSubType * 4;
  int nColInBoxAry = 0;
  for(int nOffsetX = 0; nOffsetX < 4; nOffsetX++)
  {
    for(int nOffsetY = 0; nOffsetY < 4; nOffsetY++)
    {
      //方块数组为1  墙数组为1
      if(g_szBox[nRowInBoxAry + nOffsetX][nColInBoxAry + nOffsetY] == 1 &&
        g_szWall[g_nCurBoxRow + nOffsetX][g_nCurBoxCol + nOffsetY] == 1)
      {
        return 0;
      }
    }
  } 


  return 1;
}


void FixBox()
{
  for(int nOffsetX = 0; nOffsetX < 4; nOffsetX++)
  {
    for(int nOffsetY = 0; nOffsetY < 4; nOffsetY++)
    {
      if(g_szWall[g_nCurBoxRow + nOffsetX][g_nCurBoxCol + nOffsetY] == 2)
      {
        g_szWall[g_nCurBoxRow + nOffsetX][g_nCurBoxCol + nOffsetY] = 1;
      }
    }
  }   
}


void ClearLine()
{
  for(int nRow = WALL_ROW - 2; nRow > 0; nRow--)
  {
    if(IsFullLine(nRow))
    {
      for(int _nRow = nRow; _nRow > 0; _nRow--)
      {
        for(int nCol = 1; nCol <= WALL_COL - 2; nCol++)
        {
          g_szWall[_nRow][nCol] = g_szWall[_nRow - 1][nCol];
        }
      }
      nRow++;
    }

  }

}


int IsFullLine(int nRow)
{
  int nFull = 1;
  for(int nCol = 1; nCol <= WALL_COL - 2; nCol++)
  {
    if(g_szWall[nRow][nCol] == 0)
    {
      nFull = 0;
      break;
    }
  }


  return nFull;

}

(还有一些头文件 函数  自己写一下  10分钟搞定)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值