c++双缓存技术实现控制台不闪屏
贪吃蛇为例
实现双缓存技术实现控制台不闪屏,以上次写的贪吃蛇为例子。
链接:简单的贪吃蛇
话不多说,上代码。
#include<iostream>
#include<Windows.h>
#include<conio.h>
using namespace std;
typedef struct Node
{
int Data[2]; //数据域
struct Node *next; //指针域
}Snake;
Snake * NewSnake(){
//初始化蛇
Snake *head, *p,*tail;
head = (Snake*)malloc(sizeof(Snake));
p = (Snake*)malloc(sizeof(Snake));
tail = (Snake*)malloc(sizeof(Snake));
if (head == NULL || p == NULL || tail == NULL)
return head;
head->Data[0] = 10;
head->Data[1] = 10;
p->Data[0] = 11;
p->Data[1] = 10;
tail->Data[0] = 12;
tail->Data[1] = 10;
head->next = p;
p->next = tail;
tail->next = NULL;
return head;
}
void PrintSnake(Snake *head,int Map[20][20])
{
//载入地图
Snake* p = head;
if (p != NULL){
while (p != NULL)
{
Map[p->Data[0]][p->Data[1]] = 2;
p = p->next;
}
}
}
void Move(Snake * head,bool & GameState, int Map[20][20], int cx, int cy){
//改变方向,刷新数据
Snake *one = head;
Snake *two = (Snake*)malloc(sizeof(Snake));
two->Data[0] = one->Data[0];
two->Data[1] = one->Data[1];
two->next = one->next;
one->Data[0] += cx;
one->Data[1] += cy;
one->next = two;
if (Map[one->Data[0]][one->Data[1]] == 3)
{
//如果吃到豆子,则产生新的豆子
Map[one->Data[0]][one->Data[1]] = 2;
while (true)
{
int Dx = rand() % 20;
int Dy = rand() % 20;
if (