模式上的闪烁元素 在我的考试中,关于C++中的元素闪烁问题
问题是 打印“C++中的星图和奇数行中的偶数位置必须闪烁”< /P> 我已经有了星型的代码,也可以识别所需位置上的元素,但我不知道如何使它们闪烁模式上的闪烁元素 在我的考试中,关于C++中的元素闪烁问题,c++,C++,问题是 打印“C++中的星图和奇数行中的偶数位置必须闪烁”< /P> 我已经有了星型的代码,也可以识别所需位置上的元素,但我不知道如何使它们闪烁 #include<iostream> using std::cin; using std::cout; using std::endl; void oddline(int* i) { for(int j=0;j<=i;j++) { if(j%2==0) { cout<<"*
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
void oddline(int* i)
{
for(int j=0;j<=i;j++)
{
if(j%2==0)
{ cout<<"*"; }
else{
cout<<"*";//These are the positions which are required to blink.
}
}
int main()
{
for(int i=0;i<10;i++)
{
if(i%2==0)
oddline(&i);
else
for(int j=0;j<=i;j++)
{ cout<<"*";
}
cout<<endl;
}
}
}
#包括
使用std::cin;
使用std::cout;
使用std::endl;
无效oddline(int*i)
{
对于(int j=0;j您需要指定打印星星的每一行的屏幕坐标。使用计时器清除并重新打印该行,以获得所需的闪烁效果
包括windows.h
并使用SetConsoleCursorPosition()
设置文本的x-y坐标。这仅适用于windows平台
在VC++中尝试以下操作:
#include<chrono>
#include<thread>
#include<iostream>
#include <windows.h>
void gotoxy(int x, int y)
{
COORD coordinate;
coordinate.X = x;
coordinate.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coordinate);
}
void oddline(int i, bool clear)
{
for (int j = 0; j <= i; j++)
{
if (j % 2 == 0) {
std::cout << "*";
}
else {
std::cout << (clear? " ": "X");
}
}
}
void printStars(bool clear = false)
{
gotoxy(0, 5);
for (int i = 0; i < 10; i++){
if (i % 2 == 0) {
oddline(i,clear);
}
else
for (int j = 0; j <= i; j++)
{
std::cout << "*";
}
std::cout << std::endl;
}
}
int main()
{
int count = 0;
while (count < 50)
{
printStars(); // redraw all stars ( draw only target starts for perf)
std::this_thread::sleep_for(std::chrono::milliseconds(300));
printStars(true); // erase target stars
std::this_thread::sleep_for(std::chrono::milliseconds(300));
count ++;
}
return 0;
}
#包括
#包括
#包括
#包括
void gotoxy(整数x,整数y)
{
坐标;
坐标X=X;
坐标Y=Y;
设置控制台或位置(GetStdHandle(标准输出句柄),坐标);
}
无效oddline(内部i,布尔清除)
{
对于(int j=0;j清除屏幕并用空间而不是“星形”重写输出)?对于屏幕清除部分,它不是C++的一部分,必须使用OS特定的功能(函数或终端控制代码)。我假设“闪烁”意味着您不应该渲染任何可见的内容(如<代码>”<代码> >。所以它在存在中闪烁,在不存在中闪烁这意味着正常的闪烁,就像它经常出现和消失在什么平台上?mac、windows linux等等。因为我们在大学的实验室里使用windows,它可能是windows。我们知道OP在windows上吗?我对chrono或thread不太了解,所以我现在正在研究它们,以便更好地理解它。请编辑:这一行只是为了睡觉对于300ms。您可以用嵌套的for循环替换它,以便处理延迟相同的量。
void delay()
{
int m = 1000; //adjust
int n = 3200; //adjust
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
// do nothing
}
}
}