how to call a function pressing a key?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Azo0ka
Prole
Posts: 2
Joined: Sun Sep 03, 2023 9:48 am

how to call a function pressing a key?

Post by Azo0ka »

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.
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
User avatar
Bobble68
Party member
Posts: 162
Joined: Wed Nov 30, 2022 9:16 pm
Contact:

Re: how to call a function pressing a key?

Post by Bobble68 »

You've declared your function as love.drawGame(), but you are calling it as just drawGame(), which is why it's not working.

Side note, your code is a little weird - shouldn't drawing the game be handled by love.draw()? Having it where it is will make it only draw when you are pressing 1.
Dragon
Azo0ka
Prole
Posts: 2
Joined: Sun Sep 03, 2023 9:48 am

Re: how to call a function pressing a key?

Post by Azo0ka »

Ok. thanks... I know that the code can be weird, but I want to make it work and, then, make it a little bit clean. The idea is that love.draw() is a menu to selct between 2 options (P1 vs com or P1 vs P2). Making this way, when I press "1", it gives me this error:
Error
main.lua:44: attempt to call global 'p1vsCom' (a nil value)
Traceback
[love "callbacks.lua"]:228: in function 'handler'
main.lua:44: in function <main.lua:36>
[love "callbacks.lua"]:154: in function <[love "callbacks.lua"]:144>
[C]: in function 'xpcall'
if declaring function just like "drawGame()" nothing happens when the "1" key is pressed... Don't understand...
User avatar
milon
Party member
Posts: 472
Joined: Thu Jan 18, 2018 9:14 pm

Re: how to call a function pressing a key?

Post by milon »

Seems like you've only given us part of the code. The code you pasted initially (and please, use [ code ] tags next time for readability) does not contain 'p1vsCom' but the error message you quoted indicates it's present on line 44. It will be difficult to help you without seeing the whole thing.

Having said that, you shouldn't use love.draw() as a menu. The function love.draw() is what love calls every frame to draw your game to the screen. If you make that a player-select menu, that's the ONLY thing that will ever get drawn. You'll want to look into the idea of scenes or states to handle different drawing scenarios. But honestly, don't start with that. Start by sticking numberOfPlayers=2 (or whatever you want to call the variable) into your main.lua and just focus on the gameplay. Get fancy with menus later once you've figured out how to draw and move things on screen. :)
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 6 guests