目前,为了解决延迟问题,在我的游戏中(本质上是tron灯周期,但是要移动,必须解决一个数学问题),每当一名玩家转身时,我都会将游戏状态发送给两名玩家。在前端,我有一个绘制函数和一个未绘制函数(以考虑存在延迟的时间)。当前,设置未执行所需的行为:玩家之间彼此不同步(两个玩家的用户转向的移动或位置都不相同)。有人可以帮我吗?这是我第一次玩游戏。这是后端代码:
def send_answer(answer, game_object, player_type, current_position, current_direction):
creator_directions = ['p1up', 'p1right', 'p1down', 'p1left']
opponent_directions = ['p2up', 'p2right', 'p2down', 'p2left']
with transaction.atomic():
# d for direction
if player_type == 'creator':
for d in creator_directions:
if is_correct_answer(getattr(game_object, d), int(answer)):
context['move_direction'] = d
print(context)
new_problem = generate_problem_game(
game_object.level, game_object.operation, game_object, player_type)
setattr(game_object, d, new_problem)
game_object.save()
context['new_problem'] = [d, new_problem]
# calculate draw and undraw positions
p1_last_pos = game_object.p1lastposupdate
p1_lp_timestamp = game_object.p1lpuTimestamp
time_inbetween = timezone.now() - p1_lp_timestamp
ti_milliseconds = time_inbetween.seconds * 1000
p1_lp_split = p1_last_pos.split(',')
p1_lp_x = int(p1_lp_split[0])
p1_lp_y = int(p1_lp_split[1])
draw_positions = []
if current_direction == 'up' or current_direction == 'down':
# works with 100ms loop on front end
# cycles move 4 pixels every 100ms
space_inbetween_x = int(
(ti_milliseconds / 100) * 4)
for i in range(0, space_inbetween_x, 4):
draw_positions.append(
'%s,%s' % (str(p1_lp_x + i), str(p1_lp_y)))
elif current_direction == 'left' or current_direction == 'right':
space_inbetween_y = int(
(ti_milliseconds / 100) * 4)
for i in range(0, space_inbetween_y, 4):
draw_positions.append(
'%s,%s' % (str(p1_lp_x), str(p1_lp_y + i)))
context['cdp'] = draw_positions
context[
'p1_last_pos_update'] = game_object.p1lastposupdate
elif player_type == 'opponent':
for d in opponent_directions:
if is_correct_answer(getattr(game_object, d), int(answer)):
context['move_direction'] = d
# cpl = current_problem_list
"""
cpl_base = [
cpld for cpld in opponent_problems if cpld != d]
cpl = []
for cpld in cpl_base:
cpl.append(getattr(game_object, cpld))
"""
new_problem = generate_problem_game(
game_object.level, game_object.operation, game_object, player_type)
setattr(game_object, d, new_problem)
game_object.save()
context['new_problem'] = [d, new_problem]
# calculate draw and undraw positions
p2_last_pos = game_object.p2lastposupdate
p2_lp_timestamp = game_object.p2lpuTimestamp
time_inbetween = timezone.now() - p2_lp_timestamp
ti_milliseconds = time_inbetween.seconds * 1000
p2_lp_split = p2_last_pos.split(',')
p2_lp_x = int(p2_lp_split[0])
p2_lp_y = int(p2_lp_split[1])
draw_positions = []
if current_direction == 'up' or current_direction == 'down':
# works with 100ms loop on front end
# cycles move 4 pixels every 100ms
space_inbetween_x = int(
(ti_milliseconds / 100) * 4)
for i in range(0, space_inbetween_x, 4):
draw_positions.append(
'%s,%s' % (str(p2_lp_x + i), str(p2_lp_y)))
elif current_direction == 'left' or current_direction == 'right':
space_inbetween_y = int(
(ti_milliseconds / 100) * 4)
for i in range(0, space_inbetween_y, 4):
draw_positions.append(
'%s,%s' % (str(p2_lp_x), str(p2_lp_y + i)))
context['odp'] = draw_positions
context[
'p2_last_pos_update'] = game_object.p2lastposupdate
我认为应该这样做,但是如果您需要前端代码,则可以在app.js的apollius.com上找到它