how to call a function pressing a key?
Posted: Sun Sep 03, 2023 9:56 am
Hi everyone,
I'm a noob trying to make a remake of Pong. I have a menu screen and I want to go to the game pressing the "1" key, but it doesn´t work. The welcome screen is showed, but, when pressing the selected key, it doesn't work. here is the main.lua code.
I'm a noob trying to make a remake of Pong. I have a menu screen and I want to go to the game pressing the "1" key, but it doesn´t work. The welcome screen is showed, but, when pressing the selected key, it doesn't work. here is the main.lua code.
push = require 'push'
WINDOW_WIDTH = 1280
WINDOW_HEIGHT = 720
VIRTUAL_WIDTH = 432
VIRTUAL_HEIGHT = 243
--[[
Runs when the game first starts up, only once; used to initialize the game.
]]
function love.load()
-- use nearest-neighbor filtering on upscaling and downscaling to prevent blurring of text
-- and graphics; try removing this function to see the difference!
love.graphics.setDefaultFilter('nearest', 'nearest')
-- FONT TO BE USED
smallFont = love.graphics.newFont ('NEONLEDLight.otf', 25)
love.graphics.setFont (smallFont)
-- initialize our virtual resolution, which will be rendered within our
-- actual window no matter its dimensions; replaces our love.window.setMode call
-- from the last example
push:setupScreen(VIRTUAL_WIDTH, VIRTUAL_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT, {
fullscreen = false,
resizable = false,
vsync = true
})
end
--[[
Keyboard handling, called by LÖVE2D each frame;
passes in the key we pressed so we can access.
]]
function love.keypressed(key)
-- keys can be accessed by string name
if key == 'escape' then
-- function LÖVE gives us to terminate application
love.event.quit()
end
if key == '1' then
drawGame()
end
end
--[[
Called after update by LÖVE2D, used to draw anything to the screen,
updated or otherwise.
]]
function love.draw()
-- begin rendering at virtual resolution
push:apply('start')
love.graphics.clear(40/255, 45/255, 52/255, 255/255)
-- condensed onto one line from last example
-- note we are now using virtual width and height now for text placement
love.graphics.printf('Pong!', 0, 20, VIRTUAL_WIDTH, 'center')
love.graphics.printf('1 - Player 1 vs. Com\n2 - Player 1 vs. Player 2\n3 - Com vs. Com', 0, 100, VIRTUAL_WIDTH, 'center')
-- end rendering at virtual resolution
push:apply('end')
end
function love.drawGame()
push:apply('start')
love.graphics.clear(40/255, 45/255, 52/255, 255/255)
--Draw P1 Paddle
love.graphics.rectangle('fill', 10, 30, 5, 20)
--Draw P2 Paddle
love.graphics.rectangle('fill', VIRTUAL_WIDTH - 10, VIRTUAL_HEIGHT - 50, 5, 20)
--Draw Ball
love.graphics.rectangle('fill', VIRTUAL_WIDTH / 2 - 2, VIRTUAL_HEIGHT / 2 - 2, 4, 4)
push:apply('end')
end