俄罗斯方块c语言代码_第1页
俄罗斯方块c语言代码_第2页
俄罗斯方块c语言代码_第3页
俄罗斯方块c语言代码_第4页
俄罗斯方块c语言代码_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、#include<windows.h>#include<time.h>#include<conio.h>#include<stdio.h>#include<stdlib.h>const int back = 30;/限定变量不允许被改变,表示背景颜色const int speed = 0, 12, 9, 6, 3, 1;const int number = 0, 2, 3, 5, 7, 11, 15;const int col = 9, 10, 11, 12, 13, 14, 15;/七种方块的颜色int rank = 1;int l

2、evel = 0; /0表示既不是向右也不是向左,1表示向左,2表示向右int kind;int pri_kind; / 7大类,颜色标记,调用colcol_kind;int revolve = 0; / 旋转状态标记/struct/当前坐标及之前坐标 int x; int y; current, before;/struct/定义一个结构体 内容包括坐标,颜色,是否显示方块 int x; int y; int color; bool having;/布尔型变量 作逻辑判断 运算结果为真或为假,有方块为真,没有方块为假 board1525;/struct/19种方块的坐标位置,然后由这些坐标点

3、控制光标绘制成方块 int vary_x4; int vary_y4; vary = 0, 2, 4, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -2, -3, 0, 2, 2, 0, 0, 0, -1, -1, 0, -2, -2, -4, 0 , 0, -1, -1, 0, 0, 2, 2, 0, -1, -1, -2, 0, 2, 2, 4, 0, 0, -1, -1, 0, 0, -2, -2, 0, -1, -1, -2, 0, 0, 2, 4, 0, -1, 0, 0, 0, 0, 0, 2, 0, -1, -2, -2, 0, 0, -2, -4, 0,

4、 -1, -1, -1, 0, 2, 2, 2, 0, 0, -1, -2, 0, 2, 4, 4, 0, 0, 0, -1, 0, -2, -2, -2, 0, 0, -1, -2, 0, 0, 2, 4, 0, -1, -1, -1, 0, 0, 0, -2, 0, -1, -2, -2, 0 , 2, 4, 2, 0, 0, 0, -1, 0, 0, 2, 0, 0, -1, -1, -2, 0, -2, 0, 2, 0, -1, -1, -1, 0, 0, -2, 0, 0, -1, -1, -2,;/struct int sum; int connection_x5; int con

5、nection_y5; connection = 2, -2, 2, 0, 1, 1, 0, 0, 2, 2, -2, 0, 0, 2, -2, 2, 0, 0, 4, -2, 0, 4, -2, 0, 0, -1, 1, 4, -4, 2, -2, 4, 0, 0, -1, 1, 4, -2, 2, 0, 0, 0, 0, 0, 0,;/void gotoxy(int x, int y)/光标移动函数 COORD pos;/COORD是Windows API中定义的一种结构,表示一个字符在控制台屏幕上的坐标 pos.X = x; pos.Y = y; SetConsoleCursorPosi

6、tion(GetStdHandle(STD_OUTPUT_HANDLE), pos);/使光标 到(x,y)这个位置/void color(int b)/颜色函数 HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE) ; SetConsoleTextAttribute(hConsole, b) ;/void PrintGarphy()/画游戏界面函数 color(1101);/边框的颜色 int i, j; gotoxy(2, 2);/开始画边框 for(i = 0; i < 26; i+) printf(""); got

7、oxy(2, 28); for(i = 0; i < 26; i+) printf(""); for(i = 0; i < 26; i+) gotoxy(2, 2 + i); printf(""); gotoxy(34, 2 + i); if(i = 8 | i = 16) printf(""); else printf(""); gotoxy(52, 2 + i); printf(""); int t;FILE *fp = fopen("C:els.txt",

8、"r");/最高分的取出 fscanf (fp, "%d", &t); fclose(fp); color(46);/最高分的颜色 gotoxy(37, 12);/最高分的显示位置 printf("最高纪录: %d", t); color(46); gotoxy(37, 14); printf("当前得分: 0"); color(46); gotoxy(37, 16); printf("级数 "); color(39); gotoxy(36, 20); printf(" 操作

9、"); gotoxy(37, 22); printf(" 按空格 暂停"); gotoxy(37, 24); printf(" 按 ESC 退出"); gotoxy(37, 26); printf(" 编写人 129全体"); color(back); for(i = 0; i < 25; i+)/做一个空白的界面 gotoxy(4, 3 + i); for(j = 0; j < 15; j+) printf(" "); /void GameOver()/游戏结束的函数 color(391);

10、 for(int i = 24; i >= 0; i-)/清屏 for(int j = 0; j < 15; j+)/ gotoxy(32 - j * 2, 3 + i); printf(""); Sleep(10);/执行挂起一段时间 实现一种效果 gotoxy(13,10); printf("GAMEOVER!"); gotoxy(13,11); printf("GAMEOVER!"); gotoxy(13,12); printf("GAMEOVER!"); gotoxy(13,13); print

11、f("GAMEOVER!"); gotoxy(13,14); printf("GAMEOVER!"); Sleep(2000); color(back); for(int j = 0; j < 25; j+)/恢复起始的效果 for(int i = 0; i < 15; i+) boardij.having = 0; gotoxy(4 + 2 * i, j + 3); printf(" "); Sleep(10);/执行挂起一段时间 实现一种效果 color(34); gotoxy(42, 16); color(34);

12、gotoxy(42, 16); while(rank-) printf(" "); color(46); gotoxy(47, 14); printf(" ");/void GetDepth(int *pdepth)/判断是否达到最大高度 (*pdepth) = 25; for(int j = 0; j < 4; j+) int sum = 0; int n = (current.x + varykind.vary_xj - 4) / 2; int m = current.y + varykind.vary_yj - 3; if(m < 0)

13、 m = 0; m+; for(m; m < 25; m+) if(boardnm.having = 0 && n >= 0 && n < 15 && m >= 0 && m < 25) sum+; else break; if(sum < (*pdepth) (*pdepth) = sum; /void CheckFull(int *pscore)/消行函数 int i, j; int sum = 0; int s = 0, 1, 3, 6, 10; for(i = 0; i < 25;

14、 i+) bool mark = 1; for(j = 0; j < 15; j+) if(boardji.having = 0) mark = 0; boardji.color = back; if(mark) sum+; for(int t = i; t > 0; t-) gotoxy(4, 3 + t); for(j = 0; j < 15; j+) if(boardjt-1.having) color(boardjt-1.color); printf(""); boardjt.color = boardjt-1.color; boardjt.hav

15、ing = 1; boardjt-1.having = 0; else color(back); printf(" "); boardjt.color = boardjt.color; boardjt.having = 0; boardjt-1.having = 0; (*pscore) += ssum; color(46); gotoxy(47, 14); printf("%d", *pscore); if(*pscore >= 140) rank = 5; else if(*pscore >= 90) rank = 4; else if(

16、*pscore >= 50) rank = 3; else if(*pscore >= 20) rank = 2; else if(*pscore >= 1) rank = 1; color(44); gotoxy(42, 16); for(i = 0; i < rank && *pscore; i+) printf("");/表示等级 int t; FILE *fp = fopen("C:els.txt", "r");/最高分所在文件 fscanf (fp, "%d", &

17、amp;t); fclose(fp); if(*pscore > t) fp = fopen("C:els.txt", "w"); fprintf (fp, "%d", *pscore); fclose(fp); color(43); gotoxy(47, 12); printf("%d", *pscore); fclose(fp);/void PrintNext(int pkind)/显示下一个个方块 int i; int x = 43, y = 6; color(34); for(i = 0; i <

18、; 4; i+) gotoxy(x - 3, y + i - 2); printf(" "); pri_kind = pkind; if(pkind = 6) pkind = 15, x-; else if(pkind = 5) pkind = 11, x-; else if(pkind = 4) pkind = 7, x-; else if(pkind = 3) pkind = 5, x -= 2; else if(pkind = 2) pkind = 3, x += 2; else if(pkind = 1) pkind = 2, x-; else pkind = 0,

19、 x -= 3; color(colpri_kind); for(i = 0; i < 4; i+)/在右上角正中间显示下一个方块 gotoxy(x + varypkind.vary_xi, y + varypkind.vary_yi); printf(""); /void CheckBoundary()/检查边界线,判断方块在左边和右边是否能进行变换和移动 int i, n, m; for(i = 0; i < 4; i+) m = current.x + varykind.vary_xi; if(m < 4 | m > 32) current.x

20、 += (m < 4 ? 2 : -2); i = -1; for(i = 0; i < 4; i+) / 对四个小方块分别检查 m = current.x + varykind.vary_xi; n = current.y + varykind.vary_yi; m = (m - 4) / 2; n = n - 3; if(n < 0 | n > 24 | boardmn.having) if(level = 1) / 左移 current.x += 2; /复原 else if(level = 2) / 右移 current.x -= 2; /复原 level =

21、0; break; /void LevelMove()/下落速度函数 CheckBoundary(); if(level = 0)return; int i; color(back); for(i = 0; i < 4; i+) if(current.y + varykind.vary_yi > 2) gotoxy(before.x + varykind.vary_xi, before.y + varykind.vary_yi); puts(" "); color(colpri_kind); for(i = 0; i < 4; i+) if(current

22、.y + varykind.vary_yi > 2) gotoxy(current.x + varykind.vary_xi, current.y + varykind.vary_yi); puts(""); before.x = current.x;/void Revolve(int *prev_count) /旋转函数 int i; before.x = current.x; before.y = current.y; (*prev_count)+; (*prev_count) %= connectionpri_kind.sum; current.x = curr

23、ent.x + connectionpri_kind.connection_x *prev_count; current.y = current.y + connectionpri_kind.connection_y *prev_count; CheckBoundary(); for(i = 0; i < 4; i+) int m = (current.x - 4 + varynumberpri_kind + *prev_count.vary_xi) / 2; int n = current.y + varynumberpri_kind + *prev_count.vary_yi - 3

24、; if(boardmn.having | m > 14 | m < 0 | n > 24) current.x = current.x - connectionpri_kind.connection_x *prev_count; current.y = current.y - connectionpri_kind.connection_y *prev_count; revolve = 0; (*prev_count)-; before.x = current.x; before.y = current.y; return ; color(back); for(i = 0;

25、i < 4; i+) if(before.y + varykind.vary_yi > 2) gotoxy(before.x + varykind.vary_xi, before.y + varykind.vary_yi); puts(" "); kind = numberpri_kind + *prev_count; color(colpri_kind); for(i = 0; i < 4; i+) if(current.y + varykind.vary_yi > 2) gotoxy(current.x + varykind.vary_xi, c

26、urrent.y + varykind.vary_yi); puts(""); Sleep(speedrank); before.x = current.x; before.y = current.y;/void ChoiceDirection(int *prev_count)/按键(方块变换) int t = 50 / rank; while(t-) if(_kbhit() char c = _getch(); switch(c) case 0x48:/ 上 revolve = 1; Revolve(&prev_count);/旋转 break; case 0x5

27、0: /下 level = 0; return; case 0x4b: /左 before.x = current.x; current.x = current.x-2; level = 1; LevelMove(); break; case 0x4d: /右 before.x = current.x; current.x += 2; level = 2; LevelMove(); break; case ' ': _getch(); break; case 27: color(back); exit(1); default: break; Sleep(speedrank);

28、/void StraightFall(int depth)/下落函数 int i; int rev_count = 0; before.x = current.x; before.y = current.y; while(GetDepth(&depth), depth-) ChoiceDirection(&rev_count); GetDepth(&depth); if(depth = 0) revolve = 0; before.x = current.x; before.y = current.y; break; color(back); for(i = 0; i

29、< 4; i+) if(before.y + varykind.vary_yi > 2) gotoxy(before.x + varykind.vary_xi, before.y + varykind.vary_yi); puts(" ");/之前的变为空格 current.y+; color(colpri_kind); for(i = 0; i < 4; i+) if(current.y + varykind.vary_yi > 2) gotoxy(current.x + varykind.vary_xi, current.y + varykind

30、.vary_yi); puts("");/下面的进行补充 revolve = 0; before.x = current.x; before.y = current.y; ChoiceDirection(&rev_count); if(level | revolve) level = 0; revolve = 0; ChoiceDirection(&rev_count); StraightFall(depth); /递归调用 /void Start() gotoxy(12,10); printf("1 开始游戏n"); gotoxy(12

31、,11); printf("2 结束游戏n"); gotoxy(9,12); printf("请输入数字后输入回车n");int main()/主函数 int ab; FILE *fp = fopen("c:123sa.c", "w"); srand(unsigned long)time(0); system("mode con cols=56 lines=30");/画总界面大小 system("color 24");/右侧边栏的颜色 int score = 0; fp = fopen("C:els.txt", "r");/打开最高分的文件 if(fp = NULL) f

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论