首先,我正在制作一个炸弹人,我想让它在局域网中工作。游戏有一个主循环,它在其中移动炸弹人,使用TCP通过套接字发送和接收信息。使用服务器然后它绘制它需要绘制的所有内容。问题是游戏的刷新率应该是每50ms(20fps)刷新一次,并且发送和接收游戏需要继续循环的所有数据需要127ms(平均值)。因此,它会移动炸弹人,并等待在重新绘制屏幕之前检索数据的方法。换句话说,由于数据包的传输速度较慢,游戏运行缓慢。数据包不是很大(至少我认为是这样),它们包含5个整数和一个图像.GIF 60x30。
问题不仅在于发送和接收数据包所需的时间(平均)太长。它也不是常数,有时只需要0ms,其他时间只需1000ms。我的第一个问题是:
为什么会这样?为什么发送和接收包的时间变化如此?
我的传输工作代码就是这样:游戏检索包(来自其他游戏的信息)并使用辅助线程类将自己的包发送到服务器,辅助线程类从服务器发送和接收包并存储它们在属性中。当游戏在屏幕中绘制所需内容时,线程开始运行(发送/接收)。然后游戏等待线程完成他的任务以移动到下一次迭代。我这样做是希望在游戏开始时,另一个班级可以在转移中工作,因此,给它一点时间来完成它的工作,所以游戏必须等待更少才能完成。它奏效了,但差别不够大。
--------------------------这是游戏循环的代码(只有数据发送部分)------ --------------
while (Running)
packet = DataExchange.retrieve(); //DataExchange is the auxiliary class object
//decompress the received package
Sprite =packet.Sprite.getImage();
X = packet.coordenadaX;
Y = packet.coordenadaY;
B = packet.M; //M is a class with 3 integers
packet.SetPacket(b1.x, b1.y, new PaqueteImagen(b1.SpriteActual) , BombaAliada); //Compress the packet that the game will send
DataExchange.SetPacket( packet ); //set the packet of the auxiliary class to the packet that will be sent.
Thread t = new Thread( DataExchange );
t.start(); //start the trasference thread
if (b1.isAlive()) controlPj.MovePj(); //moves the bomberman (b1)
Draw(); //here the game draws in the screen.
while (t.isAlive()); //waits for the auxiliary thread to end.
}
setVisible(false);
}
-----------------------------这是辅助类------------- -----------
public class DataExchange implements Runnable {
ObjectOutputStream output =null;
ObjectInputStream input = null;
PaqueteBomberman packet;
Socket socket;
public DataExchange( Socket socket, PacketClass P){ //constructor
this.socket = socket;
packet = P;
}
public PacketClass retrieve ( ){ //get packet
return packet;
}
public void SetPacket( PacketClass P){ //set packet
packet = P;
}
@Override
public void run(){
try{
output = new ObjectOutputStream(socket.getOutputStream());
output.flush();
input = new ObjectInputStream(socket.getInputStream());
output.writeObject(packet); //sends the packet to the server
packet = (PacketClass) input.readObject();//receives the packet from the server
}
catch(Exception e){}
}
}
------------------------------这是服务器的代码---------- ---------
public class Server {
private ServerSocket serverSocket = null;
private Socket socket = null;
private Socket socket2 = null;
public boolean conected = false;
private ObjectInputStream Packetinput = null;
private ObjectOutputStream Packetoutput = null;
private ObjectInputStream Packetinput2 = null;
private ObjectOutputStream Packetoutput2 = null;
private PacketClass packet1 = null;
private PacketClass packet2 = null;
public static void main(String[] args) throws Exception {
Server Servidor = new Server();
Servidor.run();
}
public void run() throws Exception{
serverSocket = new ServerSocket(208);
socket = serverSocket.accept(); //socket for player 1
socket2 = serverSocket.accept(); //socket for player 2
conected=true;
while ( conected ){
//=====create ImputStream and OutputStream================================
Packetinput = new ObjectInputStream(socket.getInputStream());
Packetoutput = new ObjectOutputStream(socket.getOutputStream());
Packetinput2 = new ObjectInputStream(socket2.getInputStream());
Packetoutput2 = new ObjectOutputStream(socket2.getOutputStream());
//==========receives the package from one player and sends it to the other=================
packet1 = (PacketClass) Packetinput.readObject();
packet2 = (PacketClass) Packetinput2.readObject();
Packetoutput.writeObject(packet2);
Packetoutput2.writeObject(packet1);
}
socket.close();
}
}
这是游戏的局域网部分。第二个问题是:如何真正提高数据传输速度,以便游戏循环不必等待它需要的数据这么久?我需要一个非常好的改进,比如从120ms到60-80ms,有什么想法吗?