目前,考虑到延迟,在我的游戏中(基本上是tron lightcycles,但要移动,必须解决一个数学问题),每次一个玩家转向时,我都会向两个玩家发送游戏状态。在前端,我有一个用于绘制的函数和一个用于取消绘制的函数(用于说明何时存在延迟)。目前,设置未执行所需的行为:两名玩家彼此不同步(两名玩家的移动或转身位置都不相同)。有人能帮我吗?这是我第一次做游戏。以下是后端代码(当用户提交数学问题的答案以转换其角色时,此代码将运行):
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: player 1 last position
p1_last_pos = game_object.p1lastposupdate
# p1_lp_timestamp: player 1 last position timestamp
p1_lp_timestamp = game_object.p1lpuTimestamp
time_inbetween = timezone.now() - p1_lp_timestamp
# ti_milliseconds: time inbetween, milliseconds
ti_milliseconds = time_inbetween.seconds * 1000
# p1_lp_split: player 1 last_position (split)
p1_lp_split = p1_last_pos.split(',')
# p1_lp_x: player 1 last position, x
p1_lp_x = int(p1_lp_split[0])
# p1_lp_y: player 1 last_position, y
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
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 = player 2 last position
p2_last_pos = game_object.p2lastposupdate
# p2_lp_timestamp: player 2 last position timestamp
p2_lp_timestamp = game_object.p2lpuTimestamp
time_inbetween = timezone.now() - p2_lp_timestamp
# ti_milliseconds: time inbetween, milliseconds
ti_milliseconds = time_inbetween.seconds * 1000
# p2_lp_split: player 2 last position (split)
p2_lp_split = p2_last_pos.split(',')
# p2_lp_x: player 2 last position, x
p2_lp_x = int(p2_lp_split[0])
# p2_lp_y: player 2 last position, y
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上找到它
目前没有回答
相关问题 更多 >
编程相关推荐