keyboard keyisdown is not activating - lua

I am trying to make my paddle move up and down when I press the d and a buttons.
When i press the d and a buttons nothing happens at all... they just sit there.....
anyway the code is below if you know please tell me. thanks
I am aware the indenting is wrong i had to change if for this post:)
window_width = 580
window_height = 420
playerScore = 0
enemyScore = 0
pSpeed = 200
player1Y = 100
player2Y = -20
function love.load()
love.window.setMode(window_width, window_height)
end
function love.draw()
love.graphics.printf('hello pong', 0, window_height / 2 - 6, window_width, 'center')
love.graphics.rectangle("fill", 500, player1Y , 10, 50)
love.graphics.rectangle("fill", 40, 220, 10, 50)
love.graphics.rectangle("fill", 100, 230, 7, 5)
end
function update(dt)
if love.keyboard.isDown('d') then
player1Y = player1Y + pSpeed * dt
elseif love.keyboard.isDown('a') then
player1Y = player1Y + -pSpeed * dt
end
end

Try pSpeed = 10 instead of 200, so your rectangles aren't jumping beyond window_height in 2 steps.
And change colors for each rectangle, so it's easier to see what's happening.
function love.draw()
love.graphics.setColor( 0, 0, 0 ) -- black
love.graphics.printf( 'hello pong', 0, window_height /2 -6, window_width, 'center' )
love.graphics.setColor( 1, 0, 0 ) -- red
love.graphics.rectangle( "fill", 500, player1Y, 10, 50 )
love.graphics.setColor( 0, 1, 0 ) -- green
love.graphics.rectangle( "fill", 40, 220, 10, 50 )
love.graphics.setColor( 0, 0, 1 ) -- blue
love.graphics.rectangle( "fill", 100, 230, 7, 5 )
end

I think you should write "love.update(dt)" instead of writing only "update(dt)". I tried changing that and it worked! Also, indenting does not affect the way your code works in Lua, but it does massively improve readability.

Related

(gLua) Derma not working. attempt to call method 'Close' (a nil value)

Error at function ContextClose()
And all derma menu not working
attempt to call method 'Close' (a nil value)
I've create derma menu and it worked in garrysmod\lua folder,
then I moved flies to addon floder and it stopped working.
cl_init.lua
AddCSLuaFile("pbfhud.lua")
include("pbfhud.lua")
pbfhud.lua
AddCSLuaFile()
local statBarW, statBarH = ScrW() / 400, ScrH() / 10
local barW, barH = ScrW() / 4, ScrH() / 10
local btnW, btnH = ScrW() / 4, ScrH() / 20
surface.CreateFont( "NotDermaDefault", {
font = "Arial",
extended = false,
size = 25,
weight = 500,
blursize = 0,
scanlines = 0,
antialias = true,
underline = false,
italic = false,
strikeout = false,
symbol = false,
rotary = false,
shadow = false,
additive = false,
outline = false,
} )
local function DrawInventorySlot( )
end
local function DrawBar(x, y, w, h, color, text, val, name)
local rtext = text .. ": " .. val
local tW, tH = surface.GetTextSize(rtext)
tH = tH / 2
if (name == true) then
tW = tW / 7
end
draw.RoundedBox(0, x, y, w, h, color)
draw.SimpleText(rtext, "NotDermaDefault", barW / 2 - tW / 7, barH / 2 - tH + y, Color( 255, 255, 255, 255 ), TEXT_ALIGN_CENTER, TEXT_ALIGN_TOP)
end
function ContextOpen()
gui.EnableScreenClicker( true )
menu = vgui.Create("DFrame")
menu:SetSize(ScrW(), ScrH() + 100)
menu:ShowCloseButton(false)
menu:SetTitle("")
menu:Center()
menu:SetDraggable(false)
menu:SetBackgroundBlur(true)
menu:SetMouseInputEnabled(true)
menu.Paint = function()
draw.RoundedBox(0, 0, 0, ScrW(), ScrH(), Color(0,0,0,100))
local blur = Material("pp/blurscreen")
local function blurPanel(panel, amount)
local x, y = panel:LocalToScreen(0, 0)
local scrW, scrH = ScrW(), ScrH()
surface.SetDrawColor(255, 255, 255)
surface.SetMaterial(blur)
for i = 1, 6 do
blur:SetFloat("$blur", (i / 3) * (amount or 6))
blur:Recompute()
render.UpdateScreenEffectTexture()
surface.DrawTexturedRect(x * -1, y * -1, scrW, scrH)
end
end
blurPanel(menu, 5)
draw.RoundedBox(0, 0, 0, ScrW() / 4, ScrH(), Color(0,0,0,50))
-- Stats
DrawBar(0, 0, barW, barH, Color(235, 235, 235, 140), "Имя", LocalPlayer():getDarkRPVar("rpname"), true )
DrawBar(0, statBarH, LocalPlayer():Health() * statBarW, barH, Color(255, 50, 43, 140), "Здоровье", LocalPlayer():Health(), false )
DrawBar(0, statBarH * 2, LocalPlayer():Armor() * statBarW, barH, Color(43, 128, 255, 140), "Броня", LocalPlayer():Armor(), false )
DrawBar(0, statBarH * 3, barW, barH, Color(255, 149, 43, 140), "Работа", LocalPlayer():getDarkRPVar("job"), false )
DrawBar(0, statBarH * 4, barW, barH, Color(0, 138, 11, 140), "Зарплата", LocalPlayer():getDarkRPVar("salary"), false )
-- Inventory
draw.RoundedBox(0, barW, 0, ScrW() / 1.7, ScrH(), Color(0,0,0,50))
end
-- Buttons
--fb = vgui.Create("DButton", menu)
--fb:SetSize(btnW, btnH)
--fb:SetPos(0, btnH * 12)
--fb:SetText("")
end
function ContextClose()
menu:Close()
gui.EnableScreenClicker( false )
end
hook.Add ("OnContextMenuOpen", "Context", ContextOpen)
hook.Add ("OnContextMenuClose", "Context", ContextClose)
Files location
..addons\pbf_hud\lua\autorun\client\cl_init.lua
..addons\pbf_hud\lua\autorun\client\pbfhud.lua
SOLVED
I used same identifiers for hooks. If you have same problem just change hook indentifier:
From:
hook.Add ("OnContextMenuOpen", "Context", ContextOpen)
hook.Add ("OnContextMenuClose", "Context", ContextClose)
To:
hook.Add ("OnContextMenuOpen", "Context_open", ContextOpen)
hook.Add ("OnContextMenuClose", "Context_close", ContextClose)

Love2d / LUA grid locked movement NO DIAGONAL

I thought this would be a common problem but after days of research I can't find a solution. Very new to programming in general and LUA specifically. I'm building a SUPAPLEX clone as a CS50 personal project: the character moves along the grid based map and there's a code that everyone seems to suggest (attached). On release of arrow buttons the movement is continued until the end of a tile, smoothly. But if 2 movement buttons are pushed, it causes brief diagonal movement and that's the problem I'm unsuccessfully trying to solve.
Basically I'm trying to either ignore any input until the movement of the sprite is finished at the end of the grid tile or prevent updating until movement in one direction is complete. Seems like a simple thing but I'm about to give up this whole thing. Frustrating. Any input is hiiiighly appreciated and I'm sure this would be a lot of help for very many people...
function love.load()
love.keyboard.setKeyRepeat(true)
player = {
grid_x = 256,
grid_y = 256,
act_x = 256,
act_y = 256,
speed = 5,
}
map = {
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
{ 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
{ 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
{ 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1 },
{ 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
}
function testMap(x, y)
if map[(player.grid_y / 32) + y][(player.grid_x / 32) + x] == 1 then
return false
end
return true
end
function love.keypressed(key)
if key == "up" then
player.grid_y = player.grid_y - 32
elseif key == "down" then
player.grid_y = player.grid_y + 32
elseif key == "left" then
player.grid_x = player.grid_x - 32
elseif key == "right" then
player.grid_x = player.grid_x + 32
end
end
end
function love.update(dt)
player.act_y = player.act_y - ((player.act_y - player.grid_y) * player.speed * dt)
player.act_x = player.act_x - ((player.act_x - player.grid_x) * player.speed * dt)
end
function love.draw()
love.graphics.rectangle("fill", player.act_x, player.act_y, 32, 32)
for y=1, #map do
for x=1, #map[y] do
if map[y][x] == 1 then
love.graphics.rectangle("line", x * 32, y * 32, 32, 32)
end
end
end
end
you're trying to get it to only walk along grid-lines?
take out love.keyboard.setKeyRepeat(true)
and don't use love.keypressed(key)
that's for one-at-a-time keypresses, and it would be hard to use that
with love.keyreleased() to see if all the other keys are released.
use isDown instead, and if one of them isDown, then none of the other dir keys allow input. (along with the couple player.act lines you already have in your update)
player = {
grid_x = 256,
grid_y = 256,
act_x = 256,
act_y = 256,
speed = 5,
dir = ''
}
function love.update(dt)
if love.keyboard.isDown("up", "down", "left", "right") then
if love.keyboard.isDown("up") and ( player.dir == 'up' or player.dir == '' ) then
player.dir = 'up' -- only go up if currently held, or no other dir key being pressed
player.grid_y = player.grid_y - 32
elseif love.keyboard.isDown("down") and ( player.dir == 'down' or player.dir == '' ) then
player.dir = 'down' -- only go down if currently held...
player.grid_y = player.grid_y + 32
elseif key == "left" and ( player.dir == 'left' or player.dir == '' ) then
player.dir = 'left'
player.grid_x = player.grid_x - 32
elseif key == "right" and ( player.dir == 'right' or player.dir == '' ) then
player.dir = 'right'
player.grid_x = player.grid_x + 32
end
else -- none of those keys are being pressed, so player is idle
player.dir = ''
end -- isDown()
player.act_y = player.act_y - ((player.act_y - player.grid_y) * player.speed * dt)
player.act_x = player.act_x - ((player.act_x - player.grid_x) * player.speed * dt)
end -- update()

How to repeat an image like css in lua?

https://gyazo.com/d0d0bab65c0a7060972988a5e73c7959
That was achieved by the this:
local x = script.Parent.Smile
local y = script.Parent.Smile2
while true do
x:TweenPosition(UDim2.new(0, 0, 1, 0))
y:TweenPosition(UDim2.new(0, 0, 1, 0))
wait(.1)
x.Position = y.Position + UDim2.new(0, 0, -1, 0)
y.Position = UDim2.new(0, 0, 0, 0)
end
I was wondering if there was a better way to do it and make it smoother(slower)?
If you want it to look as if it endlessly descends, try adding a "Linear" parameter to your tweens. It will keep the tween constant in speed. For example:
local x = script.Parent.Smile
local y = script.Parent.Smile2
while true do
x:TweenPosition(UDim2.new(0, 0, 1, 0),"Out","Linear",0.1)
y:TweenPosition(UDim2.new(0, 0, 1, 0),"Out","Linear",0.1)
wait(.105) -- making sure to tween again after 0.1 seconds, the 0.105 safely accounts for any latency in the game
x.Position = y.Position + UDim2.new(0, 0, -1, 0)
y.Position = UDim2.new(0, 0, 0, 0)
end
Try that script above, if it doesn't work you can blame me for it

GMOD 3D2D Sign with website

I'm trying to make a 3D2D Sign that has text on it and displays a website, so that i can walk around it and the website will stay in the same position. So far I have got this code but don't know how to import the website.
include("shared.lua")
function ENT:Initialize()
self:SetMaterial("models/shiny")
self:SetRenderMode(RENDERMODE_NORMAL)
self:SetColor(Color( 0, 0, 0, 255 ))
end
function ENT:Draw()
self:DrawModel()
local ENTPos = self:GetPos()+Vector(0,0,26)
local ENTAng = self:GetAngles()
ENTAng:RotateAroundAxis(ENTAng:Forward(), 0)
ENTAng:RotateAroundAxis(ENTAng:Right(), 270)
ENTAng:RotateAroundAxis(ENTAng:Up(), 90)
local url = "https://www.google.com/"
local webPage = vgui.Create("F1HTML")
webPage:OpenURL(url)
cam.Start3D2D(ENTPos + ENTAng:Up() * 2, ENTAng, 0.5)
draw.RoundedBox( 2, -220, -62.5, 440, 227, Color(0, 0, 0, 255) );--Black Body
draw.RoundedBox( 2, -220, -62.5, 440, 30, Color(255, 0, 0, 255) );--Red Title
local FontNameTitle = "HUDNumber5"
draw.SimpleText( "Server", FontNameTitle, 0, -33, Color( 255, 255, 255, 255 ), TEXT_ALIGN_CENTER, TEXT_ALIGN_TOP)
cam.End3D2D()
end
To display a website in 3D space like you are trying to do, I'd recommend using the 3D2D derma project by HandsomeMatt.
Derma can, as you may already know, be used to open a website in a panel.
Check the Derma Basic Guide on how to use derma.
It allows for you to draw derma panels in a 3D2D space, like you want!

Image won't move in Lua LÖVE game

I am writting a game in Lua LÖVE framework, for the LudumDare26. I can't seem to figure out why my "player" won't move when I press WASD.
function love.draw()
playerX = 10
playerY = 10
if love.keyboard.isDown ("w") then
playerY = playerY - 1
elseif love.keyboard.isDown ("s") then
playerY = playerY + 1
elseif love.keyboard.isDown ("a") then
playerX = playerX - 1
elseif love.keyboard.isDown ("d") then
playerX = playerX + 1
end
local x = love.mouse.getX()
local y = love.mouse.getY()
love.graphics.setColor(0, 200, 255, 100)
love.graphics.rectangle("fill", 0, 0, 800, 300)
love.graphics.setColor(255, 255, 255, 255)
love.graphics.rectangle("fill", xCloud, 128, 128, 50)
love.graphics.setColor(50, 160, 80, 255)
love.graphics.rectangle("fill", 0, 300, 800, 300)
love.graphics.setColor(255, 0, 0, 255)
love.graphics.draw(corsair, x - 16, y - 16, 0, 1, 1, 0, 0)
love.graphics.draw(player, playerX, playerY)
love.graphics.line(playerX, playerY, x, y)
end
Take these lines out of love.draw() method:
playerX = 10
playerY = 10

Resources