c++游戏小技巧3:Sleep(停顿) 与 gotoxy(0,0) (无闪清屏)

因为篇幅原因,这一次我们讲两个小方法

目录

1.gotoxy

1.无闪清屏

2.移动到指定地点

2.Sleep

1.与gotoxy一起用

2.文字游戏必备

1.gotoxy

大家写游戏清屏用什么???

是不是一般都是

#include
#include
using namespace std;

int main()
{
    while(1)
    {
    	printf("脏脏包 YYDS!!!");
    	system("cls");
	}
    return 0;
}

发现什么没有

是不是特别闪!

于是,在奇妙的windows.h里面有这样一个函数:

SetConsoleCursorPosition(hOut,pos);

可以用来改变光标位置

光标就是你输出、输入是一闪一闪的那个东西

表示着你输入、输出到哪里了

我们就可以改变它位置

就有了一下两种用法:

1.无闪清屏

#include
#include
using namespace std;

void gotoxy(int x, int y)//覆盖清屏 ,从指定行列覆盖
{
	COORD pos = {x,y};
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(hOut, pos);
	return ;
}

int main()
{
    while(1)
    {
    	printf("脏脏包 YYDS!!!");
    	gotoxy(0,0);//从0,0 覆盖
	}
    return 0;
}//是不是还是更闪???
//等把Sleep讲了就好了

2.移动到指定地点

对比下面两段代码:

1.

#include
#include
using namespace std;

int main()
{
	system("mode con cols=50 lines=25");
    while(1)
    {
    	for(int i=1;i<=12;i++) cout<<"\n";
    	for(int i=1;i<=18;i++) cout<<" ";
    	printf("脏脏包 YYDS!!!");
    	system("cls");
	}
    return 0;
}

2.

#include
#include
using namespace std;

void gotoxy(int x, int y)//覆盖清屏 
{
	COORD pos = {x,y};
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(hOut, pos);
	return ;
}

int main()
{
	system("mode con cols=50 lines=25");
    while(1)
    {
    	gotoxy(18,12);
    	printf("脏脏包 YYDS!!!");
	}
    return 0;
}

发现了吧,是不是gotoxy更加简洁??(虽然一次感觉有点多,但多次就会写的少些)

2.Sleep

Sleep 也是 windows.h 中的一员

用法very very 简单:

Sleep(n) //停顿n毫秒

最常见的用法有两种(个人认为)

1.与gotoxy一起用

#include
#include
using namespace std;

void gotoxy(int x, int y)//覆盖清屏 
{
	COORD pos = {x,y};
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(hOut, pos);
	return ;
}

int main()
{
    while(1)
    {
    	gotoxy(0,0);
    	printf("脏脏包 YYDS!!!");
    	Sleep(20);
	}
    return 0;
}

是不是就不闪了???

但是像这种不断输出体现不了什么

如果是有改变的2D游戏中使用,效果就会特别好

2.文字游戏必备

曾经看过某些人写过的文字游戏

怎么说呢

不咋地

它一口气把要说的全部说了

就像这样:

#include
#include
using namespace std;

int main()
{
	printf("脏脏包的简介 :\n"); 
    printf("脏脏包是蒟蒻!!\n");
    printf("脏脏包 很懒!!!\n");
    printf("最近专注于教别人写游戏\n");
    printf("码龄一坤年的小黑子!\n");
    return 0;
}

是不是感觉没有一点神秘感?

用Sleep就会不一样:

#include
#include
using namespace std;
#define printf print

void print(string s,int ti)
{
	int l=s.size();
	for(int i=0;i

所以写文字游戏的时候,用这个会不会更高级一点呢????

ヾ( ̄▽ ̄)Bye~Bye~(别忘了点赞、关注!!!)

上一篇:c++游戏小技巧2:kd(类型)_L('ω')┘脏脏包└('ω')」的博客-CSDN博客

下一篇:c++游戏小技巧4:可用鼠标点击的按钮_L('ω')┘脏脏包└('ω')」的博客-CSDN博客

你可能感兴趣的:(游戏小技巧,游戏,小技巧)