编了一个窗体游戏,想弄个重玩按钮,怎么让程序从头开始执行?

fan12 2012-06-27 03:24:59
写了个简单程序,窗体的

玩完一次,想重新开始,给加个“重玩”的button,然后这个button的事件处理方法里怎么写,让游戏清零重新开始?
...全文
1292 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangdong20 2012-06-30
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 的回复:]
引用 11 楼 的回复:

写一个方法void initial();
这个函数实现游戏逻辑的初始化,也就是把所有的东西回到刚开始游戏的状态
点击button时就调用initial();方法


看13楼源代码,动态的几个label(玩家手里的牌、电脑手里的牌、玩家点数、电脑点数等)是不是应该写进initial()?还有发牌洗牌也写进initial()?发牌洗牌是不是应该分别打包成方法……
[/Quote]
可以的话,尽量打包
rqx110 2012-06-28
  • 打赏
  • 举报
回复
真无语,你都能写一个游戏了。这么个功能就把你难到了? 游戏是你自己写的吗?
wangdong20 2012-06-28
  • 打赏
  • 举报
回复
写一个方法void initial();
这个函数实现游戏逻辑的初始化,也就是把所有的东西回到刚开始游戏的状态
点击button时就调用initial();方法
fan12 2012-06-28
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 的回复:]

写一个方法void initial();
这个函数实现游戏逻辑的初始化,也就是把所有的东西回到刚开始游戏的状态
点击button时就调用initial();方法
[/Quote]

看13楼源代码,动态的几个label(玩家手里的牌、电脑手里的牌、玩家点数、电脑点数等)是不是应该写进initial()?还有发牌洗牌也写进initial()?发牌洗牌是不是应该分别打包成方法,方便调用?
fan12 2012-06-28
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]

引用 9 楼 的回复:
扑克牌类和发牌洗牌是书上弄好的,我只是给搞成窗体的,那么多label、button的处理代码感觉弄的很乱

大家说的复位具体操作用什么语句实现?是用while循环吗?用goto行不行,我觉得while循环最晕了


这些就扯远了。

一个游戏是一个class吧?哪怕你封装在控件中也是一个class。你的主程序重新new一个新的实例,不久重玩了嘛。
[/Quote]

看13楼源码,我的游戏主程序是不是不应该继承form类,窗体应该另写一个类?
fan12 2012-06-28
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 的回复:]

真无语,你都能写一个游戏了。这么个功能就把你难到了? 游戏是你自己写的吗?
[/Quote]

不是,我只是把书上写的一个例子转成窗体形式的



using System;
using System.Windows.Forms;
using System.Drawing;


// 窗体



//扑克点数和花色

public enum CardSuit
{

Zero_Error,
clubs,
diamonds,
hearts,
spades
}

public enum CardValue
{
Zero_Error,
Ace,
two,
three,
four,
five,
six,
seven,
eight,
nine,
ten,
Jack,
Queen,
King
}

// Structure:card
//===========================================

struct card
{
public CardSuit suit;
public CardValue val;

public int CardValue
{
get
{
int retval;

if( (int) this.val>=10)
retval=10;
else
if ( (int) this.val==1)
retval=11;
else
retval=(int) this.val;
return retval;
}
}

public override string ToString()
{
return (string.Format("{0} of {1}",this.val.ToString("G"), this.suit.ToString("G")));
}
}

// 扑克牌类
//==========================================

class deck
{
public card[] cards=new card[53];
int next;

// deck()
//Constructor for setting up a regular deck
//==========================================
public deck()
{
next = 1;

cards[0].val = 0;
cards[0].suit = 0;

int currcard = 0;
for ( int suitctr = 1; suitctr<5;suitctr++)
{
for ( int valctr=1;valctr<14;valctr++)
{
currcard=(valctr)+((suitctr-1)*13);
cards[currcard].val=(CardValue)valctr;
cards[currcard].suit=(CardSuit) suitctr;
}
}
}

//shuffle()
//===========================================

public void shuffle()
{
Random rnd = new Random();
int sort1;
int sort2;
card tmpcard = new card();

for (int ctr = 0; ctr<100;ctr++)
{
sort1 = (int) ((rnd.NextDouble()*52) +1);
sort2 = (int) ((rnd.NextDouble()*52) +1);

tmpcard = this.cards[sort1];
this.cards[sort1]=this.cards[sort2];
this.cards[sort2] = tmpcard;
}

this.next = 1;

}

//dealCard()
// Returns next card in deck
// =============================================

public card dealCard()
{
if (next>52)
{
return (this.cards[0]);
}
else
{
return this.cards[next++];
}
}
}

// 游戏主程序
//====================================================

class CardGame : Form
{
static deck mydeck = new deck();
static card[] pHand = new card[10];
static card[] cHand = new card[10];

private Label lblThink;
private Label lblPlayer;
private Label lblComput;
private Label lblPtotal;
private Label lblPto;
private Label lblCtotal;
private Label lblPcard1;
private Label lblPcard2;
private Label lblPcard3;
private Label lblCcard1;
private Label lblCcard2;
private Button btnLicense;
private Button btnWant;
private Button btnShow;
private Button btnReplay;


public CardGame()
{
InitializeComponent();
}

private void InitializeComponent()
{
this.Text = "Hello haha!";
this.StartPosition = FormStartPosition.CenterScreen;

// Create the controls...
Label myDateLabel = new Label();
Label myLabel = new Label();

lblThink = new Label();
lblPlayer = new Label();
lblComput = new Label();
lblPtotal = new Label();
lblPto = new Label();
lblCtotal = new Label();
lblPcard1 = new Label();
lblPcard2 = new Label();
lblPcard3 = new Label();
lblCcard1 = new Label();
lblCcard2 = new Label();
btnLicense = new Button();
btnWant = new Button();
btnShow = new Button();
btnReplay = new Button();


btnLicense.Location = new Point(150,100);
btnLicense.AutoSize = true;
btnLicense.Text = "显示发牌";
btnLicense.Click += new System.EventHandler(this.btnLicense_Click);

myLabel.Text = "This program was executed at:";
myLabel.AutoSize = true;
myLabel.Left = 50;
myLabel.Top = 20;



lblPlayer.Text = "玩家手里:";
lblPlayer.AutoSize = true;
lblPlayer.Left = 50;
lblPlayer.Top = 180;

lblPcard1.Text = "第一张牌:";
lblPcard1.AutoSize = true;
lblPcard1.Left = 80;
lblPcard1.Top = 200;

lblPcard2.Text = "第二张牌:";
lblPcard2.AutoSize = true;
lblPcard2.Left = 80;
lblPcard2.Top = 220;

lblPcard3.Text = "第三张牌:";
lblPcard3.AutoSize = true;
lblPcard3.Left = 300;
lblPcard3.Top = 220;

lblComput.Text = "电脑手里:";
lblComput.AutoSize = true;
lblComput.Left = 50;
lblComput.Top = 250;

lblCcard1.Text = "第一张牌:";
lblCcard1.AutoSize = true;
lblCcard1.Left = 80;
lblCcard1.Top = 270;

lblCcard2.Text = "第二张牌:";
lblCcard2.AutoSize = true;
lblCcard2.Left = 80;
lblCcard2.Top = 290;

lblPtotal.Text = "玩家点数 ..";
lblPtotal.AutoSize = true;
lblPtotal.Left = 50;
lblPtotal.Top = 310;

lblCtotal.Text = "电脑点数 ..";
lblCtotal.AutoSize = true;
lblCtotal.Left = 50;
lblCtotal.Top = 330;

btnWant.Location = new Point(100,350);
btnWant.AutoSize = true;
btnWant.Text = "要牌";
btnWant.Click += new System.EventHandler(this.btnWant_Click);

btnShow.Location = new Point(350,350);
btnShow.AutoSize = true;
btnShow.Text = "开牌";
btnShow.Click += new System.EventHandler(this.btnShow_Click);

btnReplay.Location = new Point(250,450);
btnReplay.AutoSize = true;
btnReplay.Text = "重玩";
btnReplay.Click += new System.EventHandler(this.btnReplay_Click);

lblPto.Text = "玩家最终点数 ..";
lblPto.AutoSize = true;
lblPto.Left = 50;
lblPto.Top = 400;

DateTime currDate = DateTime.Now;
myDateLabel.Text = currDate.ToString();

myDateLabel.AutoSize = true;
myDateLabel.Left = 50 + myLabel.PreferredWidth + 10;
myDateLabel.Top = 20;

this.Width = 500;
this.Height = 600;

//Add the control to the form...

this.Controls.Add(btnLicense);
this.Controls.Add(btnWant);
this.Controls.Add(btnShow);
this.Controls.Add(btnReplay);
this.Controls.Add(myDateLabel);
this.Controls.Add(myLabel);
this.Controls.Add(lblPlayer);
this.Controls.Add(lblComput);
this.Controls.Add(lblPcard1);
this.Controls.Add(lblPcard2);
this.Controls.Add(lblPcard3);
this.Controls.Add(lblCcard1);
this.Controls.Add(lblCcard2);
this.Controls.Add(lblPtotal);
this.Controls.Add(lblPto);
this.Controls.Add(lblCtotal);

}

protected void btnLicense_Click ( object sender, System.EventArgs e)
{

lblPcard1.Text += pHand[0].ToString();
lblPcard2.Text += pHand[1].ToString();
lblCcard1.Text += cHand[0].ToString();
lblCcard2.Text += cHand[1].ToString();
int pTotal = pHand[0].CardValue+pHand[1].CardValue;
lblPtotal.Text += pTotal.ToString();
int cTotal = cHand[0].CardValue;
lblCtotal.Text += cTotal.ToString();
//btnLicense.Visible = false;
}

protected void btnReplay_Click ( object sender, System.EventArgs e)
{

/* lblPcard1.Text = "第一张牌:";
lblPcard2.Text = "第二张牌:";
lblCcard1.Text = "第一张牌:";
lblCcard2.Text = "第二张牌:";
lblPtotal.Text = "玩家点数 ..";
lblCtotal.Text = "电脑点数 ..";
*/
Application.Restart();
}

protected void btnWant_Click ( object sender, System.EventArgs e)
{
pHand[2] = mydeck.dealCard();
int pTotal = pHand[0].CardValue+pHand[1].CardValue+pHand[2].CardValue;
lblPcard3.Text += pHand[2].ToString();
lblPto.Text += pTotal.ToString();
btnWant.Visible = false;

}

protected void btnShow_Click ( object sender, System.EventArgs e)
{
int pTotal = pHand[0].CardValue+pHand[1].CardValue+pHand[2].CardValue;
int cTotal = cHand[0].CardValue+cHand[1].CardValue;

if (cTotal<17)
{
cHand[2] = mydeck.dealCard();
cTotal += cHand[2].CardValue;
}

if (pTotal>21)
{
MessageBox.Show("You Busted!");
}
else if (cTotal>21)
{
MessageBox.Show("You Win! Boss is Busted, It's " + cTotal.ToString() + " Its 3rd card is " + cHand[2].CardValue.ToString());
}
else if (pTotal>cTotal)
{
MessageBox.Show("Congraduations! You Win! Boss is "+ cTotal.ToString() + " Its 3rd card is " + cHand[2].CardValue.ToString());
}
else
{
MessageBox.Show("OH, Boss is " + cTotal.ToString() + ", You Lose!" + " Its 3rd card is " + cHand[2].CardValue.ToString());
}
}

public static void Main()
{
int pCardCtr = 0;
int pTotal = 0;
int cTotal = 0;

CardGame frm = new CardGame();


Label lblShuffle = new Label();
Label lblDealing = new Label();


lblShuffle.Text = "正在洗牌...";

pTotal = 0;
cTotal = 0;
pCardCtr = 0;

for ( int ctr = 0; ctr<10; ctr++)
{
pHand[ctr].val = 0;
pHand[ctr].suit = 0;
}
mydeck.shuffle();

lblShuffle.AutoSize = true;
lblShuffle.Left = 50;
lblShuffle.Top = 50;

lblDealing.Text = "正在发牌...";

pHand[0] = mydeck.dealCard();
cHand[0] = mydeck.dealCard();
pHand[1] = mydeck.dealCard();
cHand[1] = mydeck.dealCard();

do
{
pTotal += pHand[pCardCtr].CardValue;

pCardCtr++;
} while ((int)pHand[pCardCtr].val !=0);

cTotal = cHand[0].CardValue;

lblDealing.AutoSize = true;
lblDealing.Left = 50;
lblDealing.Top = 70;

frm.Controls.Add(lblShuffle);
frm.Controls.Add(lblDealing);

Application.Run(frm);

}
}
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 的回复:]
扑克牌类和发牌洗牌是书上弄好的,我只是给搞成窗体的,那么多label、button的处理代码感觉弄的很乱

大家说的复位具体操作用什么语句实现?是用while循环吗?用goto行不行,我觉得while循环最晕了
[/Quote]

这些就扯远了。

一个游戏是一个class吧?哪怕你封装在控件中也是一个class。你的主程序重新new一个新的实例,不久重玩了嘛。
fan12 2012-06-28
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]

引用 7 楼 的回复:

引用 3 楼 的回复:

这要看你的程序逻辑是怎么样的,最简单的办法就是把你初始化游戏环境时候的代码封装起来,初始化用它,复位也可以用它


就是扑克牌游戏,洗牌发牌都是单独的类,应该算是封装吧,不用管

然后哪些算游戏环境初始化,窗体设置的那些label、button等算不算?是不是应该打下包,“重玩”button的事件程序里给调用一下?

……
[/Quote]

扑克牌类和发牌洗牌是书上弄好的,我只是给搞成窗体的,那么多label、button的处理代码感觉弄的很乱

大家说的复位具体操作用什么语句实现?是用while循环吗?用goto行不行,我觉得while循环最晕了
cnfixit 2012-06-27
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]

引用 3 楼 的回复:

这要看你的程序逻辑是怎么样的,最简单的办法就是把你初始化游戏环境时候的代码封装起来,初始化用它,复位也可以用它


就是扑克牌游戏,洗牌发牌都是单独的类,应该算是封装吧,不用管

然后哪些算游戏环境初始化,窗体设置的那些label、button等算不算?是不是应该打下包,“重玩”button的事件程序里给调用一下?

玩家和电脑手里的牌变量呢?应该怎……
[/Quote]
在你的游戏开始以前的准备工作都可以叫初始化,至于到底是那些,除了你没人知道,玩家和电脑手里的牌你是置为空也好,先发了也好都是你来决定的,你都把人和电脑打牌的逻辑弄出来了,会想不通这点东西?
fan12 2012-06-27
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

这要看你的程序逻辑是怎么样的,最简单的办法就是把你初始化游戏环境时候的代码封装起来,初始化用它,复位也可以用它
[/Quote]

就是扑克牌游戏,洗牌发牌都是单独的类,应该算是封装吧,不用管

然后哪些算游戏环境初始化,窗体设置的那些label、button等算不算?是不是应该打下包,“重玩”button的事件程序里给调用一下?

玩家和电脑手里的牌变量呢?应该怎么处理?
__天涯寻梦 2012-06-27
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]
引用 2 楼 的回复:

引用 1 楼 的回复:

Application.Restart();


这个。。。还是从游戏逻辑上复位更好一些


是啊,用Application.Restart();窗口刷新那一下太明显了
[/Quote]
逻辑上重玩简单点说就是把所有的变量包括定义的字段和拖的控件都设定为初始值,具体要看你的程序才能知道怎么写
fan12 2012-06-27
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

引用 1 楼 的回复:

Application.Restart();


这个。。。还是从游戏逻辑上复位更好一些
[/Quote]

是啊,用Application.Restart();窗口刷新那一下太明显了
fan12 2012-06-27
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

Application.Restart();
[/Quote]

这个程序是从哪儿开始重执行?这个只对窗体起作用还是对其他程序语句也起作用?
cnfixit 2012-06-27
  • 打赏
  • 举报
回复
这要看你的程序逻辑是怎么样的,最简单的办法就是把你初始化游戏环境时候的代码封装起来,初始化用它,复位也可以用它
matrixcl 2012-06-27
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

Application.Restart();
[/Quote]

这个。。。还是从游戏逻辑上复位更好一些
__天涯寻梦 2012-06-27
  • 打赏
  • 举报
回复
Application.Restart();

111,083

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧