-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.lua
216 lines (160 loc) · 7.21 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
push = require 'push'
Class = require 'class'
require 'Ball'
require 'Paddle'
WINDOW_WIDTH = 1280
WINDOW_HEIGHT = 720
VIRTUAL_WIDTH = WINDOW_WIDTH / 2
VIRTUAL_HEIGHT = WINDOW_HEIGHT / 2
timer = 0 -- timer since the game began
-- from this till next comment
block_pos = {} -- table to store block positions
rows, columns = 30, 30 -- you decide how many would fill the entire screen
block_width = math .floor( VIRTUAL_WIDTH /columns )
block_height = math .floor( VIRTUAL_HEIGHT /rows )
-- all this for randomly instantiating the block
-- Runs once when game first starts up; used to initialize settings.
function love .load()
-- seed the random number generator, then shake those dice a few times
math.randomseed( os.time() ) ; math.random() ; math.random() ; math.random()
love .graphics .setDefaultFilter('nearest', 'nearest')
push :setupScreen( VIRTUAL_WIDTH, VIRTUAL_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT, {
fullscreen = false,
vsync = true,
resizable = true
} )
local function generate_column( col )
for row = 0, rows -1 do
if love .math .random() *100 <= chance_of_block then
local xpos = col *block_width -( block_width /2 )
local ypos = row *block_height +1
local red = row /rows
local green = math.random()
local blue = 1 -col /columns
block_pos[ #block_pos +1 ] = { x = xpos, y = ypos, r = red, g = green, b = blue }
end -- rand
end -- #rows
end -- generate_column()
chance_of_block = 33 -- % chance of placing a block
generate_column( columns ) -- rightmost, final column
chance_of_block = 44
generate_column( columns -1 ) -- repeat, for second to last column
chance_of_block = 55
generate_column( columns -2 )
chance_of_block = 66
generate_column( columns -3 )
chance_of_block = 77
generate_column( columns -4 )
chance_of_block = 88
generate_column( columns -5 )
r, g, b = 0.5, 0.5, 0.0
love .graphics .setColor( r, g, b )
smallFont = love.graphics.newFont('font.ttf', 8)
scoreFont = love.graphics.newFont('font.ttf', 16)
-- defining score
score = 0
-- defining paddle ( x, y, w, h )
local thirds = VIRTUAL_HEIGHT /3
local begin = thirds +math.random( thirds )
paddle = Paddle( 5, VIRTUAL_HEIGHT / 2 - 15, 5, 30)
-- defining ball position
ball = Ball( VIRTUAL_WIDTH /2 -3, VIRTUAL_HEIGHT /2 -3, 6, 6 )
gameState = 'start'
end
function love .resize( w, h )
push :resize( w, h )
end -- resize
function love .keypressed( key )
if key == 'escape' then -- takes priority over other keys
love .event .quit()
-- to restart the whole game for testing purpose
-- takes in consideration the changes done and load according to that
elseif key == 'r' then
timer = 0
love .event .quit("restart")
elseif key == 'enter' or key == 'return' or key == 'space' then
if gameState == 'start' then
gameState = 'play'
score = 0
elseif gameState == 'play' then
gameState = 'start'
score = 0
ball :reset()
paddle:reset()
end -- gameState
end -- enter
end -- keypressed
function love .update( dt )
timer = timer +dt
if gameState == 'play' then
paddle :update( dt )
ball :update( dt )
if ball :collides( paddle ) then
ball .dx = -ball .dx -- horizontal deflection
end
if ball .y <= 0 then -- top wall
ball .dy = -ball .dy -- vertical deflection
ball .y = 0
end
if ball .y >= VIRTUAL_HEIGHT -ball .size then -- bottom wall
ball .dy = -ball .dy -- vertical deflection
ball .y = VIRTUAL_HEIGHT -ball .size
end
if ball .x +ball .width >= VIRTUAL_WIDTH then -- right wall
ball .dx = -ball .dx -- horizontal deflection
end
if love .keyboard .isDown( 'w', 'up' ) then
paddle .dy = -paddle.speed -- negative velocity to move paddle up
elseif love .keyboard .isDown( 's', 'down' ) then
paddle .dy = paddle .speed -- positive velocity to move paddle down
else
paddle .dy = 0
end
if ball.x > VIRTUAL_WIDTH *0.5 then -- only test for block-collision on right half of screen so that it becomes fast and optimized
for index = 1, #block_pos do
local block = block_pos[index]
if ball :collides( { x = block .x, y = block .y, width = block_width, height = block_height } ) then
for i = index, #block_pos -1 do
block_pos[i] = block_pos[i +1]
end -- shuffle every entry in list down one
block_pos [#block_pos] = nil -- erase last entry
collectgarbage() -- make sure that last position is empty
-- this should be automatic, but just to be certain there aren't any errors
score = score +1
ball .dx = -ball .dx -- horizontal deflection
break -- don't try to access last entry in block_pos, because it no longer exists
end -- test for collision
end -- loop through all block_pos
end -- right side of screen
end -- gameState == 'play'
end -- update()
function love .draw()
push :apply( 'start' )
love .graphics .clear(55/255, 55/255, 55/255)
ball :render() -- print ball
paddle :render() -- print paddle
love .graphics .setFont( smallFont ) -- sets active font
if gameState == 'start' then
love .graphics .printf(
'Welcome to Brick Breaker!', -- text to render
0, -- starting X (0 since we're going to center it based on width)
VIRTUAL_HEIGHT /6 -4, -- starting Y (halfway down screen) subtract half of font for proper alignment
VIRTUAL_WIDTH, -- number of pixels to center within (the entire screen here)
'center' ) -- alignment mode, can be 'center', 'left', or 'right'
love .graphics .printf( 'Press w and s, or arrows, to move the paddle',
0, VIRTUAL_HEIGHT /6 +10 , VIRTUAL_WIDTH, 'center' )
elseif gameState == 'play' then
if timer < 5 then
love .graphics .printf( 'Goodluck!', 0, VIRTUAL_HEIGHT /6 -4, VIRTUAL_WIDTH, 'center' )
end -- three second timer
end
-- printing
love .graphics .setFont( scoreFont )
love .graphics .printf( 'Score : ' ..score ..'', 20, 1, 170, 'left' )
for b = 1, #block_pos do -- draw blocks
local block = block_pos[b]
love .graphics .setColor( block .r, block. g, block .b )
love .graphics .rectangle( 'fill', block.x, block.y, block_width -3, block_height -3 )
end -- #block_pos
push :apply('end')
end