《C#面向对象程序设计》课程设计说明.docx
《《C#面向对象程序设计》课程设计说明.docx》由会员分享,可在线阅读,更多相关《《C#面向对象程序设计》课程设计说明.docx(17页珍藏版)》请在冰点文库上搜索。
《C#面向对象程序设计》课程设计说明
21点游戏程序设计说明
1、功能:
一个21点小游戏,游戏开始时,如下第一张截图显示,然后按下开始,再按下发牌,就会庄贤两家各发两张牌,庄家有一张牌是盖着的,接下来贤家根据自己的点数决定要不要据需要牌。
如果点数大于21点则这一句就输了,扣25块的金额,如果按不要,且没大于21点,则庄家盖着的牌翻过了,然后庄贤两家的点数进行比较,谁的点数最接近21点则谁赢这一局。
2、主要实现方法:
定义发牌函数:
fapai()来产生一张牌,通过然后将牌所对应的图片通过pictureBox.ImageLocation=..............放入pictureBox中。
添加两个timer空间,timer1来控制发牌时庄贤两家各发的两种牌。
Timer2控制当贤家点击不要之后庄家是否继续要牌,所以根据人的思维,在timer2中,只有当庄家的点数小于16点时才继续要牌,否则则不要了,与贤家进行比较,定义了两个函数showwin()和showloss()分别来显示输了个赢了一局。
然后还有check()和comper()两个函数,check()来检查发牌和要牌过程中庄家和贤家的点数是否大于了21点。
Comper()函数则用来最后庄贤两家的分数进行比较时看谁的分数更接近21点。
还添加了一个规则窗体来显示游戏规则。
运行截图:
程序源代码:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
namespace_1点游戏vs2005版
{
publicpartialclassForm1:
Form
{
publicForm1()
{
InitializeComponent();
label4.Text=moneyNew.ToString();
}
privatevoidForm1_Load(objectsender,EventArgse)
{
button1.Hide();
button2.Hide();
button3.Hide();
button4.Show();
}
Randomra=newRandom();
stringstr;
intcount1=0;
inttimer1count=0;
inttimer2count=0;
intnum;
intscore_Zhuang=0;
intscore_Xian=0;
stringjpg=".jpg";
stringdizhi="image\\";
intmoney=100;
intmoneyNew=100;//通过改money和moneyNew可改初始的钱数;
stringpcdi;
规则f1=new规则();
publicvoidfapai()
{
intpai;
pai=ra.Next(1,5);
if(pai==1)
{
num=ra.Next(1,14);
str="m"+num.ToString();
if(num==1)
num=11;
if(num==11)
{
str="mj";
num=10;
}
if(num==12)
{
str="mq";
num=10;
}
if(num==13)
{
str="mk";
num=10;
}
}
if(pai==2)
{
num=ra.Next(1,14);
str="f"+num.ToString();
if(num==1)
num=11;
if(num==11)
{
str="fj";
num=10;
}
if(num==12)
{
str="fq";
num=10;
}
if(num==13)
{
str="fk";
num=10;
}
}
if(pai==3)
{
num=ra.Next(1,14);
str="h"+num.ToString();
if(num==1)
num=11;
if(num==11)
{
str="hj";
num=10;
}
if(num==12)
{
str="hq";
num=10;
}
if(num==13)
{
str="hk";
num=10;
}
}
if(pai==4)
{
num=ra.Next(1,14);
str="r"+num.ToString();
if(num==1)
num=11;
if(num==11)
{
str="rj";
num=10;
}
if(num==12)
{
str="rq";
num=10;
}
if(num==13)
{
str="rk";
num=10;
}
}
}
publicvoidrestart()//重新开始
{
score_Zhuang=0;
score_Xian=0;
label1.Text="0";
count1=0;
timer2count=0;
button1.Hide();
button2.Hide();
button3.Hide();
label4.Text=money.ToString();
if(money<=0)
{
MessageBox.Show("GameOver!
YouLoss!
");
newGamer();
button3.Hide();
money=moneyNew;
label4.Text=money.ToString();
}
if(money>=2*moneyNew)
{
MessageBox.Show("Congratulations!
YouWin!
");
newGamer();
button3.Hide();
money=moneyNew;
label4.Text=money.ToString();
}
label6.Text="0";
button4.Show();
}
voidshowwin()
{
MessageBox.Show("你赢了25元!
\n继续吗?
","你赢了这盘!
",MessageBoxButtons.YesNo);
money+=25;
restart();
}
voidshowloss()
{
MessageBox.Show("你输了25元!
\n继续吗?
","你输了这盘!
",MessageBoxButtons.YesNo);
money-=25;
restart();
}
voidcheck(inti,intj)
{
if(i>21)
{
showwin();
}
if(j>21)
{
showloss();
}
}
voidcomper(inti,intj)
{
if(i>j)
{
showloss();
}
elseif(i{
showwin();
}
else
{
MessageBox.Show("平局");
restart();
}
}
privatevoidtimer1_Tick_1(objectsender,EventArgse)//控制刚开始发牌时发的四张牌。
{
if(timer1count==0)
{
fapai();
pictureBox1.ImageLocation=dizhi+str+jpg;
score_Xian=score_Xian+num;
}
if(timer1count==1)
{
fapai();
pictureBox7.ImageLocation=dizhi+"di"+jpg;
score_Zhuang+=num;
pcdi=str;
}
if(timer1count==2)
{
fapai();
pictureBox2.ImageLocation=dizhi+str+jpg;
score_Xian=score_Xian+num;
label1.Text=score_Xian.ToString();
}
if(timer1count==3)
{
fapai();
pictureBox8.ImageLocation=dizhi+str+jpg;
score_Zhuang+=num;
}
timer1count+=1;
if(timer1count>3)
{
timer1.Stop();
timer1count=0;
}
}
privatevoidtimer2_Tick_1(objectsender,EventArgse)//控制庄家的要牌动作;
{
if(score_Zhuang<=15)
{
timer2count++;
if(timer2count==1)
{
fapai();
pictureBox9.ImageLocation=dizhi+str+jpg;
score_Zhuang+=num;
}
if(timer2count==2)
{
fapai();
pictureBox10.ImageLocation=dizhi+str+jpg;
score_Zhuang+=num;
}
if(timer2count==3)
{
fapai();
pictureBox11.ImageLocation=dizhi+str+jpg;
score_Zhuang+=num;
}
if(timer2count==4)
{
fapai();
pictureBox12.ImageLocation=dizhi+str+jpg;
score_Zhuang+=num;
}
}
elseif(score_Zhuang>15&&score_Zhuang<=21)
{
timer2.Stop();
label6.Text=score_Zhuang.ToString();
comper(score_Zhuang,score_Xian);
}
else
{
timer2.Stop();
label6.Text=score_Zhuang.ToString();
showwin();
}
}
voidnewGamer()
{
pictureBox1.Image=null;
pictureBox2.Image=null;
pictureBox3.Image=null;
pictureBox4.Image=null;
pictureBox5.Image=null;
pictureBox6.Image=null;
pictureBox7.Image=null;
pictureBox8.Image=null;
pictureBox9.Image=null;
pictureBox10.Image=null;
pictureBox11.Image=null;
pictureBox12.Image=null;
button3.Show();
button4.Hide();
button4.Text="开始";
}
privatevoidbutton4_Click_1(objectsender,EventArgse)//开始
{
newGamer();
}
privatevoidbutton3_Click_1(objectsender,EventArgse)//发牌
{
button3.Hide();
button1.Show();
button2.Show();
timer1.Start();
}
privatevoidbutton1_Click_1(objectsender,EventArgse)//要牌
{
count1++;
if(count1==1)
{
fapai();
pictureBox3.ImageLocation=dizhi+str+jpg;
score_Xian+=num;
label1.Text=score_Xian.ToString();
check(score_Zhuang,score_Xian);
}
if(count1==2)
{
fapai();
pictureBox4.ImageLocation=dizhi+str+jpg;
score_Xian+=num;
label1.Text=score_Xian.ToString();
check(score_Zhuang,score_Xian);
}
if(count1==3)
{
fapai();
pictureBox5.ImageLocation=dizhi+str+jpg;
score_Xian+=num;
label1.Text=score_Xian.ToString();
check(score_Zhuang,score_Xian);
}
if(count1==4)
{
fapai();
pictureBox6.ImageLocation=dizhi+str+jpg;
score_Xian+=num;
label1.Text=score_Xian.ToString();
check(score_Zhuang,score_Xian);
}
}
privatevoidbutton2_Click_1(objectsender,EventArgse)//不要
{
pictureBox7.ImageLocation=dizhi+pcdi+jpg;
timer2.Start();
}
privatevoid游戏规则ToolStripMenuItem_Click_1(objectsender,EventArgse)
{
f1.ShowDialog();
}
privatevoid开始SToolStripMenuItem_Click_1(objectsender,EventArgse)
{
newGamer();
}
privatevoid退出XToolStripMenuItem1_Click_1(objectsender,EventArgse)
{
Application.Exit();
}
}
}