纸牌游戏程序摸鱼设计制作
此游戏设计属于简单的卡牌游戏,是儿童益智类游戏。适用于儿童的认知教育。
游戏规则很简单:找配对的牌消去。
这设置主要培养儿童的心理素质,在少儿心理的成长和发展阶段,逐步完善心理的感知和认知,意识和思维,注意力和记忆。多玩游戏,在游戏中学习,对儿童少儿心理发展有所裨益。
游戏规则极为简单,如加些通道要求,就变成连连看游戏了。如设置卡牌在界面上显示牌背的盲牌形式,点击牌背显示牌面配对消牌,就真成了摸鱼了。这主要培养注意力和短期记忆。
卡牌算法设计:54张牌面制作显示。洗牌,随机显示52张牌。牌序0到53换算为A234...到JQK,加大小王。程序设计了漂亮友好的游戏界面。界面上纸牌显示圆角牌背牌面。界面提示文字显示。
下面是纸牌游戏设计的通用部分:
shuffle_cards ( ){ // 洗牌设计 }
cardback (){ //发牌时隐藏牌面显示牌背图案 }
transcards ( ){ //传入牌序号num转为牌点和牌花色,画出牌张 }
print_t ( ){ //打印标题提示等文字
//** 参数:位置 sx , sy,字串 t$,文字尺寸 tsize }
我多年程序设计的方法就是崇尚简捷简洁,用最简单的语言写图形界面,设计最简单的算法达到程序要求。卡牌制作和文字显示可作为卡牌游戏程序的通用功能函数。程序设计提供了界面设计图案和文字显示方法,卡牌制作,洗牌设计和定位画出卡牌的方法。为提高玩家的兴趣,程序提供了2套图案方法,以及完成游戏使用时间显示。
游戏代码是用MySpringC编译器在安卓手机上编写的,简单的语言表述,设计思路适用于初学者。可制作成安卓手机桌面app应用程序。
程序提供了一个框架,VB , VC, VS 或 JAVA 改写可作为参考。
此样例可复制粘贴到编译器直接使用。
//以下是源码:
//*********************************************
//* 纸牌游戏《摸鱼》 Version 1.1.0 *
//* 制作:张纯叔 ( 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]; //洗牌后的牌序列
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]; //计分
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 ; //设置卡牌花色
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("纸牌游戏-摸鱼 ");
//**********
cas3="🐡"; //预设牌背图案
drawcover ();
round=0 ; total=0 ; //start round
isdo=0 ;
while (){}
}//main ()
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=80; //A
num=13 ;
transcards ();
px=400; py=90; //A
num=0;
transcards ();
px=600; py=120; //K
num=25;
transcards ();
px=500; py=130; //K
num=51;
transcards ();
t$=cas4 ;
sx=450; sy=350; tsize=150;
print_t ();
t$="纸牌游戏" ;
sx=100; sy=160; tsize=60;
print_t ();
t$="摸 鱼" ;
sx=100; sy=260; tsize=80;
print_t ();
t$="欢迎加入纸牌游戏团" ;
sx=135; sy=440; tsize=50;
print_t1 ();
ss="Copyright v.1.1.0 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-10)/80) ; //纸牌排列坐标
dy=(int)((y+100)/140) ;
if (dy<1) dy=1 ;
if (dy>6) dy=6 ; //防越界
cs.SetFillMode (1);
cs.SetTextStyle (0); //0正常,1粗体
cs.SetColor(255,0,120,0);
cs.DrawRect (4,4,718,60); // clear print x y
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 ()
catchit (){
if (kn==1){
pn= (dy-1)*9+dx ; //还原牌序号
num=pnum[pn] ;
p1=num ;
c1=(num-num/13*13) ;
px=dx*78+8 ; //传入dx,dy调整画牌left,top
py=dy*140-20;
px1=px ; py1=py ; }
if (kn==2){
pn= (dy-1)*9+dx ;
num=pnum[pn] ;
p2=num ;
c2=(num-num/13*13) ;
px=dx*78+8 ; //调整画牌left,top
py=dy*140-20;
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-30,px1+78,py1+95);
cs.DrawRect (px2,py2-30,px2+78,py2+95);
cs.Update () ;
}
if (p1==52&&p2==53){ //大小王对
cs.SetColor (255,0,120,0); //擦除
fn[p1]=1 ;
fn[p2]=1 ;
cs.DrawRect (px1,py1-30,px1+78,py1+95);
cs.DrawRect (px2,py2-30,px2+78,py2+95);
cs.Update () ;
}
dn=0 ; //计分
for (i=0;i<54;i++){
if (fn[i]==1) dn=dn+2 ; }
total=(round-1)*1000+dn ;
if (dn==108) { //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 ();
isdo=0 ; }
}//catchit ()
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<54;k++){ //显示卡牌花色
px=(k-k/9*9)*78+8;
py=k/9*140+120;
num=k ; //洗牌后 pnum[k] ;
if (mode==0) transcards ();
if (mode==1) setcard ();
}
}//set_mode ()
//纸牌序列:
//cardP num 0 to 12 黑桃 A234..JQK
//cardP num 13 to 25 红心 A234..JQK
//cardP num 26 to 38 梅花 A234..JQK
//cardP num 39 to 51 方块 A234..JQK
//52 大王 53 小王
// VB6,VS2007 编程:设picturebox:
// cardP(54)图片数组
// 图片高宽为单张纸牌图大小
// 预制扑克牌序列图片底图,程序启动时按序将
// 每单张图片BitBlt..SRCCOPY到图片数组,
// cardP (i)就是整付牌的图片,洗牌发牌都要用到。
// 本程序简化用 num 序号表示54张牌
//********************
shuffle_cards (){ //洗牌设计
cs.SetColor (255,0,120,0); //backcolor
cs.DrawRect (2,2,718,1000);
cs.Update ();
for (i=0;i<=53;i++){ pnum[i]=-1 ; fn[i]=0 ; }
pn=0;
//*** * 洗牌 **********************
t$="游戏开始 >>> 洗牌 开始摸鱼 " ;
sx=50; sy=50; tsize=30;
print_t ();
for (i=0;i<54;i++){ //洗牌
ResetPiece: //已生成牌有相同则重新生成
Rn=random()*54; //随机洗牌
num=(int )(Rn);
pnum[i]=num ;
for (k=0;k<i; k++){
if (num==pnum[k]) goto ResetPiece ; }
}
for (k=0;k<54;k++){ //显示牌背面图案
px=(k-k/9*9)*78+8;
py=k/9*140+120;
//cardback (); //洗牌后可显示牌背或不显示
//num=pnum[k] ;
//transcards (); //用于洗牌查验测试,游戏时注释掉
}
}//shuffle_cards ()
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<54;k++){ //显示牌面图案
num=pnum[k] ;
px=(k-k/9*9)*78+8;
py=k/9*140+120;
if (mode==0) transcards ();
if (mode==1) setcard () ; }
dn=0 ; //计数
round=round+1 ;
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
// ts1=" ";
// ts2=" ";
}//start ()
setcard (){ //num 转换 cardnum, cardstyle
//牌序号转为牌点和牌花色,传入px,py 画出牌张
//此为游戏通用设置,启动界面画牌
string sx$[15] ;
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]="🐻" ;
if (num==-1) return ;
//**** draw cards 圆角 ****
cs.SetTextStyle (0); //0正常,1粗体
cs.SetColor (255,0,60,0); //黑色框线
cs.DrawRect (px+13,py-30,px+67,py+95);
cs.DrawRect (px,py-20,px+78,py+83);
cs.DrawCircle (px+10,py-20,9);
cs.DrawCircle (px+10,py+85,9);
cs.DrawCircle (px+69,py+85,9);
cs.DrawCircle (px+69,py-20,9);
cs.SetColor (255,250,250,250); //牌面白色
cs.DrawRect (px+13,py-28,px+65,py+93);
cs.DrawRect (px+2,py-20,px+76,py+83);
cs.DrawCircle (px+12,py-18,10);
cs.DrawCircle (px+12,py+83,9);
cs.DrawCircle (px+67,py+83,9);
cs.DrawCircle (px+67,py-19,9);
//******* 画出牌面
cardnum=(num-num/13*13)+1;
cardstyle=num/13;
cas1=intToString(cardnum);
if(cardnum==1) cas1="A";
if(cardnum==11) cas1="J";
if(cardnum==12) cas1="Q";
if(cardnum==13) cas1="K";
if (cardstyle==0) cas2="♠";
if (cardstyle==1) cas2="❤";
if (cardstyle==2) cas2="♣";
if (cardstyle==3) cas2="🔶";
if (num>51){ cas2=" " ; cas1=" " ; } //大小王
if(cardstyle==0||cardstyle==2) {
cs.SetColor (255,0,0,0); }
if(cardstyle==1||cardstyle==3) {
cs.SetColor (255,250,0,0); }
cs.SetTextSize (30);
n=0;
if (cardnum==10) n=8;
cs.DrawText (cas1,px+14-n,py);
cs.DrawText (cas2,px+5,py+30);
cs.SetTextSize (26);
cas4="🐠" ;
if (num==52){ //大王
cs.SetColor(255,255,0,0) ;
cs.DrawText ("Joker",px+5,py+10);
cas4="🐳" ; }
if (num==53){ //小王
cs.SetColor(255,0,0,0) ;
cs.DrawText ("Joker",px+5,py+10);
cas4="🐬" ; }
cs.SetTextSize (48); //牌面图
if (num<52) cas4=sx$[cardnum] ;
cs.DrawText (cas4,px+16,py+72);
cs.Update ();
}//setcard ()
transcards (){ //num 转换 cardnum, cardstyle
//牌序号转为牌点和牌花色,传入px,py 画出牌张
//此为游戏通用设置,启动界面画牌
if (num==-1) return ;
//**** draw cards 圆角 ****
cs.SetTextStyle (0); //0正常,1粗体
cs.SetColor (255,0,60,0); //黑色框线
cs.DrawRect (px+13,py-30,px+67,py+95);
cs.DrawRect (px,py-20,px+78,py+83);
cs.DrawCircle (px+10,py-20,9);
cs.DrawCircle (px+10,py+85,9);
cs.DrawCircle (px+69,py+85,9);
cs.DrawCircle (px+69,py-20,9);
cs.SetColor (255,250,250,250); //牌面白色
cs.DrawRect (px+13,py-28,px+65,py+93);
cs.DrawRect (px+2,py-20,px+76,py+83);
cs.DrawCircle (px+12,py-18,10);
cs.DrawCircle (px+12,py+83,9);
cs.DrawCircle (px+67,py+83,9);
cs.DrawCircle (px+67,py-19,9);
//******* 画出牌面
cardnum=(num-num/13*13)+1;
cardstyle=num/13;
cas1=intToString(cardnum);
if(cardnum==1) cas1="A";
if(cardnum==11) cas1="J";
if(cardnum==12) cas1="Q";
if(cardnum==13) cas1="K";
if (cardstyle==0) cas2="♠";
if (cardstyle==1) cas2="❤";
if (cardstyle==2) cas2="♣";
if (cardstyle==3) cas2="🔶";
if (num>51){ cas2=" " ; cas1=" " ; } //大小王
if(cardstyle==0||cardstyle==2) {
cs.SetColor (255,0,0,0); }
if(cardstyle==1||cardstyle==3) {
cs.SetColor (255,250,0,0); }
cs.SetTextSize (30);
n=0;
if (cardnum==10) n=8;
cs.DrawText (cas1,px+14-n,py);
cs.DrawText (cas2,px+5,py+30);
cs.SetTextSize (26);
cas4="🐠" ;
if (num==52){ //大王
cs.SetColor(255,255,0,0) ;
cs.DrawText ("Joker",px+5,py+10);
cas4="🐳" ; }
if (num==53){ //小王
cs.SetColor(255,0,0,0) ;
cs.DrawText ("Joker",px+5,py+10);
cas4="🐬" ; }
cs.SetTextSize (48); //牌面图
cs.DrawText (cas4,px+16,py+72);
cs.Update ();
}//transcards ()
cardback (){ //draw cards back picture
//发牌时隐藏牌面显示牌背图案
if (num==-1) return ;
//**** draw cards 圆角 ****
cs.SetColor (255,0,60,0); //黑色框线
cs.DrawRect (px+13,py-30,px+67,py+95);
cs.DrawRect (px,py-20,px+78,py+83);
cs.DrawCircle (px+10,py-20,9);
cs.DrawCircle (px+10,py+85,9);
cs.DrawCircle (px+69,py+85,9);
cs.DrawCircle (px+69,py-20,9);
cs.SetColor (255,20,180,200); //牌背色
cs.DrawRect (px+13,py-28,px+65,py+93);
cs.DrawRect (px+2,py-20,px+76,py+83);
cs.DrawCircle (px+12,py-18,9);
cs.DrawCircle (px+12,py+83,9);
cs.DrawCircle (px+67,py+83,9);
cs.DrawCircle (px+67,py-19,9);
cs.SetTextSize(50);
cs.DrawText (cas3,px+8,py+50); //show 背花
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 ()
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);
}
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 ()
//纸牌游戏《摸鱼》V.1.1.0 设计制作
// 2022年10月9日完成于上海
//**** End ****