gtk snake
本帖最后由 DNFCF 于 2011-05-10 15:01 编辑
- #include<gtk/gtk.h>
- #include<gdk/gdkkeysyms.h>
- #define LEFT 1
- #define UP 2
- #define RIGHT 3
- #define DOWN 4
- #define MAXLENGTH 20
- #define WIDTH 30
- #define HEIGHT 20
- struct _point
- {
- int x;
- int y;
- };
- typedef struct _point point;
- GdkPixbuf *pixbuf=NULL;
- GdkPixbuf *pix1=NULL;
- GdkPixbuf *pix2=NULL;
- GtkWidget *image[WIDTH][HEIGHT];
- gint locate[MAXLENGTH][2];
- gint slength=0;
- gint forward=DOWN;
- gint now_forward=DOWN;
- gint headx;
- gint heady;
- gint tailx;
- gint taily;
- gint beanx;
- gint beany;
- static gint timer=0;
- void init(gint length);
- void bean();
- point rand_point();
- void move();
- void erase();
- void draw();
- void clean();
- point rand_point()
- {
- point p;
- p.x=g_random_int_range(0,WIDTH);
- p.y=g_random_int_range(0,HEIGHT);
- return p;
- }
- int is_bean_right_place(point p)
- {
- int i=0;
- for(;i<slength;i++)
- if(p.x==locate[i][0]&&p.y==locate[i][1])
- return 0;
- return 1;
- }
- void bean()
- {
- point p=rand_point();
- while(!is_bean_right_place(p)) p=rand_point();
- gtk_image_set_from_pixbuf(GTK_IMAGE(image[p.x][p.y]),pix2);
- beanx=p.x;
- beany=p.y;
- }
- void erase()
- {
- g_print("enter erase....\n");
- gtk_image_set_from_pixbuf(GTK_IMAGE(image[tailx][taily]),pixbuf);
- g_print("out erase....\n");
- }
- void draw()
- {
- g_print("enter draw....\n");
- gtk_image_set_from_pixbuf(GTK_IMAGE(image[headx][heady]),pix1);
- g_print("out draw....\n");
- }
- void init(gint length)
- {
- int i=0;
- slength=length;
- for(i;i<length;i++)
- {
- locate[i][0]=5;
- locate[i][1]=i;
- gtk_image_set_from_pixbuf(GTK_IMAGE(image[5][i]),pix1);
- }
- }
- void move()
- {
- gint len=slength-1;
- headx=locate[len][0];
- heady=locate[len][1];
- tailx=locate[0][0];
- taily=locate[0][1];
- erase();
- switch(forward)
- {
- case UP:
- heady--;
- if(heady==-1) heady=HEIGHT-1;
- break;
- case DOWN:
- heady++;
- if(heady==HEIGHT) heady=0;
- break;
- case LEFT:
- headx--;
- if(headx==-1) headx=WIDTH-1;
- break;
- case RIGHT:
- headx++;
- if(headx==WIDTH) headx=0;
- break;
- }//switch
- int i=0;
- for(;i<slength;i++)
- {
- locate[i][0]=locate[i+1][0];
- locate[i][1]=locate[i+1][1];
- }
- locate[len][0]=headx;
- locate[len][1]=heady;
- draw();
- if(headx==beanx&&heady==beany)
- {
- }
- }
- void game_run()
- {
- move();
- }
- void start_button_clicked(GtkWidget *widget,gpointer data)
- {
- timer=gtk_timeout_add(50,(GtkFunction)game_run,NULL);
- }
- void stop_button_clicked(GtkWidget *widget ,gpointer data)
- {
- gtk_timeout_remove(timer);
- }
- void key_press(GtkWidget *widget ,GdkEventKey *event,gpointer data)
- {
- switch(event->keyval)
- {
- case GDK_Up:
- if(forward!=DOWN)
- forward=now_forward=UP;
- break;
- case GDK_Down:
- if(forward!=UP)
- forward=now_forward=DOWN;
- break;
- case GDK_Left:
- if(forward!=RIGHT)
- forward=now_forward=LEFT;
- break;
- case GDK_Right:
- if(forward!=LEFT)
- forward=now_forward=RIGHT;
- break;
- }//switch
- }
- int main(int argc,char **argv)
- {
- GtkWidget *window;
- GtkWidget *vbox;
- GtkWidget *start_button;
- GtkWidget *stop_button;
- GtkWidget *sep;
- GtkWidget *bbox;
- GtkWidget *table;
- gtk_init(&argc,&argv);
- window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
- gtk_window_set_default_size(GTK_WINDOW(window),600,500);
- gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER);
- gtk_container_set_border_width(GTK_CONTAINER(window),5);
- vbox=gtk_vbox_new(FALSE,0);
- gtk_container_add(GTK_CONTAINER(window),vbox);
- pixbuf=gdk_pixbuf_new_from_file("white20.png",NULL);
- pix1=gdk_pixbuf_new_from_file("snake.png",NULL);
- pix2=gdk_pixbuf_new_from_file("bean.png",NULL);
- table=gtk_table_new(WIDTH,HEIGHT,FALSE);
- int i,j;
- for(i=0;i<WIDTH;i++)
- for(j=0;j<HEIGHT;j++)
- {
- image[i][j]=gtk_image_new_from_pixbuf(pixbuf);
- gtk_widget_set_size_request(image[i][j],20,20);
- gtk_table_attach(GTK_TABLE(table),
- image[i][j],
- i,i+1,
- j,j+1,
- GTK_FILL|GTK_EXPAND,
- GTK_FILL|GTK_EXPAND,
- 0,0);
- }
- gtk_box_pack_start(GTK_BOX(vbox),table,TRUE,TRUE,0);
- sep=gtk_hseparator_new();
- gtk_box_pack_start(GTK_BOX(vbox),sep,TRUE,TRUE,5);
- bbox=gtk_hbutton_box_new();
- gtk_box_pack_start(GTK_BOX(vbox),bbox,FALSE,FALSE,0);
- GtkWidget *label=gtk_label_new("snake length : 1");
- gtk_box_pack_start(GTK_BOX(bbox),label,FALSE,FALSE,0);
- start_button=gtk_button_new_with_label("Start");
- gtk_box_pack_start(GTK_BOX(bbox),start_button,FALSE,FALSE,0);
- stop_button=gtk_button_new_with_label("Stop");
- gtk_box_pack_start(GTK_BOX(bbox),stop_button,FALSE,FALSE,0);
- g_signal_connect(start_button,"clicked",G_CALLBACK(start_button_clicked),NULL);
- g_signal_connect(stop_button,"clicked",G_CALLBACK(stop_button_clicked),NULL);
- g_signal_connect(G_OBJECT(window),"key_press_event",G_CALLBACK(key_press),NULL);
- g_signal_connect_swapped(G_OBJECT(window),"destroy",G_CALLBACK(gtk_main_quit),NULL);
- init(5);
- bean();
- gtk_widget_show_all(window);
- gtk_main();
- return FALSE;
- }
复制代码模仿写了一个snake小游戏,可是每次它不会自动的走,只有每次按下按钮”start“时候,它才会走,而且每次走一两步就停了,
找了好久都没找到错误,求高手指点下。。。。。。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
给自己顶一下、。。。。。。。。。。