如何解决我无法理解我的诺基亚经典蛇游戏翻拍的运动代码出了什么问题请帮我
请帮帮我。 正如我们在蛇游戏中所知道的,如果我们当前正在向右移动:我们不能向左移动,我们必须向上或向下移动或坚持向右移动。其他方向以此类推。所以我想出了以下代码:
extends Area2D
export var SPEED=100
var input_vector= Vector2.ZERO
var canMove = {'left':false,'right':false,'up':false,'down':false}
var moving = {'left':false,'down':false}
func _physics_process(delta):
position += input_vector*SPEED*delta
if Input.is_action_just_pressed("ui_right"):
input_vector.x=1
input_vector.y=0
if Input.is_action_just_pressed("ui_left"):
input_vector.x=-1
input_vector.y=0
if Input.is_action_just_pressed("ui_up"):
input_vector.x=0
input_vector.y=-1
if Input.is_action_just_pressed("ui_down"):
input_vector.x=0
input_vector.y=1
if input_vector.x<0:
moving['left']=true
if input_vector.x>0:
moving['right']=true
if input_vector.y<0:
moving['up']=true
if input_vector.y>0:
moving['down']=true
if moving['left']==true:
canMove['right']=false
canMove['up']=true
canMove['down']=true
if moving['right']==true:
canMove['left']=false
canMove['up']=true
canMove['down']=true
if moving['up']==true:
canMove['right']=true
canMove['left']=true
canMove['down']=false
if moving['down']==true:
canMove['right']=true
canMove['left']=true
canMove['up']=false
if canMove['left']==false:
input_vector.x= abs(input_vector.x)
if canMove['right']==false:
input_vector.x= -abs(input_vector.x)
if canMove['up']==false:
input_vector.y= abs(input_vector.y)
if canMove['down']==false:
input_vector.y= -abs(input_vector.y)
解决方法
position += input_vector*SPEED*delta
之后。第一个块基于input_vector
写入Input
。
第二个块基于moving
写入input_vector
。由于我们是基于 input_vector
编写 Input
,因此这些块组合基于 moving
编写 input_vector
。
第三个块基于canMove
写入moving
。按照与上述相同的逻辑,您正在根据 canMove
编写 Input
。你什么时候检查canMove
?最后的。这不是你想要的。您想先检查 canMove
,然后再写入 input_vector
和 moving
。
其实你可以结合检查,例如:
if Input.is_action_just_pressed("ui_right") and canMove['right']:
我认为这不是表示 moving
的好方法。相反,我建议使用 Enum。即:
enum Direction {UP,DOWN,LEFT,RIGHT,NONE}
var moving = Direction.NONE
你也可以去掉canMove
。规则是您不能向与您正在移动的方向相反的方向移动。因此,您可以直接检查 moving
:
if Input.is_action_just_pressed("ui_right") and moving != Direction.LEFT:
input_vector.x=1
input_vector.y=0
moving = Direction.RIGHT
if Input.is_action_just_pressed("ui_left") and moving != Direction.RIGHT:
input_vector.x=-1
input_vector.y=0
moving = Direction.LEFT
if Input.is_action_just_pressed("ui_up") and moving != Direction.DOWN:
input_vector.x=0
input_vector.y=-1
moving = Direction.UP
if Input.is_action_just_pressed("ui_down") and moving != Direction.UP:
input_vector.x=0
input_vector.y=1
moving = Direction.DOWN
那个块应该足够了。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。