gpt4 book ai didi

Java:客户端、客户端、(线程)服务器、流客户端信息、JPanel 创建但消息(?)阻止游戏开始

转载 作者:行者123 更新时间:2023-12-01 15:23:33 29 4
gpt4 key购买 nike

我正在开发一个小型客户端1/客户端2、服务器(线程)TCP 游戏。在尝试处理延迟问题时,我意识到我的 transmitState() 中存在缺陷。它强制将不必要的信息传递到通讯流中,从而造成迟缓,将汽车留在 JPanel 的不同位置,这基本上是错误的。所以我把它拿出来了。

但是在这样做的过程中,我造成了问题,尽管所有通信、协议(protocol)、线程、“信使”和一切都正常工作,但在我的逻辑中的某个地方,我通过不允许重绘而停止了游戏的开始()transmitState()receiveState() 按逻辑顺序完成其工作。

我真的看不出我哪里出了问题。有什么建议吗?

注意:keyPressed case 语句中的 if 条件测试让 Sprite 向左向右“旋转”360 度等,而 vel 是速度,因此不能超过 100。 carRem = 汽车远程客户端。我知道有很多代码,但过去我因为没有包含足够的代码而被(正确地)告知,所以我认为所有这些都显示了需要显示的内容(如果我错了,请告诉我:)) .

传输状态:

public void transmitState(String s)
{
try
{
outputStream.writeBytes(s + "\n");
}
catch(IOException e)
{
System.err.println(e);
}
}

接收状态:

    public void receiveState()
{
try
{
messageIn = inputStream.readLine();
}
catch(IOException e)
{
System.err.println(e);
}
testState(messageIn);
}

测试状态:

public void testState(String s)
{
if (s.equals("l") || s.equals("r") || s.equals("u") || s.equals("d"))
{
c = s.charAt(0);
this.i2 = i2;
this.vel2 = vel2;
this.carRem = carRem;

switch (c)
{
case 'l':
if (i2 == 0)
{
i2 = 15;
}
else
{
i2--;
}
carRem.setCurrentImage(i2);
break;
case 'r':
if (i2 == 15)
{
i2 = 0;
}
else
{
i2++;
}
carRem.setCurrentImage(i2);
break;
case 'u':
if (vel2 == 100)
{

}
else
{
vel2 = vel2 + 10;
carRem.setVel(vel2);
}
break;
case 'd':
if (vel2 == 0)
{

}
else
{
vel2 = vel2 - 10;
carRem.setVel(vel2);
}
break;
}
}
}

JPANEL(具有大多数客户端功能):

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class UIPanel extends JPanel implements ActionListener, KeyListener
{
//sprite creation stuff, works fine (excluded here)
private Timer timer;
private CarLocal carLoc;
private CarRemote carRem;
private String carLocColour;

private ClientMessenger cliMess = null;

public UIPanel(CarLocal carLoc, CarRemote carRem, ClientMessenger cliMess)
{
this.carLoc = carLoc;
this.carRem = carRem;
this.cliMess = cliMess;

images1 = new ImageIcon[TOTAL_IMAGES];
images2 = new ImageIcon[TOTAL_IMAGES];

carLocColour = carLoc.getColour();
//setting sprites and testing order (works fine and excluded here)

this.addKeyListener(this);
this.setFocusable(true);

timer = new Timer(80, this);
timer.start();
}

public void keyTyped(KeyEvent e)
{

}

public void keyPressed(KeyEvent e)
{
int i1 = carLoc.getCurrentImage();
int i2 = carRem.getCurrentImage();
int vel1 = carLoc.getVel();
int vel2 = carRem.getVel();
switch (e.getKeyCode())
{
case KeyEvent.VK_LEFT:
if(i1 == 0)
{
i1 = 15;
}
else
{
i1--;
}
carLoc.setCurrentImage(i1);
cliMess.transmitState("l");
break;
case KeyEvent.VK_RIGHT:
if(i1 == 15)
{
i1 = 0;
}
else
{
i1++;
}
carLoc.setCurrentImage(i1);
cliMess.transmitState("r");
break;
case KeyEvent.VK_UP:
if(vel1 == 100)
{

}
else
{
vel1 = vel1 + 10;
carLoc.setVel(vel1);
cliMess.transmitState("u");
}
break;
case KeyEvent.VK_DOWN:
if(vel1 == 0)
{

}
else
{
vel1 = vel1 - 10;
carLoc.setVel(vel1);
cliMess.transmitState("d");
}
break;
}

carLoc.setCurrentImage(i1);
carLoc.setVel(vel1);;
}

public void keyReleased(KeyEvent e)
{

}

public void actionPerformed(ActionEvent e)
{
repaint();
cliMess.receiveState();
}

public void paintComponent(Graphics g)
{
for(int iVel1 = carLoc.getVel(); iVel1 > 0; iVel1 = iVel1 - 10)
{
carLoc.forwardCalc();
}
for(int iVel2 = carRem.getVel(); iVel2 > 0; iVel2 = iVel2 - 10)
{
carRem.forwardCalc();
}

super.paintComponent(g);
int i1X = carLoc.getCarX();
int i1Y = carLoc.getCarY();
int i2X = carRem.getCarX();
int i2Y = carRem.getCarY();
int currentImage1 = carLoc.getCurrentImage();
int currentImage2 = carRem.getCurrentImage();
Color c0 = Color.black;
//includes some graphics stuff, works fine, excluded here

images1[currentImage1].paintIcon( this, g, i1X, i1Y);
images2[currentImage2].paintIcon( this, g, i2X, i2Y);
}
}

最佳答案

根据您计划发送的数据量,您可能需要调用flush()确保内部缓冲区已满的情况下发送数据。

关于Java:客户端、客户端、(线程)服务器、流客户端信息、JPanel 创建但消息(?)阻止游戏开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10514431/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com