Page 1 of 2

Collision border problem within my game.

Posted: Wed Dec 22, 2021 5:05 pm
by dsk031
Alright on to another smaller problem.. The tutorial that I am following he has only 7 ends in this line of code but now when I try and run this code on LOVE it wants me to put another end after line 83, which would make this 8 Ends in my code.. Also, once I do put an End at line 83 to close off function, game runs and I have another problem after that.. The collision does not work when it goes off of boundaries (Top and bottom screen) it is suppose to bounce back off of the screen.


I apologize, I am quite new to programming, I am trying to learn Lua for the sake of trying to make my on game on Core Games..

Code: Select all


function love.update(dt)
	if gameState == 'play' then
		-- detect ball collision with paddles, reversing dx if true and
		-- slightly increasing it, then altering the dy based on the position of the ball
		if ball:collides(player1) then
			ball.dx = -ball.dx * 1.03
			ball.x = player1.x + 5
-- line 83 -- > end?
			-- keep velocity going in the same direction, but randomize it
			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

			-- keep velocity going in the same direction, but randomize it
		    if ball.dy < 0 then
				ball.dy = -math.random(10, 150)
			else
				ball.dy = math.random(10, 150)
			end
		end

		-- detect upper and lower screen boundary collision and reverse if collides
		if ball.y <= 0 then
			ball.y = 0
			ball.dy = -ball.dy
		end
	end

		-- -4 to account for the balls size
		if ball.y >= VIRTUAL_HEIGHT - 4 then
			ball.y = VIRTUAL_HEIGHT - 4
			ball.dy = -ball.dy
		end
	end
	

Re: Collision border problem within my game.

Posted: Wed Dec 22, 2021 5:30 pm
by milon
It's hard to tell for sure without seeing all of the code. I can't test this solution either, but here's my guess based on what you've posted:

It looks like an issue of code indentation. Don't add 'end' at your line 83. The description on line 79 says you need to alter dy in the case of a collision, so it's correct to have line 83 commented (or removed). So, don't change any of that. :)

The comment on line 104 says to check for upper & lower boundary collision next. There's an upper boundary check ('if ball.y <= 0 ...') followed by an extra end. Comment out the 'end' on line 109, and add an 'end' at line 117 (so there's 3 'end' lines in a row). This new end should close the love.update(dt) function, and the overall flow of the function is now in 4 parts:
1. Check for collision with player1
2. Check for collision with player2
3. Check for upper boundary collision
4. Check for lower boundary collision

Run it and see if it works. It also makes me question what about collision of the ball with the left or right sides? I don't see any code cover that, but maybe that's taken care of elsewhere in your tutorial.

Re: Collision border problem within my game.

Posted: Thu Dec 23, 2021 1:43 pm
by dsk031
milon wrote: Wed Dec 22, 2021 5:30 pm It's hard to tell for sure without seeing all of the code. I can't test this solution either, but here's my guess based on what you've posted:

It looks like an issue of code indentation. Don't add 'end' at your line 83. The description on line 79 says you need to alter dy in the case of a collision, so it's correct to have line 83 commented (or removed). So, don't change any of that. :)

The comment on line 104 says to check for upper & lower boundary collision next. There's an upper boundary check ('if ball.y <= 0 ...') followed by an extra end. Comment out the 'end' on line 109, and add an 'end' at line 117 (so there's 3 'end' lines in a row). This new end should close the love.update(dt) function, and the overall flow of the function is now in 4 parts:
1. Check for collision with player1
2. Check for collision with player2
3. Check for upper boundary collision
4. Check for lower boundary collision

Run it and see if it works. It also makes me question what about collision of the ball with the left or right sides? I don't see any code cover that, but maybe that's taken care of elsewhere in your tutorial.

Alright, so I tired to do the end at 117 so that their are 3 ends and that does not work.. I did however find the tutorials source code and compared it to mine.. Almost everything is identical to his except the ends at this point, I have 8 while he has 7.. This is driving me crazy I've looked it over and cross compared like 10x ..

This is the code provided with tutorial..

Code: Select all

function love.update(dt)
    if gameState == 'play' then
        -- detect ball collision with paddles, reversing dx if true and
        -- slightly increasing it, then altering the dy based on the position of collision
        if ball:collides(player1) then
            ball.dx = -ball.dx * 1.03
            ball.x = player1.x + 5

            -- keep velocity going in the same direction, but randomize it
            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

            -- keep velocity going in the same direction, but randomize it
            if ball.dy < 0 then
                ball.dy = -math.random(10, 150)
            else
                ball.dy = math.random(10, 150)
            end
        end

        -- detect upper and lower screen boundary collision and reverse if collided
        if ball.y <= 0 then
            ball.y = 0
            ball.dy = -ball.dy
        end

        -- -4 to account for the ball's size
        if ball.y >= VIRTUAL_HEIGHT - 4 then
            ball.y = VIRTUAL_HEIGHT - 4
            ball.dy = -ball.dy
        end
    end

This is my code..

Code: Select all

function love.update(dt)
	if gameState == 'play' then
		-- detect ball collision with paddles, reversing dx if true and
		-- slightly increasing it, then altering the dy based on the position of the ball
		if ball:collides(player1) then
			ball.dx = -ball.dx * 1.03
			ball.x = player1.x + 5

			-- keep velocity going in the same direction, but randomize it
			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

			-- keep velocity going in the same direction, but randomize it
		    if ball.dy < 0 then
				ball.dy = -math.random(10, 150)
			else
				ball.dy = math.random(10, 150)
			end
		end

		-- detect upper and lower screen boundary collision and reverse if collides
		if ball.y <= 0 then
			ball.y = 0
			ball.dy = -ball.dy
		end

		-- -4 to account for the balls size
		if ball.y >= VIRTUAL_HEIGHT - 4 then
			ball.y = VIRTUAL_HEIGHT - 4
			ball.dy = -ball.dy
		end
	end
end

Re: Collision border problem within my game.

Posted: Thu Dec 23, 2021 1:47 pm
by dsk031
I'm seeing my ends look a bit spaced out on this code on here but his isn't, but on Sublime they aren't spaced out like that it's just like his.

Edit: So, I commented out my code and then copied and pasted his code to my main.lua, same thing happens it asked for another end after the function under if ball:collides(player1) then .. I've looked above this code to see if there is anything different before this code but can't see anything..

Re: Collision border problem within my game.

Posted: Thu Dec 23, 2021 1:52 pm
by milon
dsk031 wrote: Thu Dec 23, 2021 1:43 pmAlright, so I tired to do the end at 117 so that their are 3 ends and that does not work..
Your code is structured correctly; I see no problems with it. What do you mea it "does not work"?

I cannot test this at all since I only have the one function. Please post the entire project so we can help you better. (FYI, after this morning I likely won't be online until next week.)

Re: Collision border problem within my game.

Posted: Thu Dec 23, 2021 2:03 pm
by dsk031
Trying to add my code via attachments.. one sec..

Re: Collision border problem within my game.

Posted: Thu Dec 23, 2021 2:03 pm
by dsk031
here are two more..
I also have a font being used that I can't attach so you might have to get a font somewhere in windows or wherever.. font.ttf is called in a line

Re: Collision border problem within my game.

Posted: Thu Dec 23, 2021 3:20 pm
by pgimeno
His code uses spaces, your code uses tabs. Tabs are 8 columns; most editors allow you to change how they look like for you, and that causes a lot of pain because it may be different for everyone else. Configuring your editor to use spaces instead of tabs solves that problem.

I suspect there's something fishy about that tutorial. Can you provide a link?

Re: Collision border problem within my game.

Posted: Thu Dec 23, 2021 3:32 pm
by dsk031
pgimeno wrote: Thu Dec 23, 2021 3:20 pm His code uses spaces, your code uses tabs. Tabs are 8 columns; most editors allow you to change how they look like for you, and that causes a lot of pain because it may be different for everyone else. Configuring your editor to use spaces instead of tabs solves that problem.

I suspect there's something fishy about that tutorial. Can you provide a link?


https://www.youtube.com/watch?v=jZqYXSmgDuM&t=3281s

https://learning.edx.org/ also has it, I don't think there is anything fishy because it is from freecodecamp.org but it also could be the updates or variations that LOVE has had that makes things not work like they should in the tutorial, for instance I found that I had to change love.graphics.clear(40, 45, 52, 255) to love.graphics.clear(40/255, 45/255, 52/255, 1) I found this fix somewhere else..

Re: Collision border problem within my game.

Posted: Thu Dec 23, 2021 4:10 pm
by pgimeno
I've found the source here: https://github.com/games50/pong/blob/ma ... n.lua#L105

The function isn't finished at that point; it keeps going. The end of the function is at line 175.