加点逻辑

This commit is contained in:
iridiumR 2022-05-07 14:25:22 +08:00
parent ede662b491
commit 4aae506847
2 changed files with 85 additions and 10 deletions

View file

@ -1,8 +1,82 @@
#include "snake.h" #include "snake.h"
Body head[40]={{0,0},0,-1}; static Body snake[40] = {{0, 0}, 0};
static unsigned char len = 0;
void snake_init() 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();
} }

View file

@ -2,10 +2,11 @@
#include <stdio.h> #include <stdio.h>
#define bool char #define bool char
typedef enum #define MOVE_STATE char
{ #define R 0
N=0,U, D, L, R #define U 1
} MOVE_STATE; #define L 2
#define D 3
struct DOT struct DOT
@ -13,14 +14,14 @@ struct DOT
unsigned char x; unsigned char x;
unsigned char y; unsigned char y;
} ; } ;
typedef struct DOT Dot;
struct BODY struct BODY
{ {
dot d; Dot d;
MOVE_STATE ms; MOVE_STATE m;
char id;
}; };
typedef struct BODY Body; typedef struct BODY Body;
typedef struct DOT Dot;