Pong Game Error

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
Galvatron
Prole
Posts: 1
Joined: Mon Jul 13, 2020 3:34 pm

Pong Game Error

Post by Galvatron »

I'm relatively new to love 2D and am currently working on CS50's introduction to game development course. I have this error and can't find the source. The error is:
Error Syntax error: main.lua:223: 'end' expected (to close 'function' at line 172) near '<eof>'
Traceback
[C]: at 0x010d75fb20
[C]: in function 'require'
[C]: in function 'xpcall'
[C]: in function 'xpcall'

Code: Select all

push = require 'push'
Class = require 'Class'
require 'Paddle'
require 'Ball'

WINDOW_WIDTH = 1280
WINDOW_HEIGHT = 720

VIRTUAL_WIDTH = 432
VIRTUAL_HEIGHT = 243

Paddle_Speed = 200

function love.load()

	love.graphics.setDefaultFilter ('nearest', 'nearest')

	love.window.setTitle('Pong')

	math.randomseed(os.time())

	smallfont = love.graphics.newFont('font.ttf', 8)
	largefont = love.graphics.newFont('font.ttf', 16)
	scorefont = love.graphics.newFont("font.ttf", 32)

	push:setupScreen(VIRTUAL_WIDTH, VIRTUAL_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT, {
	fullscreen = false, 
	resizable = false, 
	vsync = true})
end 

player_1_score = 0
player_2_score = 0

serving_Player = 1

player_1 = Paddle(10,30,5,20)
player_2 = Paddle(VIRTUAL_WIDTH - 10, VIRTUAL_HEIGHT - 30, 5,20)

Ball = Ball(VIRTUAL_WIDTH /2 -2, VIRTUAL_HEIGHT /2 -2, 4, 4)

gameState = 'start'

function love.update(dt)

	if gameState == 'serve' then

        Ball.dy = math.random(-50, 50)

        if serving_Player == 1 then
            Ball.dx = math.random(140, 200)
        else
            Ball.dx = -math.random(140,200)
        end
	elseif gameState == 'play' then

		if Ball:Collides(player_1) then
			Ball.dx = -Ball.dx * 1.03
			Ball.x = player_1.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(player_2) then
			Ball.dx = -Ball.dx * 1.03
			Ball.x = player_2.x - 4

			if Ball.dy < 0 then
				Ball.dy = -math.random(10,150)
			else
				Ball.dy = math.random(10,150)
			end
		end

			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

		if Ball.x < 0 then
			serving_Player = 1
			player_2_score = player_2_score + 1

			if player_2_score == 10 then
				WinningPlayer = 2
				gameState = 'done'
			else	
				Ball:Reset()
				gameState = 'serve'
			end
		end

		if Ball.x > VIRTUAL_WIDTH then
			serving_Player = 2
			player_1_score = player_1_score + 1

			if player_1_score == 10 then
				WinningPlayer = 1
				gameState = 'done'
			else
				Ball:Reset()	
				gameState = 'serve'
			end
		end		
	end

	if love.keyboard.isDown ('w') then
		player_1.dy = -Paddle_Speed
	elseif love.keyboard.isDown ('s') then

		player_1.dy = Paddle_Speed
	else
		player_1.dy = 0
	end

	if love.keyboard.isDown ('up') then
		player_2.dy = -Paddle_Speed
	elseif love.keyboard.isDown ('down') then
		player_2.dy = Paddle_Speed
	else
		player_2.dy = 0
	end

	if gameState == 'play' then

		Ball:Update(dt)
	end

	player_1:Update(dt)
	player_2:Update(dt)
end

function love.keypressed(key)

	if key == 'escape' then
		
		love.event.quit()
	elseif key == "enter" or key == 'return' then
		
		if gameState == 'start' then

			gameState = 'serve'
		elseif gameState == 'serve'then
			gameState = 'play'
		elseif gameState == 'done' then

			gameState = 'serve'

			Ball:Reset()

			player_1_score = 0
			player_2_score = 0

			if WinningPlayer == 1 then
				serving_Player = 2
			else
				serving_Player = 1
			end 
		end
	end
end

function love.draw()

	push:apply('start')

	love.graphics.clear(40/255, 45/255, 52/255, 1)

	PrintScore()


	if gameState == 'start' then
		love.graphics.setFont(smallfont)
		love.graphics.printf('Welcome to Pong!' , 0 , 10 , VIRTUAL_WIDTH, 'center')
		love.graphics.printf('Press Enter to Serve!' , 0 , 20 , VIRTUAL_WIDTH, 'center')
	elseif gameState == 'serve' then

		love.graphics.setFont(smallfont)

		love.graphics.printf('Player ' .. tostring(serving_Player) .. "'s serve", 0 , 10, VIRTUAL_WIDTH, 'center')
		love.graphics.printf('Press Enter to Serve', 0 , 20, VIRTUAL_WIDTH, 'center')
	elseif gameState == 'done' then

		love.graphics.setFont(largefont)
		love.graphics.printf('Player ' .. tostring(WinningPlayer) .. " Wins!", 0 , 10, VIRTUAL_WIDTH, 'center')
		love.graphics.setFont(smallfont)
		love.graphics.printf('Press Enter to Restart', 0, 20, VIRTUAL_WIDTH, 'center')
	else if gameState == 'play' then

		--nothing to display
	end
	
	player_1:Render()
	player_2:Render()
	Ball:Render()

	displayFPS()

	push:apply('end')
end

function displayFPS()

	love.graphics.setFont(smallfont)
	love.graphics.setColor(0,1,0,1)
	love.graphics.print('FPS: ' .. tostring(love.timer.getFPS()), 10, 10)
end

function PrintScore()

	love.graphics.setFont(scorefont)
	love.graphics.print(tostring(player_1_score), VIRTUAL_WIDTH / 2 - 50, VIRTUAL_HEIGHT /3)
	love.graphics.print(tostring(player_2_score), VIRTUAL_WIDTH / 2 + 30, VIRTUAL_HEIGHT /3)
end
User avatar
pgimeno
Party member
Posts: 3657
Joined: Sun Oct 18, 2015 2:58 pm

Re: Pong Game Error

Post by pgimeno »

Galvatron wrote: Mon Jul 13, 2020 3:50 pm

Code: Select all

	else if gameState == 'play' then
I presume it's because of this line. That should have been `elseif`, without a space.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Pong Game Error

Post by zorg »

(Note that both are valid if coded correctly, but the elseif is probably the better choice for a few reasons, one being that it's a bit more compact)

Code: Select all

if x then
    --[[ if x is true]]
elseif y then
    --[[ if x is false but y is true]]
else
    --[[ if x is false and y is false]]
end

Code: Select all

if x then
    --[[ if x is true]]
else
    --[[ if x is false]]
    if y then
        --[[ if x is false but y is true]]
    else
        --[[ if x is false and y is false]]
    end
    --[[ if x is false]]
end
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 2 guests