love2d - Flawed game logic in Lua code -
the code simple snake clone , don't want let snake me able go left if it's going right already.
it works if press left when going right if example presses left within time frame starts move left.
function self.update(dt) if love.keyboard.isdown(self.left) , self.prevvelocity.x ~= 1 self.velocity.x = -1 self.velocity.y = 0 end if love.keyboard.isdown(self.right) , self.prevvelocity.x ~= -1 self.velocity.x = 1 self.velocity.y = 0 end if love.keyboard.isdown(self.up) , self.prevvelocity.y ~= 1 self.velocity.x = 0 self.velocity.y = -1 end if love.keyboard.isdown(self.down) , self.prevvelocity.y ~= -1 self.velocity.x = 0 self.velocity.y = 1 end if self.timesincelastmove < self.speedinverted self.timesincelastmove = self.timesincelastmove + dt else table.remove(self.tail, 1) tail = { x = self.position.x, y = self.position.y } table.insert(self.tail, tail) self.position.x = self.position.x + self.velocity.x * tilesize self.position.y = self.position.y + self.velocity.y * tilesize self.prevvelocity = self.velocity self.timesincelastmove = 0; end end
function self.update(dt) if love.keyboard.isdown(self.left) , self.prevvelocity.x ~= 1 self.velocity.x = -1 self.velocity.y = 0 end if love.keyboard.isdown(self.right) , self.prevvelocity.x ~= -1 self.velocity.x = 1 self.velocity.y = 0 end if love.keyboard.isdown(self.up) , self.prevvelocity.y ~= 1 self.velocity.x = 0 self.velocity.y = -1 end if love.keyboard.isdown(self.down) , self.prevvelocity.y ~= -1 self.velocity.x = 0 self.velocity.y = 1 end self.timesincelastmove = self.timesincelastmove + dt if self.timesincelastmove >= self.speedinverted self.timesincelastmove = self.timesincelastmove - self.speedinverted self.position.x = self.position.x + self.velocity.x * tilesize self.position.y = self.position.y + self.velocity.y * tilesize table.remove(self.tail, 1) local head = { x = self.position.x, y = self.position.y } table.insert(self.tail, head) self.prevvelocity = { x = self.velocity.x, y = self.velocity.y } end end
Comments
Post a Comment