-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
208 lines (168 loc) · 4.34 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
--handle bricks - collide with bricks
function love.load(arg)
if arg[#arg] == "-debug" then require("mobdebug").start() end
block = {}
block.Color = {255, 0, 0}
paddle = {}
paddle.Color = {0, 0, 255}
bricks = {}
bricks.PosX = 13
bricks.PosY = 25
bricks.CountX = 10
bricks.CountY = 3
bricks.Width = 65
bricks.Height = 26
bricks.Color = {0, 255, 0}
bricks.SpacingX = 14
bricks.SpacingY = 31
newGame()
end
function newGame()
setupBricks()
reset()
end
function setupBricks()
bricks.bricks = {}
i = 0
for y = 0,bricks.CountY-1 do
for x = 0,bricks.CountX-1 do
brick = {}
brick.PosX = bricks.PosX + (bricks.Width + bricks.SpacingX) * x
brick.PosY = bricks.PosY + (bricks.Height + bricks.SpacingY) * y
brick.Width = bricks.Width
brick.Height = bricks.Height
brick.Color = bricks.Color
brick.Alive = true
bricks.bricks[i] = brick
i = i + 1
end
end
end
function bounceOnScreen()
if block.PosX < 0 then
block.VelocityX = -block.VelocityX
end
if block.PosX + block.Width >= love.graphics.getWidth() then
block.VelocityX = -block.VelocityX
end
if block.PosY < 0 then
block.VelocityY = -block.VelocityY
end
end
function doesCollide(rect)
--following - http://devmag.org.za/2009/04/13/basic-collision-detection-in-2d-part-1/
outsideTop = block.PosY + block.Height < rect.PosY
outsideBottom = block.PosY > rect.PosY + rect.Height
outsideLeft = block.PosX + block.Width < rect.PosX
outsideRight = block.PosX > rect.PosX + rect.Width
return not (outsideTop or outsideBottom or outsideLeft or outsideRight)
end
function bounceOnRect(rect)
if doesCollide(rect) then
--from .net intersection
x = math.max(block.PosX, rect.PosX)
num1 = math.min(block.PosX + block.Width, rect.PosX + rect.Width)
y = math.max(block.PosY, rect.PosY)
num2 = math.min(block.PosY + block.Height, rect.PosY + rect.Height);
intX = num1 - x
intY = num2 - y
delta = 10
if intX - intY < delta then
block.VelocityX = -block.VelocityX
end
if intY - intX < delta then
block.VelocityY = -block.VelocityY
end
end
end
function bounceOnPaddle(paddle)
bounceOnRect(paddle)
end
function handleKeyboard(dt)
newPaddleX = paddle.PosX
if love.keyboard.isDown("left") then
newPaddleX = newPaddleX - dt * 500
end
if love.keyboard.isDown("right") then
newPaddleX = newPaddleX + dt * 500
end
if(newPaddleX < 0) then
newPaddleX = 0
end
if(newPaddleX + paddle.Width > love.graphics.getWidth()) then
newPaddleX = love.graphics.getWidth() - paddle.Width
end
if not doesCollide(paddle) then
paddle.PosX = newPaddleX
end
end
function moveBlock(dt)
newBlockX = block.PosX + block.VelocityX * dt
newBlockY = block.PosY + block.VelocityY * dt
--if not doesCollide(paddle) then
block.PosX = newBlockX
block.PosY = newBlockY
--end
end
function bounceOnBricks()
for i = 0,#bricks.bricks do
b = bricks.bricks[i]
if b.Alive and doesCollide(b) then
bounceOnRect(b)
b.Alive = false
end
end
end
function love.update(dt)
moveBlock(dt)
bounceOnBricks()
bounceOnPaddle(paddle)
bounceOnScreen()
handleKeyboard(dt)
end
function drawBrick(brickIndex)
b = bricks.bricks[brickIndex]
if b.Alive then
love.graphics.setColor(b.Color)
love.graphics.rectangle("fill", b.PosX, b.PosY, b.Width, b.Height)
love.graphics.setColor(255, 255, 255)
love.graphics.rectangle("line", b.PosX, b.PosY, b.Width, b.Height)
end
end
function drawBricks()
for i = 0,#bricks.bricks do
drawBrick(i)
end
end
function love.draw()
love.graphics.setColor(block.Color)
love.graphics.rectangle("fill", block.PosX, block.PosY, block.Width, block.Height)
love.graphics.setColor(paddle.Color)
love.graphics.rectangle("fill", paddle.PosX, paddle.PosY, paddle.Width, paddle.Height)
drawBricks()
end
function reset()
block.PosX = 26
block.PosY = 282
block.Width = 50
block.Height = 50
block.VelocityX = 100
block.VelocityY = 100
paddle.PosX = 68
paddle.PosY = 506
paddle.Width = 260
paddle.Height = 50
paddle.VelocityX = 0
paddle.VelocityY = 0
end
function love.keyreleased(key)
if key == "n" then
newGame()
end
if key == "r" then
reset()
end
if key == "escape" then
love.event.quit()
end
end