使用C语言以及gtk编程设计一个贪吃蛇游戏。
功能需求:
(1)窗口界面设计:创建各种窗口和按钮标签等;
(2)小蛇移动控制:按键盘上的上下左右键控制小蛇上下左右移动;
(3)游戏进程控制:若小蛇碰到界面中的豆子,则界面中豆子消失,小蛇变长;
(4)死亡判断:小蛇撞墙或撞到自己身体时即死亡,小蛇进入传送点时从另一特定。
main函数:
#include <stdio.h>
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <stdlib.h>
#define length 50
#define side 24
GtkWidget *window;
GtkWidget *fixed;
GtkWidget *snake[length];
GtkWidget *food[length];
GtkWidget *border_up;
GtkWidget *border_down;
GtkWidget *border_left;
GtkWidget *border_right;
GtkWidget *game_pause;
GtkWidget *game_speed_label;
GtkWidget *game_speed[4];
GtkWidget *death;
GtkWidget *win;
GdkColor color[7];
gint function(gpointer data);
void up(void);
void down(void);
void left(void);
void right(void);
gboolean key_control(GtkWidget *widget,GdkEventKey *event);
void show_pause(void);
void speed_control0(void);
void speed_control1(void);
void speed_control2(void);
void eat(void);
void showwin(void);
void showdeath(void);
gpointer data;
gint sign;
int i=0;
int j=0;
int h=0;
int k=0;
int snake_x[length],snake_y[length],food_x[length],food_y[length];
int flag=3;
int jieshu=6;
int stop=1;
int speed=0;
int op_speed[3]={100,300,500};
char dir[4][4]={"上","下","左","右"};
int main(int argc,char *argv[ ])
{
gtk_init(&argc,&argv);
window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
gdk_color_parse ("#00F0F0", &color[0]);//天蓝色
gdk_color_parse ("#F00000", &color[1]);//红色
gdk_color_parse ("#0F0000", &color[2]);//黑色
gdk_color_parse ("#00F000", &color[3]);//绿色
gdk_color_parse ("#F0F000", &color[4]);//黄色
gdk_color_parse ("#F0F0F0", &color[5]);//无色
gdk_color_parse ("#F0F0FF", &color[6]);//银白色1
gtk_window_set_title(GTK_WINDOW(window),"Snake");
gtk_widget_set_usize(window,580,480);
gtk_widget_set_uposition(window,400,100);
gtk_widget_modify_bg(window,GTK_STATE_NORMAL,&color[0]);
g_signal_connect(G_OBJECT(window),"destroy",G_CALLBACK(gtk_main_quit),NULL);
gtk_window_set_resizable(GTK_WINDOW(window),FALSE);
fixed=gtk_fixed_new();