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:48:25 +08:00

103 lines
No EOL
1.4 KiB
C

#include "snake.h"
static Body snake[40] = {{0, 0}, 0};
static unsigned char len = 0;
void snake_init()
{
// Initialize snake
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++)
{
//All body parts move
body_move(snake[i]);
}
for(i = 1; i <= len; i++)
{
//Sync move direction for next move
snake[i].m = snake[i - 1].m;
}
}
void body_add()
{
// Copy last body part to new body part
len++
snake[len].m = snake[len - 1].m;
snake[len].d.x = snake[len - 1].d.x;
snake[len].d.y = snake[len - 1].d.y;
// Judge the direction of new body part
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 snake_draw()
{
int i=1;
addDot(nake[0].d.x,nake[0].d.y);
addDot(nake[0].d.x+1,nake[0].d.y);
addDot(nake[0].d.x,nake[0].d.y+1);
addDot(nake[0].d.x-1,nake[0].d.y);
addDot(nake[0].d.x,nake[0].d.y-1);
for(;i<=len;i++)
{
addDot(nake[i].d.x,nake[i].d.y);
}
}
void snake_test()
{
//Add 14 Body to test
int i = 0;
for(; i < 15; i++)
body_add();
}