0

I was implementing Pong - 3 update (GD50) but got stuck in the paddle movement and font size rendering in LOVE. I cross checked the whole code from GD50 repo. On checking LOVE resources, I couldn't find any difference in love.keyboard.isDown() function between older and newer versions of LOVE (I was guessing maybe I am using newer version released after course videos were made).

There is no movement of paddles and font size of "Hello Pong!" is also not right. It is using scoreFont but I changed the active font to scoreFont(32) after printing text in smallFont(8). So, it should display it in smallFont right? I am not getting where am I doing it wrong. And I have no clue why there is no movement of paddles when pressing 'w' 's' 'up' 'down'. Any help is appreciated.

Here is the code and snapshot of render :

    push = require 'push'
    
    WINDOW_WIDTH = 1280
    WINDOW_HEIGHT = 720
    
    VIRTUAL_WIDTH = 432
    VIRTUAL_HEIGHT = 243
    
    PADDLE_SPEED = 200
    
    function love.load()
        love.graphics.setDefaultFilter('nearest', 'nearest')
        
        -- more "retro" font
        smallFont = love.graphics.newFont('font.ttf', 8)
    
        -- larger font for drawing score on the screen
        scoreFont = love.graphics.newFont('font.ttf', 32)
    
        -- set Love2D active font ot smallFont
        love.graphics.setFont(smallFont)
    
        push:setupScreen(VIRTUAL_WIDTH, VIRTUAL_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT, {
            fullscreen = false,
            resizable = false,
            vsync = true
        })
    
        player1Score = 0
        player2Score = 0
    
        player1Y = 30
        player2Y = VIRTUAL_HEIGHT - 50
    
    end
    
    function love.update(dt)
        -- player 1 movement
        if love.keyboard.isDown('w') then
            player1Y = player1Y + -PADDLE_SPEED * dt
        elseif love.keyboard.isDown('s') then
            player1Y = player1Y + PADDLE_SPEED * dt
        end
    
        -- player 2 movement
        if love.keyboard.isDown('up') then
            player2Y = player2Y + -PADDLE_SPEED * dt
        elseif love.keyboard.isDown('down') then
            player2Y = player2Y + PADDLE_SPEED * dt
        end
    end
    
    function love.keypressed(key)
        if key == 'escape' then
            love.event.quit()
        end
    end
    
    
    function love.draw()
    
        push:apply('start')
    
        -- clear the screen with a specific color
        love.graphics.clear(40 / 255, 45 / 255, 52 / 255, 255 / 255)
    
        -- Into
        love.graphics.printf('Hello Pong!', 0, 20, VIRTUAL_WIDTH, 'center')
    
        -- Score card
        love.graphics.setFont(scoreFont)
        love.graphics.print(tostring(player1Score), VIRTUAL_WIDTH / 2 - 50, VIRTUAL_HEIGHT / 3)
        love.graphics.print(tostring(player2Score), VIRTUAL_WIDTH / 2 + 30, VIRTUAL_HEIGHT / 3)
       
        -- Paddles and ball 
        love.graphics.rectangle('fill', 10, 30, 5, 20)
    
        love.graphics.rectangle('fill', VIRTUAL_WIDTH - 10, VIRTUAL_HEIGHT - 50, 5, 20)
    
        love.graphics.rectangle('fill', VIRTUAL_WIDTH / 2 - 2, VIRTUAL_HEIGHT / 2 - 2, 4, 4)
    
        push:apply('end')
    end
  • both fonts loaded in love.load()
  • "Hello Pong!" printing in love.draw()
  • Paddles movements specified in love.update(dt)

Pong 3 render

1 Answer 1

0

First, setFont property needs to be set/reset in your love.draw() function. According to the documentation, a custom font needs to be declared in the love.draw() function or else Love will use its default font. Placing it right before you print Hello Pong! will fix this issue.

Second, for the paddles, you have hard-coded their vertical positions. Instead, you need to create Player 1's paddle the vertical property of player1Y and Player 2's paddle the vertical property of player2Y. This will link the paddle to the property that isDown() will be modifying; currently, the variables are being updates, but the paddle's don't use them.

Otherwise, everything else in your code appears to be fine.

Hope this helps. If it does, feel free to click the check mark next to my answer. But if not, leave me a comment and I will try to help further.

1
  • Thanks a lot! It worked.
    – K_lash96
    Apr 9, 2021 at 18:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .