当前位置: 首页 > 编程笔记 >

C语言代码实现俄罗斯方块

晋骏喆
2023-03-14
本文向大家介绍C语言代码实现俄罗斯方块,包括了C语言代码实现俄罗斯方块的使用技巧和注意事项,需要的朋友参考一下

这里为大家敲写一段怎样用C语言实现俄罗斯方块:

首先推荐大家使用CodeBlocks这个软件,方便添加不同的工程。
代码中有很多注释便于理解!

下面是效果图和全部的代码以及注释,大家可以观看并自己新增内容!

1、首先是main.c文件:

#include <stdio.h>
#include <stdlib.h>
#include "game.h"

int main()
{
 gameInit();
 return 0;
}

2、然后是mywindows.h文件:

#ifndef MYWINDOWS_H_INCLUDED
#define MYWINDOWS_H_INCLUDED

// 封装系统函数-系统调用模块
#include <windows.h>

// 初始化句柄
extern void initHandle();

// 设置颜色
extern void setColor(int color);

// 设置光标位置
extern void setPos(int x, int y);

// 设置光标是否可见
extern void setCursorVisible(int flag);

// 关闭句柄
extern void closeHandle();

#endif // MYWINDOWS_H_INCLUDED

3、接下来是mywindows.c文件:

#include "mywindows.h"

HANDLE handle;

// 初始化句柄
void initHandle()
{
 handle = GetStdHandle(STD_OUTPUT_HANDLE);
}

// 设置颜色
void setColor(int color)
{
 SetConsoleTextAttribute(handle, color);
}

void setPos(int x, int y)
{
 //, ,
 COORD coord = {x*2, y};
 SetConsoleCursorPosition(handle, coord);
}

// 设置光标是否可见
void setCursorVisible(int flag)
{
 CONSOLE_CURSOR_INFO info;
 info.bVisible = flag; //光标是否可见
 info.dwSize = 100; //光标宽度1-100
 SetConsoleCursorInfo(handle, &info);
}

// 关闭句柄
void closeHandle()
{
 CloseHandle(handle);
}

4、下面是data.h文件,也就是数据库的存储地址:

#ifndef DATA_H_INCLUDED
#define DATA_H_INCLUDED


//函数声明\变量声明
// 存储数据-数据模块
//界面数组
extern int windowShape[25][26];

extern int block[7][4][4][4];

#endif // DATA_H_INCLUDED

5、数据库内容:data.c

#include "data.h"

//函数的定义

int windowShape[25][26] =
{
 {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,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,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1},
 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1},
 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1},
 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1},
 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1},
 {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
};// 边框为1,游戏池长度为14


int block[7][4][4][4] =
{
 {
 //Z{}
 {{1,1,0,0},{0,1,1,0},{0,0,0,0},{0,0,0,0}},
 {{0,1,0,0},{1,1,0,0},{1,0,0,0},{0,0,0,0}},
 {{1,1,0,0},{0,1,1,0},{0,0,0,0},{0,0,0,0}},
 {{0,1,0,0},{1,1,0,0},{1,0,0,0},{0,0,0,0}}
 },
 {
 //S
 {{0,1,1,0},{1,1,0,0},{0,0,0,0},{0,0,0,0}},
 {{1,0,0,0},{1,1,0,0},{0,1,0,0},{0,0,0,0}},
 {{0,1,1,0},{1,1,0,0},{0,0,0,0},{0,0,0,0}},
 {{1,0,0,0},{1,1,0,0},{0,1,0,0},{0,0,0,0}}
 },
 {
 //L{}
 {{1,0,0,0},{1,0,0,0},{1,1,0,0},{0,0,0,0}},
 {{1,1,1,0},{1,0,0,0},{0,0,0,0},{0,0,0,0}},
 {{1,1,0,0},{0,1,0,0},{0,1,0,0},{0,0,0,0}},
 {{0,0,1,0},{1,1,1,0},{0,0,0,0},{0,0,0,0}}
 },
 {
 //J
 {{0,1,0,0},{0,1,0,0},{1,1,0,0},{0,0,0,0}},
 {{1,0,0,0},{1,1,1,0},{0,0,0,0},{0,0,0,0}},
 {{1,1,0,0},{1,0,0,0},{1,0,0,0},{0,0,0,0}},
 {{1,1,1,0},{0,0,1,0},{0,0,0,0},{0,0,0,0}}
 },
 {
 //I{}
 {{1,1,1,1},{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,0}},
 {{1,1,1,1},{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,0}}
 },
 {
 //T
 {{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}},
 {{1,0,0,0},{1,1,0,0},{1,0,0,0},{0,0,0,0}}
 },
 {
 //田
 {{1,1,0,0},{1,1,0,0},{0,0,0,0},{0,0,0,0}},
 {{1,1,0,0},{1,1,0,0},{0,0,0,0},{0,0,0,0}},
 {{1,1,0,0},{1,1,0,0},{0,0,0,0},{0,0,0,0}},
 {{1,1,0,0},{1,1,0,0},{0,0,0,0},{0,0,0,0}}
 }


};

6、游戏头部:game.h:

#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED

// 游戏逻辑模块

#include <stdio.h>
#include <time.h>

typedef struct{
 int x;
 int y;
 int shape;
 int status;
 int color;
}BLOCK;

// 定义当前正在下落的方块,和下一个方块
BLOCK curBlock;
BLOCK nextBlock;

// 绘制游戏池边框
extern void gamePool(int x, int y);

// 打印操作说明
extern void printRule();

// 打印分数和等级
extern void printGradeLevel(int num);

// 游戏计时
extern void gameTime(clock_t startTime);

// 打印一个方块
extern void printBlock(int x, int y, int shape, int status, int color);

// 删除一个方块
extern void delBlock(int x, int y, int shape, int status);

//方块左移
extern void leftBlock();

//方块右移
extern void rightBlock();

//方块下移
extern int downBlock();

//方块变形
extern void changeBlock();

//方块直接落底
extern void bottomBlock();

//游戏暂停
extern void pause();

//随机产生游戏第一个方块
extern void startBlock();

//随机产生下一个方块
extern void blockNext();

//拷贝方块:把下一个方块变成当前正在下落的方块
extern void copyBlock();

//碰撞检测
extern int crash(int x, int y, int shape, int status);

//保存方块
extern void saveBlock();

//刷新游戏池
extern void updateGamePool();

//消行检测
extern void lineClear();

//消行下移
extern void lineDown(int line);

// 初始化游戏
extern void gameInit();


#endif // GAME_H_INCLUDED

7、最后一部分,游戏内容,game.c文件:

#include "game.h"
#include "mywindows.h"
#include "data.h"
#include <conio.h>

int level = 1;
int grade = 100;

// 打印游戏池
void gamePool(int x, int y)
{
 int i, j;
 for(i=0;i<25;i++)
 {
 for(j=0;j<26;j++)
 {
  if(windowShape[i][j] == 1)
  {
  setColor(0xc0);
  setPos(x+j,y+i);
  printf(" "); // printf("%2s", "");
  }
 }
 }
}

// 打印操作说明
void printRule()
{
 setColor(0x0f);
 setPos(31, 9);
 printf("操作规则:");
 setPos(32, 11);
 printf("按a或A左移");
 setPos(32, 12);
 printf("按d或D右移");
 setPos(32, 13);
 printf("按s或S下移");
 setPos(32, 14);
 printf("按w或W变形");
 setPos(32, 15);
 printf("按空格暂停");
 setPos(32, 16);
 printf("按回车直接下落");

}

void printGradeLevel(int num)
{
 switch(num)
 {
 case 0: break;
 case 1: grade += 10;break;
 case 2: grade += 25;break;
 case 3: grade += 50;break;
 case 4: grade += 70;break;
 }

 //等级-->作业

 setColor(0x09);
 setPos(3,6);
 printf("分数:%d", grade);

 setColor(0x0a);
 setPos(3,7);
 printf("等级:%d", level);
}

void gameTime(clock_t startTime)
{
 //clock_t endTime = clock();
 //clock_t ti = (endTime-startTime)/CLOCKS_PER_SEC;
 setColor(0x0d);
 setPos(3,8);
 printf("本次游戏运行时间:%ld", (clock()-startTime)/CLOCKS_PER_SEC);
}

// 打印一个方块
void printBlock(int x, int y, int shape, int status, int color)
{
 int i,j;
 setColor(color);
 for(i=0;i<4;i++)
 {
 for(j=0;j<4;j++)
 {
  if(1 == block[shape][status][i][j])
  {
  setPos(x+j,y+i);
  printf("■");
  }
 }
 }
}

// 删除一个方块
void delBlock(int x, int y, int shape, int status)
{
 int i, j;
 for(i=0;i<4;i++)
 {
 for(j=0;j<4;j++)
 {
  if(1 == block[shape][status][i][j])
  {
  setPos(x+j,y+i);
  printf(" "); //打印两个空格
  }
 }
 }
}

//方块左移
void leftBlock()
{
 //已经显示的方块删除,改变坐标,重新打印
 if(crash(curBlock.x-1, curBlock.y, curBlock.shape,curBlock.status) == -1)
 {
 return;
 }
 delBlock(curBlock.x,curBlock.y,curBlock.shape,
  curBlock.status);
 curBlock.x -= 1;
 printBlock(curBlock.x,curBlock.y,curBlock.shape,
  curBlock.status,curBlock.color);
}

//方块右移
void rightBlock()
{
 if(crash(curBlock.x+1, curBlock.y, curBlock.shape,curBlock.status) == -1)
 {
 return;
 }
 delBlock(curBlock.x,curBlock.y,curBlock.shape,
  curBlock.status);
 curBlock.x += 1;
 printBlock(curBlock.x,curBlock.y,curBlock.shape,
  curBlock.status,curBlock.color);
}

//方块下移
int downBlock()
{
 if(crash(curBlock.x, curBlock.y+1,curBlock.shape,curBlock.status) == -1)
 {
 //落到游戏池底部,产生新方块
 saveBlock();
 lineClear();
 updateGamePool();
 copyBlock();
 return -1;
 }else if(crash(curBlock.x, curBlock.y+1,curBlock.shape,curBlock.status) == -2)
 {
 //游戏结束
 return -2;
 }else{
 delBlock(curBlock.x,curBlock.y,curBlock.shape,
  curBlock.status);
 curBlock.y += 1;
 printBlock(curBlock.x,curBlock.y,curBlock.shape,
  curBlock.status,curBlock.color);
 return 0;
 }

}

//方块变形
void changeBlock()
{
 if(crash(curBlock.x, curBlock.y, curBlock.shape, (curBlock.status+1)%4) == -1){
 return;
 }
 delBlock(curBlock.x,curBlock.y,curBlock.shape,
  curBlock.status);
 curBlock.status = (curBlock.status+1)%4;
 printBlock(curBlock.x,curBlock.y,curBlock.shape,
  curBlock.status,curBlock.color);
}

//方块直接落底
void bottomBlock()
{
 while(1){
 //流程参考方块下落
 if(downBlock() !=0){
  return;
 }
 }
}

//游戏暂停
void pause()
{
 //
}

//随机产生游戏第一个方块
void startBlock()
{
 //设置时间为随机数种子
 srand((unsigned)time(NULL));
 //初始化curBlock
 curBlock.x = 22;
 curBlock.y = 1;
 //rand取一个随机整数
 curBlock.shape = rand()%7; //0-6
 curBlock.status = rand()%4; //0-3
 curBlock.color = rand()%0x10;
 if(0x00 == curBlock.color)
 {
 curBlock.color = 0x0f;
 }
 printBlock(curBlock.x, curBlock.y, curBlock.shape,
  curBlock.status, curBlock.color);
}

//随机产生下一个方块
void blockNext()
{
 //初始化nextBlock
 delBlock(nextBlock.x,nextBlock.y,
  nextBlock.shape,nextBlock.status);
 nextBlock.x = 34;
 nextBlock.y = 2;
 nextBlock.shape = rand()%7;
 nextBlock.status = rand()%4;
 nextBlock.color = rand()%0x10;
 if(0x00 == nextBlock.color)
 {
 nextBlock.color = 0x0f;
 }
 printBlock(nextBlock.x,nextBlock.y,
  nextBlock.shape,nextBlock.status,nextBlock.color);
}

//拷贝方块:把下一个方块变成当前正在下落的方块
void copyBlock()
{
 //当前方块=下一个方块
 curBlock = nextBlock;
 curBlock.x = 22;
 curBlock.y = 1;
 printBlock(curBlock.x, curBlock.y, curBlock.shape,
  curBlock.status, curBlock.color);
 blockNext();
}

//碰撞检测
//x,y为方块的下一个位置, status下一种形态
//碰撞返回-1,未碰撞返回0, 游戏结束返回-2
int crash(int x, int y, int shape, int status)
{
 int i,j;
 for(i=0;i<4;i++)
 {
 for(j=0;j<4;j++)
 {
  if(block[shape][status][i][j] == 1)
  {
  if(windowShape[i+y][j+x-15] == 1)
  {
   //方块一产生就发生碰撞
   if(curBlock.x == 22 && curBlock.y == 1)
   {
   return -2;
   }
   return -1;
  }
  }
 }
 }
 return 0;
}

//保存方块
void saveBlock()
{
 int i,j;
 for(i=0;i<4;i++)
 {
 for(j=0;j<4;j++)
 {
  if(block[curBlock.shape][curBlock.status][i][j] == 1)
  {
  windowShape[i+curBlock.y][j+curBlock.x-15] = 1;
  }
 }
 }
}

//刷新游戏池
void updateGamePool()
{
 int i,j;
 //从下到上刷新游戏池
 for(i=23;i>0;i--)
 {
 for(j=1;j<15;j++)
 {
  if(windowShape[i][j] == 1)
  {
  setColor(0x0e);
  setPos(j+15,i);
  printf("■");
  }else{
  setColor(0x00);
  setPos(j+15,i);
  printf(" ");
  }
 }
 }
}

//消行检测
//检测满行->这一行所有值都为1
void lineClear()
{
 int i,j;
 int num = 0; //统计一次消行数目
 for(i=23;i>0;i--)
 {
 int total = 0;
 for(j=1;j<15;j++)
 {
  total += windowShape[i][j];
 }
 if(total == 14)
 {
  //满行
  lineDown(i);
  i += 1;
  num += 1;
 }
 }
 printGradeLevel(num);
}

//消行下移
//从满行的这行开始,上面所有行顺序下移
void lineDown(int line)
{
 int i,j;
 for(i=line;i>1;i--)
 {
 for(j=1;j<15;j++)
 {
  windowShape[i][j] = windowShape[i-1][j];
 }
 }
}


// 初始化游戏
void gameInit()
{
 //第一步,必须初始化句柄
 //clock_t startTime = clock();
 initHandle();
 setCursorVisible(FALSE);

 gamePool(15,0);
 printRule();
 printGradeLevel(0);

 startBlock();
 blockNext();
 //定时开始时间,结束时间,通过控制时间差实现定时
 clock_t start,stop;
 start = clock();

 while(1) //for(;;)
 {
 //检测是否有按键按下
 if(kbhit())
 {
  switch(getch())
  {
  case 'a':
  case 'A':
  case 75:
  leftBlock();break;
  case 'd':
  case 'D':
  case 77:
  rightBlock();break;
  case 's':
  case 'S':
  case 80:
  downBlock();break;
  case 'w':
  case 'W':
  case 72:
  changeBlock();break;
  case 32:
  pause();break;
  case 13:
  bottomBlock();break;
  }
 }
 //获取时间
 stop = clock();
 if(stop-start>0.5*CLOCKS_PER_SEC)
 {
  downBlock();
  //重新计时
  start = stop;
 }
 }
}

好,到现在这个游戏也就结束了!

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

 类似资料:
  • 本文向大家介绍C语言实现俄罗斯方块源代码,包括了C语言实现俄罗斯方块源代码的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了C语言实现俄罗斯方块的具体代码,供大家参考,具体内容如下 GitHub:【C语言】实现俄罗斯方块源代码 Head.h Draw.h Init.h game.h main.cpp 配置文件:RockShape.ini 更多关于俄罗斯方块的文章,请点击查看专题:《俄罗

  • 本文向大家介绍C语言源码实现俄罗斯方块,包括了C语言源码实现俄罗斯方块的使用技巧和注意事项,需要的朋友参考一下 介绍 俄罗斯方块(Tetris, 俄文:Тетрис)是一款电视游戏机和掌上游戏机游戏,它由俄罗斯人阿列克谢·帕基特诺夫发明,故得此名。俄罗斯方块的基本规则是移动、旋转和摆放游戏自动输出的各种方块,使之排列成完整的一行或多行并且消除得分。由于上手简单、老少皆宜,从而家喻户晓,风靡世界。

  • 本文向大家介绍C语言实现俄罗斯方块小游戏,包括了C语言实现俄罗斯方块小游戏的使用技巧和注意事项,需要的朋友参考一下 C语言实现俄罗斯方块小游戏的制作代码,具体内容如下 更多俄罗斯方块精彩文章请点击专题:俄罗斯方块游戏集合 进行学习。 更多有趣的经典小游戏实现专题,分享给大家: C++经典小游戏汇总 python经典小游戏汇总 JavaScript经典游戏 玩不停 java经典小游戏汇总 javas

  • 本文向大家介绍C# 实现俄罗斯方块(附源码),包括了C# 实现俄罗斯方块(附源码)的使用技巧和注意事项,需要的朋友参考一下 概述 俄罗斯方块(Tetris)是一款由俄罗斯人阿列克谢·帕基特诺夫发明的休闲游戏,帕基特诺夫爱玩拼图,从拼图游戏里得到灵感,设计出了俄罗斯方块。由于上手简单、老少皆宜,从而家喻户晓,风靡世界。本文简述如何通过C#来实现俄罗斯方块,仅供学习分享使用,如有不足之处,还请指正。

  • 本文向大家介绍60行js代码实现俄罗斯方块,包括了60行js代码实现俄罗斯方块的使用技巧和注意事项,需要的朋友参考一下 这是我之前网上看到的,很牛逼的一位大神写的,一直膜拜中 以上所述就是本文的全部内容,希望大家能够喜欢。

  • 本文向大家介绍基于VC 6.0使用C语言实现俄罗斯方块,包括了基于VC 6.0使用C语言实现俄罗斯方块的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了C语言实现俄罗斯方块的具体代码,供大家参考,具体内容如下 裸写的俄罗斯方块的代码,有意见或者想征用,直接评论留言即可。 效果如下: 代码: 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。