Page 1 of 1

Seriously, WHY ISN'T THIS printf PRINTING?

Posted: Sun Jul 12, 2020 12:59 am
by viniAb
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

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
BALL CLASS

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
PADDLE CLASS

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
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

Re: Seriously, WHY ISN'T THIS printf PRINTING?

Posted: Sun Jul 12, 2020 3:41 am
by Darlex
It looks like you missed a ' in love.draw

Re: Seriously, WHY ISN'T THIS printf PRINTING?

Posted: Sun Jul 12, 2020 7:40 am
by zorg
At least in the code you provided, center lacks the opening '.
Otherwise, i don't see any glaring errors in the code... well, except the fact that you're using window width/height in some places after you've applied push... shouldn't those be virtual width/height?




That said, if you haven't yet, i'd say get the latest löve version, and the latest push (if you want to use it), since i'm assuming you're another CS50 class taker... it'll be better to use those in the long run imho, after you finish this one that is.

Re: Seriously, WHY ISN'T THIS printf PRINTING?

Posted: Sun Jul 12, 2020 5:32 pm
by viniAb
zorg wrote: Sun Jul 12, 2020 7:40 am That said, if you haven't yet, i'd say get the latest löve version, and the latest push (if you want to use it), since i'm assuming you're another CS50 class taker... it'll be better to use those in the long run imho, after you finish this one that is.
I believe my love2d is the latest version (11.3) and so is the push library. I've been working on it since last week so I guess there weren't any major updates in any of them since then...

Re: Seriously, WHY ISN'T THIS printf PRINTING?

Posted: Sun Jul 12, 2020 9:09 pm
by pgimeno
Did you get your printf to work already? Zorg got it right, you need to use VIRTUAL_xxxx instead of WINDOW_xxxx in the printf calls.

Re: Seriously, WHY ISN'T THIS printf PRINTING?

Posted: Sun Jul 12, 2020 11:38 pm
by viniAb
pgimeno wrote: Sun Jul 12, 2020 9:09 pm Did you get your printf to work already? Zorg got it right, you need to use VIRTUAL_xxxx instead of WINDOW_xxxx in the printf calls.
HOLY CRAP IT WORKED

Can't believe it was all because of this VIRTUAL x WINDOW stuff

By the way, can someone explain what exactly would be virtual height and width? Still not clear... :P

Re: Seriously, WHY ISN'T THIS printf PRINTING?

Posted: Mon Jul 13, 2020 12:47 am
by sphyrth
viniAb wrote: Sun Jul 12, 2020 11:38 pm By the way, can someone explain what exactly would be virtual height and width? Still not clear... :P
You use VIRTUAL_WIDTH and VIRTUAL_HEIGHT in push to force that resolution (width & height) to your game regardless of your window's true width & height.

In your case your game will force the 432x243 resolution and scales that up to fit your true window size (900x500).

It's not obvious until you use full-screen. It will try fit your game to any monitor (whether it's widescreen or 4:3).

Re: Seriously, WHY ISN'T THIS printf PRINTING?

Posted: Mon Jul 13, 2020 11:28 am
by pgimeno
Yeah, you need to draw using these "virtual" coordinates, because that's what Push is all about. You draw to a smaller "virtual" screen of size VIRTUAL_WIDTH x VIRTUAL_HEIGHT, so your drawing coordinates must be within that range. Push then stretches that virtual screen to the size of the real screen.

Your virtual height is 243. This means that if you draw something to Y=244, it wont be visible. And your window height is 500, so if you draw at Y=WINDOW_HEIGHT/2, you're drawing at Y=250, which is out of the drawing area. That's why the printf wasn't visible.