-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.lua
284 lines (248 loc) · 6.96 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
-- Space War 1.0
-- author : Debugger
-- developed using : lua + lovve 2d
--
-- github repo link: https://github.com/bhattsameer/space-war
--
-- Licence = MIT
local math = require 'math'
require 'conf'
local score = 0
local t = 0
love.graphics.getDefaultFilter('nearest','nearest')
enemy = {}
enemies_controller = {}
enemies_controller.enemies = {}
-- load enemy image
enemies_controller.image = love.graphics.newImage('graphics/enemy.png')
--
--particle_systems = {}
--particle_systems.list = {}
--particle_systems.image = love.graphics.newImage('graphics/enemy_particle.png')
--local ps = {}
--function particle_systems:spawn(x, y)
-- ps.ps = love.graphics.newParticleSystem(particle_systems.image, 32)
-- ps.x = x
-- ps.y = y
-- ps.ps:setParticleLifetime(2,4)
-- ps.ps:setEmissionRate(5)
-- ps.ps:setSizeVariation(1)
-- ps.ps:setLinearAcceleration(-20, -20, 20, 20)
-- ps.ps:setColors(100,255,100, 255, 0, 255, 0, 255)
-- table.insert(particle_systems.list, ps)
--end
--function particle_systems:draw()
-- for _, v in pairs(particle_systems.list) do
-- love.graphics.draw(v.ps, v.x, v.y)
-- end
--end
--function particle_systems:update(dt)
-- for _, v in pairs(particle_systems.list) do
-- v.ps:update(dt)
-- end
--end
name = 1
function checkCollisions(enemies, bullets)
explosion = love.audio.newSource('sound/explosion.mp3', 'stream')
for i, e in ipairs(enemies) do
for j, b in pairs(bullets) do
if b.y <= e.y + e.height and b.x > e.x and b.x < e.x + e.width then
love.audio.play(explosion)
--particle_systems:spawn(e.x, e.y)
table.remove(enemies, i)
table.remove(bullets, j)
score = score + 1
-- more enemies
if score < 490 then
w = math.random(0, 700)
enemies_controller:spawnEnemy(w , 0)
end
-- boss
if score == 500 then
enemies_controller.image = love.graphics.newImage('graphics/enemy_particle.png')
for i=1,10 do
enemies_controller:spawnEnemy(i*75,0)
enemies_controller:spawnEnemy(i*75,35)
enemies_controller:spawnEnemy(i*75,70)
enemies_controller:spawnEnemy(i*75,105)
enemies_controller:spawnEnemy(i*75,140)
end
end
end
end
end
end
function love.load()
-- x = 0
-- y = 0
-- game music
local game_music = love.audio.newSource('sound/game_music.mp3', 'stream')
game_music:setLooping(true)
love.audio.play(game_music)
game_over = false
game_win = false
backgroundImage = love.graphics.newImage('graphics/starfield.png')
--player and fire details
player = {}
dragging = {}
player.x = 0
player.y = 550
player.width = 110
player.height = 110
dragging.active = false
dragging.x = 0
dragging.y = 0
player.bullets = {}
player.cooldown = 20
player.speed = 10
--load player image
player.image = love.graphics.newImage('graphics/player.png')
player.fire_sound = love.audio.newSource('sound/laser_gun.wav', 'stream')
player.fire = function()
if player.cooldown <= 0 then
love.audio.play(player.fire_sound)
player.cooldown = 10
bullet = {}
bullet.x = player.x + 11.5
bullet.y = player.y
table.insert(player.bullets, bullet)
end
end
-- S
--enemies_controller:spawnEnemy(160 , 40) -- e.h = 20 e.w= 40
--enemies_controller:spawnEnemy(160 , 10)
--enemies_controller:spawnEnemy(120, 10)
--enemies_controller:spawnEnemy(80 , 10)
--enemies_controller:spawnEnemy(80 , 10)
--enemies_controller:spawnEnemy(80 , 40)
--enemies_controller:spawnEnemy(80 , 80)
--enemies_controller:spawnEnemy(120 , 100)
--enemies_controller:spawnEnemy(160 , 105)
--enemies_controller:spawnEnemy(160 , 145)
--enemies_controller:spawnEnemy(160 , 180)
--enemies_controller:spawnEnemy(120 , 180)
--enemies_controller:spawnEnemy(80 , 180)
--enemies_controller:spawnEnemy(80 , 140)
for i = 0, 10 do
enemies_controller:spawnEnemy(i * 75 , 0)
--enemies_controller:spawnEnemy(i * 75 , 40)
end
end
function enemies_controller:spawnEnemy(x, y)
enemy = {}
enemy.x = x
enemy.y = y
enemy.width = 40
enemy.height = 20
enemy.bullets = {}
enemy.cooldown = 20
enemy.speed = .5
table.insert(self.enemies, enemy)
end
function enemy:fire()
if self.cooldown <= 0 then
self.cooldown = 20
bullet = {}
bullet.x = self.x + 35
bullet.y = self.y
table.insert(self.bullets, bullet)
end
end
function love.update(dt)
player.cooldown = player.cooldown - 1
--keyboard process
if love.keyboard.isDown('right') then
--x = x + 1
if player.x < 760.5 then
player.x = player.x + player.speed
end
end
if love.keyboard.isDown('left') then
--x = x - 1
if player.x > 0 then
player.x = player.x - player.speed
end
end
--if love.keyboard.isDown('up') then
-- if player.y > 0 then
-- player.y = player.y - player.speed
-- end
--end
--if love.keyboard.isDown('down') then
-- if player.y < 550 then
-- player.y = player.y + player.speed
-- end
--end
-- mouse controller
if dragging.active then
if player.x > 0 or player.x < 760.5 then
player.x = (love.mouse.getX() - dragging.x) - player.speed
end
if player.y > 0 or player.y < 550 then
player.y = (love.mouse.getY() - dragging.y) - player.speed
end
end
-- if love.keyboard.isDown("space") then
if game_over == false then
player.fire()
end
-- end
if #enemies_controller.enemies == 0 then
game_win = true
end
for _,e in pairs(enemies_controller.enemies) do
if e.y >= love.graphics.getHeight()/2 + 215 then
game_over = true
end
e.y = e.y + 1 * e.speed
end
for i,b in ipairs(player.bullets) do
if b.y < -10 then
table.remove(player.bullets, i)
end
b.y =b.y - 10
end
checkCollisions(enemies_controller.enemies, player.bullets)
end
function love.draw()
--draw background
love.graphics.draw(backgroundImage)
-- font size
font = love.graphics.newFont(14)
love.graphics.setFont(font)
-- score board
love.graphics.print('Your score: ' ..score, 340, 583)
-- win - loose statements
if game_over then
love.graphics.print('Game Over!', 340, 10)
return
elseif game_win then
love.graphics.print('You Won!', 340, 10)
end
-- draw player
love.graphics.setColor(255,255,255)
love.graphics.draw(player.image, player.x, player.y)
--draw enemies
love.graphics.setColor(255,255,255)
for _,e in pairs(enemies_controller.enemies) do
love.graphics.draw(enemies_controller.image, e.x, e.y)
end
--draw enemy fire
--draw fire
love.graphics.setColor(255,255,255)
for _,v in pairs(player.bullets) do
love.graphics.rectangle("fill", v.x, v.y, 10, 10)
end
end
function love.mousepressed(x, y, button, istouch)
if button == 1 and x > player.x and x < player.x + player.width and y < player.y + player.height then
dragging.active = true
dragging.x = x - player.x
dragging.y = y - player.y
end
end
function love.mousereleased(x, y, button)
if button == 1 then
dragging.active = false
end
end