こんにちは、誰か助けてください。アレグロ (ubuntu) でスネーク ゲームをコーディングしようとしています。このコードを書くことはできましたが、うまくいかず、コンパイルしても問題が見つからないようです。ある種のメモリの問題を実行する (メモリ マップ) が表示され、続けて何度も実行しても何も起こらず、開始しても数分後にクラッシュする
#include <allegro.h>
#include <stdlib.h>
#include <stdio.h>
struct snake_node
{
int dir;
int x;
int y;
struct snake_node * next;
}snake_node;
typedef struct snake_node * snake;
snake ajout_debut(snake snake,int x, int y);
void game_over(BITMAP * buffer,int score);
void draw_map(BITMAP * buffer);
void draw_menu(BITMAP * buffer);
void generate_new_apple( BITMAP * buffer,int x,int);
void move(BITMAP* buffer,snake snake,int *score);
int main()
{
BITMAP* sprite1 ;
int score=0;
snake snake;
allegro_init();
install_keyboard();
set_color_depth(16);
if(set_gfx_mode(GFX_AUTODETECT,1366,768,0,0)!=0)
{
set_gfx_mode(GFX_TEXT,0,0,0,0);
allegro_message("impossible %s \n",allegro_error);
return -1;
}
sprite1 = load_bitmap("sprite.bmp", NULL) ;
if(!sprite1)
{
set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
allegro_message("Erreur ! Impossible de lire le fichier image !");
return 1;
}
BITMAP * buffer=create_bitmap(1366,768);
draw_sprite(buffer, sprite1,0,0);
blit(buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H);
sleep(1);
while(!key[KEY_ESC])
{
clear_bitmap(buffer);
draw_menu(buffer);
if(key[KEY_ENTER])
{
clear_bitmap(buffer);
ajout_debut(snake,500,250);
while(!key[KEY_ESC])
{
draw_map(buffer);
move(buffer,snake,&score);
blit(buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H);
}
}
}
destroy_bitmap(buffer);
allegro_exit();
return 0;
}
END_OF_MAIN()
void draw_menu(BITMAP * buffer)
{
textprintf(buffer,font,600,220,makecol(255,255,0)," ****** ");
textprintf(buffer,font,600,250,makecol(255,255,0),"*** START GAME ***");
textprintf(buffer,font,600,270,makecol(255,100,0)," (press ENTER) ");
textprintf(buffer,font,600,300,makecol(255,255,0),"*** HELP ***");
textprintf(buffer,font,600,320,makecol(255,100,0)," (press H) ");
textprintf(buffer,font,600,350,makecol(255,255,0),"*** QUIT GAME ***");
textprintf(buffer,font,600,370,makecol(255,100,0)," (press ESC) ");
textprintf(buffer,font,600,400,makecol(255,255,0)," ****** ");
//blit(buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H);
}
void draw_map(BITMAP * buffer)
{
rectfill(buffer,0,0,1366,10,makecol(255,0,0));
rectfill(buffer,1366,0,1356,768,makecol(255,0,0));
rectfill(buffer,1366,768,0,758,makecol(255,0,0));
rectfill(buffer,0,0,10,768,makecol(255,0,0));
//blit(buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H);
}
void generate_new_apple( BITMAP * buffer,int x,int y)
{
rectfill(buffer,x,y,x+10,y+10,makecol(255,255,0));
}
void game_over(BITMAP * buffer,int score)
{
clear_bitmap(buffer);
textprintf(buffer,font,SCREEN_W/2,SCREEN_H/2,makecol(255,255,0), "*** GAME OVER ***");
textprintf(buffer,font,SCREEN_W/2,SCREEN_H/2+30,makecol(255,255,0),"*** SCORE :%d ***",score);
}
snake ajout_debut(snake s,int x, int y)
{
snake nouv;
nouv=(snake)malloc(sizeof(snake));
nouv->x=x+10;
nouv->y=y+10;
nouv->next=s;
s=nouv;
return s;
}
void move(BITMAP* buffer,snake snake,int *score)
{
int applex = (int)(rand()%1336+10);
int appley = (int)(rand()%738+10);
generate_new_apple(buffer,applex,appley);
clear_keybuf();
if (key[KEY_LEFT])
{
while(snake->next!=NULL)
{
if(snake->x > 10)
snake->x=snake->x-1;
else
game_over(buffer,score);
}
}
if(key[KEY_RIGHT])
{
while(snake->next!=NULL)
{
if(snake->x < 1356)
snake->x=snake->x+1;
else
game_over(buffer,score);
}
}
if(key[KEY_UP])
{
while(snake->next!=NULL)
{
if(snake->y > 10)
snake->y=snake->y-1;
else
snake->y=snake->y+1;
}
}
if(key[KEY_DOWN])
{
while(snake->next!=NULL)
{
if(snake->y < 758)
snake->y=snake->y+1;
else
game_over(buffer,score);
}
}
if(snake->x==applex && snake->y==appley)
{
rectfill(buffer,applex,appley,applex+10,appley+10,makecol(0,0,0));
applex = (int)(rand()%1336+10);
appley = (int)(rand()%738+10);
generate_new_apple(buffer,applex,appley);
snake=ajout_debut(snake,snake->x,snake->y);
*score++;
}
}