C语言实现俄罗斯方块小游戏

所属分类: 软件编程 / C 语言 阅读数: 44
收藏 0 赞 0 分享

C语言实现俄罗斯方块小游戏的制作代码,具体内容如下

#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
 
 
#define TTY_PATH "/dev/tty" 
#define STTY_ON "stty raw -echo -F" 
#define STTY_OFF "stty -raw echo -F" 
 
int map[21][14]; 
char direct; 
 
int node[7][4][16]={ 
 {{0,0,0,0,1,1,1,1,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,0,0,0,1,1,1,1,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,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}}, 
 {{0,1,0,0,1,1,1,0,0,0,0,0,0,0,0,0},//3边加一中点 
 {0,1,0,0,0,1,1,0,0,1,0,0,0,0,0,0}, 
 {0,0,0,0,1,1,1,0,0,1,0,0,0,0,0,0}, 
 {0,1,0,0,1,1,0,0,0,1,0,0,0,0,0,0}}, 
 {{0,1,1,0,0,1,0,0,0,1,0,0,0,0,0,0},//右锄头型 
 {0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,0}, 
 {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,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}, 
 {0,1,0,0,0,1,0,0,0,1,1,0,0,0,0,0}, 
 {0,0,0,0,1,1,1,0,1,0,0,0,0,0,0,0}}, 
 {{0,1,0,0,0,1,1,0,0,0,1,0,0,0,0,0},//右曲折型 
 {0,1,1,0,1,1,0,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,1,1,0,1,1,0,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}, 
 {1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0}} 
 }; 
 
typedef struct block 
{ 
 int x; 
 int y; 
 int blockType; 
 int blockDirect; 
}Block; 
Block bl; 
 
void init_map()//初始化边框 
{ 
 int i,j; 
 for(i=0; i<21; i++) 
 for(j=0; j<14; j++) 
 { 
 if(j==0 || j==13) 
 map[i][j] = 200; 
 else if(i==20) 
 map[i][j] = 201; 
 else 
 map[i][j] = 0; 
 } 
} 
void new_block()//生成随机的俄罗斯方块 
{ 
 int blockType = rand()%7; 
 int blockDirect = rand()%4; 
 int x = 1; 
 int y = 5; 
 bl.x = x; 
 bl.y = y; 
 bl.blockType = blockType; 
 bl.blockDirect = blockDirect; 
} 
 
void input()//将移动后的俄罗斯方块,导入地图中作标记 
{ 
 int i, j; 
 for(i=0; i<4; i++) 
 for(j=0; j<4; j++) 
 if(node[bl.blockType][bl.blockDirect][i*4+j]==1) 
 { 
 map[bl.x+i][bl.y+j] = 1; 
 } 
} 
void output()//移动时,将之前俄罗斯方块在地图信息清空。 
{ 
 int i, j; 
 for(i=0; i<4; i++) 
 for(j=0; j<4; j++) 
 if(node[bl.blockType][bl.blockDirect][i*4+j]==1) 
 { 
 map[bl.x+i][bl.y+j] = 0; 
 } 
} 
 
void change()//俄罗斯方格在碰撞后融入,固定 
{ 
 int i, j; 
 for(i=0; i<4; i++) 
 for(j=0; j<4; j++) 
 if(node[bl.blockType][bl.blockDirect][i*4+j]==1) 
 { 
 map[bl.x+i][bl.y+j] = 10; 
 } 
 for(j=1; j<13; j++) 
 if(map[5][j] == 10) 
 { 
 system("clear"); 
 printf("game over !!!!!!!!!\n"); 
 exit(1); 
 } 
} 
 
 
void print_map()//打印地图,显示信息 
{ 
 int i,j; 
 for(i=5; i<21; i++) 
 { 
 for(j=0; j<14; j++) 
 { 
 if(map[i][j]==200)//左右边界 
 printf("#"); 
 else if(map[i][j]==201)//下边界 
 printf(" # "); 
 else if(map[i][j]==0)//空白地 
 printf(" "); 
 else if(map[i][j]==1)//移动的俄罗斯方块 
 printf(" * "); 
 else if(map[i][j]==10)//固定的俄罗斯方块 
 printf(" @ "); 
 } 
 printf("\n"); 
 } 
} 
void delLine(int n)//消行 
{ 
 int i,j; 
 for(j = 1; j<13; j++) 
 map[n][j] = 0; 
 for(i = n; i>5 ; i--) 
 for(j = 1; j<13; j++) 
 if(map[i-1][j] != 1) 
 map[i][j] = map[i-1][j]; 
} 
 
void isFillLine()//是否满足消行条件 
{ 
 
 int i,j; 
 int fals; 
 for(i=19; i>5; i--) 
 { 
 fals = 1; 
 for(j=1; j<13; j++) 
 { 
 if(map[i][j] != 10) 
 { 
 fals = 0; 
 continue; 
 } 
 } 
 if(fals) 
 { 
 delLine(i); 
 } 
 } 
} 
void down()//下移 
{ 
 int i, j; 
 int fale = 1; 
 for(i=3; i>=0; i--) 
 for(j=0; j<4; j++) 
 if(node[bl.blockType][bl.blockDirect][i*4+j] == 1) 
 if(map[bl.x+i+1][bl.y+j] == 10 || map[bl.x+i+1][bl.y+j] == 201) 
 { 
 change(); 
 fale = 0; 
 new_block(); 
 isFillLine(); 
 return; 
 } 
 if(fale) 
 { 
 output(); 
 bl.x += 1; 
 input(); 
 } 
 
} 
void right()//右移 
{ 
 int i, j; 
 int fale = 1; 
 for(i=3; i>=0; i--) 
 for(j=0; j<4; j++) 
 if(node[bl.blockType][bl.blockDirect][i*4+j] == 1) 
 if(map[bl.x+i][bl.y+j+1] == 10 || map[bl.x+i][bl.y+j+1] == 200) 
 { 
 fale = 0; 
 return; 
 } 
 if(fale) 
 { 
 output(); 
 bl.y += 1; 
 input(); 
 } 
 
} 
void left()//左移 
{ 
 int i, j; 
 int fale = 1; 
 for(i=3; i>=0; i--) 
 for(j=0; j<4; j++) 
 if(node[bl.blockType][bl.blockDirect][i*4+j] == 1) 
 if(map[bl.x+i][bl.y+j-1] == 10 || map[bl.x+i][bl.y+j-1] == 200) 
 { 
 fale = 0; 
 return; 
 } 
 if(fale) 
 { 
 output(); 
 bl.y -= 1; 
 input(); 
 } 
 
} 
 
void change_block()//俄罗斯方块变形 
{ 
 int i,j; 
 output(); 
 int fals = 1; 
 bl.blockDirect += 1; 
 bl.blockDirect %= 4; 
 for(i=0; i<4; i++) 
 for(j=0; j<4; j++) 
 if(node[bl.blockType][bl.blockDirect][i*4+j]==1) 
 if(map[bl.x+i][bl.y+j] != 0 ) 
 { 
 fals = 0; 
 break; 
 } 
 if(fals) 
 { 
 input(); 
 }else 
 { 
 bl.blockDirect -= 1; 
 input(); 
 } 
} 
 
char in_direct()//非堵塞输入 
{ 
 fd_set fd; 
 struct timeval tv; 
 char ch; 
 FD_ZERO(&fd); 
 FD_SET(0, &fd); 
 tv.tv_sec = 0; 
 tv.tv_usec = 10; 
 if(select(1, &fd ,NULL, NULL, &tv) > 0) 
 { 
 ch = getchar(); 
 } 
 return ch; 
} 
int main()//q 退出游戏,a,d 左右移动,空格变形 
{ 
 srand(time(NULL)); 
 init_map(); 
 new_block(); 
 input(); 
 char ch; 
 int num = 0; 
 while(1) 
 { 
 usleep(500000); 
 system(STTY_ON TTY_PATH); 
 ch = in_direct(); 
 system(STTY_OFF TTY_PATH); 
 system("clear"); 
 if(ch == 'a' && num <= 1) 
 { 
 left(); 
 print_map(); 
 num++; 
 continue; 
 }else if(ch == 'd' && num <= 1) 
 { 
 right(); 
 print_map(); 
 num++; 
 continue; 
 }else if(ch == ' ' && num <= 1 ) 
 { 
 change_block(); 
 print_map(); 
 num++; 
 continue; 
 }else if(ch == 'q') 
 { 
 system("clear"); 
 printf("gave over!!!!!\n"); 
 exit(0); 
 } 
 down(); 
 print_map(); 
 num = 0; 
 
 } 
 return 0; 
}

更多俄罗斯方块精彩文章请点击专题:俄罗斯方块游戏集合 进行学习。

更多有趣的经典小游戏实现专题,分享给大家:

C++经典小游戏汇总

python经典小游戏汇总

JavaScript经典游戏 玩不停

java经典小游戏汇总

javascript经典小游戏汇总

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

更多精彩内容其他人还在看

C++广播通信实例

这篇文章主要介绍了C++实现广播通信的方法,实例讲述了C++ socket广播通信的原理与实现方法,需要的朋友可以参考下
收藏 0 赞 0 分享

C++计算ICMP头的校验和实例

这篇文章主要介绍了C++计算ICMP头的校验和的方法,代码简单实用,对于校验ICMP报文来说有不错的实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C++设置超时时间的简单实现方法

这篇文章主要介绍了C++设置超时时间的简单实现方法,涉及系统函数setsockopt对套接口的操作,具有一定的实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C++实现ping程序实例

这篇文章主要介绍了C++实现ping程序实例,涉及C++对于ICMP数据包的发送与回显处理,具有一定的实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C++之boost::array的用法

这篇文章主要介绍了C++之boost::array的用法,以实例的形式简单讲述了静态数组的容器boost::array的使用技巧,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C++之Boost::array用法简介

这篇文章主要介绍了C++之Boost::array用法简介,较为详细的分析了Boost::array中的常见用法,并用实例的形式予以总结归纳,需要的朋友可以参考下
收藏 0 赞 0 分享

VC文件目录常见操作实例汇总

这篇文章主要介绍了VC文件目录常见操作实例汇总,总结了VC针对文件目录的各种常用操作,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

VC打印word,excel文本文件的方法

这篇文章主要介绍了VC打印word,excel文本文件的方法,是VC操作文本文件中非常实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

VC++获得当前进程运行目录的方法

这篇文章主要介绍了VC++获得当前进程运行目录的方法,可通过系统函数实现该功能,是非常实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

VC中SendMessage和PostMessage的区别

这篇文章主要介绍了VC中SendMessage和PostMessage的区别,较为全面的分析了SendMessage和PostMessage运行原理及用法上的不同之处,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多