C# 编写网络小游戏 猜拳
编写程序,实现以下功能:1、使用UDP协议建立点对点连接2、对方出手后提示“对方已出”并开始倒数3、接收对方信息,判断结果并显示本程序为猜拳游戏网络版,使用UDP协议进行...
编写程序,实现以下功能:1、 使用UDP协议建立点对点连接2、 对方出手后提示“对方已出”并开始倒数3、 接收对方信息,判断结果并显示 本程序为猜拳游戏网络版,使用UDP协议进行通信。双方在各自的界面中选择要出的手势,确定后对方将收到提示,并开始倒数10秒,倒数结束则随机出一种手势,发送给对方,同时接收对方的手势信息,由程序自动判断输赢。是网络小游戏,求代码!若可用,积分送上!
展开
2个回答
2013-09-05
展开全部
按照你的要求写了一个,比较的粗糙。没有实现你的10秒倒数,这个留给你自己去思考吧。我大致的说一说,写了一个服务器端 和 客户端。用到的技术有:多线程,c#网络编程。客户端连接到服务器端 通过服务器端处理和转发 猜拳的结果。在服务端中设定了连接的最大客户端数量为2 。也就是只能连2个客户端,可以视实际情况更改。写了一个下午,也没有好好的写注释。如果你是学习的话就加我讨论吧。以上是整体运行的效果,中间是服务器端,两边分别为客户端1 和客户端2////////////////////////////////////////////////////////////////////////////////服务器端界面:服务器端代码:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;namespace Server
{
public partial class Form1 : Form
{
private TcpListener listener;
private List<TcpClient> clientList;
private Thread t;
private Thread t1;
private string outTos;
public Form1()
{
InitializeComponent();
setControls();
} private void setControls()
{
clientList = new List<TcpClient>(); button1.Text = "Start";
button1.Click += new EventHandler(button1_Click);
button2.Text = "Stop";
button2.Click += new EventHandler(button2_Click);
} private void StartServer()
{
IPAddress ip = new IPAddress(new byte[] { 127, 0, 0, 1 });
listener = new TcpListener(ip, 8500);
listener.Start();
richTextBox1.Text += string.Format("服务器:{0} 端口号:{1} 已经启动...\n",ip.ToString(),8500);
}
private void GetClient()
{
do
{
if (clientList.Count >= 2)
{
//clientList.Clear();
break;
}
else
{
clientList.Add(listener.AcceptTcpClient());
outTos += string.Format("Player:{0} is connecting\n", clientList[clientList.Count - 1].Client.LocalEndPoint);
if (clientList.Count == 2)
{
try
{
t1 = new Thread(GetMsg);
t1.Start();
}
catch
{
t1.Abort();
}
}
}
}
while (true);
}
private void GetMsg()
{
do
{
int byteRead = 0;
int bufferSize = 2000;
byte[] buffer = new byte[bufferSize]; NetworkStream stream1 = clientList[0].GetStream();
byteRead = stream1.Read(buffer, 0, bufferSize);
string msg = Encoding.Unicode.GetString(buffer, 0, byteRead); NetworkStream stream2 = clientList[1].GetStream();
byteRead = stream2.Read(buffer, 0, bufferSize);
string msg1 = Encoding.Unicode.GetString(buffer, 0, byteRead); if (msg != string.Empty && msg1 != string.Empty)
{
SendMsg(GetAnsower(msg, msg1));
}
}
while (true); } private void SendMsg(int win)
{
string p1 = string.Empty;
string p2 = string.Empty;
if (win == 0)
{
p1 = "平手";
p2 = "平手";
}
if (win == 1)
{
p1 = "赢了!";
p2 = "输了!";
}
if (win == 2)
{
p1 = "输了!";
p2 = "赢了!";
} NetworkStream stream1 = clientList[0].GetStream();
byte[] buffer1 = Encoding.Unicode.GetBytes(p1);
stream1.Write(buffer1, 0, buffer1.Length); NetworkStream stream2 = clientList[1].GetStream();
byte[] buffer2 = Encoding.Unicode.GetBytes(p2);
stream2.Write(buffer2, 0, buffer2.Length);
outTos += string.Format("消息已发送 玩家1:{0} 玩家2:{1}\n",p1,p2);
} private int GetAnsower(string m1,string m2)
{
int win = 0;
if (m1 == "剪刀")
{
if (m2 == "布") win = 1;
if (m2 == "石头") win = 2;
if (m2 == "剪刀") win = 0;
}
if (m1 == "石头")
{
if (m2 == "剪刀") win = 1;
if (m2 == "布") win = 2;
if (m2 == "石头") win = 0;
}
if (m1 == "布")
{
if (m2 == "石头") win = 1;
if (m2 == "剪刀") win = 2;
if (m2 == "布") win = 0;
}
return win;
} void button1_Click(object sender, EventArgs e)
{
try
{
StartServer();
t = new Thread(GetClient);
t.Start();
timer1.Start();
}
catch (Exception ex)
{
t.Abort();
t1.Abort();
timer1.Stop();
timer1.Stop();
}
}
void button2_Click(object sender, EventArgs e)
{
t.Abort();
t1.Abort();
listener.Stop();
timer1.Stop();
MessageBox.Show("服务器已关闭!");
} private void timer1_Tick(object sender, EventArgs e)
{
richTextBox1.Text += outTos;
outTos = string.Empty;
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;namespace Server
{
public partial class Form1 : Form
{
private TcpListener listener;
private List<TcpClient> clientList;
private Thread t;
private Thread t1;
private string outTos;
public Form1()
{
InitializeComponent();
setControls();
} private void setControls()
{
clientList = new List<TcpClient>(); button1.Text = "Start";
button1.Click += new EventHandler(button1_Click);
button2.Text = "Stop";
button2.Click += new EventHandler(button2_Click);
} private void StartServer()
{
IPAddress ip = new IPAddress(new byte[] { 127, 0, 0, 1 });
listener = new TcpListener(ip, 8500);
listener.Start();
richTextBox1.Text += string.Format("服务器:{0} 端口号:{1} 已经启动...\n",ip.ToString(),8500);
}
private void GetClient()
{
do
{
if (clientList.Count >= 2)
{
//clientList.Clear();
break;
}
else
{
clientList.Add(listener.AcceptTcpClient());
outTos += string.Format("Player:{0} is connecting\n", clientList[clientList.Count - 1].Client.LocalEndPoint);
if (clientList.Count == 2)
{
try
{
t1 = new Thread(GetMsg);
t1.Start();
}
catch
{
t1.Abort();
}
}
}
}
while (true);
}
private void GetMsg()
{
do
{
int byteRead = 0;
int bufferSize = 2000;
byte[] buffer = new byte[bufferSize]; NetworkStream stream1 = clientList[0].GetStream();
byteRead = stream1.Read(buffer, 0, bufferSize);
string msg = Encoding.Unicode.GetString(buffer, 0, byteRead); NetworkStream stream2 = clientList[1].GetStream();
byteRead = stream2.Read(buffer, 0, bufferSize);
string msg1 = Encoding.Unicode.GetString(buffer, 0, byteRead); if (msg != string.Empty && msg1 != string.Empty)
{
SendMsg(GetAnsower(msg, msg1));
}
}
while (true); } private void SendMsg(int win)
{
string p1 = string.Empty;
string p2 = string.Empty;
if (win == 0)
{
p1 = "平手";
p2 = "平手";
}
if (win == 1)
{
p1 = "赢了!";
p2 = "输了!";
}
if (win == 2)
{
p1 = "输了!";
p2 = "赢了!";
} NetworkStream stream1 = clientList[0].GetStream();
byte[] buffer1 = Encoding.Unicode.GetBytes(p1);
stream1.Write(buffer1, 0, buffer1.Length); NetworkStream stream2 = clientList[1].GetStream();
byte[] buffer2 = Encoding.Unicode.GetBytes(p2);
stream2.Write(buffer2, 0, buffer2.Length);
outTos += string.Format("消息已发送 玩家1:{0} 玩家2:{1}\n",p1,p2);
} private int GetAnsower(string m1,string m2)
{
int win = 0;
if (m1 == "剪刀")
{
if (m2 == "布") win = 1;
if (m2 == "石头") win = 2;
if (m2 == "剪刀") win = 0;
}
if (m1 == "石头")
{
if (m2 == "剪刀") win = 1;
if (m2 == "布") win = 2;
if (m2 == "石头") win = 0;
}
if (m1 == "布")
{
if (m2 == "石头") win = 1;
if (m2 == "剪刀") win = 2;
if (m2 == "布") win = 0;
}
return win;
} void button1_Click(object sender, EventArgs e)
{
try
{
StartServer();
t = new Thread(GetClient);
t.Start();
timer1.Start();
}
catch (Exception ex)
{
t.Abort();
t1.Abort();
timer1.Stop();
timer1.Stop();
}
}
void button2_Click(object sender, EventArgs e)
{
t.Abort();
t1.Abort();
listener.Stop();
timer1.Stop();
MessageBox.Show("服务器已关闭!");
} private void timer1_Tick(object sender, EventArgs e)
{
richTextBox1.Text += outTos;
outTos = string.Empty;
}
}
}
2013-09-05
展开全部
using System; namespace W23 { class begin1 //定义一个begin1的类;// { public static void Main() //主函数// { int rock1=3; //rock代表石头// int jian1=2; //jian 代表剪子// int bu1=1; //bu代表布// System.Random rnd=new System.Random(); string aj=Console.ReadLine(); int nbr=(int) rnd.Next(1,3); //随机数// string rnd1=Convert.ToString(nbr); //将rnd的int转化为string// string rock=Convert.ToString(rock1); //同上// string jian=Convert.ToString(jian1); //同上// string bu=Convert.ToString(bu1); if ((rnd1==rock)&&(aj==bu)||(rnd1==jian)&&(aj==rock)||(rnd1==bu)&&(aj==jian)) { Console.WriteLine("你胜利了!"); } if ((rnd1==rock)&&(aj==jian)||(rnd1==jian)&&(aj==bu)||(rnd1==bu)&&(aj==rock)) { Console.WriteLine("我出局了!"); } if ((rnd1==rock)&&(aj==rock)||(rnd1==jian)&&(aj==jian)||(rnd1==bu)&&(aj==bu)) { Console.WriteLine("我的我是平手!"); } } } } 是我复制的 http://zhidao.baidu.com/question/38999923.html 加分哈
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |