Skip to content

Instantly share code, notes, and snippets.

@keomamallett
Created April 27, 2019 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keomamallett/7f24b3212611ebba003076e7b9511c7e to your computer and use it in GitHub Desktop.
Save keomamallett/7f24b3212611ebba003076e7b9511c7e to your computer and use it in GitHub Desktop.
This Lua/Love code moves a square across the screen and when the left side exceed the window width, the square reappears in the upper left of the window.
-- 1. in the draw function, update the code to set the rectangle to fill mode instead of line mode (***)
-- 2. in the draw function, update the code to centre the rectangle, in the game window (***)
-- 3. in the update function, update the code to make the rectangle move when you press the arrow keys
function love.load()
window_width, window_height = love.window.getMode()
rectOne = {}
rectOne.width = 200
rectOne.height = 150
rectOne.x = window_width /2 - rectOne.width / 2
rectOne.y = window_height /2 - rectOne.height / 2
rectOne.move_by_x = 300
rectOne.move_by_y = 200
end
function love.update(dt)
-- if 5 < 9 then
rectOne.x = rectOne.x + rectOne.move_by_x * dt
rectOne.y = rectOne.y + rectOne.move_by_y * dt
-- end
-- if rectOne.x (left side of rectangle) is greater than screen width, set right side of rectOne equal to ZERO
if rectOne.x > window_width then
rectOne.x = -rectOne.width
rectOne.y = -rectOne.height
end
end
function love.draw()
love.graphics.rectangle("fill", rectOne.x, rectOne.y, rectOne.width, rectOne.height)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment