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

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

目录

1.gotoxy

1.无闪清屏

2.移动到指定地点

2.Sleep

1.与gotoxy一起用

2.文字游戏必备

1.gotoxy

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

是不是一般都是

#include<bits/stdc++.h>
#include<windows.h>
using namespace std;

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

发现什么没有

是不是特别闪!

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

SetConsoleCursorPosition(hOut,pos);

可以用来改变光标位置

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

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

我们就可以改变它位置

就有了一下两种用法:

1.无闪清屏

#include<bits/stdc++.h>
#include<windows.h>
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<bits/stdc++.h>
#include<windows.h>
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<bits/stdc++.h>
#include<windows.h>
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更加简洁??(虽然一次感觉有点多,但多次就会写的少些)

(当然,学了window之后可以用别的方法置中)

就是这个:

#include<bits/stdc++.h>
#include<windows.h>
using namespace std;

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

void prin(string s,int X,int Y)
{
	HANDLE hConsole=GetStdHandle(STD_OUTPUT_HANDLE);  
	CONSOLE_SCREEN_BUFFER_INFO bInfo;	
	GetConsoleScreenBufferInfo(hConsole, &bInfo);
	int y=bInfo.dwMaximumWindowSize.Y,x=bInfo.dwMaximumWindowSize.X;
	gotoxy((x-s.size())/2+X,y/2+Y);
	cout<<s;
}

int main()
{
	system("mode con cols=60 lines=25");
	prin("请输出20个字符:\n",0,-1);
	string s="";
	char c;
	prin("",-1,0);
	for(int i=1;i<=20;i++)
	{
		c=cin.get();
		if(c=='\n') {i--;continue;}
		s+=c;
		system("cls");
		int k=20-i;
		string l="";
		if(k==0) l="0";
		while(k)
		{
			l+=char(k%10+'0');
			k/=10;
		}
		reverse(l.begin(),l.end());
		prin("请输出"+l+"个字符",0,-1);
		prin(s,0,0);
	}
    return 0;
}

2.Sleep

Sleep 也是 windows.h 中的一员

用法very very 简单:

Sleep(n) //停顿n毫秒

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

1.与gotoxy一起用

#include<bits/stdc++.h>
#include<windows.h>
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<bits/stdc++.h>
#include<windows.h>
using namespace std;

int main()
{
	printf("脏脏包的简介 :\n"); 
    printf("一个初一的、擅长优化别人的代码(最后发表出去)的、特爱颓废的蒟蒻");
    return 0;
}

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

用Sleep就会不一样:

#include<bits/stdc++.h>
#include<windows.h>
using namespace std;
#define printf print

void print(string s,int ti)
{
	int l=s.size();
	for(int i=0;i<l;i++) cout<<s[i],Sleep(ti);
	return ;
}

int main()
{
	printf("脏脏包的简介 :\n",40); 
    printf("一个初一的、擅长优化别人的代码(最后发表出去)的、特爱颓废的蒟蒻\n",40);
    return 0;
}

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

3.后文

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

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

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

  • 11
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
游戏分里外两个部分组成,里部分(用户不可见) 通过里部分执行判断,地图数组更改,和各种值的改变。更改完里部分再根据相应变化更改表部分。(用户可视部分)表部分的打印通过gotoxy去到相应坐标再printf打印出字符,通过文本函数改变文字字体颜色与文字背景颜色与字符组合实现图形界面。 程序通过 计数器+循环判断 的思想,类似单核cpu的多线程实现(单线程在不同程序/函数间来回执行)省去了多线程。(具体过程在功能设计与描述有详细描述) 另AI实现与加强依赖于rand随机函数的运用,进一步强化AI,增加游戏乐趣 功能方面,游戏参考于80年代任天堂红白机(FC/FamilyComputer)上的游戏坦克大战(Battle City),包括地图,游戏模式等等(当时的游戏直接烧在电路板上)。所以游戏平衡方面已经有了很好的参考,无需再花大量时间测试平衡性。 但诸如地图中的树林元素,随机道具等没有实现。但较之原版,该游戏由C/C++编写PC运行,由字符界面实现游戏画面。原版一辆坦克的子弹未消失之前不能发射第二颗。导致子弹打击远处CD长,近处CD短。该游戏每个子弹都有相同CD,子弹未消失只要CD达到即可发射第二颗,第三颗…增加了真实性,相较于原版是个改进。且考虑到PC性能不一内置了游戏速度调整。玩家可根据PC性能调整至合适的速度。
下面是代码 #include #include #include #include #define Height 25 //迷宫的高度,必须为奇数 #define Width 25 //迷宫的宽度,必须为奇数 #define Wall 1 #define Road 0 #define Start 2 #define End 3 #define Esc 5 #define Up 1 #define Down 2 #define Left 3 #define Right 4 int map[Height+2][Width+2]; void gotoxy(int x,int y) //移动坐标 { COORD coord; coord.X=x; coord.Y=y; SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), coord ); } void hidden()//隐藏光标 { HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO cci; GetConsoleCursorInfo(hOut,&cci;); cci.bVisible=0;//赋1为显示,赋0为隐藏 SetConsoleCursorInfo(hOut,&cci;); } void create(int x,int y) //随机生成迷宫 { int c[4][2]={0,1,1,0,0,-1,-1,0}; //四个方向 int i,j,t; //将方向打乱 for(i=0;i<4;i++) { j=rand()%4; t=c[i][0];c[i][0]=c[j][0];c[j][0]=t; t=c[i][1];c[i][1]=c[j][1];c[j][1]=t; } map[x][y]=Road; for(i=0;i<4;i++) if(map[x+2*c[i][0]][y+2*c[i][1]]==Wall) { map[x+c[i][0]][y+c[i][1]]=Road; create(x+2*c[i][0],y+2*c[i][1]); } } int get_key() //接收按键 { char c; while(c=getch()) { if(c==27) return Esc; //Esc if(c!=-32)continue; c=getch(); if(c==72) return Up; //上 if(c==80) return Down; //下 if(c==75) return Left; //左 if(c==77) return Right; //右 } return 0; } void paint(int x,int y) //画迷宫 { gotoxy(2*y-2,x-1); switch(map[x][y]) { case Start: printf("入");break; //画入口 case End: printf("出");break; //画出口 case Wall: printf("▇");break; //画墙 case Road: printf(" ");break; //画路 } } void game() { int x=2,y=1; //玩家当前位置,刚开始在入口处 int c; //用来接收按键 while(1) { gotoxy(2*y-2,x-1); printf("●"); //画出玩家当前位置 if(map[x][y]==End) //判断是否到达出口 { gotoxy(30,24); printf("到达终点,按任意键结束"); getch(); break; } c=get_key(); if(c==Esc) { gotoxy(0,24); break; } switch(c) { case Up: //向上走 if(map[x-1][y]!=Wall) { paint(x,y); x--; } break; case Down: //向下走 if(map[x+1][y]!=Wall) { paint(x,y); x++; } break; case Left: //向左走 if(map[x][y-1]!=Wall) { paint(x,y); y--; } break; case Right: //向右走 if(map[x][y+1]!=Wall) { paint(x,y); y++; } break; } } } int main() { system("title yourname"); int i,j; srand((unsigned)time(NULL)); //初始化随即种子 hidden(); //隐藏光标 for(i=0;i<=Height+1;i++) for(j=0;j<=Width+1;j++) if(i==0||i==Height+1||j==0||j==Width+1) //初始化迷宫 map[i][j]=Road; else map[i][j]=Wall; create(2*(rand()%(Height/2)+1),2*(rand()%(Width/2)+1)); //从随机一个点开始生成迷宫,该点行列都为偶数 for(i=0;i<=Height+1;i++) //边界处理 { map[i][0]=Wall; map[i][Width+1]=Wall; } for(j=0;j<=Width+1;j++) //边界处理 { map[0][j]=Wall; map[Height+1][j]=Wall; } map[2][1]=Start; //给定入口 map[Height-1][Width]=End; //给定出口 for(i=1;i<=Height;i++) for(j=1;j<=Width;j++) //画出迷宫 paint(i,j); game(); //开始游戏 getch(); return 0; }
好的,以下是一个简单的打飞机小游戏C++代码: ```c++ #include<iostream> #include<Windows.h> #include<conio.h> #include<time.h> using namespace std; const int width = 40; //游戏边框宽度 const int height = 20; //游戏边框高度 int x, y; //飞机位置坐标 int ex, ey; //敌机位置坐标 int bulletx, bullety; //子弹位置坐标 int score; //得分 void gotoxy(int x, int y) //光标定位函数 { COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos); } void drawBorder() //画游戏边框 { for (int i = 0; i < height; i++) { gotoxy(0, i); cout << "|"; gotoxy(width, i); cout << "|"; } for (int i = 0; i < width; i++) { gotoxy(i, 0); cout << "-"; gotoxy(i, height); cout << "-"; } } void drawPlane() //画飞机 { gotoxy(x, y); cout << " /\\ "; gotoxy(x, y + 1); cout << " / \\ "; gotoxy(x, y + 2); cout << "/____\\"; } void drawEnemy() //画敌机 { gotoxy(ex, ey); cout << " /\\ "; gotoxy(ex, ey + 1); cout << " / \\ "; gotoxy(ex, ey + 2); cout << "/____\\"; } void drawBullet() //画子弹 { gotoxy(bulletx, bullety); cout << "|"; } void movePlane() //移动飞机 { if (_kbhit()) { //判断是否有按键 char key = _getch(); //获取按键 switch (key) { case 'a': //向左移动 if (x > 1) { x--; } break; case 'd': //向右移动 if (x < width - 7) { x++; } break; case 'w': //向上移动 if (y > 1) { y--; } break; case 's': //向下移动 if (y < height - 3) { y++; } break; case ' ': //发射子弹 bulletx = x + 2; bullety = y - 1; break; } } } void moveEnemy() //移动敌机 { ey++; if (ey >= height - 2) { //敌机飞出屏幕 ey = 1; ex = rand() % (width - 5) + 1; //重新生成敌机位置 } } void moveBullet() //移动子弹 { if (bullety > 0) { bullety--; } else { //子弹飞出屏幕 bulletx = 0; bullety = 0; } } void checkHit() //检测碰撞 { if (bulletx >= ex && bulletx <= ex + 4 && bullety == ey + 2) { //子弹击中敌机 score += 10; //得分加10分 bulletx = 0; bullety = 0; ex = rand() % (width - 5) + 1; //重新生成敌机位置 ey = 1; } if ((x >= ex && x <= ex + 4 && y >= ey && y <= ey + 2) || (x + 4 >= ex && x + 4 <= ex + 4 && y >= ey && y <= ey + 2)) { //飞机撞上敌机 gotoxy(width / 2 - 4, height / 2); cout << "Game Over!"; gotoxy(width / 2 - 6, height / 2 + 1); cout << "Your Score: " << score; Sleep(3000); exit(0); } } void showScore() //显示得分 { gotoxy(width - 10, height + 1); cout << "Score: " << score; } int main() { srand((unsigned)time(NULL)); //初始化随机数生成器 x = width / 2 - 3; y = height - 4; ex = rand() % (width - 5) + 1; ey = 1; score = 0; while (true) { system("cls"); //清屏 drawBorder(); drawPlane(); drawEnemy(); drawBullet(); movePlane(); moveEnemy(); moveBullet(); checkHit(); showScore(); Sleep(50); //延时50毫秒 } return 0; } ``` 该游戏使用了Windows.h库和conio.h库,需要在Windows系统下运行。运行时,玩家可以使用键盘上的“a”、“d”、“w”、“s”来控制飞机的移动,使用空格键来发射子弹,避免被敌机撞击。游戏中,敌机会不断地从屏幕上方飞过来,玩家需要及时发射子弹来击中敌机,得分才能不断增加。若敌机撞到飞机,游戏结束,显示得分。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值