⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mouse.c

📁 一个linux 下用C写的mouse小游戏
💻 C
字号:
/*	hit mouse - mouse.c 	Copyright (c) 2003 by Linux_Lyb <linux_lyb@sohu.com> ____________________________________________________________________________		This program is free software; you can redistribute it and/or modify	it under the terms of the GNU General Public License as published by	the Free Software Foundation; either version 2, or (at your option)	any later version.	This program is distributed in the hope that it will be useful,	but WITHOUT ANY WARRANTY; without even the implied warranty of	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the	GNU General Public License for more details.	You should have received a copy of the GNU General Public License	along with this program; if not, write to the Free Software	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.	See the file COPYING in the doc/ directory for more information.*/#include "mouse.h"#include <time.h>#include <stdlib.h>#define IMAGE_NUM 4#define MOUSE_NUM 6 static GtkWidget *window = NULL;static GtkWidget *draw_area = NULL;static GdkPixbuf *image[IMAGE_NUM];static GdkPixmap *pixmap = NULL;GtkWidget *label,b_setup;gchar text[64];guint timer = 0;guint the_time = 0;gboolean playing = FALSE;gboolean pauseing = FALSE;Mouse mouse[MOUSE_NUM];//游戏相关变量guint my_time = 60;guint my_mouse = 10;guint speed = 1000;guint level = 1;guint score = 0;guint live = 3;gboolean play_game (gpointer data);gint my_rand (gint min,gint max){	return rand() % max + min;}//设置标签的内容void set_label(){	sprintf (text,"score %d live %d level %d\ntime %d mouse %d",score,live,level,my_time,my_mouse);	gtk_label_set_text (GTK_LABEL(label),text);}//画游戏画面void draw_game(){gint i;gint frame;gint width,height;	gdk_draw_rectangle (pixmap,draw_area->style->white_gc,TRUE,0, 0,		      draw_area->allocation.width,draw_area->allocation.height);	for (i=0;i<=MOUSE_NUM;i++) {		if (mouse[i].live == TRUE) {			frame = mouse[i].frame_num;			width = gdk_pixbuf_get_width (image[frame]);			height = gdk_pixbuf_get_height (image[frame]);			gdk_draw_pixbuf (pixmap,draw_area->style->white_gc,image[frame],					0,0,mouse[i].x * 70,mouse[i].y * 70,width,height,0,0,0);		}	}	gtk_widget_queue_draw (draw_area);	}//加载图像gboolean load_pixbuf(){guint i;gchar *filename;	for (i=0;i<=IMAGE_NUM;i++) {		filename = g_strdup_printf ("mouse%d.png",i);		image[i] = gdk_pixbuf_new_from_file (filename,NULL);				if (image[i] ==NULL) {			g_print ("Can't load image %s\n !!",filename);			return FALSE;		}		g_free (filename);	}	return TRUE;}void init_mouse(gint n){//加入多个地鼠不能出现在同一个位置的判断	mouse[n].live = TRUE;	mouse[n].frame_num = 0;	mouse[n].x = my_rand (0,4);	mouse[n].y = my_rand (0,4);}//初始化游戏void init_game(){gint i;	//产生随机位置的地鼠	for (i=0;i<MOUSE_NUM;i++) {		init_mouse(i);		}	draw_game();}gboolean play_game (gpointer data){gint i;	if (playing == FALSE) { //如果游戏还没开始,初始化游戏并开始		init_game();		set_label();		playing = TRUE;		return TRUE;	}		for (i=0;i<=MOUSE_NUM;i++) {		if (mouse[i].live == TRUE) {			if (mouse[i].frame_num >= IMAGE_NUM-1) {					init_mouse (i);			} else { 				mouse[i].frame_num ++;			}		}	}	draw_game();	return TRUE;}gboolean setup_game (GtkWidget *widget,gpointer data){	return TRUE;}//必需要在指定的时间内打完指定的数目gboolean time_up (gpointer data){static gint temp_int=0;	if (temp_int ==0) temp_int = my_time;	if (my_time == 0 && my_mouse > 0) {		live --;		if (live == 0 ) {			gtk_label_set_text (GTK_LABEL(label),"- Game Over -");			g_source_remove (timer);			timer = 0;			g_source_remove (the_time);			the_time = 0;			my_mouse = 10;			my_time = 60;			level = 1;			score = 0;			live = 3;			speed = 1000;			temp_int = 0;		} else {			my_time = temp_int;			gtk_label_set_text (GTK_LABEL(label),"- You Lost !! -");		}		playing = FALSE;	} else {		my_time--;		set_label();	}	return TRUE;}gboolean button_press(GtkWidget *widget,GdkEventButton *event){gint x,y;gint mouse_x,mouse_y;gint i;	if (event->button == 1 && event->type == GDK_BUTTON_PRESS) { //按下的是左键就判断是否打到了地鼠		x = (gint)event->x;		y = (gint)event->y;		for (i=0;i<MOUSE_NUM;i++) {			mouse_x = mouse[i].x * 70;			mouse_y = mouse[i].y * 70;			//判断是否在哪个地鼠的坐标内(大概位置),如果是的话,并且			//地鼠的状态(frame_num)大于1(就是地鼠已经出来了)			if (mouse[i].frame_num > 1 && x > mouse_x+10 && y > mouse_y+30  && x < mouse_x + 50 && y < mouse_y + 70) {				mouse[i].frame_num = IMAGE_NUM;				draw_game();				score = score + 10;				my_mouse--;				set_label();				if (my_mouse == 0) { //在指定的时间内打完了指定的数目,进入下一关					level++;					if ((my_mouse = level*10) > 100) my_mouse = 100 ; //比上一局多10个地鼠,最多100个					if ((my_time=60-level*2)<20) my_time = 20;  //比上一局少一秒钟,最少20秒					speed = speed - 100;					gtk_label_set_text (GTK_LABEL(label)," - You Win -");					g_source_remove (timer);					g_source_remove (the_time);					playing = FALSE;					timer = g_timeout_add (speed,play_game,NULL);					the_time = g_timeout_add (1000,time_up,NULL);				}					}		} 	}	return TRUE;}//游戏开始void start_game(){	if (timer == 0) timer = g_timeout_add (speed,play_game,NULL);	if (the_time == 0) the_time = g_timeout_add (1000,time_up,NULL);	gtk_widget_set_sensitive(b_setup,FALSE);}//暂停游戏void pause_game(GtkWidget *widget){	if (pauseing == FALSE) {		if (timer != 0) g_source_remove (timer);      		timer = 0;		gtk_button_set_label (GTK_BUTTON(widget)," Go "); 		pauseing = TRUE;	} else {		if (timer == 0) timer = g_timeout_add (speed,play_game,NULL);		gtk_button_set_label (GTK_BUTTON(widget),"Pause");		pauseing = FALSE; 	}}//停止游戏void stop_game(){	playing = FALSE;	if (timer != 0) g_source_remove (timer);      	timer = 0;		}//退出游戏void quit_game(){gint i;	stop_game();	g_object_unref (pixmap);	for (i=1;i<=IMAGE_NUM;i++) g_object_unref (image[i]);	gtk_main_quit();}static gboolean configure_event (GtkWidget *widget,GdkEventConfigure *event,gpointer data){	if (pixmap)		g_object_unref (pixmap); 	pixmap = gdk_pixmap_new (widget->window,			widget->allocation.width,widget->allocation.height,-1);	gdk_draw_rectangle (pixmap,widget->style->white_gc,TRUE,0, 0,		      widget->allocation.width,widget->allocation.height);  return TRUE;}static gboolean expose_event(GtkWidget *widget,GdkEventExpose *event,gpointer data){	gdk_draw_drawable (widget->window,		     widget->style->fg_gc[GTK_WIDGET_STATE (widget)],		     pixmap,		     event->area.x, event->area.y,		     event->area.x, event->area.y,		     event->area.width, event->area.height);  return TRUE;}gboolean create_window(){GtkWidget *vbox,*hbox;GtkWidget *b_start,*b_pause;	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);	gtk_window_set_title (GTK_WINDOW(window),"Hit Mouse,By Linux_Lyb");	gtk_window_set_resizable (GTK_WINDOW(window),FALSE);	gtk_widget_set_size_request (window,288,348);	gtk_window_set_position (GTK_WINDOW(window),GTK_WIN_POS_CENTER);	gtk_container_set_border_width (GTK_CONTAINER (window),4);	gtk_widget_set_events(window,GDK_SCROLL_MASK);	if (load_pixbuf() == FALSE) return FALSE;	g_signal_connect (G_OBJECT(window),"button_press_event",				G_CALLBACK (button_press),window);		g_signal_connect (window,"delete_event",				G_CALLBACK (quit_game),&window);	vbox = gtk_vbox_new (FALSE,0);	gtk_container_add (GTK_CONTAINER(window),vbox);	draw_area = gtk_drawing_area_new ();	gtk_widget_set_size_request (draw_area,280,250);	g_signal_connect (draw_area, "expose_event",			G_CALLBACK (expose_event), NULL);	g_signal_connect (draw_area,"configure_event",			G_CALLBACK (configure_event), NULL);	gtk_box_pack_start (GTK_BOX (vbox), draw_area, TRUE, TRUE, 0);	hbox = gtk_hbox_new (FALSE,0);	gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 5);	b_start = gtk_button_new_with_label ("Start");	gtk_box_pack_start (GTK_BOX (hbox), b_start, FALSE, FALSE, 0);	g_signal_connect (b_start,"clicked",			G_CALLBACK(start_game),NULL);	b_pause = gtk_button_new_with_label ("Pause");	gtk_box_pack_start (GTK_BOX (hbox), b_pause, FALSE, FALSE, 0);	g_signal_connect (b_pause,"clicked",			G_CALLBACK(pause_game),b_pause);	b_setup = gtk_button_new_with_label ("Setup");	gtk_box_pack_start (GTK_BOX (hbox), b_setup, FALSE, FALSE, 0);	g_signal_connect (b_setup,"clicked",			G_CALLBACK(setup_game),b_setup);	label = gtk_label_new ("Author: Linux_Lyb\nE-mail: linux_lyb@sohu.com");	gtk_box_pack_start (GTK_BOX (hbox), label, TRUE,TRUE, 0);	if (!GTK_WIDGET_VISIBLE (window))		gtk_widget_show_all (window);	else {		gtk_widget_destroy (window);		window = NULL;		return FALSE;	}	gtk_widget_show_all (window);	return TRUE;}int main(int argc,char *argv[]){	srand (time(NULL));	gtk_init (&argc,&argv);	if (create_window() == TRUE) gtk_main();	return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -