-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
379 lines (298 loc) · 8.57 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
--[[
Copyright (c) 2018 Uladzislau Tarasevich
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
Except as contained in this notice, the name(s) of the above copyright holders
shall not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
]]--
-- Asking for the libraries and custom classes --
push = require 'push'
Class = require 'class'
require 'Ball'
require 'Paddle'
require 'Player'
require 'Menu'
require 'AI'
-- Global variables definition --
-- Actual window size
WINDOW_WIDTH = 1024
WINDOW_HEIGHT = 600
-- Low definition window
VIRTUAL_WIDTH = WINDOW_WIDTH/3
VIRTUAL_HEIGHT = WINDOW_HEIGHT/3
-- Objects' parameters
PADDLE_WIDTH = 3
PADDLE_HEIGHT = 20
PADDLE_SPEED = 200
BALL_SIZE = 3
-- Game rules
ROUNDS = 10
--[[
> love.load()
> loadAssets()
> love.resize(w, h)
> setupGame()
> restartGame()
> love.update(dt)
> love.draw()
> displayFPS()
> love.keypressed(key)
]]--
-- Main function
function love.load()
-- Using filter for low graphics effect
love.graphics.setDefaultFilter('nearest', 'nearest')
loadAssets()
love.window.setTitle("Hero Pong")
-- random generator initialising
math.randomseed(os.time())
-- creating screen
push:setupScreen(VIRTUAL_WIDTH, VIRTUAL_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT,{
fullscreen = true,
resizable = true,
vsync = true
})
menu = Menu()
--setupGame()
gameState = 'start'
end
-- Loads such game assets as sounds and fonts
function loadAssets()
smallFont = love.graphics.newFont('font.ttf', 8)
scoreFont = love.graphics.newFont('font.ttf', 32)
nameFont = love.graphics.newFont('font.ttf', 32)
medFont = love.graphics.newFont('font.ttf', 16)
sounds = {
['paddle_hit'] = love.audio.newSource('sounds/paddle_hit.wav', 'static'),
['wall_hit'] = love.audio.newSource('sounds/wall_hit.wav', 'static'),
['score'] = love.audio.newSource('sounds/score.wav', 'static')
}
end
-- Screen resize function
function love.resize(w, h)
push:resize(w,h)
end
-- Games setup function
function setupGame()
ball = Ball(
VIRTUAL_WIDTH / 2 - 2,
VIRTUAL_HEIGHT / 2 - 2,
BALL_SIZE,
BALL_SIZE)
paddle1 = Paddle(10, 30, PADDLE_WIDTH, PADDLE_HEIGHT)
paddle2 = Paddle(
VIRTUAL_WIDTH - PADDLE_WIDTH - 10,
VIRTUAL_HEIGHT - PADDLE_HEIGHT - 30,
PADDLE_WIDTH,
PADDLE_HEIGHT
)
if menu.mode == 1 then
player1 = Player('Left Player', paddle1)
player2 = Player('Right Player', paddle2)
elseif menu.mode == 0 then
player1 = Player('Left Player', paddle1)
player2 = AI('Right Player', paddle2, ball)
elseif menu.mode == 2 then
player1 = AI('Left Player', paddle1, ball)
player2 = AI('Right Player', paddle2, ball)
end
gameState = 'serve'
end
-- Restarts the game after win
function restartGame()
player1.score = 0
player2.score = 0
end
-- Update function
function love.update(dt)
if gameState == 'play' or gameState == 'serve' then
-- player 1 movement
if love.keyboard.isDown('w') then
player1.paddle.dy = -PADDLE_SPEED
elseif love.keyboard.isDown('s') then
player1.paddle.dy = PADDLE_SPEED
else
player1.paddle.dy = 0
end
-- player 2 movement
if love.keyboard.isDown('up') then
player2.paddle.dy = -PADDLE_SPEED
elseif love.keyboard.isDown('down') then
player2.paddle.dy = PADDLE_SPEED
else
player2.paddle.dy = 0
end
-- ball movent while game state is 'Play'
if gameState == 'play' then
-- Speed increase coeficient
coef = 1.03
saveDistance = 2
ball:update(dt)
-- Collision for first paddle
if ball:collides(player1.paddle) then
sounds.paddle_hit:play()
ball.x = ball.x + saveDistance
ball.dx = -ball.dx * coef
if ball.dy < 0 then
ball.dy = -math.random(10,150)
else
ball.dy = math.random(10,150)
end
end
-- Collision for second paddle
if ball:collides(player2.paddle) then
sounds.paddle_hit:play()
ball.x = ball.x - saveDistance
ball.dx = -ball.dx * coef
if ball.dy < 0 then
ball.dy = -math.random(40,150)
else
ball.dy = math.random(40,150)
end
end
-- Collision with bottom and top edges of the window --
if ball.y < 0 then
sounds.wall_hit:play()
ball.y = 0
ball.dy = -ball.dy
elseif ball.y + ball.height > VIRTUAL_HEIGHT then
sounds.wall_hit:play()
ball.y = VIRTUAL_HEIGHT - ball.height
ball.dy = -ball.dy
end
-- Game score process
if ball.x < 0 then
sounds.score:play()
servePlayer = 1
player2:addScore()
gameState = 'serve'
ball:reset(2)
elseif ball.x > VIRTUAL_WIDTH then
sounds.score:play()
servePlayer = 2
player1:addScore()
gameState = 'serve'
ball:reset(1)
end
if player1.score == ROUNDS then
gameState = 'end'
playerWins = 1
elseif player2.score == ROUNDS then
gameState = 'end'
playerWins = 2
end
end
player1:update(dt)
player2:update(dt)
end
end
--[[
Draw function
]]
function love.draw()
push:apply('start')
-- Background color
love.graphics.clear(40/255,45/255,52/255,255/255)
-- Start screen --
if gameState == 'start' then
menu:render()
else
displayFPS()
love.graphics.setColor(1,1,1,1)
-- drawing two rectangles and a ball
player1.paddle:render()
player2.paddle:render()
ball:render()
-- End Screen --
if gameState == 'end' then
love.graphics.setFont(medFont)
if playerWins == 1 then
love.graphics.printf("Left player wins!", 0, 10, VIRTUAL_WIDTH, 'center')
else
love.graphics.printf("Right player wins!", 0, 10, VIRTUAL_WIDTH, 'center')
end
love.graphics.setFont(smallFont)
love.graphics.printf("Press SPACE to restart!", 0, 30, VIRTUAL_WIDTH, 'center')
end
-- Serve screen --
if gameState == 'serve' then
love.graphics.setFont(medFont)
if servePlayer == 1 then
love.graphics.printf("Left player serve!", 0, 10, VIRTUAL_WIDTH, 'center')
elseif servePlayer == 2 then
love.graphics.printf("Right player serve!", 0, 10, VIRTUAL_WIDTH, 'center')
end
love.graphics.setFont(smallFont)
love.graphics.printf("Press SPACE to serve!", 0, 30, VIRTUAL_WIDTH, 'center')
end
if gameState == 'pause' then
love.graphics.setFont(smallFont)
love.graphics.printf("Press SPACE to continue the game!", 0, 30, VIRTUAL_WIDTH, 'center')
end
if gameState == 'play' then
love.graphics.printf("Press SPACE to pause the game!", 0, 30, VIRTUAL_WIDTH, 'center')
end
love.graphics.setFont(scoreFont)
love.graphics.printf(tostring(player1.score), VIRTUAL_WIDTH/2 - 50, VIRTUAL_HEIGHT/4, VIRTUAL_WIDTH)
love.graphics.printf(tostring(player2.score), VIRTUAL_WIDTH/2 + 30, VIRTUAL_HEIGHT/4, VIRTUAL_WIDTH)
end
push:apply('end')
end
-- Display Frames Per Second
function displayFPS()
love.graphics.setFont(smallFont)
love.graphics.setColor(70/255,75/255,82/255,255/255)
love.graphics.printf("FPS " .. tostring(love.timer.getFPS()), 0, VIRTUAL_HEIGHT - 10, VIRTUAL_WIDTH, 'center')
end
-- Keyboard input handler
function love.keypressed(key)
-- exit from the game
if key == 'escape' then
if gameState ~= 'start' then
gameState = 'start'
else
love.event.quit()
end
end
-- start/pause the game
if key == 'space' then
if gameState == 'start' then
setupGame()
gameState = 'serve'
elseif gameState == 'serve' then
gameState = 'play'
elseif gameState == 'end' then
restartGame()
gameState = 'serve'
elseif gameState == 'pause' then
gameState = 'play'
else
gameState = 'pause'
end
end
-- add restart function
if key == 'r' then
restartGame()
end
if gameState == 'start' then
if key == 'left' then
menu:arrowLeft()
elseif key == 'right' then
menu:arrowRight()
end
end
end