加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
mywork.c 3.21 KB
一键复制 编辑 原始数据 按行查看 历史
莫光清 提交于 2021-06-15 11:32 . s
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
enum _FORWARD {LEFT, UP, RIGHT, DOWN };
typedef enum _FORWARD Forward;
void move(Forward fw);
void key_press(GtkWidget* widget,GdkEventKey *event,gpointer data);
static gchar* snack_file[4] = {"u.bmp","r.bmp","d.bmp","l.bmp"};
static GtkWidget *fixed;
static GtkWidget *snack_image;
gint i =75;
gint j =75;
Forward forward =UP;
void move (Forward fw){
switch(fw){
case UP:
j=j-5;
if(j<0)j=400;
gtk_image_set_from_file(GTK_IMAGE(snack_image),snack_file[0]);
gtk_fixed_move(GTK_FIXED(fixed),snack_image,i,j);
break;
case DOWN:
j = j + 5;
if ( j > 400 ) j = 0 ;
gtk_image_set_from_file(GTK_IMAGE(snack_image),snack_file[2]);
gtk_fixed_move(GTK_FIXED(fixed),snack_image,i,j);
break;
case LEFT :
i = i - 5;
if ( i < 0 ) i = 400 ;
gtk_image_set_from_file(GTK_IMAGE(snack_image),snack_file[3]);
gtk_fixed_move(GTK_FIXED(fixed),snack_image,i,j);
break;
case RIGHT :
i = i + 5;
if ( i > 400 ) i = 0 ;
gtk_image_set_from_file(GTK_IMAGE(snack_image),snack_file[1]);
gtk_fixed_move(GTK_FIXED(fixed),snack_image,i,j);
break;
}
}
void key_press (GtkWidget* widget,GdkEventKey *event,gpointer data)
{
switch(event->keyval)
{
case GDK_Up :
forward = UP;
move(forward);
break;
case GDK_Down :
forward = DOWN;
move(forward);
break;
case GDK_Left :
forward = LEFT;
move(forward);
break;
case GDK_Right :
forward = RIGHT;
move(forward);
break;
}
}
int main(int argc,char *argv[]){
GtkWidget* window;
GtkWidget* vbox;
GtkWidget* bbox;
GtkWidget* sep;
GtkWidget* frame;
GtkWidget* button;
GtkWidget*
gtk_init(&argc,&argv);
window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window),"snack");
gtk_container_set_border_width(GTK_CONTAINER(window),10);
g_signal_connect(G_OBJECT(window),"destroy",
G_CALLBACK(gtk_main_quit),NULL);
g_signal_connect(G_OBJECT(window),"key_press_event",
G_CALLBACK(key_press),NULL);//keyboard input
vbox = gtk_vbox_new(FALSE,0);
gtk_container_add(GTK_CONTAINER(window),vbox);
frame=gtk_frame_new(NULL);
gtk_frame_set_shadow_type(GTK_FRAME(frame),GTK_SHADOW_ETCHED_OUT);
fixed=gtk_fixed_new();
gtk_fixed_set_has_window(GTK_FIXED(fixed),TRUE);
gtk_widget_set_size_request(fixed,400,400);
gtk_container_add(GTK_CONTAINER(frame),fixed);
gtk_box_pack_start(GTK_BOX(vbox),frame,TRUE,TRUE,5);
sep = gtk_hseparator_new();
gtk_box_pack_start(GTK_BOX(vbox),sep,FALSE,FALSE,5);
bbox = gtk_hbutton_box_new();
gtk_button_box_set_layout(GTK_BUTTON_BOX(bbox),GTK_BUTTONBOX_END);
gtk_box_pack_start(GTK_BOX(vbox),bbox,FALSE,FALSE,5);
button = gtk_button_new_from_stock(GTK_STOCK_QUIT);
g_signal_connect(G_OBJECT(button),"clicked",
G_CALLBACK(gtk_main_quit),NULL);
gtk_box_pack_end(GTK_BOX(bbox),button,FALSE,FALSE,5);
snack_image = gtk_image_new_from_file(snack_file[0]);
gtk_fixed_put(GTK_FIXED(fixed),snack_image,i,j);
gtk_widget_show_all(window);
gtk_main();
return FALSE ;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化