User Tools

Site Tools


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 ''love.keyboard.isDown("key-string")'' to detect whether a key is being held down. Define functions ''love.keypressed(key)'' and ''love.keyreleased(key)'' to do something when a key is pressed or released.
 +
 +Resources:
 +  * https://www.love2d.org/wiki/love.keyboard
 +  * https://www.love2d.org/wiki/KeyConstant
 +
 +<file lua main.lua>
 +--[[
 +    Move a circle around using keyboard input with ability to
 +    change color and speed.
 +    Mithat Konar
 +--]]
 +
 +function love.load()
 +    -- circle's constants
 +    CIRCLE_SIZE = 30
 +    DEFAULT_SPEED = 100
 +    circle_speed = DEFAULT_SPEED
 +    
 +    -- circle's variables
 +    -- 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 == 'c' then
 +        local randomColor = {math.random(), math.random(), math.random()}
 +        love.graphics.setColor(randomColor)
 +    end
 +    
 +    -- change speed when 'j', 'k', and 'l' keys are pressed.
 +    if key == 'j' then
 +        circle_speed = circle_speed / 2
 +    end
 +    if key == 'k' then
 +        circle_speed = DEFAULT_SPEED
 +    end
 +    if key == 'l' then
 +        circle_speed = circle_speed * 2
 +    end    
 +end
 +
 +function love.keyreleased(key)
 +    -- Change color when key is released
 +    if key == 'c' then
 +        local randomColor = {math.random(), math.random(), 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('right') or love.keyboard.isDown('d') then
 +        circleX = circleX + circle_speed * dt
 +    end
 +    if love.keyboard.isDown('left') or love.keyboard.isDown('a') then
 +        circleX = circleX - circle_speed * dt
 +    end
 +    if love.keyboard.isDown('up') or love.keyboard.isDown('w') then
 +        circleY = circleY - circle_speed * dt
 +    end
 +    if love.keyboard.isDown('down') or love.keyboard.isDown('s') then
 +        circleY = circleY + circle_speed * dt
 +    end
 +end
 +
 +function love.draw()
 +    love.graphics.print(circleX .. ', ' .. circleY, 5, 5)
 +    love.graphics.print('Circle speed: ' .. circle_speed, 5, 25)
 +    love.graphics.circle('fill', circleX, circleY, CIRCLE_SIZE)
 +end
 +</file>
love/keyboard_events.txt · Last modified: 2021/10/05 03:21 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki