Is there a way to not clear the screen on update?
Posted: Thu Nov 05, 2015 4:33 am
I have started reading The Nature of Code and am trying to port the examples from processing.js to love2D.
I've started trying to implement the Random Walker, however the draw function seems to clear the screen every frame, when what I want it to do is to keep the last frame and then draw on top of it.
I was looking at just pushing the new coordinates each frame to an array and then drawing all the coordinates on each frame, but that makes a very large array, very quick.
Does anyone know how to do this?
main.lua
walker.lua
Thanks for your help.
I've started trying to implement the Random Walker, however the draw function seems to clear the screen every frame, when what I want it to do is to keep the last frame and then draw on top of it.
I was looking at just pushing the new coordinates each frame to an array and then drawing all the coordinates on each frame, but that makes a very large array, very quick.
Does anyone know how to do this?
main.lua
Code: Select all
-- Example I.1: Traditional random walk
-- Set the window title
love.window.setTitle('Example I.1: Traditional random walk')
-- This function is called exactly once at the beginning of the game.
function love.load ()
theWalker = require('Walker')
theWalker:init(love.window.getWidth() / 2, love.window.getHeight() / 2, 0, 0, love.window.getWidth(), love.window.getHeight())
end
-- Callback function used to update the state of the game every frame.
function love.update (delta)
-- Make `theWalker` take a step
theWalker:step()
end
-- Callback function used to draw on the screen every frame.
function love.draw ()
-- Draw `theWalker` to the screen
love.graphics.setColor(255, 255, 255)
love.graphics.rectangle('fill', theWalker.x, theWalker.y, 1, 1)
end
-- Callback function triggered when a key is pressed.
function love.keypressed (key)
-- On `escape` quit
if key == 'escape' then
love.event.quit()
end
end
Code: Select all
local Walker = {}
-- Takes in constraints for the walker
function Walker:init (x, y, x1, y1, x2, y2)
self.x = x
self.y = y
self.constraints = {}
self.constraints.x1 = x1
self.constraints.y1 = y1
self.constraints.x2 = x2
self.constraints.y2 = y2
end
function Walker:step ()
-- Generate a number between 0 and 3 inclusive
choice = math.floor(math.random() * 4)
-- Calulate the movement
if choice == 0 then
self.x = self.x + 1
elseif choice == 1 then
self.x = self.x - 1
elseif choice == 2 then
self.y = self.y + 1
else
self.y = self.y - 1
end
-- Constrain movement to the window's bounds
if self.x < self.constraints.x1 then
self.x = self.constraints.x1
end
if self.x > self.constraints.x2 then
self.x = self.constraints.x2
end
if self.y < self.constraints.y1 then
self.y = self.constraints.y1
end
if self.y > self.constraints.y2 then
self.y = self.constraints.y2
end
end
return Walker