Seriously, WHY ISN'T THIS printf PRINTING?
Posted: Sun Jul 12, 2020 12:59 am
I'm a newbie game developer and am developing my first game (which is just a copy of PONG). The gameplay works just fine, but I can't print anything. Neither the game name at the top, neither the scores. I have tried using the default font, using a different font, using the setColor method to make sure it was being printed in white, used print instead of printf (got an exception), changed the font size, location... I have done everything I could do with my small knowledge of love2d and the tips found in topics in this forum, but nothing helped. So I'm here asking myself: WHAT IS WRONG WITH MY CODE?
I will post 3 pieces of code: my main.lua function, the Ball class, the Paddle class
(obs: when I was testing the printf method before actually starting developing the game it worked. But not anymore)
MAIN FILE
BALL CLASS
PADDLE CLASS
By the way, the texts aren't where they're supposed to be because it is hard to tell where they are supposed to be without printing, so I left all of them with the same coordinates (that I supposed will lead to the upper center of the screen)
Please, send help
I will post 3 pieces of code: my main.lua function, the Ball class, the Paddle class
(obs: when I was testing the printf method before actually starting developing the game it worked. But not anymore)
MAIN FILE
Code: Select all
push = require 'push'
Class = require 'class'
require 'Paddle'
require 'Ball'
WINDOW_WIDTH = 900
WINDOW_HEIGHT = 500
VIRTUAL_WIDTH = 432
VIRTUAL_HEIGHT = 243
PADDLE_SPEED = 200
function love.load()
love.window.setTitle('PONG')
love.graphics.setDefaultFilter('nearest', 'nearest')
math.randomseed(os.time())
titleFont = love.graphics.newFont("ARCADECLASSIC.ttf", 60)
push:setupScreen(VIRTUAL_WIDTH, VIRTUAL_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT,
{
fullscreen = false,
resizable = false,
vsync = true
}
)
player1Score = 0
player2Score = 0
player1 = Paddle(10, 30, 5, 20)
player2 = Paddle(VIRTUAL_WIDTH - 10, VIRTUAL_HEIGHT - 30, 5, 20)
ball = Ball(VIRTUAL_WIDTH/2-2, VIRTUAL_HEIGHT/2-2, 4, 4)
gameState = 'start'
end
function love.draw()
push:apply('start')
love.graphics.setColor(1, 1, 1)
player1:render()
player2:render()
ball:render()
love.graphics.setFont(titleFont)
love.graphics.printf('PONG', 0, WINDOW_HEIGHT/2-6, WINDOW_WIDTH, center')
push:apply('end')
end
--game states (start, play)
function love.keypressed(key)
if key == 'escape' then
love.event.quit()
elseif key == 'enter' or key == 'return' then
if gameState == 'start' then
gameState = 'play'
end
end
end
function love.update(dt)
if gameState=='play' then
--checks if ball collides with paddles
if ball:collides(player1) then
ball.dx = -ball.dx * 1.03
ball.x = player1.x+5
if ball.dy < 0 then
ball.dy = -math.random(10, 150)
else
ball.dy = math.random(10, 150)
end
end
if ball:collides(player2)then
ball.dx = -ball.dx * 1.03
ball.x = player2.x - 4
if ball.dy < 0 then
ball.dy = -math.random(10, 150)
else
ball.dy = math.random(10, 150)
end
end
--check if ball collides with windows borders
if ball.y <= 0 then
ball.y = 0
ball.dy = -ball.dy
end
if ball.y >= VIRTUAL_HEIGHT - 4 then
ball.y = VIRTUAL_HEIGHT - 4
ball.dy = -ball.dy
end
--check if player scores
if ball.x <= 0 then
ball:reset()
player2Score = player2Score + 1
gameState = 'start'
elseif ball.x>=VIRTUAL_WIDTH then
ball:reset()
player1Score = player1Score + 1
gameState = 'start'
end
--score check
if player1Score == 10 then
love.graphics.printf('PLAYER 1 WINS',0,WINDOW_HEIGHT/2-6,WINDOW_WIDTH,'center')
gameState = 'start'
end
if player2Score == 10 then
love.graphics.printf('PLAYER 2 WINS',0,WINDOW_HEIGHT/2-6,WINDOW_WIDTH,'center')
gameState = 'start'
end
ball:update(dt)
player1:update(dt)
player2:update(dt)
end
--player 1
if love.keyboard.isDown('w') then
player1.dy = -PADDLE_SPEED
elseif love.keyboard.isDown('s') then
player1.dy = PADDLE_SPEED
else
player1.dy = 0
end
--player 2
if love.keyboard.isDown('up') then
player2.dy = -PADDLE_SPEED
elseif love.keyboard.isDown('down') then
player2.dy = PADDLE_SPEED
else
player2.dy = 0
end
end
Code: Select all
Ball = Class{}
function Ball:init(x, y, width, height)
self.x = x
self.y = y
self.width = width
self.height = height
self.dx = math.random(2) == 1 and 100 or -100
self.dy= math.random(-50, 50) * 1.5
end
function Ball:reset()
self.x = VIRTUAL_WIDTH/2-2
self.y = VIRTUAL_HEIGHT/2-2
self.dx = math.random(2) == 1 and 100 or -100
self.dy= math.random(-50, 50) * 1.5
end
function Ball:update(dt)
self.x = self.x + self.dx*dt
self.y = self.y + self.dy*dt
end
function Ball:collides(paddle)
if self.x > paddle.x + paddle.width or paddle.x > self.x + self.width then
return false
end
if self.y > paddle.y + paddle.height or paddle.y > self.y + self.height then
return false
end
return true
end
function Ball:render()
love.graphics.rectangle('fill', self.x, self.y, self.width, self.height)
end
Code: Select all
Paddle = Class{}
function Paddle:init(x, y, width, height)
self.x = x
self.y = y
self.width = width
self.height = height
self.dy = 0
end
function Paddle:update(dt)
if self.dy < 0 then
self.y = math.max(0, self.y + self.dy*dt)
else
self.y = math.min(VIRTUAL_HEIGHT - self.height, self.y+ self.dy*dt)
end
end
function Paddle:render()
love.graphics.rectangle('fill', self.x, self.y, self.width, self.height)
end
Please, send help