时间显示。解决不了!求教!
有没有什么办法可以让时间的显示和图形移动两者之间互不干扰的?////////////2017-10-31////////
# define _CRT_SECURE_NO_WARNINGS // 忽略警告
# include <stdio.h> //输入输出
# include <conio.h> //输入输出 getch
# include <windows.h> //系统API函数
# include <stdlib.h> //动态空间分配
# include <string.h> //字符串操作
# include <io.h> //浏览文件夹
# include <time.h> //时间函数
# include <pthread.h> //多线程编程
typedef struct SNAKE
{
COORD XY;
SNAKE * next , *previous;
} snake;
void CreateCursorPosition (COORD xy)
{
HANDLE hOut = GetStdHandle (STD_OUTPUT_HANDLE);
SetConsoleCursorPosition (hOut , xy);
}
snake* Initsnake(COORD xy)//初始化蛇身
{
snake *tail , *p , *head;
int i;
tail=(snake*)malloc(sizeof(snake));//从蛇尾开始,头插法,以x,y设定开始的位置//
if (!tail)
return 0;
// tail->XY.X =xy.X;
// tail->XY.Y =xy.Y;
tail->XY = xy;
tail->next= tail->previous =NULL;
p = tail;
for (i = 1;i <= 4; i++)
{
head=(snake*)malloc(sizeof(snake));
if (!head)
return 0;
head->next=tail;
tail->previous = head;
head->previous = NULL;
head->XY.X = xy.X + 2*i;
head->XY.Y = xy.Y;
tail=head;
}
head->previous = p;//指向尾节点
while (p != head)
{
CreateCursorPosition (p->XY);
printf ("◆");
p = p->previous;
}
CreateCursorPosition (p->XY);
printf ("◆");
return head;
}
void SnakeMove (snake * head)
{
snake * tail , *p , *t;
tail = t = head->previous;
while (true)
{
p = (snake *) malloc (sizeof (snake));
if (!p)
return ;
p->next = head;
head->previous = p;
p->previous = NULL;
p->XY.X = head->XY.X + 2;
p->XY.Y = head->XY.Y;
head = p;
while (t)
{
CreateCursorPosition (t->XY);
printf ("◆");
t = t->previous;
}
CreateCursorPosition (tail->XY);
printf (" ");
t = tail->previous;
free (tail);
tail = t;
if (GetAsyncKeyState (VK_ESCAPE))
break;
Sleep (300);
}
}
void CleanConsole (COORD xy)
{
HANDLE hOut = GetStdHandle (STD_OUTPUT_HANDLE);
FillConsoleOutputCharacterA (hOut , ' ' , 80 , xy , NULL);
}
void * showtime (void *)
{
struct tm * pt;
time_t t;
while (true)
{
t = time (NULL);
pt = localtime (&t);
COORD xy = {0 , 0};
CreateCursorPosition (xy);
printf ("北京时间:%d:%d:%d" , pt->tm_hour , pt->tm_min , pt->tm_sec);
Sleep (500);
CleanConsole (xy);
}
return 0;
}
void CreateLine (void)
{
pthread_t thid;
pthread_attr_t attr;
pthread_attr_init (&attr);
pthread_attr_setscope (&attr , PTHREAD_SCOPE_PROCESS);
pthread_attr_setdetachstate (&attr , PTHREAD_CREATE_JOINABLE);//PTHREAD_CREATE_DETACHDE
pthread_create (&thid , &attr , showtime , (void*) 0);
// pthread_join (thid , NULL);
pthread_detach (thid);
}
int main (void)
{
snake * head;
COORD xy = {10 , 5};
head = Initsnake(xy);
CreateLine ();
SnakeMove (head);
getch ();
return 0;
}
[此贴子已经被作者于2017-10-31 20:28编辑过]