#include
#include
#include
int main()
{
int times;//游戲的次數(shù)
int mousex=0;//老鼠的X軸坐標(biāo)
int mousey=0;//老鼠的Y軸坐標(biāo)
int posx=0;//錘子的X軸坐標(biāo)
int posy=0;//錘子的Y軸坐標(biāo)
int hits=0;//統(tǒng)計擊中老鼠的次數(shù)
int missed=0;//統(tǒng)計擊錯的次數(shù)
int i,row,col;//循環(huán)打老鼠效果的變量
//1.九個老鼠洞以及打老鼠的次數(shù)
srand(time(0));//隨機種子
printf("□□□\n□□□\n□□□\n");
printf("請輸入打老鼠的次數(shù):\n");
scanf("%d",×);
//2.循環(huán)多次打老鼠的效果
for(i=1;i<=times;i++)
{
//老鼠的隨機坐標(biāo)
mousex=rand()%3+1;//老鼠的X軸坐標(biāo)從1-3的隨機數(shù)
mousey=rand()%3+1;//老鼠的Y軸坐標(biāo)從1-3的隨機數(shù)
//判斷錘子的使用范圍
do
{
printf("請輸入錘子的坐標(biāo):\n");
scanf("%d%d",&posx,&posy);
}while(posx<1 || posx>3 || posy<1 || posy>3);
//判斷錘子和老鼠的坐標(biāo)是否一致
if(mousex==posx && mousey==posy)//擊中老鼠
hits++;
else
missed++;
//循環(huán)打印地圖
for(row=1;row<=3;row++)
{
for(col=1;col<=3;col++)
{
if(row==posy&&col==posx)//錘子的坐標(biāo)
printf("■");
else if(row==mousey&&col==mousex)//老鼠的坐標(biāo)
printf("☆");
else
printf("□");
}
printf("\n");
}
printf("擊中%d次,擊錯%d次\n",hits,missed);
}
return 0;
}
-
C語言編程
+關(guān)注
關(guān)注
6文章
90瀏覽量
21152
原文標(biāo)題:C語言模擬打地鼠小游戲
文章出處:【微信號:cyuyanxuexi,微信公眾號:C語言編程學(xué)習(xí)基地】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
相關(guān)推薦
labviEW之打地鼠游戲
打地鼠小游戲
打地鼠游戲LABVIEW
打地鼠游戲
C語言小游戲源碼分享
11個按鍵+LCD1602顯示器+9個燈+蜂鳴器打地鼠小游戲
Atmega16 AVR 單片機 proteus 仿真 打地鼠 游戲
![Atmega16 AVR 單片機 proteus 仿真 <b class='flag-5'>打</b><b class='flag-5'>地鼠</b> <b class='flag-5'>游戲</b>](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
評論