-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
218 lines (166 loc) · 6.69 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
--[[
GD50
Breakout Remake
Originally developed by Atari in 1976. An effective evolution of
Pong, Breakout ditched the two-player mechanic in favor of a single-
player game where the player, still controlling a paddle, was tasked
with eliminating a screen full of differently placed bricks of varying
values by deflecting a ball back at them.
This version is built to more closely resemble the NES than
the original Pong machines or the Atari 2600 in terms of
resolution, though in widescreen (16:9) so it looks nicer on
modern systems.
Credit for graphics:
https://opengameart.org/users/buch
Credit for music:
http://freesound.org/people/joshuaempyre/sounds/251461/
http://www.soundcloud.com/empyreanma
]]
require 'src/Dependencies'
function love.load()
love.graphics.setDefaultFilter('nearest', 'nearest')
math.randomseed(os.time())
love.window.setTitle('Breakout')
gFonts = {
['small'] = love.graphics.newFont('fonts/font.ttf', 8),
['medium'] = love.graphics.newFont('fonts/font.ttf', 16),
['large'] = love.graphics.newFont('fonts/font.ttf', 32)
}
love.graphics.setFont(gFonts['small'])
gTextures = {
['background'] = love.graphics.newImage('graphics/background.png'),
['main'] = love.graphics.newImage('graphics/breakout.png'),
['arrows'] = love.graphics.newImage('graphics/arrows.png'),
['hearts'] = love.graphics.newImage('graphics/hearts.png'),
['particle'] = love.graphics.newImage('graphics/particle.png')
}
gFrames = {
['paddles'] = GenerateQuadsPaddles(gTextures['main']),
['balls'] = GenerateQuadsBalls(gTextures['main']),
['bricks'] = GenerateQuadsBricks(gTextures['main']) ,
['hearts'] = GenerateQuads(gTextures['hearts'], 10, 9)
}
push:setupScreen(VIRTUAL_WIDTH, VIRTUAL_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT, {
vsync = true,
fullscreen = false,
resizable = true
})
gSounds = {
['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'),
['confirm'] = love.audio.newSource('sounds/confirm.wav', 'static'),
['select'] = love.audio.newSource('sounds/select.wav', 'static'),
['no-select'] = love.audio.newSource('sounds/no-select.wav', 'static'),
['brick-hit-1'] = love.audio.newSource('sounds/brick-hit-1.wav', 'static'),
['brick-hit-2'] = love.audio.newSource('sounds/brick-hit-2.wav', 'static'),
['hurt'] = love.audio.newSource('sounds/hurt.wav', 'static'),
['victory'] = love.audio.newSource('sounds/victory.wav', 'static'),
['high-score'] = love.audio.newSource('sounds/high_score.wav', 'static'),
['pause'] = love.audio.newSource('sounds/pause.wav', 'static'),
['music'] = love.audio.newSource('sounds/music.wav', 'static')
}
-- the state machine we'll be using to transition between various states
-- in our game instead of clumping them together in our update and draw
-- methods
--
-- our current game state can be any of the following:
-- 1. 'start' (the beginning of the game, where we're told to press Enter)
-- 2. 'paddle-select' (where we get to choose the color of our paddle)
-- 3. 'serve' (waiting on a key press to serve the ball)
-- 4. 'play' (the ball is in play, bouncing between paddles)
-- 5. 'victory' (the current level is over, with a victory jingle)
-- 6. 'game-over' (the player has lost; display score and allow restart)
gStateMachine = StateMachine {
['start'] = function() return StartState() end,
['play'] = function() return PlayState() end,
['serve'] = function() return ServeState() end,
['game-over'] = function() return GameOverState() end,
['victory'] = function() return VictoryState() end,
['high-score'] = function() return HighScoreState() end,
['enter-high-score'] = function() return EnterHighScoreState() end
}
gStateMachine:change('start', {
highScores = loadHighScores()
})
love.keyboard.keysPressed = {}
end
function love.resize(w, h)
push:resize(w, h)
end
function love.update(dt)
gStateMachine:update(dt)
love.keyboard.keysPressed = {}
end
function love.keypressed(key)
-- add to our table of keys pressed this frame
love.keyboard.keysPressed[key] = true
end
function love.keyboard.wasPressed(key)
if love.keyboard.keysPressed[key] then
return true
else
return false
end
end
function love.draw()
push:apply('start')
local backgroundWidth = gTextures['background']:getWidth()
local backgroundHeight = gTextures['background']:getHeight()
love.graphics.draw(gTextures['background'], 0, 0, 0, VIRTUAL_WIDTH/ (backgroundWidth - 1), VIRTUAL_HEIGHT/ (backgroundHeight - 1))
gStateMachine:render()
displayFPS()
push:apply('end')
end
function loadHighScores()
love.filesystem.setIdentity('breakout')
if not love.filesystem.exists('breakout.lst') then
local scores = ''
for i = 10, 1, -1 do
scores = scores .. 'CTO\n'
scores = scores .. tostring(i * 1000) .. '\n'
end
love.filesystem.write('breakout.lst', scores)
end
local name = true
local currentName = nil
local counter = 1
local scores = {}
for i = 1, 10 do
scores[i] = {
name = nil,
score = nil
}
end
for line in love.filesystem.lines('breakout.lst') do
if name then
scores[counter].name = string.sub(line, 1, 3)
else
scores[counter].score = tonumber(line)
counter = counter + 1
end
name = not name
end
return scores
end
function displayFPS()
love.graphics.setFont(gFonts['small'])
love.graphics.setColor(0, 255, 0, 255)
love.graphics.print('FPS: '.. tostring(love.timer.getFPS()), 5, 5)
end
function renderHealth(health)
local healthX = VIRTUAL_WIDTH - 100
for i = 1, health do
love.graphics.draw(gTextures['hearts'], gFrames['hearts'][1], healthX, 4)
healthX = healthX + 11
end
for i = 1, 3 - health do
love.graphics.draw(gTextures['hearts'], gFrames['hearts'][2], healthX, 4)
healthX = healthX + 11
end
end
function renderScore(score)
love.graphics.setFont(gFonts['small'])
love.graphics.print('Score: ', VIRTUAL_WIDTH - 60, 5)
love.graphics.printf(tostring(score), VIRTUAL_WIDTH - 50, 5, 40, 'right')
end