Witam! Otóż mam problem z napisaniem snake'a. Piszę program w pośpiechu, dlatego przepraszam za jakieś dziwne nieużywane wcale zmienne. Piszę grę- snake w allegro w oparciu o listę jednokierunkową. Problem jest taki, że po zjedzeniu pokarmu wąż nie wydłuża się (mignie na chwilkę dodawany element). Nie proszę o gotowca, bo nie o to chodzi w nauce ;) Tylko o naprowadzenie w czym tkwi błąd (pewnie błędy... ;))
Pozdrawiam!
#include <allegro.h>
//CONFIGURATION
int x1, x2, y1, y2, f1, g1, help;
int res_x=1280, res_y=800;
int head_dir=5; // 0-gora 1-dol 2-prawo 3-lewo
int score=0;
BITMAP * bufor = NULL;
int step=5;
int timer=30;
/////////////////////////////////////////////
void init();
void deinit();
//////////////////////////LISTA JEDNOKIERUNKOWA/////////////////////////////////
typedef struct element
{
int posx;
int posy;
int head_dir;
struct element* next;
} element;
typedef struct list
{
int snake_lenght;
element* first;
} list;
element* End=NULL;
list * wskaznik;
int dlugosc_listy;
int dlugosc_listy2;
int dlugosc_listy3;
void initialization(list * pointer)
{
element* New=NULL;
element* Tmp=NULL;
New=NULL;
End=NULL;
pointer->snake_lenght=1;
pointer->first=NULL;
New=(element*)malloc(sizeof(element));
New->next=NULL;
New->posx=res_x/2;
New->posy=res_y/2;
pointer->first=New;
End=pointer->first;
}
void addelement(list * pointer)
{
element* temp=NULL;
element* Tmp=NULL;
End=NULL;
dlugosc_listy3=0;
Tmp=End=pointer->first;
while(Tmp)
{
End=Tmp;
Tmp=Tmp->next;
dlugosc_listy3++;
}
temp=(element*)malloc(sizeof(element));
switch (End->head_dir) {
case 0 :
temp->posx=End->posx;
temp->posy=(End->posy)+5;
temp->next=NULL;
break;
case 1 :
temp->posx=End->posx;
temp->posy=(End->posy)-5;
temp->next=NULL;
break;
case 2 :
temp->posx=(End->posx)+5;
temp->posy=End->posy;
temp->next=NULL;
break;
case 3 :
temp->posx=(End->posx)-5;
temp->posy=End->posy;
temp->next=NULL;
break;
}
++(pointer->snake_lenght);
if(!End)
{
pointer->first=temp;
}
End->next=temp;
}
list waz;
///////////////////////////////////////////////////////////////////////////
void movesnake(list * pointer)
{
dlugosc_listy=0;
element * temp=NULL;
element * temp2=NULL;
if (key[KEY_UP] && !key[KEY_LEFT] && !key[KEY_RIGHT] && head_dir!=1){
pointer->first->head_dir=0;
}
if (key[KEY_DOWN] && !key[KEY_LEFT] && !key[KEY_RIGHT]&& head_dir!=0){
pointer->first->head_dir=1;
}
if (key[KEY_LEFT] && !key[KEY_UP] && !key[KEY_DOWN] && head_dir!=3){
pointer->first->head_dir=2;
}
if (key[KEY_RIGHT] && !key[KEY_UP] && !key[KEY_DOWN] && head_dir!=2){
pointer->first->head_dir=3;
}
switch (pointer->first->head_dir) {
case 0 :
pointer->first->posy-=step;
break;
case 1 :
pointer->first->posy+=step;
break;
case 2 :
pointer->first->posx-=step;
break;
case 3 :
pointer->first->posx+=step;
break;
}
temp= pointer->first;
while(temp->next!=NULL)
{
temp2=temp-> next;
temp2->posx=temp->posx;
temp2->posy=temp->posy;
temp= temp-> next;
dlugosc_listy++;
}
}
void rysuj(list * pointer)
{
element * temp;
dlugosc_listy2=0;
temp=pointer->first;
do
{
rectfill( bufor, temp->posx ,temp->posy, temp->posx+5, temp->posy+5, makecol( 255, 255, 30 ));
temp= temp-> next;
dlugosc_listy2++;
}while(temp);
}
void food()
{
f1=(rand()%(res_x/step) - 2)*step + 2*step;
g1=(rand()%(res_y/step) - 2)*step + 2*step;
}
int main() {
init();
srand(time(NULL));
bufor = create_bitmap( res_x, res_y );
x1=res_x/2;
x2=res_x/2 +5;
y1=res_y/2;
y2=res_y/2 +5;
wskaznik=&waz;
initialization(&waz);
food();
while (!key[KEY_ESC]) {
//tworzenie nowego jedzenia i wydluzanie weza
if(waz.first->posx==f1 && waz.first->posy==g1)
{
food();
score+=10;
addelement(&waz);
}
textprintf_ex( bufor, font, 20, 20, makecol( 255, 255, 255 ),-1, "Score: %d X:%d Y:%d %d %d %d", score, End->posx, End->posy, dlugosc_listy, dlugosc_listy2, dlugosc_listy3);
//rysowanie jedzenia
rectfill( bufor, f1, g1, f1+5 , g1+5, makecol( 255, 0, 0 ));
//rysowanie ramki
rect(bufor, 0, 0, res_x-1, res_y-1, makecol(192,192,192));
rysuj(&waz);
movesnake(&waz);
///////////////////////////////////////////////////////////////////////
if(key[KEY_T] && timer>=10) timer--;
if(key[KEY_R]) timer+=100;
//zmiana położenia
rest(timer);
blit( bufor, screen, 0, 0, 0, 0, res_x, res_y );
clear_to_color( bufor, makecol( 0, 0, 0 ) );
//warunek kontynuacji- sciana
if(waz.first->posx<=-5 || waz.first->posx>=res_x+5 || waz.first->posy>=res_y+5 || waz.first->posy<=-5) {rest(500); break;}
}
deinit();
return 0;
}
END_OF_MAIN()
void init() {
int depth, res;
allegro_init();
depth = desktop_color_depth();
if (depth == 0) depth = 32;
set_color_depth(depth);
res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, res_x, res_y, 0, 0);
if (res != 0) {
allegro_message(allegro_error);
exit(-1);
}
install_timer();
install_keyboard();
install_mouse();
/* add other initializations here */
}
void deinit() {
clear_keybuf();
/* add other deinitializations here */
}