-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
117 lines (82 loc) · 2.54 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
-- Importing required libraries
push = require 'Libraries/push'
math = require 'math'
Class = require 'Libraries/class'
-- Importing classes defined
require 'Classes/Ball'
require 'Classes/Paddle'
require 'Classes/StateMachine'
-- Importing states defined
require 'States/BaseState'
require 'States/StartState'
require 'States/PlayState'
require 'States/ServeState'
require 'States/CountDownState'
require 'States/EndState'
-- Defining screen constants
WINDOW_WIDTH = 1280
WINDOW_HEIGHT = 720
VIRTUAL_WIDTH = 432
VIRTUAL_HEIGHT = 243
-- Variables required for scoring
winner = nil
leftPlayerScore = 0
rightPlayerScore = 0
function love.load()
-- Filter to be nearest
love.graphics.setDefaultFilter('nearest', 'nearest')
-- Defining fonts
biggestFont = love.graphics.newFont("Fonts/font.ttf", 48)
bigFont = love.graphics.newFont("Fonts/font.ttf", 32)
smallFont = love.graphics.newFont("Fonts/font.ttf", 16)
-- Setting window title
love.window.setTitle("Pong")
-- Setting up screen
push:setupScreen(VIRTUAL_WIDTH, VIRTUAL_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT,{
fullscreen = false,
resizable = true,
vsync = true
})
-- Defining state machine with all states
stateMachine = StateMachine {
['start'] = function() return StartState() end,
['play'] = function() return PlayState() end,
['serve'] = function() return ServeState() end,
['countdown'] = function() return CountDownState() end,
['end'] = function() return EndState() end
}
-- Initializing state machine with start state
stateMachine:change('start')
-- Initializing keys pressed
love.keyboard.keysPressed = {}
end
function love.keypressed(key)
-- Keeping track of keys pressed in each frame
love.keyboard.keysPressed[key] = true
-- Quitting application on escape
if key == 'escape' then
love.event.quit()
end
end
function love.keyboard.wasPressed(key)
-- Returning whether or not the key was pressed in current frame
return love.keyboard.keysPressed[key]
end
function love.update(dt)
-- Updating state machine
stateMachine:update(dt)
-- Resetting keys pressed
love.keyboard.keysPressed = {}
end
function love.draw()
push:start()
-- Clearing with kind of gray color background
love.graphics.clear(104 / 255, 118 / 255, 140 / 255, 255 / 255)
-- Rendering appropriate state in state machine
stateMachine:render()
push:finish()
end
function love.resize(w, h)
-- Resizing screen
push:resize(w, h)
end