嗨,有人请帮助我即时尝试使用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++;
}
}
答案 0 :(得分: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
。
建议:请自行追踪。