有个贪吃蛇的程序
[local]1[/local][/code]要求用链表的方式来编写。问题主要出在snail.c这个文件中,初学数据结构不知道问题在什么地方,请高手帮忙指正下。
#include <stdlib.h> #include <malloc.h> #include <stdio.h> #include "game.h" #include "snail.h" int isStart = 0; typedef struct tagPoint{ int x; int y; struct tagPoint *next; }Lnode,*LinkList; Lnode snail; int dx, dy; Lnode food; int score; Lnode * creatlinklist() //创建一个带头结点的空链表。本函数返回一个Lnode型指针变量 { int temp; Lnode *L; //Lnode型指针变量L L=(Lnode *)malloc(sizeof(Lnode)); //创建头结点 L->next=NULL; return L; //返回头结点的值以便于对该链表进行其他的操作 } void drawsnail(Lnode *L)//画出当前链表中存储的蛇的信息 { Lnode *p; p=L; while (p!=NULL) { DrawRect(p->x,p->y,0,255,255);//Drawrect函数用来在屏幕上用矩形显示蛇。后面是RGB分量 p=p->next; } } Lnode * step1(Lnode *L)//吃到食物表头加一,表尾不变 { Lnode *p,*n; p=L; n=(Lnode *)malloc(sizeof(Lnode)); n->x=food.x; n->y=food.y; n->next=p; drawsnail(n); return n; } Lnode * step2(Lnode *L)//没有吃到食物,表头加一同时表尾删除一个节点 { Lnode *p,*n; p=L; n=(Lnode *)malloc(sizeof(Lnode)); n->x=snail.x; n->y=snail.y; n->next=p;//表头加一 while (p->next!=NULL) { p=p->next; } p=NULL; drawsnail(n); return n; } void GameStart(){ isStart = 1; dx = 1; dy = 0; food.x = rand() % GRID_X; food.y = rand() % GRID_Y; } void GameUpdate(){ Lnode *L; L=creatlinklist(); if (isStart){ // 游戏在运行中 snail.x = (snail.x + dx + GRID_X) % GRID_X; snail.y = (snail.y + dy + GRID_Y) % GRID_Y; if (snail.x == food.x && snail.y == food.y) { // 吃到了 L=step1(L); score += 10; //加分 UpdateScore(score); // 重新产生food位置 food.x = rand() % GRID_X; food.y = rand() % GRID_Y; } else {//没有吃到 L=step2(L); } if (score == 100){ // 结束游戏 isStart = 0; //这个必须先,否则。。。。哈哈 EndShowScore(score); } }else{ // 游戏处于暂停中,或者未开始 snail.x = 10; snail.y = 10; score = 0; UpdateScore(score); } // 绘图 // DrawRect(snail.x,snail.y,0,255,255); DrawRect(food.x,food.y,255,0,0); } void GameKeyPress(int key){ switch(key){ case KEY_UP: dx = 0; dy = -1; break; case KEY_DOWN: dx = 0; dy = 1; break; case KEY_LEFT: dx = -1; dy = 0; break; case KEY_RIGHT: dx = 1; dy = 0; break; } } void GameOver(){ }