#include <curses.h>
#include <malloc.h>
#include <pthread.h>
#include <stdlib.h>
#define UP 259
#define DOWN 258
#define RIGHT 260
#define LEFT 261
void tcs();
void cztcs();
void czsw();
void dt();
void scwei();
void cjtou();
void jc1();
void jc2(int *a);
void jc3();
void jc4();
int zbpd(int x, int y);
int swpd(int q, int w);
struct szb {
int x;
int y;
struct szb *next;
};
struct swzb {
int i;
int j;
} sw;
struct szb *begin, *end, *new = NULL, *old = NULL;
int fx;
void scwei() {
old = begin;
begin = begin->next;
free(old);
}
void cjtou() {
new = (struct szb *)malloc(sizeof(struct szb));
switch (fx) {
case UP:
new->x = end->x;
new->y = end->y - 1;
break;
case DOWN:
new->x = end->x;
new->y = end->y + 1;
break;
case RIGHT:
new->x = end->x - 1;
new->y = end->y;
break;
case LEFT:
new->x = end->x + 1;
new->y = end->y;
break;
}
end->next = new;
new->next = NULL;
end = new;
}
void cztcs() {
while (begin != NULL) {
old = begin;
begin = begin->next;
free(old);
}
tcs();
}
void czsw() {
int x=rand()%20;
int y=rand()%20;
while(x==0){
x=rand()%20;
}
while(y==0){
y=rand()%20;
}
sw.i = x;
sw.j = y;
}
void tcs() {
struct szb *p1, *p2;
p1 = p2 = (struct szb *)malloc(sizeof(struct szb));
p1->x = 1;
p1->y = 1;
begin = p1;
int n = 2;
while (n != 0) {
p1 = (struct szb *)malloc(sizeof(struct szb));
p2->next = p1;
p1->x = p2->x + 1;
p1->y = p2->y;
p2 = p1;
--n;
}
p2->next = NULL;
end = p2;
}
void dt() {
int x, y;
for (y = 0; y < 20; y++) {
if (y == 0) {
for (x = 0; x < 20; x++) {
printw("--");
}
printw("\n");
}
if (y > 0 || y < 20) {
for (x = 0; x < 21; x++) {
if (x == 0 || x == 20) {
printw("|");
} else if (zbpd(x, y)) {
printw("[]");
} else if (swpd(x, y)) {
printw("sw");
} else {
printw(" ");
}
}
printw("\n");
}
if (y == 19) {
for (x = 0; x < 20; x++) {
printw("--");
}
printw("\n");
}
}
}
void jc1() {
if (end->x == 0 || end->y == -1 || end->x == 20 || end->y == 20) {
cztcs();
fx = LEFT;
}
}
void jc2(int *a) {
switch (*a) {
case UP:
if (fx == DOWN) {
*a = DOWN;
}
break;
case DOWN:
if (fx == UP) {
*a = UP;
}
break;
case RIGHT:
if (fx == LEFT) {
*a = LEFT;
}
break;
case LEFT:
if (fx == RIGHT) {
*a = RIGHT;
}
break;
default:
break;
}
}
void jc3() {
struct szb *p;
p = begin;
while (p != end) {
if (end->x == p->x && end->y == p->y) {
cztcs();
}
p = p->next;
if(p==NULL){
break;
}
}
}
void jc4() {
if (end->x == sw.i && end->y == sw.j) {
czsw();
cjtou();
}
}
int zbpd(int x, int y) {
struct szb *p;
p = begin;
while (p != NULL) {
if (p->x == x && p->y == y) {
return 1;
}
p = p->next;
}
return 0;
}
int swpd(int q, int w) {
if (sw.i == q && sw.j == w) {
return 1;
}
return 0;
}
void *anjian() {
int a;
while (1) {
a = getch();
jc2(&a);
switch (a) {
case UP:
fx = UP;
break;
case DOWN:
fx = DOWN;
break;
case RIGHT:
fx = RIGHT;
break;
case LEFT:
fx = LEFT;
break;
}
}
}
int main() {
initscr();
keypad(stdscr, 1);
pthread_t th1;
pthread_create(&th1, NULL, anjian, NULL);
tcs();
czsw();
dt();
fx = LEFT;
while (1) {
cjtou();
scwei();
jc1();
jc3();
jc4();
move(0, 0);
dt();
usleep(200000);
refresh();
}
endwin();
return 0;
}
贪吃蛇代码
最新推荐文章于 2024-05-14 12:02:03 发布