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

📄 check.c

📁 this is a robot game which is use gtk++ and it is same as in linux robot game player need to save
💻 C
字号:
#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <time.h>



time_t t1,t2;
GtkWidget *table;
GtkWidget *window;
GtkWidget *button;
GtkWidget *button1;
GtkWidget *box;
GtkWidget *label;
int tlpointer=0;
int t;
int n;
int r;
int k=0;
int self[2]={15,15};
int robot[2][50];
int teleport[2][20];
int debri[2][50];


int counter=0;
gint delete_event( GtkWidget *widget,
                   GdkEvent  *event,
                   gpointer   data )
{
    gtk_main_quit ();
    return FALSE;
}



void result(gchar *result)
{ 


    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

    gtk_window_set_title (GTK_WINDOW (window), "Game result");

	


        g_signal_connect (G_OBJECT (window), "delete_event",
                      G_CALLBACK (delete_event), NULL);

    gtk_container_set_border_width (GTK_CONTAINER (window), 20);



table = gtk_table_new (2,8 , TRUE);

    gtk_container_add (GTK_CONTAINER (window), table);








	box = gtk_hbox_new (FALSE, 0);
	gtk_container_set_border_width (GTK_CONTAINER (box), 0);
	label = gtk_label_new (result);
	gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
	gtk_widget_show (label);
	      
	gtk_table_attach_defaults (GTK_TABLE (table), box, 2, 8, 0,1);
        gtk_widget_show(table);

    button = gtk_button_new_with_label ("ok");

       g_signal_connect (G_OBJECT (button), "clicked",
       G_CALLBACK (delete_event), NULL);
gtk_table_attach_defaults (GTK_TABLE (table), button, 3, 5, 1,2);


    gtk_widget_show (button);

    gtk_widget_show (box);

    gtk_widget_show (window);
    gtk_main ();

    


}









void tl()
{int sj=0;
sj=teleport[0][tlpointer]*teleport[1][tlpointer];
sj=sj%t;
self[0]=teleport[0][sj];
self[1]=teleport[1][sj];
sj=0;
tlpointer=(tlpointer+1)%t;
}







//----------------------------------
void input()
{int a,b,i;
printf("Enter no of robots:-       ");
scanf("%d",&a);
n=a;
for(i=0;i<n;i++)
{
printf("Enter x position of robot:-      ");
scanf("%d",&a);
printf("Enter y position of robot:-      ");
scanf("%d",&b);
robot[0][i]=a;robot[1][i]=b;
}
printf("Enter no of teleport positions:-       ");
scanf("%d",&a);
t=a;
for(i=0;i<t;i++)
{
printf("Enter x coordinate of position :-      ");
scanf("%d",&a);
printf("Enter y coordinate of position :-      ");
scanf("%d",&b);
teleport[0][i]=a;teleport[1][i]=b;
}

}
//-----------------------------
void selfmove()
{int p=0;int i=0,j=0;

counter++;
for(i=0;i<n;i++)
{if(robot[0][i]==self[0] && robot[1][i]==self[1])
{p=-1;}}
	if(p==-1)
	{gtk_widget_destroy(window);
	result("You have lost");
	printf("YOU LOOSE\n");
	printf("total no of moves:-   %d\n",counter);
	(void) time(&t2);}
p=0;
for(i=0;i<n;i++)
{if(robot[0][i]!=-1)
{p=-1;}
}
	if(p==0)
	{gtk_widget_destroy(window);
	result("you have won");
	printf("YOU win\n");
	printf("total no of moves:-   %d\n",counter);
	(void) time(&t2);}
p=0;
for(i=0;i<=k;i++)
{	for(j=0;j<n;j++)
		{if(debri[0][i]==robot[0][j]  && debri[1][i]==robot[1][j])
			{robot[0][j]=-1;
			 robot[1][j]=-1;}
		}
}

}

//-----------------------------
void checkdebri(int a,int b)
{int ch=0,i=0,at=-1,j,r;
for(i=0;i<k;i++)
{if (a==debri[0][i] && b==debri[1][i])
{ch=-1;at=i;break;}}
if(ch==-1)
{j=a-self[0];r=b-self[1];
if((debri[0][at]+j)>=0 && (debri[0][at]+j)<31 && (debri[1][at]+r)>=0 && (debri[1][at]+r)<31)
{debri[0][at]=debri[0][at]+j;
 debri[1][at]=debri[1][at]+r;
self[0]=a;self[1]=b;
}}
else
{self[0]=a;self[1]=b;}
ch=-1;at=0;



}
//--------------------------------
void s1()
{checkdebri(self[0]-1,self[1]-1);
}
void s2()
{checkdebri(self[0]-1,self[1]);}
void s3()
{checkdebri(self[0]-1,self[1]+1);}
void s4()
{checkdebri(self[0],self[1]-1);}
void s5()
{checkdebri(self[0],self[1]);}
void s6()
{checkdebri(self[0],self[1]+1);}
void s7()
{checkdebri(self[0]+1,self[1]-1);}
void s8()
{checkdebri(self[0]+1,self[1]);
self[0]++;}
void s9()
{checkdebri(self[0]+1,self[1]+1);}

//-----------------------------



//--------------------------------
int pos(int a,int b)
{if(a>b){return (a-b);}
 else return (b-a);}


GtkWidget *xpm_label_box( gchar     *xpm_filename,
                          gchar     *label_text )
{
    GtkWidget *box;
    GtkWidget *label;
    GtkWidget *image;
    box = gtk_hbox_new (FALSE, 0);
    gtk_container_set_border_width (GTK_CONTAINER (box), 0);
    image = gtk_image_new_from_file (xpm_filename);
    label = gtk_label_new (label_text);
    gtk_box_pack_start (GTK_BOX (box), image, FALSE, FALSE,0);
    gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
    gtk_widget_show (image);
    gtk_widget_show (label);
    return box;
}



void destroy()
{gtk_widget_destroy(table);
selfmovecreate();
}




void moverobot()
{ int i=0;
	for(i=0;i<n;i++)
            {
		if(robot[0][i]!=-1)
		{if(robot[0][i]>self[0])
			{robot[0][i]--;}
		else if(robot[0][i]<self[0])
			{robot[0][i]++;}
		if(robot[1][i]>self[1])
			{robot[1][i]--;}
		else if(robot[1][i]<self[1])
			{robot[1][i]++;}
		}
	    }
}

void makedebri()
{int i,j,p=0;r=-1;
 int n=4;
	for(i=0;i<n;i++)
	{	if(robot[0][i]!=-1)
		for(j=i+1;j<n;j++)
		{if (robot[0][i]==robot[0][j])
			{if(robot[1][i]==robot[1][j])
				{robot[0][j]=-1;robot[1][j]=-1;p=-1;}
			}
		}
		if(p==-1)
		{debri[0][k]=robot[0][i];
		 debri[1][k]=robot[1][i];
		 k++;
		robot[0][i]=-1;
		robot[1][i]=-1;
		p=0;}
	}
}



int selfmovecreate()
{ int p=0,q=0,r=-1;
    int a=0;
    int n=4;
    int i=0;
    int j=0,l;
 table = gtk_table_new (31, 32, TRUE);

    gtk_container_add (GTK_CONTAINER (window), table);
	for(i=0;i<=30;i++)
{
		for(j=0;j<=30;j++)
		{

	if(i==self[0] && j==self[1])
    		{
                 box = xpm_label_box ("self.bmp", "");
		 button = gtk_button_new();
		 gtk_widget_show (box);
		 gtk_container_add (GTK_CONTAINER (button), box);
		}
	else{
		for(a=0;a<n;a++)
		{if(i==robot[0][a] && j==robot[1][a])
		    {p=-1;}}
		if(k!=0)
		for(a=0;a<k;a++)
		 {if(i==debri[0][a] && j==debri[1][a])
		    {p=-2;}}
		for(a=0;a<t;a++)
		 {if(i==teleport[0][a] && j==teleport[1][a])
		    {q=-1;r=a;}}
		if(p==-1 && q==-1)
		{box = xpm_label_box ("trobot.bmp", "");
		 button = gtk_button_new();
		 gtk_widget_show (box);
		 gtk_container_add (GTK_CONTAINER (button), box);
		}
		else if(p==-2 && q==-1 )
		{box = xpm_label_box ("tdebri.bmp", "");
		 button = gtk_button_new();
		 gtk_widget_show (box);
		 gtk_container_add (GTK_CONTAINER (button), box);
		}
		else if(q==-1 )
		{box = xpm_label_box ("teli.bmp", "");
		 button = gtk_button_new();
		 gtk_widget_show (box);
		 gtk_container_add (GTK_CONTAINER (button), box);
		}
		else if(p==-1 )
		{box = xpm_label_box ("robot.bmp", "");
		 button = gtk_button_new();
		 gtk_widget_show (box);
		 gtk_container_add (GTK_CONTAINER (button), box);}
		else if(p==-2)
		{box = xpm_label_box ("debri.bmp", "");
		 button = gtk_button_new();
		 gtk_widget_show (box);
		 gtk_container_add (GTK_CONTAINER (button), box);}
		else {button = gtk_button_new_with_label (" ");}
		p=0;q=0;
	     }
			
		gtk_table_attach_defaults (GTK_TABLE (table), button, i, i+1, j, j+1);

		gtk_widget_show (button);
  		if(pos(i,self[0])<=1 && pos(j,self[1])<=1)
			{
//-----------------------------------------------------
			if(i==self[0]-1 && j==self[1]-1)
			{g_signal_connect (G_OBJECT (button), "clicked",
                      G_CALLBACK (s1),NULL);}
			else if(i==self[0]-1 && j==self[1])
			{g_signal_connect (G_OBJECT (button), "clicked",
                      G_CALLBACK (s2),NULL);}
			else if(i==self[0]-1 && j==self[1]+1)
			{g_signal_connect (G_OBJECT (button), "clicked",
                      G_CALLBACK (s3),NULL);}
			else if(i==self[0]&& j==self[1]-1)
			{g_signal_connect (G_OBJECT (button), "clicked",
                      G_CALLBACK (s4),NULL);}
			else if(i==self[0] && j==self[1])
			{g_signal_connect (G_OBJECT (button), "clicked",
                      G_CALLBACK (s5),NULL);}
			else if(i==self[0] && j==self[1]+1)
			{g_signal_connect (G_OBJECT (button), "clicked",
                      G_CALLBACK (s6),NULL);}
			else if(i==self[0]+1 && j==self[1]-1)
			{g_signal_connect (G_OBJECT (button), "clicked",
                      G_CALLBACK (s7),NULL);}
			else if(i==self[0]+1 && j==self[1])
			{g_signal_connect (G_OBJECT (button), "clicked",
                      G_CALLBACK (s8),NULL);}
			else
			{g_signal_connect (G_OBJECT (button), "clicked",
                      G_CALLBACK (s9),NULL);}
			
			
//----------------------------------------------------
			g_signal_connect (G_OBJECT (button), "clicked",
                      G_CALLBACK (moverobot),NULL);

			g_signal_connect (G_OBJECT (button), "clicked",
                      G_CALLBACK (makedebri),NULL);

			g_signal_connect (G_OBJECT (button), "clicked",
                      G_CALLBACK (selfmove),NULL);

			g_signal_connect (G_OBJECT (button), "clicked",
                      G_CALLBACK (destroy),NULL);
}
			 
		

}		}

	box = xpm_label_box ("sj.bmp", "Teleport");
		 button = gtk_button_new();
		 gtk_widget_show (box);
		 gtk_container_add (GTK_CONTAINER (button), box);
		 gtk_table_attach_defaults (GTK_TABLE (table), button, 0, 31, 31,32);


		gtk_widget_show (button);
		g_signal_connect (G_OBJECT (button), "clicked",
                      G_CALLBACK (tl),NULL);
		g_signal_connect (G_OBJECT (button), "clicked",
                      G_CALLBACK (moverobot),NULL);

			g_signal_connect (G_OBJECT (button), "clicked",
                      G_CALLBACK (makedebri),NULL);

			g_signal_connect (G_OBJECT (button), "clicked",
                      G_CALLBACK (selfmove),NULL);

			g_signal_connect (G_OBJECT (button), "clicked",
                      G_CALLBACK (destroy),NULL);
    gtk_widget_show (table);
	return 0;
}





int main( int   argc,
          char *argv[] )
{  input();
    gtk_init (&argc, &argv);
	(void) time(&t1);
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_widget_set_size_request(window,800,600);


    gtk_window_set_title (GTK_WINDOW (window), "Robot game");

    g_signal_connect (G_OBJECT (window), "delete_event",
                      G_CALLBACK (delete_event), NULL);

    gtk_container_set_border_width (GTK_CONTAINER (window),0);
//----------------------------------------------------------

	gtk_widget_show (window);
	selfmovecreate();
	
//---------------------------------------------------------------------------
    gtk_main ();

printf("Time taken by player  = %d seconds\n", (int) t2-t1);
    return 0;
}

⌨️ 快捷键说明

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