-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
284 lines (240 loc) · 8.11 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
push = require 'push'
Class = require 'Class'
require 'Paddle'
require 'Ball'
WINDOW_WIDTH = 1280
WINDOW_HEIGHT = 720
VIRTUAL_WIDTH = 432
VIRTUAL_HEIGHT = 243
PADDLE_SPEED = 200
function love.load()
-- set love's default filter to "nearest-neighbor", which essentially
-- means there will be no filtering of pixels (blurriness), which is
-- important for a nice crisp, 2D look
love.graphics.setDefaultFilter('nearest', 'nearest')
love.window.setTitle('Pong')
math.randomseed(os.time())
smallFont = love.graphics.newFont('font.ttf', 8)
largeFont = love.graphics.newFont('font.ttf', 16)
scoreFont = love.graphics.newFont('font.ttf', 32)
love.graphics.setFont(smallFont)
sounds = {
['paddle_hit'] = love.audio.newSource('sounds/paddle_hit.wav', 'static'),
['score'] = love.audio.newSource('sounds/score.wav', 'static'),
['wall_hit'] = love.audio.newSource('sounds/wall_hit.wav', 'static')
}
push:setupScreen(VIRTUAL_WIDTH, VIRTUAL_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT, {
fullscreen = false,
resizable = true,
vsync = true,
canvas = false
})
player1 = Paddle(10, 30, 5, 20)
player2 = Paddle(VIRTUAL_WIDTH - 10, VIRTUAL_HEIGHT - 30, 5, 20)
-- place a ball in the middle of the screen
ball = Ball(VIRTUAL_WIDTH / 2 - 2, VIRTUAL_HEIGHT / 2 - 2, 4, 4)
player1Score = 0
player2Score = 0
servingPlayer = 1
winningPlayer = 0
gameState = 'start'
end
function love.resize(w, h)
push:resize(w, h)
end
--[[
Called every frame, passing in `dt` since the last frame. `dt`
is short for `deltaTime` and is measured in seconds. Multiplying
this by any changes we wish to make in our game will allow our
game to perform consistently across all hardware; otherwise, any
changes we make will be applied as fast as possible and will vary
across system hardware.
]]
function love.update(dt)
if gameState == 'serve' then
-- before switching to play, initialize ball's velocity based
-- on player who last scored
ball.dy = math.random(-50, 50)
if servingPlayer == 1 then
ball.dx = math.random(140, 200)
else
ball.dx = -math.random(140, 200)
end
elseif gameState == 'play' then
-- detect ball collision with paddles, reversing dx if true and
-- slightly increasing it, then altering the dy based on the position
-- at which it collided, then playing a sound effect
if ball:collides(player1) then
ball.dx = -ball.dx * 1.03
ball.x = player1.x + 5
-- keep velocity going in the same direction, but randomize it
if ball.dy < 0 then
ball.dy = -math.random(10, 150)
else
ball.dy = math.random(10, 150)
end
sounds['paddle_hit']:play()
end
if ball:collides(player2) then
ball.dx = -ball.dx * 1.03
ball.x = player2.x - 4
-- keep velocity going in the same direction, but randomize it
if ball.dy < 0 then
ball.dy = -math.random(10, 150)
else
ball.dy = math.random(10, 150)
end
sounds['paddle_hit']:play()
end
-- detect upper and lower screen boundary collision, playing a sound
-- effect and reversing dy if true
if ball.y <= 0 then
ball.y = 0
ball.dy = -ball.dy
sounds['wall_hit']:play()
end
-- -4 to account for the ball's size
if ball.y >= VIRTUAL_HEIGHT - 4 then
ball.y = VIRTUAL_HEIGHT - 4
ball.dy = -ball.dy
sounds['wall_hit']:play()
end
-- if we reach the left or right edge of the screen, go back to serve
-- and update the score and serving player
if ball.x < 0 then
servingPlayer = 1
player2Score = player2Score + 1
sounds['score']:play()
-- if we've reached a score of 10, the game is over; set the
-- state to done so we can show the victory message
if player2Score == 10 then
winningPlayer = 2
gameState = 'done'
else
gameState = 'serve'
-- places the ball in the middle of the screen, no velocity
ball:reset()
end
end
if ball.x > VIRTUAL_WIDTH then
servingPlayer = 2
player1Score = player1Score + 1
sounds['score']:play()
if player1Score == 10 then
winningPlayer = 1
gameState = 'done'
else
gameState = 'serve'
ball:reset()
end
end
end
--
-- paddles can move no matter what state we're in
--
-- player 1
if love.keyboard.isDown('w') then
player1.dy = -PADDLE_SPEED
elseif love.keyboard.isDown('s') then
player1.dy = PADDLE_SPEED
else
player1.dy = 0
end
-- player 2
if love.keyboard.isDown('up') then
player2.dy = -PADDLE_SPEED
elseif love.keyboard.isDown('down') then
player2.dy = PADDLE_SPEED
else
player2.dy = 0
end
-- update our ball based on its DX and DY only if we're in play state;
-- scale the velocity by dt so movement is framerate-independent
if gameState == 'play' then
ball:update(dt)
end
player1:update(dt)
player2:update(dt)
end
--[[
A callback that processes key strokes as they happen, just the once.
Does not account for keys that are held down, which is handled by a
separate function (`love.keyboard.isDown`). Useful for when we want
things to happen right away, just once, like when we want to quit.
]]
function love.keypressed(key)
-- `key` will be whatever key this callback detected as pressed
if key == 'escape' then
-- the function LÖVE2D uses to quit the application
love.event.quit()
-- if we press enter during either the start or serve phase, it should
-- transition to the next appropriate state
elseif key == 'enter' or key == 'return' then
if gameState == 'start' then
gameState = 'serve'
elseif gameState == 'serve' then
gameState = 'play'
elseif gameState == 'done' then
-- game is simply in a restart phase here, but will set the serving
-- player to the opponent of whomever won for fairness!
gameState = 'serve'
ball:reset()
-- reset scores to 0
player1Score = 0
player2Score = 0
-- decide serving player as the opposite of who won
if winningPlayer == 1 then
servingPlayer = 2
else
servingPlayer = 1
end
end
end
end
function love.draw()
-- drawing with push in virtual
push:start()
love.graphics.clear(40/255, 45/255, 52/255, 255/255)
if gameState == 'start' then
-- UI messages
love.graphics.setFont(smallFont)
love.graphics.printf('Welcome to Pong!', 0, 10, VIRTUAL_WIDTH, 'center')
love.graphics.printf('Press Enter to begin!', 0, 20, VIRTUAL_WIDTH, 'center')
elseif gameState == 'serve' then
-- UI messages
love.graphics.setFont(smallFont)
love.graphics.printf('Player ' .. tostring(servingPlayer) .. "'s serve!",
0, 10, VIRTUAL_WIDTH, 'center')
love.graphics.printf('Press Enter to serve!', 0, 20, VIRTUAL_WIDTH, 'center')
elseif gameState == 'play' then
-- no UI messages to display in play
elseif gameState == 'done' then
-- UI messages
love.graphics.setFont(largeFont)
love.graphics.printf('Player ' .. tostring(winningPlayer) .. ' wins!',
0, 10, VIRTUAL_WIDTH, 'center')
love.graphics.setFont(smallFont)
love.graphics.printf('Press Enter to restart!', 0, 30, VIRTUAL_WIDTH, 'center')
end
displayScore()
player1:render()
player2:render()
ball:render()
displayFPS()
push:finish()
end
function displayScore()
-- score display
love.graphics.setFont(scoreFont)
love.graphics.print(tostring(player1Score), VIRTUAL_WIDTH / 2 - 50,
VIRTUAL_HEIGHT / 3)
love.graphics.print(tostring(player2Score), VIRTUAL_WIDTH / 2 + 30,
VIRTUAL_HEIGHT / 3)
end
function displayFPS()
-- simple FPS display across all states
love.graphics.setFont(smallFont)
love.graphics.setColor(0, 255/255, 0, 255/255)
love.graphics.print('FPS: ' .. tostring(love.timer.getFPS()), 10, 10)
love.graphics.setColor(255, 255, 255, 255)
end