怎么样将两个运动的“点”显示不同的颜色

C语言 码拜 8年前 (2016-05-18) 1137次浏览
本人知道改变控制台颜色的是SetConsoleTextAttribute函数,但是这个是改变从这个代码开始之后的全部颜色。
那么本人该怎么样做才能使两个不断改变位置的点的颜色不一样呢?
下面是本人本人写的代码,本人不知道该怎么样添加SetConsoleTextAttribute函数,使一个点显示红色另一个点显示其他颜色(例如绿色)

#include<stdio.h>
#include<windows.h>
#include<process.h>
#define T 10
void HideCursor()
{
	CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };//后面为0,则表示看不见光标
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void gotoxy(int x, int y)
{
	COORD c;
	c.X = x; c.Y = y;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}
void createaball()
{
	int a = 2, b = 0, x = 0, y = 0;
	printf("%s\n", "X");//貌似不能输入黑色方块,所以用X代替了,黑色方块是ALT+41462
	Sleep(T);
	system("cls");//清屏函数指令
loop:
	for (b = 1; b <= 56; b++)
	{
		if (b <28)
		{
			y = b;
		}
		else               //相当于Y方向的折回
			y = 56 - b;
		if (a == 236)      //清零计数器a
		{
			a = 0;
		}
		if (a < 118)
		{
			x = a;
		}
		else				//相当于X方向的折回
			x = 236 - a;
		gotoxy(x, y);
		printf("%s\n", "X");//貌似不能输入黑色方块,所以用X代替了,黑色方块是ALT+41462
		Sleep(T);
		system("cls");//清屏函数指令
		a = a + 2;
	}
goto loop;
}
void f1(void* p)
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);  // 获取控制台句柄
	SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED);
	createaball();
}
void f2(void* p)
{
	Sleep(5000);
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);  // 获取控制台句柄
	SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_GREEN);
	createaball();
}
int main()
{
	SetConsoleTitle(L"printf()动画");
	HideCursor();
	_beginthread(f1, 0, 0);
	_beginthread(f2, 0, 0);
	HANDLE hEvent;
	hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
	WaitForSingleObject(hEvent, INFINITE);
}
解决方案

30

#include "stdafx.h"
#include "colorConsole.h"
/*
HANDLE initiate()
{
	HANDLE hOutput; 
	hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	return hOutput;
}
*/
BOOL textout(HANDLE hOutput,int x,int y,WORD wColors[],int nColors,LPTSTR lpszString)
{
    DWORD cWritten; 
    BOOL fSuccess; 
    COORD coord; 
   
	coord.X = x;              // 输出第一个字符所在的列即X坐标 
    coord.Y = y;              //   所在的行即Y坐标      
    fSuccess = WriteConsoleOutputCharacter(		//输出字符串
        hOutput,              // 屏幕缓冲区的句柄
        lpszString,           // 指向字符串的指针 
        lstrlen(lpszString),  // 字符串长度
        coord,                // 输出第一个字符的坐标
        &cWritten);           // 实际输出字符的个数
    if (! fSuccess) 
        cout<<"error:WriteConsoleOutputCharacter"<<endl;
    
	    
	for (;fSuccess && coord.X < lstrlenA((LPCSTR)lpszString)+x; coord.X += nColors) 
    {
        fSuccess = WriteConsoleOutputAttribute(		//为已输出的字符串上色
            hOutput,          // 屏幕缓冲区的句柄
            wColors,          // 颜色数组
            nColors,          // 颜色数目
            coord,            // 输出第一个字符的坐标
            &cWritten);       // 实际输出字符的个数
    }
    if (! fSuccess) 
    	cout<<"error:WriteConsoleOutputAttribute"<<endl;
	return 0;
}

10

仅供参考:

// processor: x86
#include <windows.h>
#include <process.h>    /* _beginthread, _endthread */
#include <stddef.h>
#include <stdlib.h>
#include <conio.h>
void Bounce( void *ch );
void CheckKey( void *dummy );
/* GetRandom returns a random integer between min and max. */
#define GetRandom( min, max ) ((rand() % (int)(((max) + 1) - (min))) + (min))
BOOL repeat = TRUE;     /* Global repeat flag and video variable */
HANDLE hStdOut;         /* Handle for console window */
CONSOLE_SCREEN_BUFFER_INFO csbi;    /* Console information structure */
int main()
{
    CHAR    ch = "A";
    hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
    /* Get display screen"s text row and column information. */
   GetConsoleScreenBufferInfo( hStdOut, &csbi );
    /* Launch CheckKey thread to check for terminating keystroke. */
    _beginthread( CheckKey, 0, NULL );
    /* Loop until CheckKey terminates program. */
    while( repeat )
    {
        /* On first loops, launch character threads. */
        _beginthread( Bounce, 0, (void *) (ch++)  );
        /* Wait one second between loops. */
        Sleep( 1000L );
    }
    return 0;
}
/* CheckKey - Thread to wait for a keystroke, then clear repeat flag. */
void CheckKey( void *dummy )
{
    _getch();
    repeat = 0;    /* _endthread implied */
}
/* Bounce - Thread to create and and control a colored letter that moves
 * around on the screen.
 *
 * Params: ch - the letter to be moved
 */
void Bounce( void *ch )
{
    /* Generate letter and color attribute from thread argument. */
    char    blankcell = 0x20;
    char    blockcell = (char) ch;
    BOOL    first = TRUE;
   COORD   oldcoord, newcoord;
   DWORD   result;
    /* Seed random number generator and get initial location. */
    srand( _threadid );
    newcoord.X = GetRandom( 0, csbi.dwSize.X - 1 );
    newcoord.Y = GetRandom( 0, csbi.dwSize.Y - 1 );
    while( repeat )
    {
        /* Pause between loops. */
        Sleep( 100L );
        /* Blank out our old position on the screen, and draw new letter. */
        if( first )
            first = FALSE;
        else
         WriteConsoleOutputCharacter( hStdOut, &blankcell, 1, oldcoord, &result );
         WriteConsoleOutputCharacter( hStdOut, &blockcell, 1, newcoord, &result );
        /* Increment the coordinate for next placement of the block. */
        oldcoord.X = newcoord.X;
        oldcoord.Y = newcoord.Y;
        newcoord.X += GetRandom( -1, 1 );
        newcoord.Y += GetRandom( -1, 1 );
        /* Correct placement (and beep) if about to go off the screen. */
        if( newcoord.X < 0 )
            newcoord.X = 1;
        else if( newcoord.X == csbi.dwSize.X )
            newcoord.X = csbi.dwSize.X - 2;
        else if( newcoord.Y < 0 )
            newcoord.Y = 1;
        else if( newcoord.Y == csbi.dwSize.Y )
            newcoord.Y = csbi.dwSize.Y - 2;
        /* If not at a screen border, continue, otherwise beep. */
        else
            continue;
        Beep( ((char) ch - "A") * 1000, 500 );
    }
    /* _endthread given to terminate */
    _endthread();
}

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明怎么样将两个运动的“点”显示不同的颜色
喜欢 (0)
[1034331897@qq.com]
分享 (0)