This repository has been archived on 2024-01-06. You can view files and clone it, but cannot push or open issues or pull requests.
justhomework/MCU/Final_Design/snake.c
2022-05-07 14:25:22 +08:00

82 lines
No EOL
976 B
C

#include "snake.h"
static Body snake[40] = {{0, 0}, 0};
static unsigned char len = 0;
void snake_init()
{
snake[len].d.x = 63;
snake[len].d.y = 31;
snake[len].m = R;
len++;
}
void body_move(Body b)
{
switch(b.m)
{
case R:
b.d.x++;
break;
case L:
b.d.x--;
break;
case U:
b.d.y--;
break;
case D:
b.d.y++;
break;
}
}
void snake_move()
{
int i = 0;
for(; i <= len; i++)
{
body_move(snake[i]);
}
for(i = 1; i <= len; i++)
{
snake[i].m = snake[i - 1].m;
}
}
void body_add()
{
len++
snake[len].m = snake[len - 1].m;
snake[len].d.x = snake[len - 1].d.x;
snake[len].d.x = snake[len - 1].d.x;
switch(snake[len].m)
{
case R:
snake[len].d.x--;
break;
case L:
snake[len].d.x++;
break;
case U:
snake[len].d.y++;
break;
case D:
snake[len].d.y--;
break;
}
}
void test()
{
int i = 0;
for(; i < 15; i++)
body_add();
}