小球大作战 java_小球大作战-帧同步

帧数设定为40,每帧25MS

服务端 :基于Windows socket 的TCP,客户端:unity

基本原理,服务端轮询接受服务端发来的消息,在一帧时间内还没收到来自客户端的消息,那么就会默认收到空帧,然后广播给所有客户端,客户端发来的帧数据 由服务器收到的帧数来确定,

比如客户端25帧的时候发出了帧数据,但是服务端30帧才收到,25-29 帧数据 服务器就默认为客户端空操作,

服务端 一个房间的主循环

681f8aa8482d2ce708289d55f8b751b6.png

RecvTick和SendTick 为轮训处理  服务器手法客户端消息函数,由于每个房间的设计就只单独的一个线程,所以接受了解请求的主线程在接受到连接后要对房间加锁 ,动态条玩家,。

----------------------------

客户端收到正确的帧序列信息才开始逻辑运算,逻辑运算并不影响画面的update

服务端会发送一个时间作为随机数种子。

1.比如收到了 25  27 帧的数据,那么处理完25帧数据后 会一直轮训 26 帧的数据到来 才会进行27帧的计算

2.如果收到了重复帧 那么以存在的帧数为计算,(客户端重连 服务端可能会多发送几帧数据)

3.当玩家加入一句已有对战,服务器会一次性发送之前所有帧数据,客户端会一次性计算到当前最新帧,客户端收到的数据很可能

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以给你一个简单的示例——大球吃小球的游戏。 首先,我们需要安装Pygame库。在PyCharm中,可以打开终端并输入以下命令来安装Pygame: ``` pip install pygame ``` 然后,我们可以创建一个窗口并在窗口中绘制大球和小球。可以使用Pygame中的Surface和draw模块来实现: ```python import pygame import sys import random pygame.init() # 设置窗口大小和标题 size = width, height = 640, 480 screen = pygame.display.set_mode(size) pygame.display.set_caption('大球吃小球') # 设置大球的初始位置和速度 big_ball_pos = [width // 2, height // 2] big_ball_speed = [0, 0] # 设置小球的数量和半径 num_balls = 10 min_radius = 10 max_radius = 30 # 生成小球的位置和速度 balls = [] for i in range(num_balls): radius = random.randint(min_radius, max_radius) x = random.randint(radius, width - radius) y = random.randint(radius, height - radius) speed = [random.randint(-5, 5), random.randint(-5, 5)] balls.append([x, y, speed, radius]) # 设置大球的半径和颜色 big_ball_radius = 50 big_ball_color = (255, 0, 0) # 设置小球的颜色 ball_color = (0, 0, 255) # 进入游戏循环 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() # 让大球移动 big_ball_pos[0] += big_ball_speed[0] big_ball_pos[1] += big_ball_speed[1] # 判断大球是否碰到边界 if big_ball_pos[0] < big_ball_radius or big_ball_pos[0] > width - big_ball_radius: big_ball_speed[0] = -big_ball_speed[0] if big_ball_pos[1] < big_ball_radius or big_ball_pos[1] > height - big_ball_radius: big_ball_speed[1] = -big_ball_speed[1] # 绘制大球和小球 screen.fill((255, 255, 255)) # 设置背景色为白色 pygame.draw.circle(screen, big_ball_color, big_ball_pos, big_ball_radius) for i in range(num_balls): x, y, speed, radius = balls[i] x += speed[0] y += speed[1] # 判断小球是否碰到边界 if x < radius or x > width - radius: speed[0] = -speed[0] if y < radius or y > height - radius: speed[1] = -speed[1] # 判断大球是否吃掉了小球 if (x - big_ball_pos[0]) ** 2 + (y - big_ball_pos[1]) ** 2 <= big_ball_radius ** 2: big_ball_radius += int(radius / 10) balls.pop(i) num_balls -= 1 break balls[i] = [x, y, speed, radius] pygame.draw.circle(screen, ball_color, (x, y), radius) pygame.display.update() # 更新窗口 ``` 最后,我们可以将以上代码整合在一起,并在PyCharm中运行: ```python import pygame import sys import random pygame.init() size = width, height = 640, 480 screen = pygame.display.set_mode(size) pygame.display.set_caption('大球吃小球') big_ball_pos = [width // 2, height // 2] big_ball_speed = [0, 0] num_balls = 10 min_radius = 10 max_radius = 30 balls = [] for i in range(num_balls): radius = random.randint(min_radius, max_radius) x = random.randint(radius, width - radius) y = random.randint(radius, height - radius) speed = [random.randint(-5, 5), random.randint(-5, 5)] balls.append([x, y, speed, radius]) big_ball_radius = 50 big_ball_color = (255, 0, 0) ball_color = (0, 0, 255) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() big_ball_pos[0] += big_ball_speed[0] big_ball_pos[1] += big_ball_speed[1] if big_ball_pos[0] < big_ball_radius or big_ball_pos[0] > width - big_ball_radius: big_ball_speed[0] = -big_ball_speed[0] if big_ball_pos[1] < big_ball_radius or big_ball_pos[1] > height - big_ball_radius: big_ball_speed[1] = -big_ball_speed[1] screen.fill((255, 255, 255)) pygame.draw.circle(screen, big_ball_color, big_ball_pos, big_ball_radius) for i in range(num_balls): x, y, speed, radius = balls[i] x += speed[0] y += speed[1] if x < radius or x > width - radius: speed[0] = -speed[0] if y < radius or y > height - radius: speed[1] = -speed[1] if (x - big_ball_pos[0]) ** 2 + (y - big_ball_pos[1]) ** 2 <= big_ball_radius ** 2: big_ball_radius += int(radius / 10) balls.pop(i) num_balls -= 1 break balls[i] = [x, y, speed, radius] pygame.draw.circle(screen, ball_color, (x, y), radius) pygame.display.update() ``` 运行后,程序会创建一个窗口并在窗口中绘制一个大球和多个小球,大球可以吃掉小球。按下窗口的关闭按钮或者按下Ctrl+C可以退出程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值