-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.lua
109 lines (92 loc) · 2.86 KB
/
main.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
require("objects")
local function reset(side)
playing = false
ball:stop()
if side == "left" then
ball:attachToLeftPaddle(leftPlayer.paddle)
else
ball:attachToRightPaddle(rightPlayer.paddle)
end
starter = side
end
local function collision(a)
if a == "left" then
score:rightWins()
reset("left")
elseif a == "right" then
score:leftWins()
reset("right")
end
end
function love.load()
playing = false
altPressed = false
score = objects.Score:new()
world = love.physics.newWorld(-1000, -1000, 1000, 1000)
world:setCallbacks(collision, nil, nil, nil)
leftPlayer = objects.Player:new(objects.Paddle:new(world, 20, "left"), objects.Keys:new("w", "s"))
rightPlayer = objects.Player:new(objects.Paddle:new(world, 770, "right"), objects.Keys:new("up", "down"))
leftWall = objects.VerticalWall:new(world, -5, 300, "left")
rightWall = objects.VerticalWall:new(world, 800, 300, "right")
roof = objects.HorizontalWall:new(world, 400, 0)
floor = objects.HorizontalWall:new(world, 400, 600)
ball = objects.Ball:new(world)
starter = "left"
math.randomseed(os.time())
if (math.random(1, 2) == 1) then
reset("left")
else
reset("right")
end
end
local function move(player)
player:updatePosition()
-- TODO this doesn't really make sense
if playing == false and starter == player.paddle.side then
ball:followPaddle(player.paddle)
end
end
function love.update(dt)
move(leftPlayer)
move(rightPlayer)
world:update(dt)
end
function love.draw()
-- TODO Encapsulate these.
love.graphics.circle('fill',
ball.body:getX(),
ball.body:getY(),
10)
love.graphics.rectangle('fill',
leftPlayer.paddle.body:getX(),
leftPlayer.paddle.body:getY(),
leftPlayer.paddle.constants.WIDTH,
leftPlayer.paddle.constants.HEIGHT)
love.graphics.rectangle('fill',
rightPlayer.paddle.body:getX(),
rightPlayer.paddle.body:getY(),
rightPlayer.paddle.constants.WIDTH,
rightPlayer.paddle.constants.HEIGHT)
love.graphics.print(score.left .. " - " .. score.right, 400, 300)
end
function love.keypressed(k)
if k == " " and playing == false then
playing = true
if starter == "left" then
ball:moveRight()
else
ball:moveLeft()
end
end
if k == "lalt" then
altPressed = true
end
if k == "q" or k == "escape" or (altPressed and k == "f4") then
love.event.push("q")
end
end
function love.keyreleased(k)
if k == "lalt" then
altPressed = false
end
end