IE盒子

搜索
查看: 159|回复: 1

如何使用C语言实现打地鼠的游戏

[复制链接]

1

主题

6

帖子

8

积分

新手上路

Rank: 1

积分
8
发表于 2023-2-15 01:52:27 | 显示全部楼层 |阅读模式
在学习的过程中遇到了这个题,今天写出来,与大家分享下
先把界面贴出来给大家看看。
1、首先我们需要设置游戏需要循环的次数
2、输入锤子选择的位置(“O”代表锤子,“X”代表地鼠)
3、没有打中,请重新输入锤子的位置
4、恭喜打中了
好,贴代码
/*
        打地鼠游戏
*/
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(){
        int times = 0;
        int mousey = 0,mousex = 0;
        int num = 0;
        srand(time(0));
        int posy = 0,posx = 0;
        int hits = 0,missed = 0;
        int row = 0,col = 0;
        //获取游戏次数
        printf("请输入游戏次数:");
        scanf("%d",×);
        //打印地图
        printf("* * *\n* * *\n* * *\n");
        //游戏过程
        for(num = 1;num <= times;num++){
                //获得老鼠和锤子的位置
                mousey = rand() % 3 + 1;
                mousex = rand() % 3 + 1;
                do{
                        printf("请输入锤子的位置:");
                        scanf("%d %d",&posy,&posx);
                }while(posy < 1 || posy > 3 || posx < 1 || posx > 3);
        //修改打中和错过的个数
        if(mousey == posy && mousex == posx){
                hits++;
        }else{
                missed++;
        }
        //打印地图
                for(row = 1;row <= 3;row++){
                        for(col = 1;col <= 3;col++){
                                if(row == posy && col == posx){
                                        printf("O ");
                                }
                                else if(row == mousey && col == mousex){
                                        printf("X ");
                                }
                                else{
                                        printf("* ");
                                }
                        }
                        printf("\n");
                }
                //提示是否打中
                if(mousey == posy && mousex == posx){
                        printf("恭喜你,打中了!\n");
                }
                else{
                        printf("很遗憾,没有打中\n");
                }
                //打印总成绩
                printf("打中%d次,错过%d次\n",hits,missed);
        }
        return 0;
}END
语言的学习是日积月累的结果,需要集中的精力和日复一日的坚持。当然我要学习的东西还有很多,c语言的学习只是开始,我相信我还会学习到更多有价值的知识,而这篇博客也会成为见证我成长的开始,加油吧!相信自己一定行!
回复

使用道具 举报

0

主题

11

帖子

19

积分

新手上路

Rank: 1

积分
19
发表于 4 天前 | 显示全部楼层
沙发???
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表