Skip to content

Instantly share code, notes, and snippets.

@daem-on
Created May 1, 2017 11:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daem-on/d19443e68e6adb2e64849e224c6d27d7 to your computer and use it in GitHub Desktop.
Save daem-on/d19443e68e6adb2e64849e224c6d27d7 to your computer and use it in GitHub Desktop.
Simple pong game in lua with LÖVE
ball = {}
ball.x = 300
ball.y = 200
ball.vel = {}
ball.vel.x = 3
ball.vel.y = 1
ball.height = 30
ball.width = 30
map = {}
map.offset = 20
map.width = 600
map.height = 400
a = {}
a.width = 10
a.height = 80
a.y = 200
a.x = 580
b = {}
b.width = 10
b.height = 80
b.y = 200
b.x = 40
a.score = 0
b.score = 0
sound = love.audio.newSource("pong.wav")
scoreFont = love.graphics.newFont(30)
function love.update()
ball.x = ball.x + ball.vel.x
ball.y = ball.y + ball.vel.y
--map boundaries
if ball.x >= (map.width + map.offset) - ball.width then
b.score = b.score + 1
ball:reset()
end
if ball.x <= map.offset then
a.score = a.score + 1
ball:reset()
end
--walls bounce
if ball.y <= map.offset then
ball:bounce(1, -1)
end
if ball.y >= (map.height + map.offset) - ball.height then
ball:bounce(1, -1)
end
--paddles bounces
if ball.x > a.x - ball.width and ball.y <= a.y + a.height and ball.y >= a.y - ball.height then
ball:bounce(-1, 1)
ball.x = ball.x - 10
end
if ball.x < b.x + 5 and ball.y <= b.y + b.height and ball.y >= b.y - ball.height then
ball:bounce(-1, 1)
ball.x = ball.x + 10
end
--keys testing
if love.keyboard.isDown("up") and a.y > map.offset then
a.y = a.y - 2
end
if love.keyboard.isDown("down") and a.y + a.height < map.height + map.offset then
a.y = a.y + 2
end
if love.keyboard.isDown("w") and b.y > map.offset then
b.y = b.y - 2
end
if love.keyboard.isDown("s") and b.y + b.height < map.height + map.offset then
b.y = b.y + 2
end
end
function ball:bounce(x, y)
self.vel.x = x * self.vel.x
self.vel.y = y * self.vel.y
end
function ball:reset()
ball.x = 300
ball.y = 200
ball.vel = {}
ball.vel.x = 3
ball.vel.y = 1
ball.height = 30
ball.width = 30
end
function love.draw()
love.graphics.rectangle("fill", ball.x, ball.y, ball.width, ball.height)
love.graphics.rectangle("line", map.offset, map.offset, map.width, map.height)
--draw paddles
love.graphics.rectangle("fill", a.x, a.y, a.width, a.height)
love.graphics.rectangle("fill", b.x, b.y, b.width, b.height)
love.graphics.setFont(scoreFont)
--draw score
love.graphics.print(b.score .. " - " .. a.score, 280, 40)
end
@haseeb-heaven
Copy link

 love .
Error: Could not open file pong.wav. Does not exist.
stack traceback:
[C]: at 0x7f8418e76160
[C]: in function 'newSource'
main.lua:30: in main chunk
[C]: in function 'require'
[string "boot.lua"]:429: in function <[string "boot.lua"]:275>
[C]: in function 'xpcall'

@aidengonz
Copy link

If you don't have a sound file, delete this line.
sound = love.audio.newSource("pong.wav", "static")
If you do use this instead and make sure you have a pong.wav file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment