Java游戏服务器逻辑单线程,java – 在服务器和客户端上同步游戏逻辑

我正在用

Java创建一个简单的网络游戏,玩家可以使用键盘键移动块.游戏将在本地服务器上运行,并具有基本规则:

每个块将以1 FPS速率自动移动,但**用户可以发送多个移动

命令**在这1秒间隔内,从而更新块位置.

代码几乎完成,但是在服务器和客户端之间同步时遇到了麻烦.以下是我需要更好理解的一些代码/描述:

基础课程

class Server{

ServerSocket server = new ServerSocket(port);

while (listening) {

Socket client = server.accept();

new Thread(new ClientHandler(client)).start();

}

}

class Game implements Runnable {

public void run() {

while (! gameOver)

tick();

}

}

现在问题

class ClientHandler implements Runnable

{

Game game;

public ClientHandler(Socket client)

{

this.client = client;

//start game which runs at 1 FPS

long FPS = 1000L;

Timer timer = new Timer();

timer.schedule(new Game(FPS), 0L, FPS);

}

public void run()

{

/** Game is already running, needs to:

*

* 1 - Take any GameInput object from the user and update the game

* 2 - Send a GameState object to the user

* 3 - Client will receive it and render on screen,

* (hopefully in a synch state with the server)

*/

while ( ! game.gameOver)

{

//ObjectInputStream ois = ...;

// Line A

GameInput command = ois.readObject();

// Line B

//GameState state = game.update(command);

//ObjectOutputStream oos = ...;

// Line C

oos.writeObject(state);

}

}

}

我需要更好地理解的是如何处理A线,B线和C线.

更确切地说:

1 – 以安全的方式更新游戏线程有什么好方法?

2 – 如何处理多个命令?一个队列可能吗?

2 – 如何确保客户端和服务器同步?

我是网络编程的新手,所以感谢您的帮助!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值