-5

嗨,有人可以帮助我,我正在尝试用 allegro 编写蛇游戏(在 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++;
}
}  
4

1 回答 1

2

我注意到的一个问题。

查看编译器警告

sanke.c:134:7: warning: passing argument 2 of ‘game_over’ makes integer from pointer without a cast [enabled by default]

你有功能

void game_over(BITMAP * buffer,int score);

你打电话

game_over(buffer,score);

内部函数

void move(BITMAP* buffer,snake snake,int *score)

score因此,您将函数中的 int 指针传递给接受整数的move函数。game_over

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);
        }
    }

while检查snake->next != NULL但它从未在正文中更新,因此这是一个无限循环。

x调试器在崩溃时也显示一个奇怪的负值。

建议:请自行追踪。

于 2012-04-29T21:03:59.127 回答