关闭

打星星游戏

标签: c
545人阅读 评论(0) 收藏 举报
分类:
//大一暑假


#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

#define n 10//地图大小

    int m=1;
    int g=5;

//////////////////////////人是1,墙是9,星星是8/10,空位是0,光是2;
int count=0;
int point='w';
void move(int a[][10],int i,int j);//移动函数
void huatu(int a[n][n]);//画图
void guang(int a[][10]);

int main(void)
{
	
int a[n][n]={
{9,9,9,9,9,9,9,9,9,9},
{9,0,0,0,9,1,0,8,8,9},
{9,0,0,0,9,0,0,0,0,9},
{9,0,0,0,0,0,0,0,0,9},
{9,0,8,0,0,0,0,0,0,9},
{9,0,0,0,0,0,9,0,0,9},
{9,0,0,0,0,0,9,0,0,9},
{9,0,0,8,8,0,0,0,0,9},
{9,0,0,0,9,0,0,0,0,9},
{9,9,9,9,9,9,9,9,9,9}
};

    int ch;

    huatu(a);
    while(1)
    {  
    ch=getch();

	switch(ch)
			{
			case 72: move(a,-1,0);break;
			case 80: move(a,1,0);break;
			case 75: move(a,0,-1);break;
			case 77: move(a,0,1);break;
			case 'w': point='w';continue;
			case 's': point='s';continue;
			case 'a': point='a';continue;
			case 'd': point='d';continue;

			case 'j': guang(a);break;
			};
		system("cls");
		huatu(a);

				if(count==5)/////////终止条件
				{
     			printf("成功\n");
				_sleep(1000);
				exit(0);
				}
	}
    return 0;
}

void move(int a[][10],int i,int j)
{ 
    if(a[m+i][g+j]==9||a[m+i][g+j]==8)printf("撞墙或星星了");  //人-墙/星星
    else if(a[m+i][g+j]==0)//人-无
	{
		a[m][g]-=1;
		a[m+i][g+j]+=1;
		m+=i;g+=j;
	}

}

void guang(int a[][10])
{
	int i,j;
	     if(point=='w')for(i=m-1;i>0 && a[i][g]!=9;--i){a[i][g]+=2;if(a[i][g]==10)++count;}
	else if(point=='s')for(i=m+1;i<9 && a[i][g]!=9;++i){a[i][g]+=2;if(a[i][g]==10)++count;}
	else if(point=='a')for(i=g-1;i>0 && a[m][i]!=9;--i){a[m][i]+=2;if(a[m][i]==10)++count;}
	else if(point=='d')for(i=g+1;i<9 && a[m][i]!=9;++i){a[m][i]+=2;if(a[m][i]==10)++count;}
	/////////////////////////显示攻击
	system("cls");
	huatu(a);
	_sleep(200);

	for(i=1;i<n-1;++i)///////被攻击后就变为0
	for(j=1;j<n-1;++j)
		if(a[i][j]==2 || a[i][j]==10)a[i][j]=0;

}

void  huatu(int a[n][n])
{ int count=0;
  int i,j;
    for(i=0;i<n;i++)
    {    for(j=0;j<n;j++)
        {
           if(a[i][j]==5)count++;
            switch (a[i][j])
            {   case 9:printf("■");break;
                case 0:printf("  ");break;
				case 10:
                case 2:printf("□");break;//攻击		
                case 1:printf("㊣");break;///人
		     	case 8:printf("☆");break;//星星
                default: printf("?");
			}
		}
	printf("\n");  
	}
    printf("\n\n按上下左右移动;;;;w s a d调整攻击方向;;; j攻击");
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:269211次
    • 积分:4294
    • 等级:
    • 排名:第7558名
    • 原创:178篇
    • 转载:81篇
    • 译文:0篇
    • 评论:12条
    最新评论