- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试使用 SpriteKit 制作蛇游戏(就像旧诺基亚手机中的游戏)。问题是,当蛇的方向改变时,蛇在一个静止的垂直 block 中移动,并没有得到 L 形(如图所示)。为了进入上下文,我有一个名为 SKDSpriteNode
的类,它与 SKSpriteNode 相同,但有一个额外的 direction
属性; Snake
类有 direction
和 length
属性。有一个 snakeBody
属性,类型为 [SKDSpriteNode]
,包含蛇体的节点。
蛇的方向在游戏开始时分配给.up
,然后由用户的滑动分配。顺便说一句,changeDirection
方法在蛇的方向改变时被调用(使用 didSet
)。这是代码和图片:
extension ClassicLevelScene {
func startGame() { snake.direction = .up; moveSnake() }
func checkPlacement(for node: SKDSpriteNode) -> SKAction {
return SKAction.run({
if !(node.isInsideFrame(of: self)) { self.gameOver() }
})
}
func getMovement(for node: SKDSpriteNode) -> (once: SKAction, repetitive: SKAction) {
let movement = SKAction.move(by: node.direction.getVector(withIntensity: movementSpeed), duration: 0.5)
let moveAction = SKAction.sequence([movement, checkPlacement(for: node)])
let repetitiveMoveAction = SKAction.repeatForever(moveAction)
return (moveAction, repetitiveMoveAction)
}
func moveSnake() {
for node in snakeBody {
node.removeAllActions()
node.run(getMovement(for: node).1)
}
}
func moveOnce() {
for node in snakeBody {
node.removeAllActions()
node.run(getMovement(for: node).0)
}
}
func changeDirection() {
for i in 0..<snake.length {
if i == 0 {
snakeBody[i].direction = snake.direction
snakeBody[i].run(SKAction.move(by: snakeBody[i].direction.getVector(withIntensity: movementSpeed), duration: 0.5))
} else {
snakeBody[i].direction = snakeBody[i-1].direction
snakeBody[i].run(SKAction.move(to: snakeBody[i-1].oldPosition, duration: 0.5))
}
}
}
最佳答案
我需要发布一个答案来向您展示代码,但您应该只移动头部,让尾部拖在后面。现在有更好的方法可以做到这一点,但是为了简化您已经编写的代码,它应该是这样的
extension ClassicLevelScene {
func startGame() { snake.direction = .up; moveSnake() }
func checkPlacement(for node: SKSpriteNode) -> SKAction {
return SKAction.run({
if !(node.isInsideFrame(of: self)) { self.gameOver() }
})
}
func getMovement(for node: SKDSpriteNode) -> (once: SKAction, repetitive: SKAction) {
let movement = SKAction.move(by: node.direction.getVector(withIntensity: movementSpeed), duration: 0.5)
let moveTail = SKAction.run({self.moveTail()})
let moveAction = SKAction.sequence([movement, moveTail,checkPlacement(for: node)])
let repetitiveMoveAction = SKAction.repeatForever(moveAction)
return (moveAction, repetitiveMoveAction)
}
func moveTail()
{
for i in 1..<snake.length {
snakeBody[i].direction = snakeBody[i-1].direction
snakeBody[i].position = snakeBody[i-1].oldPosition
}
}
func moveSnake() {
let head = snakeBody[0] {
head.removeAllActions()
head.run(getMovement(for: head).1)
}
}
func moveOnce() {
let head = snakeBody[0] {
head.removeAllActions()
head.run(getMovement(for: head).0)
}
}
func changeDirection() {
let head = snakeBody[0] {
head.removeAllActions()
head.direction = snake.direction
head.run(SKAction.move(by: head.direction.getVector(withIntensity: movementSpeed), duration: 0.5))
}
}
关于swift - 蛇在垂直方 block 中移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45121946/
我是编程新手,并且卡在涉及蛇方向改变的特定部分上。该游戏与旧诺基亚手机上的游戏相同。很经典。 目前,每次我按 W、A、S 或 D 键,蛇都会移动 1 平方/20 像素。问题是我希望这个 Action
如果您熟悉任何梦幻体育选秀,选秀顺序网格如下所示: EXAMPLE 1 (3-teams): Round Team 1 Team 2 Team 3 1 1 (1.1) 2 (
我从这里得到了蛇算法的代码(在MatLab中实现) http://www.mathworks.com/matlabcentral/fileexchange/28109-snakes-active-co
我遵循了这个 link 中的示例.然而,轮廓从初始点开始收缩。是否可以做展开的轮廓?我想要像显示的图像那样的东西。左边的图像是它的样子,右边的图像是我想要的样子——向外扩展而不是收缩。红色圆圈为起点,
我正在编写一款类似于贪吃蛇的游戏。目前,我正在努力编写 move() 和 Growth() 方法。这个游戏的运作方式是,蠕虫从 1 block 开始,每 move 一步就增加 1 block ,直到达
说明 我目前正在使用 Pygame 开发蛇游戏,但我遇到了一个问题,因为我的蛇目前仅由方 block 组成,但如果蛇包含蛇头、 body 、尾部的绘制 25x25 图片,我会发现它会更好对于弯曲的 b
我是一名优秀的程序员,十分优秀!