标签:i++ src position type and ios tco .com 完整版
无聊发现自己没写过贪吃蛇,于是做了一个简单的贪吃蛇,并不打算做完整版的(只能看到贪吃蛇不断的从上往下移动)。。
效果(截图软件的问题,会有前一帧的印记):

代码:
#include <iostream>
#include <windows.h>
typedef struct Snake
{
char body[3];
int x, y;
Snake* n_node;
Snake() : body("■"), n_node(nullptr) {}
Snake(int x1, int y1) : body("■"), x(x1), y(y1), n_node(nullptr)
{
}
} SnakeBody;
void SetPosition(short x, short y)
{
HANDLE winHandle;
COORD pos = {x, y};
winHandle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(winHandle, pos);
}
SnakeBody*init(SnakeBody*head)
{
head = new Snake(0, 0);
SnakeBody*s_body = head;
for (int i = 1; i <= 4; i++)
{
SnakeBody* cur_n = new Snake(head->x, head->y + i);
s_body->n_node = cur_n;
s_body = cur_n;
}
return head;
}
void PrintBody(SnakeBody*h)
{
SnakeBody*n = h;
while (n)
{
SetPosition(n->x, n->y);
printf("%s", n->body);
n = n->n_node;
}
}
SnakeBody* move_down(SnakeBody*h)
{
SnakeBody*n = h;
while (n)
{
n->y += 1;
if (n->y == 28)
n->y = 0;
n = n->n_node;
}
return h;
}
void Game(SnakeBody*head)
{
int i = 0;
while(1)
{
PrintBody(head);
SetPosition(0, i);
printf(" ");
head = move_down(head);
i++;
if (i == 28)
i = 0;
Sleep(600);
}
}
int main(int argc, char **argv)
{
SnakeBody*h = nullptr;
h = init(h);
Game(h);
return 0;
}
标签:i++ src position type and ios tco .com 完整版
原文地址:https://www.cnblogs.com/darkchii/p/9510844.html