纸牌游戏设计制作《摸鱼2》(C语言)

纸牌游戏设计制作《摸鱼2》

此游戏设计属于简单的纸牌游戏,是儿童益智类游戏。适用于儿童的认知教育。
游戏规则极为简单,设置纸牌在界面上显示牌背的盲牌形式,点击牌背显示牌面找出相同的牌配对消牌。
这设置主要培养儿童的心理素质,在少儿心理的成长和发展阶段,逐步完善心理的感知和认知,意识和思维,注意力和记忆。盲牌形式主要培养记忆。
多玩游戏,在游戏中学习,对儿童少儿心理发展有所裨益。
此游戏可用于儿童心理辅导,培养儿童的注意力和短期记忆。
程序设置数字标示和动物图案标示,此设置能吸引儿童,提高游戏兴趣,达到培养目的。
程序只设置了2例图案,可另行增加图案。
扩展程序功能,如初级的明牌找配对,中级盲牌找配对,高级就增加难度,15对增加到25对找配对。
程序有游戏时间设置,有计分设置,此是游戏的考量。有点竞技的味道。

下面是纸牌游戏设计的通用部分:
shuffle_cards ( ){    //  洗牌设计       }
cardback ( ){    //发牌时隐藏牌面显示牌背图案    }
drawcard ( ){   //传入牌序号num转为牌点和牌花色,画出牌张     }
print_t ( ){   //打印标题提示等文字 
                     //**  参数:位置 sx ,  sy,字串 t$,文字尺寸 tsize }

我多年程序设计的方法就是崇尚简捷简洁,用最简单的语言写图形界面,设计最简单的算法达到程序要求。卡牌制作和文字显示可作为卡牌游戏程序的通用功能函数。程序设计提供了界面设计图案和文字显示方法,卡牌制作,洗牌设计和定位画出卡牌的方法。

程序设计的是手机界面,代码是用MySpringC编译器在安卓手机上编写的,简单的语言表述,设计思路适用于初学者。可制作成安卓手机桌面app应用程序。
程序提供了一个框架,VB ,  VS 或 JAVA  改写可作为参考。
此样例可复制粘贴到编译器直接使用。


//以下是源码:
//******************************************
//*    纸牌游戏《摸鱼 2》  Version   2.1.5        
//*    设计制作:张纯叔   ( micelu@126.com )  
//******************************************
Canvas cs ;       //画布
string sBarDes[10];
int nBarId[10];
float src[4];  //ClearDraw (cls) clear screen
string s,ss,ss0,ss1,ss2 ; 
string s1,s2,s3,s4,s5 ;      //print text
int sx,sy,dx,dy,px,py;      //draw
int i,j,n,t,k;    //t = times
int num;      //select card number
int pn ;        //发牌序号数
int dn,p1,p2,px1,py1,px2,py2 ;  
int pnum[54];    //洗牌后的牌序列(54张扑克牌)
int cardnum,cardstyle;   //牌点牌花色
string cas1,cas2,cas4;   //牌点牌花色图案$
string cas3,cas5 ;            //牌背花色, 下注图案
double Rn;    //random number
string t$;     //提示文字
int tsize;      //textsize
int c1,c2,c3,c4,c5,c6 ;
int isdo ;    //控制操作:  1 可操作 , 0 不可操作
int context;    //canvasProc
int obj;
int id;
int event;      //canvasProc
int kn ;
int fn[54];    //计分,可用于54张扑克牌
int tim[3];   //get Time & show finished time
 int thh,tmm,tss;
 int oldhh,oldmm,oldss;
 int newhh,newmm,newss;
 int mms;               //show using time
 string hhts,mmts,ssts;
 string ts,ts1,ts2;    
int round,rnn,total ;   //show score
int mode ;      //设置卡牌花色
string sx$[20] ;     //预设置纸牌图案
string sxa$[20] ;     //预设置纸牌图案

main(){
setDisplay(1);
  cs.SetBackground(0,120,0);
  cs.Active();
  cs.SetProc (context, mycanvasProc);
   sBarDes[0]="纸牌花色";
   nBarId[0]=100;
   sBarDes[1]="开始摸鱼 ";
   nBarId[1]=101;
   sBarDes[2]=" ";
   nBarId[2]=102;
   sBarDes[3]="  ";
   nBarId[3]=103;
   sBarDes[4]="退出程序";
   nBarId[4]=104;
   sBarDes[5]="📱" ;
   nBarId[5]=105;
   setToolBarHeight(6);
   setButtonTextSize(13);
   setToolBarBackgroundColor(255,0,120,0);
   setButtonColor(255,0,0,240);
   setButtonTextColor(255,255,255,0);
   setToolBar(100,myToolBarProc,sBarDes,nBarId,6);
  setTitle("纸牌游戏-摸鱼 2 ");
//**********
//**** 图案采用表情图案,
//**** VB,VC可采用icon图标或其他图片
      cas3="🐳";         //预设牌背图案
sx$[1]="🐭" ; sx$[2]="🐮" ; sx$[3]="🐯" ; sx$[4]="🐰" ;
sx$[5]="🐉" ; sx$[6]="🐍" ; sx$[7]="🦄" ; sx$[8]="🐑" ;
  sx$[9]="🐵" ; sx$[10]="🐓" ; sx$[11]="🐶" ;
sx$[12]="🐷" ; sx$[13]="🐋" ; sx$[14]="🐠" ;
sx$[15]="🐡" ;  
sxa$[1]="🍎" ; sxa$[2]="🍇" ; sxa$[3]="🍊" ; 
sxa$[4]="🍈" ; sxa$[5]="🍑" ; sxa$[6]="🍐" ; 
sxa$[7]="🍓" ; sxa$[8]="🍋" ; sxa$[9]="🍌" ;
sxa$[10]="🍉" ; sxa$[11]="🍏" ;
sxa$[12]="🍁" ; sxa$[13]="☘" ; 
sxa$[14]="🍂" ; sxa$[15]="🌿" ;  
      drawcover ();
      round=1 ;  total=0 ;    //start round
      isdo=0 ;
 while (){}
  }//main ()

e0873bcf5e434f5ca47cf6df8769931d.png

 

drawcover (){     // 画启动界面封面版本号
     cs.SetColor (255,0,120,0);  //back color
     cs.DrawRect (2,2,718,1030);
     cs.SetColor (255,0,250,0);
     cs.DrawRect (50,320,650,326);
     cs.SetColor (255,250,0,0);
     cs.DrawRect (52,322,648,324);
    px=300;    py=75;   //tiger
        num=3 ;   
        drawcard ();
     px=400;    py=75;   
        cardback ();
     px=500;    py=75;   
        num=3 ;   
        drawcard ();
      px=500;    py=185;   
         cardback ();
     t$=cas3 ;   //鲸鱼图
     cs.SetTextSize (120);
     cs.DrawText (t$,470,360);      
     t$="纸牌游戏" ;
     sx=100;   sy=160;   tsize=60;
     print_t ();
     t$="摸 鱼" ;
     sx=100;   sy=260;   tsize=80;
     print_t ();
     t$="( 记忆培养 )" ;
     sx=250;   sy=260;   tsize=40;
     print_t ();
     t$="欢迎加入纸牌游戏团" ;
     sx=135;   sy=440;   tsize=50;
     print_t1 ();
     cs.SetTextStyle (0);    //0正常,1粗体
     ss="@Copyright  v. 2 . 1 . 5    micelu@126.com ";
     cs.SetTextSize (22);
     cs.SetColor (255,250,250,250);
     cs.DrawText (ss,50,360);      
     cs.Update ();
     isdo=0 ;
}//drawcover ()

mycanvasProc (int context,int obj,int id,int event,float x,float y){
       if (isdo==0) return ;
  if (event==0){ //get  touch select 2 pieces
            kn=kn+1;
            if (kn>2) kn=1 ;       
  //** get piece position X Y to select cards
     dx=(int)((x+50)/100) ;     //纸牌排列坐标
     dy=(int)((y+100)/110) ;
  if (dx<1) dx=1 ;
  if (dx>6) dx=6 ;    //防越界
  if (dy<2) dy=2 ;
  if (dy>6) dy=6 ;    //防越界
         pn= (dy-2)*6+dx-1;    //还原牌序号
         num=pnum[pn] ;
         px=(dx-1)*105+50 ;    //调整画牌left,top
         py=(dy-2)*120+100;
                drawcard () ;       //显示牌面
                  sleep (500) ;
                cardback () ;         //显示牌背
   // cs.SetColor (255,0,220,0);  //clear text
  //  cs.DrawRect (0,840,450,870);  //test
    // cs.SetTextSize (24);
    if ( fn[num]==1){         
        //cs.SetColor (255,250,250,0);
      //  s1="fn = "+intToString (fn[num] ) ;     
         //cs.DrawText (s1,50,860);   
     cs.SetColor (255,0,120,0);  // 覆盖已消牌
      cs.DrawRect (px,py,px+100,py+110);          }
                   }      //do event

         cs.SetFillMode (1);
    cs.SetTextStyle (0);    //0正常,1粗体
    cs.SetColor (255,0,120,0);  //clear
    cs.DrawRect (0,0,720,80);
        t$=ts ;    //start time
        sx=15;   sy=40;   tsize=24;
            print_t ();   
        t$="第 "+intToString (round)+" 局" ;    
        sx=260;   sy=40;   tsize=30;
            print_t ();   
        s=intToString(dn);
        t$="得分: "+s+ " 分" ;    
        sx=520;   sy=40;   tsize=30;
            print_t ();   
        t$=intToString (total) ;    
        sx=390;   sy=40;   tsize=30;
            print_t ();   
       cs.Update () ;
       catchit () ;   //找配对
 }//mycanvasProc ()

192a2baaf09244dba2d7ad03a337717a.png

 

catchit (){    //找配对
      if (kn==1){
         pn= (dy-2)*6+dx-1;    //还原牌序号
         num=pnum[pn] ;
         p1=num ;
         c1=(num-num/15*15) ;
         px=(dx-1)*105+50 ;    //调整画牌left,top
         py=(dy-2)*120+100;
         px1=px ;   py1=py ;     }

     if (kn==2){
         pn= (dy-2)*6+dx-1;    //还原牌序号
         num=pnum[pn] ;
         p2=num ;
         c2=(num-num/15*15) ;
         px=(dx-1)*105+50 ;    //调整画牌left,top
         py=(dy-2)*120+100;
         px2=px ;   py2=py ;      }

   if (p1>p2||p1<p2&&c1==c2){
       cs.SetColor (255,0,120,0);  //擦除配对牌
            fn[p1]=1 ;    //消牌计分编码
            fn[p2]=1 ;
     cs.DrawRect (px1,py1,px1+100,py1+110);
     cs.DrawRect (px2,py2,px2+100,py2+110);
     cs.Update () ;      
             }
 
     dn=0 ;      //计分
     for (k=0;k<30;k++){ 
            if (fn[k]==1) dn=dn+2 ;         }
 //游戏任务完成,完成 1 局计 1000 分
     total=(round-1)*1000+dn ;      
     if (dn==60) {   //finished游戏完成计时计分
         t$="摸鱼完成  🐳" ;
         sx=140;   sy=360;   tsize=80;
         print_t ();   
    cs.SetColor(255,0,120,0); 
    cs.DrawRect (4,4,718,60);  // clear print x y
        t$=ts ;    //开始时间
        sx=30;   sy=40;   tsize=24;
            print_t ();   
        t$="第 "+intToString (round)+" 局" ;    
        sx=260;   sy=40;   tsize=30;
            print_t ();   
        total= round*1000 ;      
        t$=intToString (total) ;    
        sx=390;   sy=40;   tsize=30;
            print_t ();   
        s=intToString(dn);
        t$="得分: "+s+ " 分" ;    
        sx=520;   sy=40;   tsize=30;
            print_t ();   
     showtimes () ;   //finished & show times
         t$=ts1 ;
         sx=200;   sy=460;   tsize=40;
             print_t ();   
         t$=ts2 ;
         sx=200;   sy=500;   tsize=40;
             print_t ();   
         round=round+1 ;
         isdo=0 ;     }    //finished

}//catchit ()

shuffle_cards (){    //洗牌设计
     cs.SetColor (255,0,120,0);   //backcolor
     cs.DrawRect (2,2,718,1000);
     cs.Update ();
      for (i=0;i<30;i++){ pnum[i]=0 ;  fn[i]=0 ;  }
      pn=0;  
//**** 洗牌 **********************
      t$="游戏开始 >>>   洗牌   开始摸鱼 " ;
      sx=50;   sy=50;    tsize=30;
      print_t ();
  for (i=0;i<30;i++){    //洗牌
      ResetPiece:        //已生成牌有相同则重新生成
      Rn=random()*30;       //随机洗牌
             num=(int )(Rn) ;
             pnum[i]=num ;
         for (k=0;k<i; k++){
         if (num==pnum[k]) goto ResetPiece ;        }
                    }

   for (k=0;k<30;k++){  //显示牌背面图案
        px=(k-k/6*6)*100+50 ;
        py=k/6*120+100;
  //cardback ();  //洗牌后可显示牌背或不显示
  //num=pnum[k] ;
  //drawcard ();  //用于洗牌查验测试,游戏时注释掉
          }
 }//shuffle_cards ()

c939791af97e4c109568ba1f83108a89.png

057a7d7020ae466b98c866d9d2033fd4.png 

 

start (){    //开始游戏
      cs.SetColor (255,0,120,0);   //backcolor
      cs.DrawRect (2,2,718,1000);
        t$="纸牌游戏 >>     开始摸鱼" ;
        sx=50;   sy=50;   tsize=30;
        print_t ();
    for (k=0;k<30;k++){  //显示牌面或牌背
        num=pnum[k] ;
        px=(k-k/6*6)*105+50;
        py=k/6*120+100;
        //drawcard () ;     //明牌显示初级游戏操作
        cardback () ;         //暗牌显示牌背记忆培养
                 }

           dn=0 ;   //计分数 ,  init
           isdo=1 ;
  getTime (tim);      //取得系统时间
       oldhh=tim[0];
       oldmm=tim[1];
       oldss=tim[2];
   hhts=intToString (tim[0]);
   mmts=intToString (tim[1]);
   ssts=intToString (tim[2]);
   if (oldhh<10)hhts="0"+hhts;   //格式化显示
   if (oldmm<10)mmts="0"+mmts;
   if (oldss<10)ssts="0"+ssts;
       ss2=hhts+" : "+mmts+" : "+ssts;
   ts="开始 >  "+ss2;   //show start time
}//start ()

071f927d0aa242c793b49ed0bcc3dc66.png

 

drawcard (){  //num 转换 cardnum 画卡牌
//传入牌序号num转为牌点,传入px,py 画出牌张
//此为游戏通用设置,启动界面画牌
    //**** draw cards 圆角 ****
      cs.SetColor (255,0,200,0);  //选色  框线
      cs.DrawRect (px+5,py,px+93,py+107);
      cs.DrawRect (px,py+5,px+98,py+101);
      cs.DrawCircle (px+5,py+5,5);
      cs.DrawCircle (px+5,py+102,5);
      cs.DrawCircle (px+92,py+5,5);
      cs.DrawCircle (px+92,py+102,5);

      cs.SetColor (255,250,250,250);  //牌面白色
      cs.DrawRect (px+7,py+2,px+91,py+106);
      cs.DrawRect (px+1,py+7,px+96,py+100);
      cs.DrawCircle (px+6,py+7,5);
      cs.DrawCircle (px+6,py+101,5);
      cs.DrawCircle (px+91,py+7,5);
      cs.DrawCircle (px+91,py+101,5);
     //******* 画出牌面
        cardnum=(num-num/15*15);
        if (cardnum==0) cardnum=15;
        cas1=intToString(cardnum);
        cs.SetTextSize (30);
     n=0;
     if (cardnum>9) n=10;           
      cs.SetColor (255,250,0,0);  //牌面数字
     cs.DrawText (cas1,px+40-n,py+30);
        cs.SetTextSize (50);    //牌面2例图案
     if (mode==0) cas4=sx$[cardnum] ;
     if (mode==1) cas4=sxa$[cardnum] ;
     cs.DrawText (cas4,px+20,py+80);

         if (fn[num]==1) {
      cs.SetColor (255,0,120,0);  //擦除已配对牌张
      cs.DrawRect (px,py,px+100,py+110);   }    
          cs.Update ();
}//drawcard ()

showcards (){     //测试显示纸牌图案
     cs.SetColor (255,0,120,0);   //backcolor
      cs.DrawRect (2,2,718,1000);
        t$="纸牌游戏 >>     画牌验牌 " ;
        sx=50;   sy=50;   tsize=30;
        print_t ();
    for (k=0;k<30;k++){  //显示牌面或牌背
          num=k ;       
          px=(k-k/6*6)*105+50;
          py=k/6*120+100;
          drawcard () ;   
                 }
     cs.Update ();
}//showcards ()

cardback (){   //draw cards back picture
//发牌时隐藏牌面显示牌背图案
   // if (num==-1) return ;
    //**** draw cards 圆角 ****
      cs.SetColor (255,0,200,0);  //选色  框线
      cs.DrawRect (px+5,py,px+93,py+107);
      cs.DrawRect (px,py+5,px+98,py+101);
      cs.DrawCircle (px+5,py+5,5);
      cs.DrawCircle (px+5,py+102,5);
      cs.DrawCircle (px+92,py+5,5);
      cs.DrawCircle (px+92,py+102,5);

      cs.SetColor (255,220,180,20);  //牌背色
      cs.DrawRect (px+7,py+2,px+91,py+106);
      cs.DrawRect (px+1,py+7,px+96,py+100);
      cs.DrawCircle (px+6,py+7,5);
      cs.DrawCircle (px+6,py+101,5);
      cs.DrawCircle (px+91,py+7,5);
      cs.DrawCircle (px+91,py+101,5);
 
      cs.SetTextSize(50);   //显示牌背图案
      cs.DrawText (cas3,px+20,py+70);  // 背花
      cs.Update ();
}//cardback ()

showtimes (){    //计算使用时间
      getTime (tim);
      newhh=tim[0];
      newmm=tim[1];
      newss=tim[2];
      hhts=intToString (tim[0]);
      mmts=intToString (tim[1]);
      ssts=intToString (tim[2]);
      if (newhh<10)hhts="0"+hhts;
      if (newmm<10)mmts="0"+mmts;
      if (newss<10)ssts="0"+ssts;
 mms=((newhh-oldhh)*3600)+
        ((newmm-oldmm)*60)+((newss-oldss));    
    if (mms<0) mms=mms+864000;
        thh=mms/3600;
        tmm=(mms-thh*2600)/60;
        tss=mms-thh*3600-tmm*60;
 ss1=intToString (tmm)+" 分  "+intToString (tss)+" 秒";
 ss2=hhts+" : "+mmts+" : "+ssts;
      ts1= "完成 >  "+ss2;      //输出
      ts2= "用时 >   "+ss1;     //输出
 }//showtimes ()

set_mode (){    //设置2种纸牌花色,切换
       mode=mode+1 ;  
  if (mode>1) mode=0 ;
      cs.SetColor (255,0,120,0);   //backcolor
      cs.DrawRect (2,2,718,1000);
        t$="纸牌游戏 >>     选择卡牌花色" ;
        sx=50;   sy=50;   tsize=30;
        print_t ();
    for (k=0;k<30;k++){    //显示卡牌花色
        px=(k-k/6*6)*105+50;
        py=k/6*120+110;
        num=k ;       //洗牌后 pnum[k] ;
        drawcard () ;
              }
}//set_mode ()

print_t (){   //打印标题提示等文字
//**  参数 sx,sy, ss0=$, tsize=text size   
 cs.SetFillMode (1);//0不填色,1填色
    cs.SetTextStyle (0);    //0正常,1粗体
 cs.SetTextSize (tsize);
    cs.SetColor (255,0,200,60);
 cs.DrawText (t$,sx+2,sy+3);
    cs.SetColor (255,250,120,0);
 cs.DrawText (t$,sx,sy);
 cs.SetFillMode (0);//0不填色,1填色
    cs.SetColor (255,250,250,0);
 cs.DrawText (t$,sx,sy);
 cs.SetFillMode (1);//0不填色,1填色
      cs.Update ();
}//print _t ()

print_t1 (){   //打印标题提示等文字
//**  参数 sx,sy, ss0=$, tsize=text size   
  cs.SetFillMode (1);//0不填色,1填色
    cs.SetTextStyle (1);    //0正常,1粗体
  cs.SetTextSize (tsize);
    cs.SetColor (255,0,20,100);
 cs.DrawText (t$,sx+3,sy+4);
    cs.SetColor (255,0,250,0);
 cs.DrawText (t$,sx,sy);
 cs.SetFillMode (0);//0不填色,1填色
    cs.SetColor (255,250,0,0);
 cs.DrawText (t$,sx,sy);
 cs.SetFillMode (1);//0不填色,1填色
      cs.Update ();
}//print _t1 ()

myToolBarProc(int nBtn,int nContext){
      if(nBtn==100){// 卡牌花色
           setDisplay (1);
           set_mode () ;
              }  
      if(nBtn==101){// 开始摸鱼
           setDisplay (1);
           shuffle_cards () ;    //洗牌
           start () ;                    //开始
               }
      if(nBtn==102){//
            setDisplay (1);
            //test ****
          // shuffle_cards () ;    //洗牌
           //showcards () ;     
                }
      if(nBtn==103){//
           setDisplay (1);
                }
      if(nBtn==104){//退出程序
          clearOutput();
          cs.ClearDraw (0,src);
          setDisplay (0);
          exit (0);
               }
     if(nBtn==105){//@About
            setDisplay (1);
            drawcover ();
                 }
}//myToolBar ()


//纸牌游戏《摸鱼 2》V.2.1.0   设计制作
// 2022年10月20日完成于上海

 

//**** End ****

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值