博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
从零开始---控制台用c写俄罗斯方块游戏(2)
阅读量:4358 次
发布时间:2019-06-07

本文共 5607 字,大约阅读时间需要 18 分钟。

上回说到下移的问题,这篇就说一下刷新的问题

我们控制台输出一般都是一行一行的输出,所以,在输出屏幕的时候,我们一个画面闪到另一个画面的效果

我刚开始弄的是用system("CLS");进行清屏,但还是会有闪烁的效果,接下来我会在上一个博文的代码,现在贴上代码

// c.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include 
#include
#include
#include
#include
#define intX 10#define intY 20//显示void show(char Interface[intY][intX]){ int i, j; for (i = 0; i < intY; i++) { printf("%c", 3); for (j = 0; j < intX; j++) { if (Interface[i][j] == 0) { printf(" "); } else { printf("■"); } } printf("%c", 3); printf("\n"); } for (i = 0; i < 2 * intX + 2; i++) { printf("%c", 2); } printf("\n");}int _tmain(int argc, _TCHAR* argv[]){ //界面数组 char Interface[intY][intX] = { { 0, 0, 0, 2, 2, 2 }, { 0, 0, 0, 0, 2 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 },{ 0 }, { 0, 0, 0, 0 }, { 0 } }; /*当前状态*/ int i = 0; int j = 0; while (true) { for (i = intY - 1; i >= 0; i--) /* 继续下落 */ { for (j = 0; j < intX; j++) { if (Interface[i][j] == 2) { Interface[i + 1][j] = Interface[i][j]; Interface[i][j] = 0; /*方块下移*/ } } } show(Interface); getchar(); /*getchar();*/ }}

接下来我们写一个函数,命名为gotoxy(int x ,int y),下面是里面的实现

void gotoxy(int x, int y){    COORD c;    c.X = x;     c.Y = y;    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);}

上面那个函数要用到引用一个windows.h的文件,这个函数的作用就是直接跳到指定的位置坐标进行输出,这样做就是一点一点的把原先的输出屏幕上的东西覆盖住,这样减少闪烁效果很

明显,下面是完全代码

// c.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include 
#include
#include
#include
#include
#include "windows.h"#include "time.h"#define intX 10#define intY 20void gotoxy(int x, int y){ COORD c; c.X = x; c.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);}//显示void show(char Interface[intY][intX]){ int i, j; for (i = 0; i < intY; i++) { printf("%c", 3); for (j = 0; j < intX; j++) { if (Interface[i][j] == 0) { printf(" "); } else { printf("■"); } } printf("%c", 3); printf("\n"); } for (i = 0; i < 2 * intX + 2; i++) { printf("%c", 2); } printf("\n");}int _tmain(int argc, _TCHAR* argv[]){ //界面数组 char Interface[intY][intX] = { { 0, 0, 0, 2, 2, 2 }, { 0, 0, 0, 0, 2 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 },{ 0 }, { 0, 0, 0, 0 }, { 0 } }; /*当前状态*/ int i = 0; int j = 0; while (true) { for (i = intY - 1; i >= 0; i--) /* 继续下落 */ { for (j = 0; j < intX; j++) { if (Interface[i][j] == 2) { Interface[i + 1][j] = Interface[i][j]; Interface[i][j] = 0; /*方块下移*/ } } } show(Interface); gotoxy(0, 0); }}

因为我一讲到一个功能性代码的时候,就会把整个代码发上来,占了一大幅字,这不是我想要凑字数,是我说了这些东西,有些初学者,可能不知道怎么用,也是时间问题,只能慢慢来

上面解决下移问题和刷屏的问题,可以看到一个方块往下掉后就消失不见了,我们来弄一个手动控制下移,按一次5就下移一次。

因为很多函数都要用到界面数组信息,所以把它提取出来做成一个全局变量

要做成这个效果,就要用到一个函数了,_kbhit,或者是kbhit,两个函数的功能是一样的,只是版本不同,我这里是vs2013,所以是第一个,这个函数在#include "conio.h",引用这个头文件。kbhit() 功能及返回值: 检查当前是否有键盘输入,若有则返回一个非0值,否则返回0。看我代码里是怎么用的,不懂多看几遍。下面贴出整个代码:

// c.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include 
#include
#include
#include
#include
#include "windows.h"#include "time.h"#include "conio.h"#define intX 10#define intY 20//记录方块左上角的行和列的位置坐标int Row = 0, Column = 3;//界面数组char Interface[intY][intX] ={ { 0, 0, 0, 2, 2, 2 }, { 0, 0, 0, 0, 2 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0, 0, 0, 0 }, { 0 }}; /*当前状态*/void Down(){ int i,j; for (i = intY - 1; i >= 0; i--) /* 继续下落 */ { for (j = 0; j < intX; j++) { if (Interface[i][j] == 2) { Interface[i + 1][j] = Interface[i][j]; Interface[i][j] = 0; /*方块下移*/ } } }}//指定位置输出void Gotoxy(int x, int y){ COORD c; c.X = x; c.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);}//显示void Show(){ int i, j; for (i = 0; i < intY; i++) { printf("%c", 3); for (j = 0; j < intX; j++) { if (Interface[i][j] == 0) { printf(" "); } else { printf("■"); } } printf("%c", 3); printf("\n"); } for (i = 0; i < 2 * intX + 2; i++) { printf("%c", 2); } printf("\n");}int _tmain(int argc, _TCHAR* argv[]){ Show(); int i = 0; int j = 0; char control = '2'; while (true) { if (!_kbhit()) { control = '2'; } else { control = _getch(); } if (control == '5') { Down(); Gotoxy(0, 0); Show(); } }}

这里我们已经做出了手动控制移动效果,下一次,就是到达底部碰撞停止的功能了。

 

转载于:https://www.cnblogs.com/lsgsanxiao/p/5407636.html

你可能感兴趣的文章
关于两次指针(struct型)传参数的问题
查看>>
自己制作Linux镜像,CentOS 6.5 Docker自制CentOS镜像
查看>>
linux配置scp交互传输,Linux间传输文件的几种方法scp、sftp
查看>>
linux安装nginx映射目录,centos8自定义目录安装nginx(教程详解)
查看>>
linux cpu scheduler,A Temporal Partition-Based Linux CPU Scheduler
查看>>
c语言怎么写最小公倍数的函数,C语言 · 最小公倍数
查看>>
c语言中的头文件string.h的作用,C语言常用头文件及库函数——string.h
查看>>
c语言字符-1代表什么,玩儿转C语言:符号和字符(1)
查看>>
知道商洛学院c语言章节答案,C语言程序设计(商洛学院)知到章节答案
查看>>
c语言酒精检测仪程序代码,单片机酒精浓度测试仪,代码,原理图
查看>>
单路电压表c语言编程,单片机数字电压表的设计
查看>>
精通c语言的标准,《精通Unix下C语言编程与项目实践》之七——标准I/O重定向
查看>>
蓝桥杯c语言试题 高职,2011l蓝桥杯c语言高职真题附加答案2.doc
查看>>
c 语言 设计算法 例题,C语言程序设计例题.pdf
查看>>
p4 linux 命令行,《linux就该这么学》学习笔记25期2020-P4
查看>>
android9.0官方下载,安卓9.0系统刷机包 官方正式版
查看>>
android 模块封装,Android开发之针对联系人的封装
查看>>
android 构建期间错误,Android构建错误
查看>>
Android应用开发病虫害识别,基于Android平台的枣虫害识别系统的设计与实现
查看>>
织梦上传html文件,提高DedeCMS生成静态页html文件速度的方法
查看>>