| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 458 人关注过本帖
标题:关于输入关键字?
取消只看楼主 加入收藏
Mrrr
Rank: 2
等 级:论坛游民
帖 子:15
专家分:72
注 册:2012-9-4
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:0 
关于输入关键字?
程序代码:
     快学了一个月的C语言了,最近想写一个以前小霸王,上面的坦克大战,再发射子弹的代码上遇到了一个小麻烦。
     我是用getch(),来接受键盘的控制的。
       比如: 
            1  当我用'j'来接受发射子弹的时候,然后程序进入打印清除的循环。(这个时候我必须等到子弹发出去,消失后才能,移动自己的人物)
            2  当子弹在移动的过程中,我如果按方向键移动的话,人物不会重新打印,而是记录按键,等到子弹循环消失后,直接打印在,记录按键最后一个上(瞬移)
            3  是我逻辑错误还是没有用对?(getch())?;
            4  希望大家多提提意见_格式_排版_还有各种不好的地方都提出来;
#include <stdio.h>
#include<conio.h>
#include<windows.h>
#include<time.h>
#include<stdlib.h>
int IF(int (*arr)[20]);
void Move(int x,int y)
{
    HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos= {x, y};
    SetConsoleCursorPosition(hOut, pos);
}
void Put(int (*arr)[20])//打印地图
{
    int x=1,y=1;
    int i,j;
            arr[x][y]=2;
        for (i=0;i<20;i++)
        {
            for (j=0;j<20;j++)
            {
                if (arr[i][j]==1)
                    printf("");
                if (arr[i][j]==2)
                    printf("¤");
                if (arr[i][j]==0)
                    printf("  ");
                if (arr[i][j]==3)
                    printf("");
                if (arr[i][j]==5)
                    printf("");
                if (arr[i][j]==6)
                    printf("");
                if (arr[i][j]==7)
                    printf("");
                if (arr[i][j]==8)
                    printf("¤");
                if (arr[i][j]==9)
                    printf("");
            }
        printf("\n");
        }
}

 void character(char n1,int (*arr)[20],int *x1,int *y1)  //传数组的首地址,
{
    int x=*x1,y=*y1;
    switch(n1)
        {
    case 'w':case'W':                        //
        if(arr[*x1-1][*y1]==1||arr[*x1-1][*y1]==7||arr[*x1-1][*y1]==8)
        {
            break;
        }
            arr[(*x1)--][*y1]=2;
            arr[*x1+1][*y1]=0;

            break;
    case's':case'S':                        //
        if (arr[*x1+1][*y1]==1||arr[*x1+1][*y1]==7||arr[*x1+1][*y1]==8)
        {
            break;
        }
            arr[(*x1)++][*y1]=2;            //(*x1)++加括号
            arr[*x1-1][*y1]=0;
            break;
    case'a':case'A':                        //
        if(arr[*x1][*y1-1]==1||arr[*x1][*y1-1]==7||arr[*x1][*y1-1]==8)
        {
            break;
        }
            arr[*x1][(*y1)--]=2;
            arr[*x1][*y1+1]=0;
            break;
       
    case'd':case'D':                        //
        if(arr[*x1][*y1+1]==1||arr[*x1][*y1+1]==7||arr[*x1][*y1+1]==8)
        {
            break;
        }
            arr[*x1][(*y1)++]=2;
            arr[*x1][*y1-1]=0;
            break;
    case'k':case'K':
        if (arr[*x1+1][*y1]!=1)
        {
            int i=1;   
            while(arr[*x1+i][*y1]!=1)
            {
                if (arr[*x1+i][*y1]==7)
                {
                    arr[*x1+i][*y1]=0;
                    Move (*y1*2,*x1+i);printf("  ");
                    break;
                }
                arr[*x1][*y1]=2;
                Sleep(200);
                Move(*y1*2,*x1);printf("¤");
                arr[*x1+i][*y1]=10;
                Move(*y1*2,*x1+i);printf("");
                arr[*x1+i-1][*y1]=0;
                Move(*y1*2,*x1+i-1);printf("  ");
                i++;
            }
            Move(*y1*2,*x1+i-1);printf("  ");
        }break;
    case'i':case'I':
        if (arr[*x1-1][*y1]!=1)
        {   
            int i=1;
            while(arr[*x1-i][*y1]!=1)
            {
                if (arr[*x1-i][*y1]==7)
                {
                    arr[*x1-i][*y1]=0;
                    Move(*y1*2,*x1-i);printf("  ");
                    break;
                }
                arr[*x1][*y1]=2;
                Move(*y1*2,*x1);printf("¤");
                Sleep(200);
                arr[*x1-i][*y1]=10;
                Move(*y1*2,*x1-i);printf("");
                arr[*x1-i+1][*y1]=0;
                Move(*y1*2,*x1-i+1);printf("  ");
                i++;
            }
            Move(*y1*2,*x1-i+1);printf("  ");
        }break;
    case'j':case'J':
        if (arr[*x1][*y1-1]!=1)
        {
            int i=1;
            while (arr[*x1][*y1-i]!=1)
            {
                if (arr[*x1][*y1-i]==7)
                {
                    arr[*x1][*y1-i]=0;
                    Move ((*y1-i)*2,*x1);printf("  ");
                    break;
                }
                arr[*x1][*y1]=2;
                Sleep(200);
                Move(*y1*2,*x1);printf("¤");
                arr[*x1][*y1-i]=10;
                Move((*y1-i)*2,*x1);printf("");
                arr[*x1][*y1-i+1]=0;
                Move((*y1-i+1)*2,*x1);printf("  ");
                i++;
            }
            Move((*y1-i+1)*2,*x1);printf("  ");
        }break;
    case'l':case'L':
        if (arr[*x1][*y1+1]!=1)
        {
            int i=2;
            while (arr[*x1][*y1+i]!=1)
            {
                if(arr[*x1][*y1+i]==7)
                {
                    arr[*x1][*y1+i]=0;
                    Move((*y1+i)*2,*x1);printf("  ");
                    break;
                }
                arr[*x1][*y1]=2;
                Sleep(200);
                Move(*y1*2,*x1);printf("¤");
                arr[*x1][*y1+i]=10;
                Move((*y1+i)*2,*x1);printf("");
                arr[*x1][*y1+i-1]=0;
                if(arr[*x1][*y1+i-1]==0)
                    Move((*y1+i-1)*2,*x1);printf("  ");
                i++;
            }
            Move((*y1+i-1)*2,*x1);printf("  ");
        }
    }
    Move(y*2,x);printf("  ");//老人物坐标
    Move((*y1)*2,*x1);printf("¤");//新任务坐标
}
void Random(int (*arr)[20])//随机怪物
{
    int n=1,j;
    int sum=0;
    srand((unsigned)time(0));
    while(n)
    {
        j=1+(unsigned)(20.0*rand()/(RAND_MAX+1.0));
        n=1+(unsigned)(20.0*rand()/(RAND_MAX+1.0));
        if(arr[j][n]==0)
        {
            arr[j][n]=7;
            sum++;
        }
        if(sum==20)
        {
            n=0;
        }
       
    }
}

 void main()
{
    int open=1;
    int x=1,y=1;
    char n;
    int num=1;
        static int arr[20][20]={ //静态变量static  改变数组值后,将保存新的值
        {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
        {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},//6(1,6)
        {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
        {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
        {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
        {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
        {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
        {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
        {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
        {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
        {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
        {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
        {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},//6(12,8)
        {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
        {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
        {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
        {1,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,1},
        {1,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,1},//7(17,2)  8(17,18)
        {1,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,1},//7(18,6)
        {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
        };
        Random(arr);//随机怪物
        Put(arr);//打印地图
        while(open)
        {
            n=getch();
            character(n,arr,&x,&y);
            open=IF(arr);
            if (open==0)
            {
                Move(0,20);printf("Game ovre");
            }
                Move(40,1);printf("\t坦克大战(史前版)");
                Move(40,2);printf("\tw,s,a,d(前后左右)");
                Move(40,3);printf("\ti,k,j,l(前后左右各攻击)");
                Move(40,19);printf("\n\n\n");
        }
   
}
int IF(int (*arr)[20])

 {
    int i,j,num=0;
    for (i=0;i<20;i++)
        for (j=0;j<20;j++)
            if (arr[i][j]==7)
            {
                num=1;
            }
    return num;

 }


[ 本帖最后由 Mrrr 于 2012-11-14 19:02 编辑 ]
搜索更多相关主题的帖子: 输入关键字 小霸王 坦克大战 C语言 
2012-11-14 19:01
快速回复:关于输入关键字?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.042354 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved