📄 jing.c~
字号:
// SnakePTR.o : Defines the entry point for the application.#include <gtk/gtk.h>#include <gdk/gdkkeysyms.h>#include <gtk/gtkwindow.h>#include <libgnomeui-2.0/gnome.h>#include <libgnomeui-2.0/libgnomeui/gnome-messagebox.h>#include "DatatypeDefine.h"// Forward declarations of functions included in this code module:void InitialSnakeList();void OnDraw(GtkWidget *widget);void initialize_statusbar();void set_statusbar();gboolean CreateBean();gboolean IsKeyDown(GtkWidget* widget, GdkEventKey* event, gpointer data);gboolean OnMove(GtkWidget *widget);// Global Variables:CHARACTER BACKGROUND[MAXLINEINDEX][MAXCOLUMNINDEX]; //store the background locationstruct SNAKE *HEAD = NULL; //declare head point of the snakeliststruct SNAKE *TAIL = NULL; //declare tail point of the snakeliststruct SNAKE *BEANNODE = NULL; //declare bean point of the snakelistint timerID = 0; //mark the new timer's IDint FIRSTFLAG = 0; //mark keyboard is pressed for firstint STILLFLAG = 0; //the flag of the key Pauseint SCORE = 0; //count the scorechar VALUE[MAXSCOREBIT] = {0}; //store string style of the scorestatic GdkPixmap *pixmap = NULL; GtkWidget *window;GtkWidget *drawing_area;GtkWidget *vbox;GtkWidget *status_bar;int context_ID = 0; //the ID of the status bar/******************************************************************内容 : 開始のウィンドウを創建します*参数1: hInstance*参数2: nCmdShow *返回 : TRUE:正常 FALSE:異常*異常 : なし*****************************************************************/int InitInstance(GtkWidget *widget, GdkEventConfigure *event){ InitialSnakeList(); CreateBean(); if(pixmap) g_object_unref(pixmap); pixmap = gdk_pixmap_new(widget->window, widget->allocation.width, widget->allocation.height, -1); OnDraw(widget); return 0;}/******************************************************************内容 : リアルタイムでページを更新します*参数1: widget*参数2: event*参数3: data *返回 : TRUE:正常 FALSE:異常*異常 : なし*****************************************************************/int InvalidateRect(GtkWidget *widget , GdkEventExpose *event , gpointer data){ OnDraw(widget); gdk_draw_drawable(widget->window , widget->style->fg_gc[GTK_WIDGET_STATE(widget)] , pixmap , 0 , 0 , 0 , 0 , widget->allocation.width , widget->allocation.height); return TRUE;}/******************************************************************内容 : 設ける時延びて触発します*参数1: widget*参数2: data*返回 : TRUE:正常 FALSE:異常*異常 : なし*****************************************************************/gboolean my_timer(GtkWidget *widget , gpointer data){ if (FIRSTFLAG == -1) //if game is over,set timer ineffect { return FALSE; } if ((STILLFLAG%2) == 0) //if game is not pause,moving { OnMove(widget); gtk_widget_queue_draw_area(drawing_area, 0, 0, MAXWINDOWWIDTH , MAXWINDOWLENGTH); } return TRUE;}/******************************************************************内容 : 再び新しいゲームを始めます*参数1: widget*参数2: data*返回 : 0:正常 -1:異常*異常 : なし*****************************************************************/ int my_replay(GtkWidget *widget, gpointer data){ GtkWidget* MessageBox = gnome_message_box_new( //set a messagebox to sure "Play a new game now? ", //if start a new game GNOME_MESSAGE_BOX_QUESTION, GNOME_STOCK_BUTTON_YES, GNOME_STOCK_BUTTON_NO,NULL); if(timerID != 0) //if a timer exist,kill it { gtk_timeout_remove(timerID); timerID = 0; gtk_widget_show (MessageBox); //pop out the message box } if(gnome_dialog_run_and_close(GNOME_DIALOG (MessageBox)) == 0) { //if press the key of "YES" STILLFLAG = 0; //set all the flag as initial FIRSTFLAG = 0; SCORE = 0; if (TAIL->next != TAIL) { HEAD = HEAD->next; free (TAIL->last); TAIL->next = HEAD; } else { free (HEAD); free (TAIL); } set_statusbar(); InitialSnakeList(); CreateBean(); OnDraw(widget); gtk_widget_queue_draw_area( drawing_area, 0, 0, MAXWINDOWWIDTH, MAXWINDOWLENGTH); return 0; } else //if press the key of "NO" { //return the old game return 0; }}/******************************************************************内容 : 当面のゲームを一時停止します*参数1: widget*参数2: data*返回 : 0:正常 -1:異常*異常 : なし*****************************************************************/ int my_pause(GtkWidget *widget, gpointer data){ //if press the menu of pause when if (FIRSTFLAG != 0) //the game has started,its effect { STILLFLAG++; } else { return 0; } }void question_call_back(gint reply, gpointer data){ if(reply == 0){printf("quit"); gtk_main_quit();} else printf("not quit"); }/******************************************************************内容 : 当面遊ぶウィンドウを閉鎖します*参数1: widget*参数2: data*返回 : 0:正常 -1:異常*異常 : なし*****************************************************************/int my_quit(GtkWidget *widget , gpointer data){ GtkWidget * MessageBox = gnome_question_dialog_parented("Quit?", question_call_back, NULL, GTK_WINDOW(window)); gtk_window_set_modal(MessageBox, TRUE); printf("here"); return 0;}/******************************************************************内容 : 蛇と背景の初期化*参数 : なし *返回 : なし*異常 : なし*****************************************************************/void InitialSnakeList(){ int m = 0,n = 0; for (m=0; m<MAXLINEINDEX; m++)//set the background array { for (n = 0; n<MAXCOLUMNINDEX; n++) { BACKGROUND[m][n] = grid; } } HEAD = (struct SNAKE*)malloc(sizeof(struct SNAKE));//set the head point HEAD->direction = right; HEAD->location.bottom = (INITIALSNAKELINE + 1) * UNIT - 1; HEAD->location.top = INITIALSNAKELINE * UNIT + 1; HEAD->location.left = INITIALSNAKEHEADCOLUMN * UNIT + 1; HEAD->location.right = (INITIALSNAKEHEADCOLUMN+1) * UNIT - 1; HEAD->height = INITIALSNAKELINE; HEAD->width = INITIALSNAKEHEADCOLUMN; BACKGROUND[INITIALSNAKELINE][INITIALSNAKEHEADCOLUMN] = snake; TAIL = (struct SNAKE*)malloc(sizeof(struct SNAKE));//set the tail point TAIL->direction = right; TAIL->location.bottom = (INITIALSNAKELINE + 1) * UNIT - 1; TAIL->location.top = INITIALSNAKELINE * UNIT + 1; TAIL->location.left = INITIALSNAKETAILCOLUMN; TAIL->location.right = (INITIALSNAKETAILCOLUMN + 1) * UNIT - 1; TAIL->height = INITIALSNAKELINE; TAIL->width = INITIALSNAKEBODYCOLUMN; BACKGROUND[INITIALSNAKELINE][INITIALSNAKEBODYCOLUMN] = grid; HEAD->next = TAIL; HEAD->last = TAIL; TAIL->next = HEAD; TAIL->last = HEAD; struct SNAKE *temp = (struct SNAKE*)malloc(sizeof(struct SNAKE));//set the body point temp->direction = right; temp->location.bottom = (INITIALSNAKELINE + 1)* UNIT - 1; temp->location.top = INITIALSNAKELINE * UNIT + 1; temp->location.left = INITIALSNAKEBODYCOLUMN * UNIT + 1; temp->location.right = (INITIALSNAKEBODYCOLUMN + 1) * UNIT - 1; temp->height = INITIALSNAKELINE; temp->width = INITIALSNAKEBODYCOLUMN; BACKGROUND[INITIALSNAKELINE][INITIALSNAKEBODYCOLUMN] = snake; HEAD->next = temp; temp->next = TAIL; TAIL->last = temp; temp->last = HEAD; }/******************************************************************内容 : 豆の集合点の創建と定義*参数 : なし *返回 : TRUE:正常 FALSE:異常*異常 : なし*****************************************************************/gboolean CreateBean(){ int x = 0, y = 0, cx = 0, cy = 0; do { srand(time(0));//when bean is not on snake,create it calority x = rand()%MAXLINEINDEX; y = rand()%MAXCOLUMNINDEX; cx = x * UNIT; cy = y * UNIT; }while (BACKGROUND[x][y] == snake); BEANNODE = (struct SNAKE *)malloc(sizeof(struct SNAKE));//set the bean node point BEANNODE->location.top = cx + 1; BEANNODE->location.bottom = cx + UNIT - 1; BEANNODE->location.left = cy + 1; BEANNODE->location.right = cy + UNIT - 1; BEANNODE->width = y; BEANNODE->height = x; BEANNODE->next = NULL; BEANNODE->last = NULL; BACKGROUND[x][y] = bean; return TRUE; }/******************************************************************内容 : ウィンドウの背景を描写して、蛇の鎖の時計と豆の集合点*参数 : widget *返回 : なし*異常 : なし*****************************************************************/void OnDraw(GtkWidget *widget){ GdkGC *my_gc_background = NULL; GdkGC *my_gc_snake = NULL; GdkColor color; struct SNAKE *node = HEAD; my_gc_background = gdk_gc_new(widget->window);//set the color of the background color.red = 40000; color.green = 40000; color.blue = 40000; gdk_gc_set_rgb_fg_color(my_gc_background, &color); gdk_draw_rectangle(pixmap , my_gc_background , TRUE , 0 , 0 , widget->allocation.width , widget->allocation.height); int LineIndex = 0; int ColumnIndex = 0; for (LineIndex = 0; LineIndex <= MAXLINEINDEX; LineIndex++)//draw the grid of the back { gdk_draw_line(pixmap , widget->style->black_gc, 0,LineIndex * UNIT , MAXWIDTH, LineIndex * UNIT); } for (ColumnIndex = 0; ColumnIndex <= MAXCOLUMNINDEX; ColumnIndex++) { gdk_draw_line(pixmap , widget->style->black_gc , ColumnIndex * UNIT, 0 , ColumnIndex * UNIT, MAXLENGTH);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -