1:主窗体:
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace feixianqi
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
function fn = new function();
List<Label> lbls;
List<models.Player> Players;
models.Player currPlayer;
List<Label> btns=new List<Label>();
Label currBtn;
int step;
int curr;
private void MainForm_Load(object sender, EventArgs e)
{
lblsm.Text = "图案说明:\n█:普通格子,无任何效果;\n●:幸运轮盘\n\t\t\t\t 1:可以其他玩家互换位置;\n\t\t\t\t 2:轰炸其他玩家,使其后退6格\n※地雷:后退6格\n▲:暂停:停止一轮\n★时间隧道:前进6格";
lbls = fn.outMap(tpPan);
}
private void tsmiNewGame_Click(object sender, EventArgs e)
{
lbls.Clear();
btns.Clear();
panel1.Controls.Clear();
lbls = fn.outMap(tpPan);
CreatePlayers Cp = new CreatePlayers();
Boolean realdy = false;
while (!realdy)
{
Cp.ShowDialog();
Players = Cp.getPlayers();
if (Players.Count < 2)
{
MessageBox.Show("至少有两位玩家");
}
else
{
realdy = true;
}
}
Label lbl;
Label btn;
int pointX = 10;
int pointY = 10;
foreach (models.Player player in Players)
{
lbl = new Label();
btn = new Label();
lbl.Text = player.Name;
lbl.Size = new System.Drawing.Size(45, 20);
lbl.Location = new System.Drawing.Point(pointX, pointY);
btn.Text = "掷";
btn.Enabled = false;
btn.Click += new EventHandler(btn_Click);
btn.Location = new System.Drawing.Point(pointX + 50, pointY);
btn.Size = new System.Drawing.Size(40, 20);
btn.ForeColor = System.Drawing.Color.Salmon;
btn.BackColor = System.Drawing.Color.SeaGreen;
btn.TextAlign=ContentAlignment.MiddleCenter;
pointY += 30;
panel1.Controls.Add(lbl);
panel1.Controls.Add(btn);
btns.Add(btn);
}
curr = 0;
lblInfo.Text = "";
play();
}
void play() {
tpPan.Controls.Clear();
tpPan.Controls.AddRange(lbls.ToArray());
if (curr >= (Players.Count))
curr = 0;
foreach (Label btn in btns)
btn.Enabled = false;
for (int i = 0; i < Players.Count; )
{
if (Players[curr].paull || Players[curr].win)
{
if (Players[curr].paull)
{
Players[curr].paull = false;
if (curr == 0)
lblInfo.Text = "";
}
curr++;
if (curr >= Players.Count)
curr = 0;
}
else
break;
i++;
if(i==Players.Count){
return;
}
}
currPlayer = Players[curr];
currBtn = btns[curr];
currBtn.Enabled = true;
currPlayer = Players[curr];
}
void btn_Click(object sender, EventArgs e)
{
if (curr == 0)
lblInfo.Text = "";
Random r = new Random();
step = r.Next(1, 7);
fn.reSetlbl(step,currPlayer,Players,lblInfo);
btns[curr].Text = (currPlayer.Pos+1).ToString();
if (currPlayer.win)
{
btns[curr].Text = "WIN";
currPlayer.Pos = 99;
}
curr++;
play();
}
}
}
界面:
2:新建玩家:
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace feixianqi
{
public partial class CreatePlayers : Form
{
public CreatePlayers()
{
InitializeComponent();
}
private List<models.Player> players = new List<models.Player>();
public List<models.Player> getPlayers() {
return players;
}
private void btnCreateplayer_Click(object sender, EventArgs e)
{
players.Clear();
if (!string.IsNullOrEmpty(txtPlayerName1.Text)&& !string.IsNullOrEmpty(txtqz1.Text)) {
players.Add(new models.Player(txtPlayerName1.Text,Convert.ToChar( txtqz1.Text.Substring(0,1))));
}
if (!string.IsNullOrEmpty(txtPlayerName2.Text) && !string.IsNullOrEmpty(txtqz2.Text))
{
players.Add(new models.Player(txtPlayerName2.Text, Convert.ToChar(txtqz2.Text.Substring(0, 1))));
}
if (!string.IsNullOrEmpty(txtPlayerName3.Text) && !string.IsNullOrEmpty(txtqz3.Text))
{
players.Add(new models.Player(txtPlayerName3.Text, Convert.ToChar(txtqz3.Text.Substring(0, 1))));
}
if (!string.IsNullOrEmpty(txtPlayerName4.Text) && !string.IsNullOrEmpty(txtqz4.Text))
{
players.Add(new models.Player(txtPlayerName4.Text, Convert.ToChar(txtqz4.Text.Substring(0, 1))));
}
if (!string.IsNullOrEmpty(txtPlayerName5.Text) && !string.IsNullOrEmpty(txtqz5.Text))
{
players.Add(new models.Player(txtPlayerName5.Text, Convert.ToChar(txtqz5.Text.Substring(0, 1))));
}
if (!string.IsNullOrEmpty(txtPlayerName6.Text) && !string.IsNullOrEmpty(txtqz6.Text))
{
players.Add(new models.Player(txtPlayerName6.Text, Convert.ToChar(txtqz6.Text.Substring(0, 1))));
}
if (players.Count < 2)
{
MessageBox.Show("至少有两位玩家");
players.Clear();
}
else {
this.Close();
}
}
}
}
界面:
3:棋盘类为静态类:
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace feixianqi.models
{
public static class qiPan
{
static public int[] Map()
{
int[] luckyTurn = { 6, 23, 40, 55, 69, 83 };//幸运轮盘
int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//地雷
int[] pause = { 9, 27, 60, 93 };//暂停
int[] timeTunne = { 20, 25, 63, 72, 88, 90 };//时间隧道
int[] map = new int[99];
for (int i = 0; i < 99; i++)
map[i] = 0;
foreach (int i in luckyTurn)
map[i] = 1;
foreach (int i in landMine)
map[i] = 2;
foreach (int i in pause)
map[i] = 3;
foreach (int i in timeTunne)
map[i] = 4;
return map;
}
}
}
4玩家类:
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace feixianqi.models
{
public class Player
{
public String Name { get; set; }//玩家名
private int pos;//当前位置
public bool paull{get;set;}//是否暂停
public bool win { get; set; }//是否到达终点
public int Pos
{
get { return pos; }
set {
pos = value;
if (pos < 0)
pos = 0;
if (pos > 99)
pos = 99;
}
}
public char Qizi { get; set; }
public Player() { }
public Player(String name,char qizi) {
this.Name = name;
this.pos = 0;
this.Qizi = qizi;
this.paull=false;
this.win = false;
}
}
}
5:一些计算方法和棋盘布局
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
namespace feixianqi
{
public class function
{
int[] map = models.qiPan.Map();
List<Label> lbls;
public List<Label> outMap(TabPage Tp)//设置LABEL标签的大小及位置,用于做棋盘格
{
int[] map = models.qiPan.Map();
lbls = new List<Label>();
Label lbl;
int pointX = 25;
int pointY = 25;
for (int i = 0; i < map.Length; i++)
{
lbl = new Label();
lbl.Size = new System.Drawing.Size(20, 20);
lbl.Location = new System.Drawing.Point(pointX, pointY);
lbl.TextAlign = ContentAlignment.MiddleCenter;
lbl.Tag = map[i];
setlbl(lbl);
pointX += 25;
if ((i + 1) % 10 == 0)
{
pointX = 25;
pointY += 25;
}
Tp.Controls.Add(lbl);
lbls.Add(lbl);
}
return lbls;
}
public void reSetlbl(int step, models.Player Currplayer, List<models.Player> players,Label sm)//重设LABEL标签,更新玩家位置
{
int pos1 = Currplayer.Pos;
int pos2 = Currplayer.Pos + step;
if (pos2 >= 98) {
pos2 = 98;
Currplayer.win = true;
}
switch ( Convert.ToInt32( lbls[pos2].Tag)) {
case 0:
sm.Text += string.Format("\n{0}掷出了{1}点,现在走到{2}", Currplayer.Name, step, pos2+1);
break;
case 1:
sm.Text += string.Format("\n{0}掷出了{1}点,遇到了幸运轮盘,现在走到{2}", Currplayer.Name, step, pos2 + 1);
break;
case 2:
pos2 -= 6;
if (pos2 < 0)
pos2 = 0;
sm.Text += string.Format("\n{0}掷出了{1}点,很不幸遇到了地雷,现在走到{2}", Currplayer.Name, step, pos2 + 1);
break;
case 3:
Currplayer.paull = true;
sm.Text += string.Format("\n{0}掷出了{1}点,暂停一轮,现在走到{2}", Currplayer.Name, step, pos2 + 1);
break;
case 4:
pos2 += 6;
sm.Text += string.Format("\n{0}掷出了{1}点,很幸运遇到了时间隧道,现在走到{2}", Currplayer.Name, step, pos2 + 1);
break;
}
foreach (models.Player tempPlay in players)
{
if (!Currplayer.Name.Equals(tempPlay.Name) && tempPlay.Pos == pos2&&pos2!=0)
{
tempPlay.Pos = 0;
sm.Text += string.Format(",踩到了{0},将对方踩回了1",tempPlay.Name);
lbls[0].Text = tempPlay.Qizi.ToString();
lbls[0].ForeColor = System.Drawing.Color.Red;
break;
}
}
Currplayer.Pos = pos2;
setlbl(lbls[pos1]);
lbls[pos2].Text=Currplayer.Qizi.ToString();
lbls[pos2].ForeColor = System.Drawing.Color.Red;
}
private void setlbl(Label lbl) {//设置标签的文本,显示不同的图示
switch ( Convert.ToInt32(lbl.Tag) )
{
case 0:
lbl.Text = "█";
lbl.ForeColor = System.Drawing.Color.Black;
break;
case 1:
lbl.Text = "●";
lbl.ForeColor = System.Drawing.Color.Yellow;
break;
case 2:
lbl.Text = "※";
lbl.ForeColor = System.Drawing.Color.Green;
break;
case 3:
lbl.Text = "▲";
lbl.ForeColor = System.Drawing.Color.Blue;
break;
case 4:
lbl.Text = "★";
lbl.ForeColor = System.Drawing.Color.Red;
break;
}
}
}
}
运行如下:
在编写时,逻辑有些混乱.
对控件的属性方法不熟悉.
对于委托还是很不熟悉.
---------------------- Windows Phone 7手机开发、.Net培训、期待与您交流! ----------------------详细请查看:http://net.itheima.com/