love:keyboard_events
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
— | love:keyboard_events [2021/10/05 03:21] (current) – created mithat | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Keyboard events ====== | ||
+ | Use '' | ||
+ | |||
+ | Resources: | ||
+ | * https:// | ||
+ | * https:// | ||
+ | |||
+ | <file lua main.lua> | ||
+ | --[[ | ||
+ | Move a circle around using keyboard input with ability to | ||
+ | change color and speed. | ||
+ | Mithat Konar | ||
+ | --]] | ||
+ | |||
+ | function love.load() | ||
+ | -- circle' | ||
+ | CIRCLE_SIZE = 30 | ||
+ | DEFAULT_SPEED = 100 | ||
+ | circle_speed = DEFAULT_SPEED | ||
+ | | ||
+ | -- circle' | ||
+ | -- put circle in the center of the screen | ||
+ | circleX = love.graphics.getWidth() / 2 | ||
+ | circleY = love.graphics.getHeight() / 2 | ||
+ | | ||
+ | -- motion variables | ||
+ | isMoveRight = false | ||
+ | isMoveLeft = false | ||
+ | isMoveDown = false | ||
+ | isMoveUp = false | ||
+ | | ||
+ | end | ||
+ | |||
+ | function love.keypressed(key) | ||
+ | -- change color when key is pressed. | ||
+ | if key == ' | ||
+ | local randomColor = {math.random(), | ||
+ | love.graphics.setColor(randomColor) | ||
+ | end | ||
+ | | ||
+ | -- change speed when ' | ||
+ | if key == ' | ||
+ | circle_speed = circle_speed / 2 | ||
+ | end | ||
+ | if key == ' | ||
+ | circle_speed = DEFAULT_SPEED | ||
+ | end | ||
+ | if key == ' | ||
+ | circle_speed = circle_speed * 2 | ||
+ | end | ||
+ | end | ||
+ | |||
+ | function love.keyreleased(key) | ||
+ | -- Change color when key is released | ||
+ | if key == ' | ||
+ | local randomColor = {math.random(), | ||
+ | love.graphics.setColor(randomColor) | ||
+ | end | ||
+ | end | ||
+ | |||
+ | function love.update(dt) | ||
+ | -- detect whether keys are pressed and move accordingly. | ||
+ | -- test for both arrow and WASD keys | ||
+ | if love.keyboard.isDown(' | ||
+ | circleX = circleX + circle_speed * dt | ||
+ | end | ||
+ | if love.keyboard.isDown(' | ||
+ | circleX = circleX - circle_speed * dt | ||
+ | end | ||
+ | if love.keyboard.isDown(' | ||
+ | circleY = circleY - circle_speed * dt | ||
+ | end | ||
+ | if love.keyboard.isDown(' | ||
+ | circleY = circleY + circle_speed * dt | ||
+ | end | ||
+ | end | ||
+ | |||
+ | function love.draw() | ||
+ | love.graphics.print(circleX .. ', ' .. circleY, 5, 5) | ||
+ | love.graphics.print(' | ||
+ | love.graphics.circle(' | ||
+ | end | ||
+ | </ |
love/keyboard_events.txt · Last modified: 2021/10/05 03:21 by mithat