-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
161 lines (140 loc) · 5.02 KB
/
main.lua
File metadata and controls
161 lines (140 loc) · 5.02 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
function love.load()
love.window.setTitle("Quadruple Pong")
love.window.setMode(800, 600)
bgColor = {33/255, 33/255, 33/255}
logo = love.graphics.newImage("assets/logo.png")
playButton = love.graphics.newImage("assets/play.png")
introVideo = love.graphics.newVideo("assets/intro.ogv")
paddleImages = {
horizontal = {
love.graphics.newImage("assets/paddles/horizontal1.png"),
love.graphics.newImage("assets/paddles/horizontal2.png")
},
vertical = {
love.graphics.newImage("assets/paddles/vertical1.png"),
love.graphics.newImage("assets/paddles/vertical2.png")
}
}
music = love.audio.newSource("assets/music.ogg", "stream")
music:setLooping(true)
state = "intro"
logoScale = 0.8
buttonScale = 0.5
buttonX = 400 - playButton:getWidth()*buttonScale/2
buttonY = 100 + logo:getHeight()*logoScale + 20
paddleSpeed = 200
paddles = {
top = {x = 350, y = 0, w = 100, h = 20, dx = 1, dy = 0, type = "horizontal"},
bottom = {x = 350, y = 580, w = 100, h = 20, dx = -1, dy = 0, type = "horizontal"},
left = {x = 0, y = 250, w = 20, h = 100, dx = 0, dy = 1, type = "vertical"},
right = {x = 780, y = 250, w = 20, h = 100, dx = 0, dy = -1, type = "vertical"}
}
ball = {x = 400, y = 300, r = 10, dx = 200, dy = 150}
score = 0
font = love.graphics.newFont("assets/fonts/rainyhearts.ttf", 48)
love.graphics.setFont(font)
introVideo:play()
end
function love.update(dt)
if state == "intro" then
if not introVideo:isPlaying() then
state = "menu"
love.audio.play(music)
end
return
end
if state ~= "playing" then return end
for _, p in pairs(paddles) do
p.x = p.x + p.dx * paddleSpeed * dt
p.y = p.y + p.dy * paddleSpeed * dt
if p.dx ~= 0 then
if p.x <= 0 then p.x = 0; p.dx = 1
elseif p.x + p.w >= 800 then p.x = 800 - p.w; p.dx = -1 end
end
if p.dy ~= 0 then
if p.y <= 0 then p.y = 0; p.dy = 1
elseif p.y + p.h >= 600 then p.y = 600 - p.h; p.dy = -1 end
end
end
ball.x = ball.x + ball.dx * dt
ball.y = ball.y + ball.dy * dt
for _, p in pairs(paddles) do
if checkCollision(ball, p) then
score = score + 1
if p.type == "horizontal" then
ball.dy = -ball.dy
if ball.dy > 0 then
ball.y = p.y + p.h + ball.r
else
ball.y = p.y - ball.r
end
else
ball.dx = -ball.dx
if ball.dx > 0 then
ball.x = p.x + p.w + ball.r
else
ball.x = p.x - ball.r
end
end
break
end
end
if ball.x - ball.r < 0 or ball.x + ball.r > 800 or ball.y - ball.r < 0 or ball.y + ball.r > 600 then
resetBall()
end
end
function love.draw()
love.graphics.clear(bgColor)
if state == "intro" then
local sx = 800 / introVideo:getWidth()
local sy = 600 / introVideo:getHeight()
love.graphics.draw(introVideo, 0, 0, 0, sx, sy)
elseif state == "menu" then
love.graphics.draw(logo, 400 - logo:getWidth()*logoScale/2, 100, 0, logoScale, logoScale)
love.graphics.draw(playButton, buttonX, buttonY, 0, buttonScale, buttonScale)
elseif state == "playing" then
for _, p in pairs(paddles) do
local img
if p.type == "horizontal" then
img = (p.dx > 0) and paddleImages.horizontal[1] or paddleImages.horizontal[2]
else
img = (p.dy > 0) and paddleImages.vertical[1] or paddleImages.vertical[2]
end
love.graphics.draw(img, p.x, p.y, 0, p.w / img:getWidth(), p.h / img:getHeight())
end
love.graphics.circle("fill", ball.x, ball.y, ball.r)
love.graphics.setColor(1,1,1)
love.graphics.print("Score: "..score, 10, 10)
end
end
function love.mousepressed(mx, my, button)
if button ~= 1 then return end
if state == "menu" then
local bw, bh = playButton:getWidth()*buttonScale, playButton:getHeight()*buttonScale
if mx >= buttonX and mx <= buttonX + bw and my >= buttonY and my <= buttonY + bh then
state = "playing"
end
elseif state == "playing" then
for _, p in pairs(paddles) do
if mx >= p.x and mx <= p.x + p.w and my >= p.y and my <= p.y + p.h then
p.dx = -p.dx
p.dy = -p.dy
end
end
end
end
function checkCollision(ball, p)
return ball.x + ball.r > p.x and
ball.x - ball.r < p.x + p.w and
ball.y + ball.r > p.y and
ball.y - ball.r < p.y + p.h
end
function resetBall()
ball.x = 400
ball.y = 300
local angle = math.random() * 2 * math.pi
local speed = 200
ball.dx = math.cos(angle) * speed
ball.dy = math.sin(angle) * speed
score = 0
end